Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_pedestal_online.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_pedestal_online.cc
4 // Created: Tue Feb 23 13:01:07 EST 2016
5 // Creator: dalton (on Linux gluon05.jlab.org 2.6.32-358.18.1.el6.x86_64 x86_64)
6 //
7 
8 #include <stdint.h>
9 #include <vector>
10 #include <cmath>
11 
13 #include <JANA/JApplication.h>
14 #include <JANA/JFactory.h>
15 
16 using namespace std;
17 using namespace jana;
18 
19 #include <DAQ/DF1TDCHit.h>
20 #include <DAQ/Df250PulseIntegral.h>
21 #include <DAQ/Df250PulseData.h>
22 #include <DAQ/JEventSource_EVIO.h>
23 #include <TTAB/DTranslationTable.h>
24 
25 #include <TDirectory.h>
26 #include <TH2.h>
27 #include <TH1.h>
28 #include <TProfile.h>
29 #include <TProfile2D.h>
30 #include <TTree.h>
31 #include <TROOT.h>
32 
33 
34 static const int highcratenum=100;
35 // root hist pointers
36 static TProfile *pedestal_vevent[highcratenum];
39 
41 float pedmean, pedrms;
42 
44 
45 long int recentwalltime=0;
46 
47 static const time_t periodlength=30; // seconds
48 long int globalstarttime=0;
49 long int globalstoptime=0;
51 
52 
53 
54 // Routine used to create our JEventProcessor
55 extern "C"{
56  void InitPlugin(JApplication *app){
57  InitJANAPlugin(app);
58  app->AddProcessor(new JEventProcessor_pedestal_online());
59  }
60 } // "C"
61 
62 
63 //------------------
64 // JEventProcessor_pedestal_online (Constructor)
65 //------------------
67 {
68  VERBOSE = 0;
69 
70  if(gPARMS){
71  gPARMS->SetDefaultParameter("pedestal_online:VERBOSE", VERBOSE, "pedestal_online verbosity level");
72  }
73 }
74 
75 //------------------
76 // ~JEventProcessor_pedestal_online (Destructor)
77 //------------------
79 {
80 
81 }
82 
83 //------------------
84 // init
85 //------------------
87 {
88  if (VERBOSE>=1) printf("JEventProcessor_pedestal_online::init()\n");
89 
90  // create root folder for DAQ and cd to it, store main dir
91  maindir = gDirectory;
92  peddir = maindir->mkdir("pedestal");
93  peddir->cd();
94 
95  // Initialise histograms and variables
96  for (int i=0; i<highcratenum; i++) {
97  periodstarttime[i] = 0;
98  pedestal_vevent[i] = NULL;
99  pedestal_vtime_tree[i] = NULL;
100  }
101 
102  // back to main dir
103  maindir->cd();
104 
105  return NOERROR;
106 }
107 
108 
109 //------------------
110 // brun
111 //------------------
112 jerror_t JEventProcessor_pedestal_online::brun(JEventLoop *eventLoop, int32_t runnumber)
113 {
114  // This is called whenever the run number changes
115  return NOERROR;
116 }
117 
118 //------------------
119 // evnt
120 //------------------
121 jerror_t JEventProcessor_pedestal_online::evnt(JEventLoop *loop, uint64_t eventnumber)
122 {
123  // first look for an epics event
124  vector<const DEPICSvalue*> depicsvalue;
125  loop->Get(depicsvalue);
126  if (depicsvalue.size()>0) {
127  recentwalltime = (long int)depicsvalue[0]->timestamp;
128  if (VERBOSE>=2) printf("JEventProcessor_pedestal_online::evnt %li found epics event at time %li\n",eventnumber,recentwalltime);
129  // set the initial values
130  if (globalstarttime==0) {
132  for (int i=0; i<highcratenum; i++) periodstarttime[i] = globalstarttime;
133  if (VERBOSE>=1) printf("\nsetting the global start time %li %li\n\n",recentwalltime,globalstarttime);
134  }
135  return NOERROR;
136  }
137 
138  vector<const Df250PulseData*> f250PDs;
139  vector<const Df250PulseIntegral*> f250PIs;
140  vector<const Df125PulseIntegral*> f125PIs;
141 
142  loop->Get(f250PDs);
143  loop->Get(f250PIs);
144  loop->Get(f125PIs);
145 
146  // Although we are only filling objects local to this plugin, the directory changes: Global ROOT lock
147  japp->RootWriteLock();
148 
149  if (peddir!=NULL) peddir->cd();
150 
151  // Access F250 from Df250PulseIntegral object - pre-Fall 2016 data
152  for(unsigned int i=0; i<f250PIs.size(); i++) {
153  const Df250PulseIntegral *hit = f250PIs[i];
154  int rocid = hit->rocid;
155 
156  if(rocid>=0 && rocid<100) {
157  char cratename[255],title[255];
158  // only use time if it is good
159  if (recentwalltime>0) {
160  if (pedestal_vtime_tree[rocid]==NULL) {
161  if (VERBOSE>=1) printf("JEventProcessor_pedestal_online::evnt creating time tree and histogram for crate %i\n",rocid);
162  sprintf(cratename,"pedestal_vtime_tree_crate%i",rocid);
163  sprintf(title,"Crate %i avg pedestal (F250)",rocid);
164  pedestal_vtime_tree[rocid] = new TTree(cratename,title);
165  pedestal_vtime_tree[rocid]->Branch("time",&treetime);
166  //pedestal_vtime_tree[rocid]->Branch("pedestal",&treepedestal[rocid]);
167  pedestal_vtime_tree[rocid]->Branch("pedmean",&pedmean);
168  pedestal_vtime_tree[rocid]->Branch("pedrms",&pedrms);
169  pedestal_vtime_tree[rocid]->Branch("pednumsamps",&pednumsamps);
170  sprintf(cratename,"pedestal_vtime_hist_crate%i",rocid);
171  pedestal_vtime_hist[rocid] = new TH1F(cratename,title,100,0.,0.);
172  }
173  // keep a running stats of the pedestal
174  if (hit->pedestal>0 && hit->nsamples_pedestal!=0) pedestal_vtime_hist[rocid]->Fill(hit->pedestal/hit->nsamples_pedestal);
175  // if more than periodlength has elapsed, then end the average and fill the tree
177  if (VERBOSE>=3) printf("JEventProcessor_pedestal_online::evnt filling tree crate %i\n",rocid);
178  treetime = periodstarttime[rocid];
179  pedmean = pedestal_vtime_hist[rocid]->GetMean();
180  pedrms = pedestal_vtime_hist[rocid]->GetRMS();
181  pednumsamps = pedestal_vtime_hist[rocid]->GetEntries();
182  if (VERBOSE>=3) printf("\t\t%li %li %li %li %8.4f %8.4f %7i\n",
185  pedestal_vtime_tree[rocid]->Fill();
186  pedestal_vtime_hist[rocid]->Reset();
187  }
188  // advance to the next period
189  while (recentwalltime > periodstarttime[rocid]+periodlength) {
192  }
193  }
194 
195 
196  if (pedestal_vevent[rocid]==NULL) {
197  if (VERBOSE>=1) printf("JEventProcessor_pedestal_online::evnt creating event histogram for crate %i\n",rocid);
198  sprintf(cratename,"pedestal_vevent_crate%i",rocid);
199  sprintf(title,"Crate %i avg pedestal (F250);event num;pedestal (all chan avg)",rocid);
200  pedestal_vevent[rocid] = new TProfile(cratename,title,200,0.0,10000.0);
201  pedestal_vevent[rocid]->SetStats(0);
202 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
203  pedestal_vevent[rocid]->SetCanExtend(TH1::kXaxis);
204 #else
205  pedestal_vevent[rocid]->SetBit(TH1::kCanRebin);
206 #endif
207  }
208  pedestal_vevent[rocid]->Fill(eventnumber,hit->pedestal);
209  }
210  }
211 
212  // Access F250 from Df250PulseData object - post-Fall 2016 data
213  for(unsigned int i=0; i<f250PDs.size(); i++) {
214  const Df250PulseData *hit = f250PDs[i];
215  int rocid = hit->rocid;
216  if(rocid>=0 && rocid<100) {
217  char cratename[255],title[255];
218  // only use time if it is good
219  if (recentwalltime>0) {
220  if (pedestal_vtime_tree[rocid]==NULL) {
221  if (VERBOSE>=1) printf("JEventProcessor_pedestal_online::evnt creating time tree and histogram for crate %i\n",rocid);
222  sprintf(cratename,"pedestal_vtime_tree_crate%i",rocid);
223  sprintf(title,"Crate %i avg pedestal (F250)",rocid);
224  pedestal_vtime_tree[rocid] = new TTree(cratename,title);
225  pedestal_vtime_tree[rocid]->Branch("time",&treetime);
226  //pedestal_vtime_tree[rocid]->Branch("pedestal",&treepedestal[rocid]);
227  pedestal_vtime_tree[rocid]->Branch("pedmean",&pedmean);
228  pedestal_vtime_tree[rocid]->Branch("pedrms",&pedrms);
229  pedestal_vtime_tree[rocid]->Branch("pednumsamps",&pednumsamps);
230  sprintf(cratename,"pedestal_vtime_hist_crate%i",rocid);
231  pedestal_vtime_hist[rocid] = new TH1F(cratename,title,100,0.,0.);
232  }
233  // keep a running stats of the (single event) pedestal
234  if (hit->pedestal>0 && hit->nsamples_pedestal!=0) pedestal_vtime_hist[rocid]->Fill(hit->pedestal/hit->nsamples_pedestal);
235  // if more than periodlength has elapsed, then end the average and fill the tree
237  if (VERBOSE>=3) printf("JEventProcessor_pedestal_online::evnt filling tree crate %i\n",rocid);
238 
239  treetime = periodstarttime[rocid];
240  pedmean = pedestal_vtime_hist[rocid]->GetMean();
241  pedrms = pedestal_vtime_hist[rocid]->GetRMS();
242  pednumsamps = pedestal_vtime_hist[rocid]->GetEntries();
243  if (VERBOSE>=3) printf("\t\t%li %li %li %li %8.4f %8.4f %7i\n",
246  pedestal_vtime_tree[rocid]->Fill();
247  pedestal_vtime_hist[rocid]->Reset();
248  }
249 
250  // advance to the next period
251  while (recentwalltime > periodstarttime[rocid]+periodlength) {
254  }
255  }
256 
257  if (pedestal_vevent[rocid]==NULL) {
258  if (VERBOSE>=1) printf("JEventProcessor_pedestal_online::evnt creating event histogram for crate %i\n",rocid);
259  sprintf(cratename,"pedestal_vevent_crate%i",rocid);
260  sprintf(title,"Crate %i avg pedestal (F250);event num;pedestal (all chan avg)",rocid);
261  pedestal_vevent[rocid] = new TProfile(cratename,title,200,0.0,10000.0);
262  pedestal_vevent[rocid]->SetStats(0);
263 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
264  pedestal_vevent[rocid]->SetCanExtend(TH1::kXaxis);
265 #else
266  pedestal_vevent[rocid]->SetBit(TH1::kCanRebin);
267 #endif
268  }
269 
270  // monitor single-sample pedestal
271  double locPedestalFraction = (hit->nsamples_pedestal == 0) ? 0.0 : double(hit->pedestal)/double(hit->nsamples_pedestal);
272  pedestal_vevent[rocid]->Fill(eventnumber, locPedestalFraction);
273  pedestal_vevent[rocid]->Fill(eventnumber,0);
274  }
275  }
276 
277  // Access F125 from Df125PulseIntegral object
278  for(unsigned int i=0; i<f125PIs.size(); i++) {
279  const Df125PulseIntegral *hit = f125PIs[i];
280  int rocid = hit->rocid;
281 
282  if(rocid>=0 && rocid<100) {
283  char cratename[255],title[255];
284  // only use time if it is good
285  if (recentwalltime>0) {
286  if (pedestal_vtime_tree[rocid]==NULL) {
287  if (VERBOSE>=1) printf("JEventProcessor_pedestal_online::evnt creating time tree and histogram for crate %i\n",rocid);
288  sprintf(cratename,"pedestal_vtime_tree_crate%i",rocid);
289  sprintf(title,"Crate %i avg pedestal (F250)",rocid);
290  pedestal_vtime_tree[rocid] = new TTree(cratename,title);
291  pedestal_vtime_tree[rocid]->Branch("time",&treetime);
292  //pedestal_vtime_tree[rocid]->Branch("pedestal",&treepedestal[rocid]);
293  pedestal_vtime_tree[rocid]->Branch("pedmean",&pedmean);
294  pedestal_vtime_tree[rocid]->Branch("pedrms",&pedrms);
295  pedestal_vtime_tree[rocid]->Branch("pednumsamps",&pednumsamps);
296  sprintf(cratename,"pedestal_vtime_hist_crate%i",rocid);
297  pedestal_vtime_hist[rocid] = new TH1F(cratename,title,100,0.,0.);
298  }
299  // keep a running stats of the pedestal
300  if (hit->pedestal>0) pedestal_vtime_hist[rocid]->Fill(hit->pedestal);
301  // if more than periodlength has elapsed, then end the average and fill the tree
303  if (VERBOSE>=3) printf("JEventProcessor_pedestal_online::evnt filling tree crate %i\n",rocid);
304  treetime = periodstarttime[rocid];
305  pedmean = pedestal_vtime_hist[rocid]->GetMean();
306  pedrms = pedestal_vtime_hist[rocid]->GetRMS();
307  pednumsamps = pedestal_vtime_hist[rocid]->GetEntries();
308  if (VERBOSE>=3) printf("\t\t%li %li %li %li %f %f %i\n",
311  pedestal_vtime_tree[rocid]->Fill();
312  pedestal_vtime_hist[rocid]->Reset();
313  }
314  // advance to the next period
315  while (recentwalltime > periodstarttime[rocid]+periodlength) {
318  }
319  }
320 
321  if (pedestal_vevent[rocid]==NULL) {
322  if (VERBOSE>=1) printf("JEventProcessor_pedestal_online::evnt creating event histogram for crate %i\n",rocid);
323  sprintf(cratename,"pedestal_vevent_crate%i",rocid);
324  sprintf(title,"Crate %i avg pedestal (F125);event num;pedestal (all chan avg)",rocid);
325  pedestal_vevent[rocid] = new TProfile(cratename,title,200,0.0,10000.0);
326  pedestal_vevent[rocid]->SetStats(0);
327 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
328  pedestal_vevent[rocid]->SetCanExtend(TH1::kXaxis);
329 #else
330  pedestal_vevent[rocid]->SetBit(TH1::kCanRebin);
331 #endif
332  }
333  pedestal_vevent[rocid]->Fill(eventnumber,hit->pedestal);
334  }
335  }
336 
337  maindir->cd();
338  // Unlock ROOT
339  japp->RootUnLock();
340 
341  return NOERROR;
342 }
343 
344 
345 //------------------
346 // erun
347 //------------------
349 {
350  // This is called whenever the run number changes, before it is
351  // changed to give you a chance to clean up before processing
352  // events from the next run number.
353 
354  // Lock ROOT
355  japp->RootWriteLock();
356 
357  peddir->cd();
358 
359  char cratename[255],title[255];
360 
361  int timeofrun = (globalstoptime - globalstarttime);
362  int numberofbins = timeofrun/periodlength;
363 
364  for (int i=0; i<highcratenum; i++) {
365  // if there's a tree make a histogram
366  if (pedestal_vtime_tree[i]!=NULL) {
367  sprintf(title,"Crate %i avg pedestal (F250)",i);
368  sprintf(cratename,"pedestal_vtime_hist_crate%i",i);
369  pedestal_vtime_hist[i] = new TH1F(cratename,title,numberofbins,globalstarttime,globalstoptime);
370  pedestal_vtime_hist[i]->SetStats(0);
371  pedestal_vtime_hist[i]->GetXaxis()->SetTimeDisplay(1);
372  pedestal_vtime_hist[i]->GetXaxis()->SetTimeFormat("%H:%M %F 1970-01-01 00:00:00");
373  // pedestal_vtime_hist[i]->GetXaxis()->SetTimeFormat("%m/%d %H:%M %F 1970-01-01 00:00:00");
374 
375  Int_t nevent = pedestal_vtime_tree[i]->GetEntries();
376  for (Int_t event=0;event<nevent;event++) {
377  pedestal_vtime_tree[i]->GetEvent(event);
378  if (pednumsamps>5) {
379  int bin = pedestal_vtime_hist[i]->GetXaxis()->FindBin(treetime);
380  float pederr = pedrms/sqrt(pednumsamps);
381  if (VERBOSE>=3) printf("crate %i event %i bin %i %f %f %i %f\n",i, event,bin,pedmean, pedrms, pednumsamps,pederr);
382  pedestal_vtime_hist[i]->SetBinContent(bin,pedmean);
383  pedestal_vtime_hist[i]->SetBinError(bin,pederr);
384  }
385  }
386  pedestal_vtime_hist[i]->SetMinimum(pedestal_vtime_hist[i]->GetMinimum(0.1));
387  }
388 
389  if (pedestal_vevent[i] != NULL) {
390  pedestal_vevent[i]->SetMinimum(pedestal_vevent[i]->GetMinimum(0.1));
391  }
392  }
393 
394  // back to main dir
395  maindir->cd();
396 
397  // Unlock ROOT
398  japp->RootUnLock();
399  return NOERROR;
400 }
401 
402 //------------------
403 // fini
404 //------------------
406 {
407  // Called before program exit after event processing is finished.
408  return NOERROR;
409 }
410 
uint32_t pedestal
from Pulse Integral Data word (future)
long int recentwalltime
static const time_t periodlength
sprintf(text,"Post KinFit Cut")
static const int highcratenum
static TProfile * pedestal_vevent[highcratenum]
static TH1F * pedestal_vtime_hist[highcratenum]
jerror_t fini(void)
Called after last event of last event source has been processed.
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
static TTree * pedestal_vtime_tree[highcratenum]
JApplication * japp
InitPlugin_t InitPlugin
const bool VERBOSE
uint32_t pedestal
float treepedestal[highcratenum]
long int globalstarttime
jerror_t init(void)
Called once at program start.
double sqrt(double)
uint32_t pedestal
from Pulse Integral Data word (future)
uint32_t nsamples_pedestal
number of samples used in pedestal
uint32_t rocid
Definition: DDAQAddress.h:32
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
uint32_t nsamples_pedestal
number of samples used in pedestal
long int periodstarttime[highcratenum]
printf("string=%s", string)
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
long int globalstoptime