/[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.14 - (hide annotations) (download)
Fri Sep 7 21:14:22 2007 UTC (17 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.13: +7 -2 lines
Close MySQL connection before start the main loop in core routines

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.4 int TrigCore(UInt_t run, TFile *file, TSQLServer *dbc, 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     //
162     // DB classes
163     //
164     GL_ROOT *glroot = new GL_ROOT();
165 mocchiut 1.4 GL_TIMESYNC *dbtime = 0;
166 mocchiut 1.1 //
167     // Let's start!
168     //
169     //
170     // 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
171     // if run != 0 we must process only that run but first we have to check if the tree Trigger already exist in the file
172     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
173     //
174 mocchiut 1.4 if ( run == 0 ) reproc = true;
175 mocchiut 1.1 //
176     //
177     // Output file is "outputfile"
178     //
179     if ( !file->IsOpen() ){
180     if ( verbose ) printf(" Trigger - ERROR: cannot open file for writing\n");
181     throw -401;
182     };
183     //
184     // Retrieve GL_RUN variables from the level2 file
185     //
186     trigversion = TrigInfo(false); // we should decide how to handle versioning system
187     //
188     // create an interface to RunInfo called "runinfo"
189     //
190     runinfo = new ItoRunInfo(file);
191     //
192     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
193     //
194     sgnl = 0;
195     sgnl = runinfo->Update(run,"TRIG",trigversion);
196     if ( sgnl ){
197     if ( verbose ) printf(" Trigger - ERROR: RunInfo exited with non-zero status\n");
198     code = sgnl;
199     goto closeandexit;
200     } else {
201     sgnl = 0;
202     };
203     //
204     // number of events in the file BEFORE the first event of our run
205     //
206     nobefrun = runinfo->GetFirstEntry();
207     //
208     // total number of events in the file
209     //
210     totfileentries = runinfo->GetFileEntries();
211     //
212     // first file entry AFTER the last event of our run
213     //
214     noaftrun = runinfo->GetLastEntry() + 1;
215     //
216     // number of run to be processed
217     //
218     numbofrun = runinfo->GetNoRun();
219 mocchiut 1.12 UInt_t totnorun = runinfo->GetRunEntries();
220 mocchiut 1.1 //
221     // Try to access the Trigger tree in the file, if it exists we are reprocessing data if not we are processing a new run
222     //
223     trigtclone = (TTree*)file->Get("Trigger");
224     //
225     if ( !trigtclone ){
226     //
227     // tree does not exist, we are not reprocessing
228     //
229     reproc = false;
230 mocchiut 1.4 if ( run == 0 && verbose ) printf(" Trigger - WARNING: you are reprocessing data but Trigger tree does not exist!\n");
231     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");
232 mocchiut 1.1
233     } else {
234     //
235     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
236     //
237 mocchiut 1.7 trigtclone->SetAutoSave(900000000000000LL);
238 mocchiut 1.1 reproc = true;
239     //
240     // update versioning information
241     //
242     if ( verbose ) printf("\n Preparing the pre-processing...\n");
243     //
244 mocchiut 1.12 if ( run == 0 || totnorun == 1 ){
245 mocchiut 1.1 //
246     // we are reprocessing all the file
247     // 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
248     //
249     reprocall = true;
250     //
251     if ( verbose ) printf("\n Trigger - WARNING: Reprocessing all runs\n");
252     //
253     } else {
254     //
255     // 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
256     //
257     reprocall = false;
258     //
259 mocchiut 1.4 if ( verbose ) printf("\n Trigger - WARNING: Reprocessing run number %u \n",run);
260 mocchiut 1.1 //
261     // copying old tree to a new file
262     //
263 mocchiut 1.13 gSystem->MakeDirectory(Trigfolder.str().c_str());
264     myfold = true;
265 mocchiut 1.1 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
266     temptrig = trigtclone->CloneTree(-1,"fast");
267     temptrig->SetName("Trigger-old");
268     tempfile->Write();
269     tempfile->Close();
270     }
271     //
272     // Delete the old tree from old file and memory
273     //
274     trigtclone->Delete("all");
275     //
276     if ( verbose ) printf(" ...done!\n");
277     //
278     };
279     //
280     // create Trigger tree trigt
281     //
282     file->cd();
283     trigt = new TTree("Trigger-new","PAMELA Level2 Trigger data");
284 mocchiut 1.7 trigt->SetAutoSave(900000000000000LL);
285 mocchiut 1.1 trigt->Branch("TrigLevel2","TrigLevel2",&trig);
286     //
287     if ( reproc && !reprocall ){
288     //
289     // open new file and retrieve alo tree informations
290     //
291     tempfile = new TFile(tempname.str().c_str(),"READ");
292     trigtclone = (TTree*)tempfile->Get("Trigger-old");
293 mocchiut 1.7 trigtclone->SetAutoSave(900000000000000LL);
294 mocchiut 1.1 trigtclone->SetBranchAddress("TrigLevel2",&trigclone);
295     //
296     if ( nobefrun > 0 ){
297     if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
298 mocchiut 1.4 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
299 mocchiut 1.1 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
300     for (UInt_t j = 0; j < nobefrun; j++){
301     //
302     trigtclone->GetEntry(j);
303     //
304     // copy trigclone to trig
305     //
306 mocchiut 1.2 // trig = new TrigLevel2();
307     trig->Clear();
308 mocchiut 1.1 memcpy(&trig,&trigclone,sizeof(trigclone));
309     //
310     // Fill entry in the new tree
311     //
312     trigt->Fill();
313     //
314     };
315     if ( verbose ) printf(" Finished successful copying!\n");
316     };
317     };
318     //
319     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
320     //
321     runlist = runinfo->GetRunList();
322     //
323     // Loop over the run to be processed
324     //
325     for (UInt_t irun=0; irun < numbofrun; irun++){
326     //
327     // retrieve the first run ID to be processed using the RunInfo list
328     //
329     idRun = runlist->At(irun);
330     if ( verbose ) printf("\n\n\n ####################################################################### \n");
331 mocchiut 1.4 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
332 mocchiut 1.1 if ( verbose ) printf(" ####################################################################### \n\n\n");
333     //
334 mocchiut 1.4 runinfo->ID_ROOT_L0 = 0;
335 mocchiut 1.1 //
336     // store in the runinfo class the GL_RUN variables for our run
337     //
338     sgnl = 0;
339     sgnl = runinfo->GetRunInfo(idRun);
340     if ( sgnl ){
341     if ( verbose ) printf(" Trigger - ERROR: RunInfo exited with non-zero status\n");
342     code = sgnl;
343     goto closeandexit;
344     } else {
345     sgnl = 0;
346     };
347     //
348 mocchiut 1.4 // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0
349 mocchiut 1.1 //
350 mocchiut 1.4 if ( runinfo->ID_ROOT_L0 == 0 ){
351     if ( verbose ) printf("\n Trigger - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
352 mocchiut 1.1 code = -5;
353     goto closeandexit;
354     };
355     //
356 mocchiut 1.4 // prepare the timesync for the db
357     //
358 mocchiut 1.14 // if ( !dbc->IsConnected() ) throw -403;
359 mocchiut 1.4 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
360     //
361 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
362     //
363 mocchiut 1.14 // if ( !dbc->IsConnected() ) throw -403;
364 mocchiut 1.4 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
365 mocchiut 1.1 //
366     ftmpname.str("");
367     ftmpname << glroot->PATH.Data() << "/";
368     ftmpname << glroot->NAME.Data();
369     fname = ftmpname.str().c_str();
370     //
371     // print out informations
372     //
373 mocchiut 1.4 totevent = runinfo->NEVENTS;
374 mocchiut 1.1 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
375 mocchiut 1.4 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
376     if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
377     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);
378 mocchiut 1.1 //
379     // Open Level0 file
380     //
381     l0File = new TFile(fname.Data());
382     if ( !l0File ) {
383     if ( verbose ) printf(" Trigger - ERROR: problems opening Level0 file\n");
384     code = -6;
385     goto closeandexit;
386     };
387     l0tr = (TTree*)l0File->Get("Physics");
388     if ( !l0tr ) {
389     if ( verbose ) printf(" Trigger - ERROR: no Physics tree in Level0 file\n");
390     l0File->Close();
391     code = -7;
392     goto closeandexit;
393     };
394     l0head = l0tr->GetBranch("Header");
395     if ( !l0head ) {
396     if ( verbose ) printf(" Trigger - ERROR: no Header branch in Level0 tree\n");
397     l0File->Close();
398     code = -8;
399     goto closeandexit;
400     };
401     l0trig = l0tr->GetBranch("Trigger");
402     if ( !l0trig ) {
403     if ( verbose ) printf(" Trigger - ERROR: no Trigger branch in Level0 tree\n");
404     l0File->Close();
405     code = -402;
406     goto closeandexit;
407     };
408     //
409     l0tr->SetBranchAddress("Trigger", &triggerEvent);
410 mocchiut 1.4 l0tr->SetBranchAddress("Header", &eh);
411 mocchiut 1.1 //
412 mocchiut 1.4 nevents = l0trig->GetEntries();
413 mocchiut 1.1 //
414     if ( nevents < 1 ) {
415     if ( verbose ) printf(" Trigger - ERROR: Level0 file is empty\n\n");
416     l0File->Close();
417     code = -11;
418     goto closeandexit;
419     };
420     //
421 mocchiut 1.4 if ( runinfo->EV_TO > nevents-1 ) {
422     if ( verbose ) printf(" Trigger - ERROR: too few entries in the tree\n");
423 mocchiut 1.1 l0File->Close();
424     code = -12;
425     goto closeandexit;
426     };
427     //
428     // run over all the events of the run
429     //
430     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
431     //
432 mocchiut 1.14 if ( dbc ){
433     dbc->Close();
434     // delete dbc;
435     };
436     //
437 mocchiut 1.4 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
438 mocchiut 1.1 //
439     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
440     //
441 mocchiut 1.4 l0head->GetEntry(re);
442 mocchiut 1.1 //
443     // absolute time of this event
444     //
445 mocchiut 1.4 ph = eh->GetPscuHeader();
446     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
447 mocchiut 1.1 //
448     // paranoid check
449     //
450     if ( atime > runinfo->RUNTRAILER_TIME || atime < runinfo->RUNHEADER_TIME ) {
451     if ( verbose ) printf(" Trigger - WARNING: event at time outside the run time window, skipping it\n");
452     goto jumpev;
453     };
454     ///
455 mocchiut 1.4 l0trig->GetEntry(re);
456 mocchiut 1.1 ///
457     //
458     procev++;
459     //
460     // start processing
461     //
462 mocchiut 1.2 trig->Clear();
463     // trig = new TrigLevel2();
464 mocchiut 1.1 //
465     // now we must copy from the output structure to the level2 class:
466     //
467     trig->evcount = triggerEvent->evcount;
468     for (Int_t kk=0; kk<3;kk++){
469     trig->pmtpl[kk] = triggerEvent->pmtpl[kk];
470     trig->patternbusy[kk] = triggerEvent->patternbusy[kk];
471     }
472    
473     for (Int_t kk=0; kk<6;kk++){
474     trig->trigrate[kk] = triggerEvent->trigrate[kk];
475     trig->patterntrig[kk] = triggerEvent->patterntrig[kk];
476     }
477    
478     for (Int_t kk=0; kk<2;kk++){
479     trig->dltime[kk] = triggerEvent->dltime[kk];
480     trig->s4calcount[kk] = triggerEvent->s4calcount[kk];
481     }
482    
483     for (Int_t kk=0; kk<24;kk++){
484     trig->pmtcount1[kk] = triggerEvent->pmtcount1[kk];
485     trig->pmtcount2[kk] = triggerEvent->pmtcount2[kk];
486     }
487    
488     trig->trigconf = triggerEvent->trigconf;
489 mocchiut 1.5
490     trig->unpackError = triggerEvent->unpackError;
491 mocchiut 1.1
492     trigt->Fill();
493     //
494     //
495     jumpev:
496     debug = false;
497     //
498     };
499 mocchiut 1.4 //
500     // Here you may want to clear some variables before processing another run
501     //
502     delete dbtime;
503 mocchiut 1.1 }; // process all the runs
504     //
505     if ( verbose ) printf("\n Finished processing data \n");
506     //
507     closeandexit:
508     //
509     // 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.
510     //
511     if ( !reprocall && reproc && code >= 0 ){
512     if ( totfileentries > noaftrun ){
513     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
514     if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
515     if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
516     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
517     //
518     // Get entry from old tree
519     //
520     trigtclone->GetEntry(j);
521     //
522     // copy trigclone to trig
523     //
524 mocchiut 1.2 trig->Clear();
525 mocchiut 1.4 //
526 mocchiut 1.1 memcpy(&trig,&trigclone,sizeof(trigclone));
527     //
528     // Fill entry in the new tree
529     //
530     trigt->Fill();
531     };
532     if ( verbose ) printf(" Finished successful copying!\n");
533     };
534     };
535     //
536     // Close files, delete old tree(s), write and close level2 file
537     //
538     if ( l0File ) l0File->Close();
539     if ( tempfile ) tempfile->Close();
540 mocchiut 1.13 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
541 mocchiut 1.1 //
542     if ( code < 0 && verbose ) printf("\n Trigger - ERROR: an error occurred, try to save anyway...\n");
543     if ( verbose ) printf("\n Writing and closing rootple\n");
544     if ( runinfo ) runinfo->Close();
545     if ( trigt ) trigt->SetName("Trigger");
546     if ( file ){
547     file->cd();
548     file->Write();
549     // file->Write("Trigger");
550     };
551     //
552 mocchiut 1.13 if ( myfold ) gSystem->Unlink(Trigfolder.str().c_str());
553 mocchiut 1.1 //
554     // the end
555     //
556     if ( verbose ) printf("\n Exiting...\n");
557     if ( trigt ) trigt->Delete();
558 mocchiut 1.3 //
559     if ( trig ) delete trig;
560     if ( trigclone ) delete trigclone;
561     if ( glroot ) delete glroot;
562     if ( runinfo ) delete runinfo;
563     //
564 mocchiut 1.1 if ( code < 0 ) throw code;
565     return(code);
566     }
567    
568    
569    

  ViewVC Help
Powered by ViewVC 1.1.23