/[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.6 - (hide annotations) (download)
Fri Nov 17 10:08:11 2006 UTC (18 years ago) by mocchiut
Branch: MAIN
CVS Tags: v2r01
Changes since 1.5: +2 -0 lines
Added some checks on MySQL connection

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

  ViewVC Help
Powered by ViewVC 1.1.23