/[PAMELA software]/DarthVader/TriggerLevel2/src/TrigCore.cpp
ViewVC logotype

Annotation of /DarthVader/TriggerLevel2/src/TrigCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (hide annotations) (download)
Wed Nov 6 13:22:15 2013 UTC (11 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.24: +1 -1 lines
New code for retracking + small bug on run reprocessing fixed

1 mocchiut 1.1 //
2     // C/C++ headers
3     //
4     #include <fstream>
5     #include <string.h>
6     //
7     // ROOT headers
8     //
9     #include <TTree.h>
10     #include <TClassEdit.h>
11     #include <TObject.h>
12     #include <TList.h>
13     #include <TArrayL.h>
14     #include <TSystem.h>
15     #include <TSystemDirectory.h>
16     #include <TString.h>
17     #include <TFile.h>
18     #include <TClass.h>
19     #include <TCanvas.h>
20     #include <TH1.h>
21     #include <TH1F.h>
22     #include <TH2D.h>
23     #include <TLatex.h>
24     #include <TPad.h>
25     #include <TSQLServer.h>
26     #include <TSQLRow.h>
27     #include <TSQLResult.h>
28     #include <TClonesArray.h>
29     //
30 mocchiut 1.8 // RunInfo header
31     //
32     #include <RunInfo.h>
33     //
34 mocchiut 1.1 // YODA headers
35     //
36     #include <PamelaRun.h>
37     #include <physics/trigger/TriggerEvent.h>
38     //
39     // This program headers
40     //
41     #include <TrigCore.h>
42     #include <TrigLevel2.h>
43     #include <TrigVerl2.h>
44     //
45     using namespace std;
46     //
47     //
48     // CORE ROUTINE
49     //
50     //
51 mocchiut 1.15 int TrigCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t Trigargc, char *Trigargv[]){
52 mocchiut 1.1 //
53 mocchiut 1.11 TString processFolder = Form("TrigFolder_%u",run);
54 mocchiut 1.1 //
55     // Set these to true to have a very verbose output.
56     //
57     Bool_t verbose = false;
58     Bool_t debug = false;
59     //
60     if ( Trigargc > 0 ){
61     Int_t i = 0;
62     while ( i < Trigargc ){
63     if ( !strcmp(Trigargv[i],"-processFolder") ) {
64     if ( Trigargc < i+1 ){
65     throw -3;
66     };
67     processFolder = (TString)Trigargv[i+1];
68     i++;
69     };
70     if ( !strcmp(Trigargv[i],"-v") || !strcmp(Trigargv[i],"--verbose") ) {
71     verbose = true;
72     };
73     if ( !strcmp(Trigargv[i],"-g") || !strcmp(Trigargv[i],"--debug") ) {
74 mocchiut 1.10 verbose = true;
75 mocchiut 1.1 debug = true;
76     };
77     i++;
78     };
79     };
80     //
81     //
82     // Output directory is the working directoy.
83     //
84     const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath()));
85     //
86     // Variables for level2
87     //
88 mocchiut 1.7 Long64_t maxsize = 10000000000LL;
89     TTree::SetMaxTreeSize(maxsize);
90     //
91 mocchiut 1.1 TTree *trigt = 0;
92 mocchiut 1.4 UInt_t nevents = 0;
93 mocchiut 1.1 //
94     // variables needed to reprocess data
95     //
96     TString trigversion;
97     ItoRunInfo *runinfo = 0;
98 mocchiut 1.4 TArrayI *runlist = 0;
99 mocchiut 1.1 TTree *trigtclone = 0;
100     Bool_t reproc = false;
101     Bool_t reprocall = false;
102     UInt_t nobefrun = 0;
103     UInt_t noaftrun = 0;
104     UInt_t numbofrun = 0;
105     stringstream ftmpname;
106     TString fname;
107 mocchiut 1.4 UInt_t totfileentries = 0;
108     UInt_t idRun = 0;
109 mocchiut 1.1 //
110     // variables needed to handle error signals
111     //
112     Int_t code = 0;
113     Int_t sgnl;
114     //
115     // trigger level2 classes
116     //
117     TrigLevel2 *trig = new TrigLevel2();
118     TrigLevel2 *trigclone = new TrigLevel2();
119     //
120     // define variables for opening and reading level0 file
121     //
122     TFile *l0File = 0;
123     TTree *l0tr = 0;
124     TBranch *l0head = 0;
125     TBranch *l0trig = 0;
126 mocchiut 1.4 pamela::EventHeader *eh = 0;
127     pamela::PscuHeader *ph = 0;
128 mocchiut 1.1 pamela::trigger::TriggerEvent *triggerEvent = 0;
129     //
130     // Define other basic variables
131     //
132     UInt_t procev = 0;
133     stringstream file2;
134     stringstream file3;
135     stringstream qy;
136     Int_t totevent = 0;
137 mocchiut 1.4 UInt_t atime = 0;
138     UInt_t re = 0;
139 mocchiut 1.1 //
140     // Working filename
141     //
142     TString outputfile;
143     stringstream name;
144     name.str("");
145     name << outdir << "/";
146     //
147     // temporary file and folder
148     //
149     TFile *tempfile = 0;
150     TTree *temptrig = 0;
151     stringstream tempname;
152     stringstream Trigfolder;
153 mocchiut 1.13 Bool_t myfold = false;
154 mocchiut 1.1 tempname.str("");
155     tempname << outdir;
156     tempname << "/" << processFolder.Data();
157     Trigfolder.str("");
158     Trigfolder << tempname.str().c_str();
159     tempname << "/trigtree_run";
160     tempname << run << ".root";
161 mocchiut 1.20 UInt_t totnorun = 0;
162 mocchiut 1.1 //
163     // DB classes
164     //
165     GL_ROOT *glroot = new GL_ROOT();
166 mocchiut 1.4 GL_TIMESYNC *dbtime = 0;
167 mocchiut 1.1 //
168     // Let's start!
169     //
170     //
171     // As a first thing we must check what we have to do: if run = 0 we must process all events in the file has been passed
172     // if run != 0 we must process only that run but first we have to check if the tree Trigger already exist in the file
173     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
174     //
175 mocchiut 1.4 if ( run == 0 ) reproc = true;
176 mocchiut 1.1 //
177     //
178     // Output file is "outputfile"
179     //
180     if ( !file->IsOpen() ){
181     if ( verbose ) printf(" Trigger - ERROR: cannot open file for writing\n");
182     throw -401;
183     };
184     //
185     // Retrieve GL_RUN variables from the level2 file
186     //
187     trigversion = TrigInfo(false); // we should decide how to handle versioning system
188     //
189     // create an interface to RunInfo called "runinfo"
190     //
191     runinfo = new ItoRunInfo(file);
192     //
193     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
194     //
195     sgnl = 0;
196     sgnl = runinfo->Update(run,"TRIG",trigversion);
197     if ( sgnl ){
198     if ( verbose ) printf(" Trigger - ERROR: RunInfo exited with non-zero status\n");
199     code = sgnl;
200     goto closeandexit;
201     } else {
202     sgnl = 0;
203     };
204     //
205     // number of events in the file BEFORE the first event of our run
206     //
207     nobefrun = runinfo->GetFirstEntry();
208     //
209     // total number of events in the file
210     //
211     totfileentries = runinfo->GetFileEntries();
212     //
213     // first file entry AFTER the last event of our run
214     //
215     noaftrun = runinfo->GetLastEntry() + 1;
216     //
217     // number of run to be processed
218     //
219     numbofrun = runinfo->GetNoRun();
220 mocchiut 1.20 totnorun = runinfo->GetRunEntries();
221 mocchiut 1.1 //
222     // Try to access the Trigger tree in the file, if it exists we are reprocessing data if not we are processing a new run
223     //
224     trigtclone = (TTree*)file->Get("Trigger");
225     //
226     if ( !trigtclone ){
227     //
228     // tree does not exist, we are not reprocessing
229     //
230     reproc = false;
231 mocchiut 1.4 if ( run == 0 && verbose ) printf(" Trigger - WARNING: you are reprocessing data but Trigger tree does not exist!\n");
232     if ( runinfo->IsReprocessing() && run != 0 && verbose ) printf(" Trigger - WARNING: it seems you are not reprocessing data but Trigger\n versioning information already exists in RunInfo.\n");
233 mocchiut 1.1
234     } else {
235     //
236     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
237     //
238 mocchiut 1.7 trigtclone->SetAutoSave(900000000000000LL);
239 mocchiut 1.1 reproc = true;
240     //
241     // update versioning information
242     //
243     if ( verbose ) printf("\n Preparing the pre-processing...\n");
244     //
245 mocchiut 1.12 if ( run == 0 || totnorun == 1 ){
246 mocchiut 1.1 //
247     // we are reprocessing all the file
248     // if we are reprocessing everything we don't need to copy any old event and we can just work with the new tree and delete the old one immediately
249     //
250     reprocall = true;
251     //
252     if ( verbose ) printf("\n Trigger - WARNING: Reprocessing all runs\n");
253     //
254     } else {
255     //
256     // we are reprocessing a single run, we must copy to the new tree the events in the file which preceed the first event of the run
257     //
258     reprocall = false;
259     //
260 mocchiut 1.4 if ( verbose ) printf("\n Trigger - WARNING: Reprocessing run number %u \n",run);
261 mocchiut 1.1 //
262     // copying old tree to a new file
263     //
264 mocchiut 1.13 gSystem->MakeDirectory(Trigfolder.str().c_str());
265     myfold = true;
266 mocchiut 1.1 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
267     temptrig = trigtclone->CloneTree(-1,"fast");
268     temptrig->SetName("Trigger-old");
269     tempfile->Write();
270     tempfile->Close();
271     }
272     //
273     // Delete the old tree from old file and memory
274     //
275     trigtclone->Delete("all");
276     //
277     if ( verbose ) printf(" ...done!\n");
278     //
279     };
280     //
281     // create Trigger tree trigt
282     //
283     file->cd();
284     trigt = new TTree("Trigger-new","PAMELA Level2 Trigger data");
285 mocchiut 1.7 trigt->SetAutoSave(900000000000000LL);
286 mocchiut 1.1 trigt->Branch("TrigLevel2","TrigLevel2",&trig);
287     //
288     if ( reproc && !reprocall ){
289     //
290     // open new file and retrieve alo tree informations
291     //
292     tempfile = new TFile(tempname.str().c_str(),"READ");
293     trigtclone = (TTree*)tempfile->Get("Trigger-old");
294 mocchiut 1.7 trigtclone->SetAutoSave(900000000000000LL);
295 mocchiut 1.1 trigtclone->SetBranchAddress("TrigLevel2",&trigclone);
296     //
297     if ( nobefrun > 0 ){
298     if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
299 mocchiut 1.4 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
300 mocchiut 1.1 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
301     for (UInt_t j = 0; j < nobefrun; j++){
302     //
303 mocchiut 1.21 if ( trigtclone->GetEntry(j) <= 0 ) throw -36;
304 mocchiut 1.1 //
305     // copy trigclone to trig
306     //
307 mocchiut 1.2 // trig = new TrigLevel2();
308     trig->Clear();
309 mocchiut 1.1 memcpy(&trig,&trigclone,sizeof(trigclone));
310     //
311     // Fill entry in the new tree
312     //
313     trigt->Fill();
314     //
315     };
316     if ( verbose ) printf(" Finished successful copying!\n");
317     };
318     };
319     //
320     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
321     //
322     runlist = runinfo->GetRunList();
323     //
324     // Loop over the run to be processed
325     //
326     for (UInt_t irun=0; irun < numbofrun; irun++){
327     //
328     // retrieve the first run ID to be processed using the RunInfo list
329     //
330     idRun = runlist->At(irun);
331     if ( verbose ) printf("\n\n\n ####################################################################### \n");
332 mocchiut 1.4 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
333 mocchiut 1.1 if ( verbose ) printf(" ####################################################################### \n\n\n");
334     //
335 mocchiut 1.4 runinfo->ID_ROOT_L0 = 0;
336 mocchiut 1.1 //
337     // store in the runinfo class the GL_RUN variables for our run
338     //
339     sgnl = 0;
340     sgnl = runinfo->GetRunInfo(idRun);
341     if ( sgnl ){
342     if ( verbose ) printf(" Trigger - ERROR: RunInfo exited with non-zero status\n");
343     code = sgnl;
344     goto closeandexit;
345     } else {
346     sgnl = 0;
347     };
348     //
349 mocchiut 1.4 // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0
350 mocchiut 1.1 //
351 mocchiut 1.4 if ( runinfo->ID_ROOT_L0 == 0 ){
352     if ( verbose ) printf("\n Trigger - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
353 mocchiut 1.1 code = -5;
354     goto closeandexit;
355     };
356     //
357 mocchiut 1.4 // prepare the timesync for the db
358     //
359 mocchiut 1.15 TString host = glt->CGetHost();
360     TString user = glt->CGetUser();
361     TString psw = glt->CGetPsw();
362     TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
363     if ( !dbc->IsConnected() ) throw -403;
364 mocchiut 1.16 stringstream myquery;
365     myquery.str("");
366     myquery << "SET time_zone='+0:00'";
367 mocchiut 1.24 delete dbc->Query(myquery.str().c_str());
368 mocchiut 1.4 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
369     //
370 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
371     //
372 mocchiut 1.14 // if ( !dbc->IsConnected() ) throw -403;
373 mocchiut 1.4 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
374 mocchiut 1.1 //
375     ftmpname.str("");
376     ftmpname << glroot->PATH.Data() << "/";
377     ftmpname << glroot->NAME.Data();
378     fname = ftmpname.str().c_str();
379     //
380     // print out informations
381     //
382 mocchiut 1.4 totevent = runinfo->NEVENTS;
383 mocchiut 1.1 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
384 mocchiut 1.4 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
385     if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
386     if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i \n\n",totevent,idRun,runinfo->EV_FROM,runinfo->EV_FROM+totevent);
387 mocchiut 1.1 //
388 mocchiut 1.18 // if ( !totevent ) goto closeandexit;
389 mocchiut 1.17 //
390 mocchiut 1.1 // Open Level0 file
391     //
392 mocchiut 1.24 if ( l0File ) l0File->Close();
393 mocchiut 1.1 l0File = new TFile(fname.Data());
394     if ( !l0File ) {
395     if ( verbose ) printf(" Trigger - ERROR: problems opening Level0 file\n");
396     code = -6;
397     goto closeandexit;
398     };
399     l0tr = (TTree*)l0File->Get("Physics");
400     if ( !l0tr ) {
401     if ( verbose ) printf(" Trigger - ERROR: no Physics tree in Level0 file\n");
402     l0File->Close();
403     code = -7;
404     goto closeandexit;
405     };
406     l0head = l0tr->GetBranch("Header");
407     if ( !l0head ) {
408     if ( verbose ) printf(" Trigger - ERROR: no Header branch in Level0 tree\n");
409     l0File->Close();
410     code = -8;
411     goto closeandexit;
412     };
413     l0trig = l0tr->GetBranch("Trigger");
414     if ( !l0trig ) {
415     if ( verbose ) printf(" Trigger - ERROR: no Trigger branch in Level0 tree\n");
416     l0File->Close();
417     code = -402;
418     goto closeandexit;
419     };
420     //
421     l0tr->SetBranchAddress("Trigger", &triggerEvent);
422 mocchiut 1.4 l0tr->SetBranchAddress("Header", &eh);
423 mocchiut 1.1 //
424 mocchiut 1.4 nevents = l0trig->GetEntries();
425 mocchiut 1.1 //
426 mocchiut 1.18 if ( nevents < 1 && totevent ) {
427 mocchiut 1.1 if ( verbose ) printf(" Trigger - ERROR: Level0 file is empty\n\n");
428     l0File->Close();
429     code = -11;
430     goto closeandexit;
431     };
432     //
433 mocchiut 1.18 if ( runinfo->EV_TO > nevents-1 && totevent ) {
434 mocchiut 1.4 if ( verbose ) printf(" Trigger - ERROR: too few entries in the tree\n");
435 mocchiut 1.1 l0File->Close();
436     code = -12;
437     goto closeandexit;
438     };
439     //
440     // run over all the events of the run
441     //
442     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
443     //
444 mocchiut 1.14 if ( dbc ){
445     dbc->Close();
446 mocchiut 1.15 delete dbc;
447 pam-fi 1.22 dbc = 0;
448 mocchiut 1.14 };
449     //
450 mocchiut 1.4 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
451 mocchiut 1.1 //
452     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
453     //
454 mocchiut 1.21 if ( l0head->GetEntry(re) <= 0 ) throw -36;
455 mocchiut 1.1 //
456     // absolute time of this event
457     //
458 mocchiut 1.4 ph = eh->GetPscuHeader();
459     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
460 mocchiut 1.1 //
461     // paranoid check
462     //
463 mocchiut 1.19 if ( atime > (runinfo->RUNTRAILER_TIME+1) || atime < (runinfo->RUNHEADER_TIME-1) ) {
464 mocchiut 1.1 if ( verbose ) printf(" Trigger - WARNING: event at time outside the run time window, skipping it\n");
465     goto jumpev;
466     };
467     ///
468 mocchiut 1.21 if ( l0trig->GetEntry(re) <= 0 ) throw -36;
469 mocchiut 1.1 ///
470     //
471     procev++;
472     //
473     // start processing
474     //
475 mocchiut 1.2 trig->Clear();
476     // trig = new TrigLevel2();
477 mocchiut 1.1 //
478     // now we must copy from the output structure to the level2 class:
479     //
480     trig->evcount = triggerEvent->evcount;
481     for (Int_t kk=0; kk<3;kk++){
482     trig->pmtpl[kk] = triggerEvent->pmtpl[kk];
483     trig->patternbusy[kk] = triggerEvent->patternbusy[kk];
484     }
485    
486     for (Int_t kk=0; kk<6;kk++){
487     trig->trigrate[kk] = triggerEvent->trigrate[kk];
488     trig->patterntrig[kk] = triggerEvent->patterntrig[kk];
489     }
490    
491     for (Int_t kk=0; kk<2;kk++){
492     trig->dltime[kk] = triggerEvent->dltime[kk];
493     trig->s4calcount[kk] = triggerEvent->s4calcount[kk];
494     }
495    
496     for (Int_t kk=0; kk<24;kk++){
497     trig->pmtcount1[kk] = triggerEvent->pmtcount1[kk];
498     trig->pmtcount2[kk] = triggerEvent->pmtcount2[kk];
499     }
500    
501     trig->trigconf = triggerEvent->trigconf;
502 mocchiut 1.5
503     trig->unpackError = triggerEvent->unpackError;
504 mocchiut 1.1
505     trigt->Fill();
506     //
507     //
508     jumpev:
509     debug = false;
510     //
511     };
512 mocchiut 1.4 //
513     // Here you may want to clear some variables before processing another run
514     //
515     delete dbtime;
516 mocchiut 1.1 }; // process all the runs
517     //
518     if ( verbose ) printf("\n Finished processing data \n");
519     //
520     closeandexit:
521     //
522     // we have finished processing the run(s). If we processed a single run now we must copy all the events after our run from the old tree to the new one and delete the old tree.
523     //
524     if ( !reprocall && reproc && code >= 0 ){
525     if ( totfileentries > noaftrun ){
526     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
527     if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
528     if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
529     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
530     //
531     // Get entry from old tree
532     //
533 mocchiut 1.21 if ( trigtclone->GetEntry(j) <= 0 ) throw -36;
534 mocchiut 1.1 //
535     // copy trigclone to trig
536     //
537 mocchiut 1.2 trig->Clear();
538 mocchiut 1.4 //
539 mocchiut 1.1 memcpy(&trig,&trigclone,sizeof(trigclone));
540     //
541     // Fill entry in the new tree
542     //
543     trigt->Fill();
544     };
545     if ( verbose ) printf(" Finished successful copying!\n");
546     };
547     };
548     //
549     // Close files, delete old tree(s), write and close level2 file
550     //
551     if ( l0File ) l0File->Close();
552     if ( tempfile ) tempfile->Close();
553 mocchiut 1.13 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
554 mocchiut 1.1 //
555     if ( code < 0 && verbose ) printf("\n Trigger - ERROR: an error occurred, try to save anyway...\n");
556     if ( verbose ) printf("\n Writing and closing rootple\n");
557     if ( trigt ) trigt->SetName("Trigger");
558     if ( file ){
559     file->cd();
560 mocchiut 1.25 if ( trigt ) trigt->Write("Trigger", TObject::kOverwrite); // 10RED bug fixed
561 mocchiut 1.1 // file->Write("Trigger");
562     };
563     //
564 mocchiut 1.13 if ( myfold ) gSystem->Unlink(Trigfolder.str().c_str());
565 mocchiut 1.1 //
566     // the end
567     //
568     if ( verbose ) printf("\n Exiting...\n");
569 mocchiut 1.3 //
570     if ( glroot ) delete glroot;
571 mocchiut 1.23 if ( runinfo ) runinfo->Close();
572 mocchiut 1.3 if ( runinfo ) delete runinfo;
573     //
574 mocchiut 1.1 if ( code < 0 ) throw code;
575     return(code);
576     }
577    
578    
579    

  ViewVC Help
Powered by ViewVC 1.1.23