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

  ViewVC Help
Powered by ViewVC 1.1.23