TriggerHistory.cc

00001 #include "TriggerHistory.hh"
00002 #include "RootGraphix.hh"
00003 #include "EventHandler.hh"
00004 
00005 #include "TCanvas.h"
00006 #include "TGraphErrors.h"
00007 #include "TMultiGraph.h"
00008 #include "TCanvas.h"
00009 #include "TLegend.h"
00010 #include "TH1.h"
00011 #include "TAxis.h"
00012 #include "TList.h"
00013 #include "TROOT.h"
00014 
00015 TriggerHistory::TriggerHistory(const std::string& name) : 
00016   BaseModule(name), _graphix(0), 
00017   _canvas(0), _triggergraph(0), _eventgraph(0), _multigraph(0), _legend(0)
00018 {
00019   AddDependency("RootGraphix");
00020   AddDependency("ConvertData");
00021   
00022   RegisterParameter("update_interval", _update_interval=60,
00023                     "Minimum seconds between each point on history plot");
00024   RegisterParameter("max_points", _max_points = 100,
00025                     "Maximum points in graph before scrolling");
00026   RegisterParameter("draw_errors_x", _draw_errors_x = true,
00027                     "Draw error bars for the time axis?");
00028   RegisterParameter("draw_errors_y", _draw_errors_y = true,
00029                     "Draw error bars for the y axis?");
00030   RegisterParameter("connect_points", _connect_points = false,
00031                     "Draw a straight line connecting each point?");
00032   
00033 }
00034 
00035 TriggerHistory::~TriggerHistory()
00036 {}
00037 
00038 int TriggerHistory::Initialize()
00039 {
00040   if(_update_interval<0) _update_interval = 0;
00041   _last_update_time = 0;
00042   _last_triggerid = 0;
00043   _last_eventid = 0;
00044 
00045   _graphix = EventHandler::GetInstance()->GetModule<RootGraphix>();
00046   if(!_graphix){
00047     Message(ERROR)<<"TriggerHistory: Can't find RootGraphix during initialization!"<<std::endl;
00048     return 1;
00049   }
00050   _canvas = _graphix->GetCanvas();
00051   _canvas->SetTitle("Trigger and Event Rates");
00052   _triggergraph = new TGraphErrors();
00053   _triggergraph->SetName("triggergraph");
00054   _triggergraph->SetTitle("Trigger Rate History");
00055   _triggergraph->SetLineWidth(2);
00056   _eventgraph = new TGraphErrors();
00057   _eventgraph->SetName("eventgraph");
00058   _eventgraph->SetTitle("Recorded Event Rate History");
00059   _eventgraph->SetLineColor(kBlue);
00060   _eventgraph->SetMarkerColor(kBlue);
00061   _eventgraph->SetLineWidth(2);
00062   _multigraph = new TMultiGraph();
00063   _multigraph->SetTitle("Trigger Rate History");
00064   _multigraph->Add(_triggergraph);
00065   _multigraph->Add(_eventgraph);
00066   _legend = new TLegend(0.05,0,0.95,0.05);
00067   _legend->SetNColumns(2);
00068   _legend->SetBorderSize(1);
00069   return 0;
00070 }
00071 
00072 int TriggerHistory::Finalize()
00073 {
00074   //RootGraphix will delete the canvas...
00075   _canvas = 0;
00076   //multigraph owns trigger and event graps...
00077   delete _multigraph;
00078   delete _legend;
00079   
00080   _multigraph=0;
00081   _triggergraph=0;
00082   _eventgraph=0;
00083   _legend=0;
00084   
00085   return 0;
00086 }
00087 
00088 int TriggerHistory::Process(EventPtr evt)
00089 {
00090   RawEventPtr raw = evt->GetRawEvent();
00091   if(_last_update_time == 0){
00092     _last_update_time = raw->GetTimestamp();
00093     _last_triggerid = evt->GetEventData()->trigger_count;
00094     _last_eventid = raw->GetID();
00095   }
00096   else if( raw->GetTimestamp() - _last_update_time >= 
00097            (uint32_t)_update_interval){
00098     double ntrigs = evt->GetEventData()->trigger_count - _last_triggerid;
00099     double nevts = raw->GetID() - _last_eventid;
00100     double tavg = 0.5*(raw->GetTimestamp() + _last_update_time);
00101     double terr = tavg - _last_update_time;
00102 
00103     RootGraphix::Lock glock = _graphix->AcquireLock();
00104     _canvas->cd();
00105     _canvas->Clear();
00106     int point = _triggergraph->GetN();
00107     if(_max_points > 0 && point >= _max_points){
00108       point--;
00109       _triggergraph->RemovePoint(0);
00110       _eventgraph->RemovePoint(0);
00111     }
00112     
00113     _triggergraph->SetPoint(point, tavg, ntrigs/(2*terr));
00114     _triggergraph->SetPointError(point,
00115                           _draw_errors_x ? terr : 0., 
00116                           _draw_errors_y ? sqrt(ntrigs)/(2*terr) : 0.);
00117     _eventgraph->SetPoint(point, tavg, nevts/(2*terr));
00118     _eventgraph->SetPointError(point, 
00119                                _draw_errors_x ? terr : 0., 
00120                                _draw_errors_y ? sqrt(nevts)/(2*terr):0.);
00121     //trick to make multigraph recalculate range
00122     TH1* hist = _multigraph->GetHistogram();
00123     if(hist)
00124       hist->SetMinimum(hist->GetMaximum());
00125     
00126     _multigraph->Draw(_connect_points ? "ale" : "ae");
00127     
00128     
00129     _last_update_time = raw->GetTimestamp();
00130     _last_triggerid = evt->GetEventData()->trigger_count;
00131     _last_eventid = raw->GetID();
00132     
00133     _legend->Clear();
00134     _legend->AddEntry(_triggergraph,
00135                       Form("Triggers (%u total)",_last_triggerid),"l");
00136     _legend->AddEntry(_eventgraph,
00137                       Form("Events Recorded (%u total)",_last_eventid+1),"l");
00138   
00139     _legend->Draw();
00140     
00141     //on older root, have to update before setting time stuff
00142     if(gROOT->GetVersionInt()<53200)
00143       _canvas->Update();
00144     _multigraph->GetXaxis()->SetTimeDisplay(1);
00145     _multigraph->GetXaxis()->SetTimeFormat("%H:%M:%S");
00146     _multigraph->GetYaxis()->SetTitle("Rate [Hz]");
00147     _canvas->Modified();
00148     
00149         
00150     
00151   }
00152   return 0;
00153 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1