/[PAMELA software]/calo/flight/MyDetector1Level2/src/MyDect1Core.cpp
ViewVC logotype

Annotation of /calo/flight/MyDetector1Level2/src/MyDect1Core.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Tue Apr 18 13:12:27 2006 UTC (18 years, 9 months ago) by mocchiut
Branch: MAIN
Branch point for: v0r00
Initial revision

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 <MyDect1Core.h>
43     #include <MyDect1Level2.h>
44     #include <MyDect1Verl2.h>
45     //
46     using namespace std;
47     //
48     //
49     // CORE ROUTINE
50     //
51     //
52     int MyDect1Core(ULong64_t run, TString filename, TSQLServer *dbc){
53     //
54     // Set these to true to have a very verbose output.
55     //
56     // Bool_t debug = true;
57     Bool_t debug = false;
58     //
59     // Output directory is the working directoy.
60     //
61     const char* outdir = gSystem->WorkingDirectory();
62     //
63     // Working filename
64     //
65     TString outputfile;
66     stringstream name;
67     name.str("");
68     name << outdir << "/";
69     //
70     // Variables.
71     //
72     TFile *file = 0;
73     TTree *mydect1 = 0;
74     TTree *mydect1clone = 0;
75     //
76     Bool_t reproc = false;
77     Bool_t reprocall = false;
78     Long64_t nevents = 0LL;
79     UInt_t nobefrun = 0;
80     UInt_t noaftrun = 0;
81     UInt_t numbofrun = 0;
82     //
83     Int_t code = 0;
84     Int_t sgnl;
85     //
86     // mydetector1 level2 classes
87     //
88     MyDect1Level2 *mydec = new MyDect1Level2();
89     MyDect1Level2 *mydecclone = new MyDect1Level2();
90     //
91     // MySQL variables
92     //
93     TSQLResult *pResult;
94     TSQLRow *Row;
95     Int_t t;
96     Int_t r;
97     UInt_t procev = 0;
98     stringstream myquery;
99     //
100     // define variables where to store the absolute run header and run trailer times (unsigned long long integers, when set to a number use ULL to store the correct number).
101     //
102     ULong64_t runheadtime = 0ULL;
103     ULong64_t runtrailtime = 0ULL;
104     UInt_t evfrom = 0;
105     UInt_t evto = 0;
106     Long64_t totfileentries = 0ULL;
107     Long64_t idRun = 0LL;
108     Int_t id_reg_run=-1;
109     stringstream ftmpname;
110     TString fname;
111     //
112     // define variables for opening and reading level0 file
113     //
114     TFile *l0File = 0;
115     TTree *l0tr = 0;
116     TBranch *l0head = 0;
117     TBranch *l0registry = 0;
118     TBranch *l0trig = 0;
119     pamela::RegistryEvent *l0reg=0;
120     pamela::trigger::TriggerEvent *trig = 0;
121     //
122     TString mydect1version;
123     RunInfo *runinfo = 0;
124     TArrayL *runlist = 0;
125     Int_t totevent = 0;
126     //
127     //
128     ULong64_t atime = 0ULL;
129     Int_t ei = 0;
130     Int_t S3 = 0;
131     Int_t S2 = 0;
132     Int_t S12 = 0;
133     Int_t S11 = 0;
134     UInt_t re = 0;
135     Float_t tmptrigty = -1.;
136     //
137     // 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
138     // if run != 0 we must process only that run but first we have to check if the branch calorimeter already exist in the file
139     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
140     //
141     if ( run != 0ULL ){
142     //
143     // Run is not 0, we must process only one run .
144     //
145     //
146     // If no filename is given, use the deafult one run_id.Level2.root.
147     //
148     if ( !strcmp(filename.Data(),"") ){
149     name << run << ".Level2.root";
150     } else {
151     name << filename.Data();
152     };
153     //
154     } else {
155     //
156     // Run is 0, we must process all entries in the given file.
157     //
158     //
159     // OUCH! no filename has been given, run out!
160     //
161     if ( !strcmp(filename.Data(),"") ) {
162     printf(" MYDETECTOR1 - ERROR: processing all runs but no filename is given\n");
163     return(-4);
164     };
165     name << filename.Data();
166     reproc = true;
167     //
168     };
169     //
170     // Output file is "outputfile"
171     //
172     outputfile = name.str().c_str();
173     printf("\n Output filename is: \n %s \n\n",outputfile.Data());
174     //
175     // if file exists open it in "update" mode, if not create it (UPDATE will do it).
176     //
177     file = new TFile(outputfile.Data(),"UPDATE");
178     if ( !file->IsOpen() ){
179     printf(" MYDETECTOR1 - ERROR: cannot open file for writing\n");
180     return(-101);
181     };
182     //
183     // Update versioning information for the run(s) we are going to process. We do it calling the RunInfo constructor.
184     //
185     mydect1version = MyDect1Info(false);
186     sgnl = 0;
187     runinfo = new RunInfo(dbc,file,run,"MYDECT1",mydect1version,sgnl);
188     if ( sgnl ){
189     printf(" MYDETECTOR1 - ERROR: RunInfo exited with non-zero status\n");
190     code = sgnl;
191     goto closeandexit;
192     } else {
193     sgnl = 0;
194     };
195     //
196     // number of events in the file BEFORE the first event of our run
197     //
198     nobefrun = runinfo->GetFirstFileEntry();
199     //
200     // total number of events in the file
201     //
202     totfileentries = runinfo->GetFileEntries();
203     //
204     // first file entry AFTER the last event of our run
205     //
206     noaftrun = runinfo->GetLastFileEntry();
207     //
208     // number of run to be processed
209     //
210     numbofrun = runinfo->GetNoRun();
211     //
212     //
213     // Try to access the MyDetector1 tree in the file, if it exists we are reprocessing data if not we are processing a new run
214     //
215     mydect1clone = (TTree*)file->Get("MyDetector1");
216     //
217     // create a new MyDetector1 tree called mydect1
218     //
219     mydect1 = new TTree("MyDetector1","PAMELA Level2 MyDetector1 data");
220     mydect1->Branch("MyDect1Level2","MyDect1Level2",&mydec);
221     //
222     if ( !mydect1clone ){
223     //
224     // tree does not exist, we are not reprocessing
225     //
226     reproc = false;
227     if ( run == 0ULL ) printf(" MYDETECTOR1 - WARNING: you are reprocessing data but mydetector1 tree does not exist!\n");
228     if ( runinfo->IsReprocessing() && run != 0ULL ) printf(" MYDETECTOR1 - WARNING: it seems you are not reprocessing data but mydetector1\n versioning information already exists in RunInfo.\n");
229    
230     } else {
231     //
232     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
233     //
234     reproc = true;
235     //
236     if ( run == 0ULL ){
237     //
238     // we are reprocessing all the file
239     //
240     //
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     printf("\n MYDETECTOR1 - WARNING: Reprocessing all runs\n");
244     reprocall = true;
245     mydect1clone->Delete("all");
246     } else {
247     reprocall = false;
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     printf("\n MYDETECTOR1 - WARNING: Reprocessing run number %i \n",(int)run);
252     //
253     // access old branch in old tree
254     //
255     mydect1clone->SetBranchAddress("MyDect1Level2",&mydecclone);
256     //
257     if ( nobefrun > 0 ){
258     printf("\n Pre-processing: copying events from the old tree before the processed run\n");
259     printf(" Copying %i events in the file which are before the beginning of the run %i \n",(int)(nobefrun),(int)run);
260     printf(" Start copying at event number 0, end copying at event number %i \n",(int)nobefrun);
261     for (UInt_t j = 0; j < nobefrun; j++){
262     //
263     mydect1clone->GetEntry(j);
264     //
265     // copy caclone to ca
266     //
267     memcpy(&mydec,&mydecclone,sizeof(mydec));
268     //
269     // Fill entry in the new tree
270     //
271     mydect1->Fill();
272     //
273     };
274     printf(" Finished successful copying!\n");
275     };
276     };
277     };
278     //
279     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
280     //
281     runlist = runinfo->GetRunList();
282     //
283     // Loop over the run to be processed
284     //
285     for (UInt_t irun=0; irun < numbofrun; irun++){
286     //
287     // retrieve the first run ID to be processed using the RunInfo list
288     //
289     idRun = runlist->At(irun);
290     printf("\n\n\n ####################################################################### \n");
291     printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
292     printf(" ####################################################################### \n\n\n");
293     //
294     // store in the runinfo class the GL_RUN variables for our run
295     //
296     sgnl = runinfo->GetRunInfo(idRun);
297     if ( sgnl ){
298     printf(" MYDETECTOR1 - ERROR: RunInfo exited with non-zero status\n");
299     code = sgnl;
300     goto closeandexit;
301     } else {
302     sgnl = 0;
303     };
304     //
305     // now you can access that variables using the RunInfo class this way
306     //
307     id_reg_run = runinfo->ID_REG_RUN;
308     runheadtime = runinfo->RUNHEADER_TIME;
309     runtrailtime = runinfo->RUNTRAILER_TIME;
310     evfrom = runinfo->EV_REG_PHYS_FROM;
311     evto = runinfo->EV_REG_PHYS_TO;
312     //
313     if ( id_reg_run == -1 ){
314     printf("\n MYDETECTOR1 - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun);
315     code = -5;
316     goto closeandexit;
317     };
318     //
319     // Search in the DB the path and name of the LEVEL0 file to be processed.
320     //
321     printf("\n Querying DB for data files informations...\n");
322     //
323     // where can I find the file with the id I have just recovered?
324     //
325     myquery.str("");
326     myquery << "select PATH,NAME from GL_ROOT where ID=" << id_reg_run;
327     myquery << ";";
328     //
329     if ( debug ) printf("The query is:\n \"%s\" \n",myquery.str().c_str());
330     //
331     pResult = dbc->Query(myquery.str().c_str());
332     if ( !pResult ) {
333     printf(" MYDETECTOR1 - ERROR: error querying the DB [6]\n");
334     code = -14;
335     goto closeandexit;
336     };
337     ftmpname.str("");
338     //
339     for( r=0; r < 1000; r++){
340     Row = pResult->Next();
341     if( Row == NULL ) break;
342     for( t = 0; t < pResult->GetFieldCount(); t++){
343     //
344     // put in fpath the path to that file
345     //
346     if ( t == 0 ) ftmpname << Row->GetField(t) << "/";
347     if ( t == 1 ) ftmpname << Row->GetField(t);
348     if ( debug ) printf(" Row %i value = %s \n",t,Row->GetField(t));
349     };
350     };
351     delete pResult;
352     //
353     fname = ftmpname.str().c_str();
354     //
355     // print out informations
356     //
357     totevent = evto - evfrom + 1;
358     printf("\n LEVEL0 data file: %s \n",fname.Data());
359     printf(" RUN HEADER absolute time is: %llu \n",runheadtime);
360     printf(" RUN TRAILER absolute time is: %llu \n",runtrailtime);
361     printf(" %i events to be processed for run %llu: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evto);
362     //
363     // Open Level0 file
364     //
365     l0File = new TFile(fname.Data());
366     if ( !l0File ) {
367     printf(" MYDETECTOR1 - ERROR: problems opening Level0 file\n");
368     code = -6;
369     goto closeandexit;
370     };
371     l0tr = (TTree*)l0File->Get("Physics");
372     if ( !l0tr ) {
373     printf(" MYDETECTOR1 - ERROR: no Physics tree in Level0 file\n");
374     l0File->Close();
375     code = -7;
376     goto closeandexit;
377     };
378     l0head = l0tr->GetBranch("Header");
379     if ( !l0head ) {
380     printf(" MYDETECTOR1 - ERROR: no Header branch in Level0 tree\n");
381     l0File->Close();
382     code = -8;
383     goto closeandexit;
384     };
385     l0registry = l0tr->GetBranch("Registry");
386     if ( !l0registry ) {
387     printf(" MYDETECTOR1 - ERROR: no Registry branch in Level0 tree\n");
388     l0File->Close();
389     code = -9;
390     goto closeandexit;
391     };
392     l0trig = l0tr->GetBranch("Trigger");
393     if ( !l0trig ) {
394     printf(" MYDETECTOR1 - ERROR: no Trigger branch in Level0 tree\n");
395     l0File->Close();
396     code = -104;
397     goto closeandexit;
398     };
399     //
400     l0tr->SetBranchAddress("Trigger", &trig);
401     l0tr->SetBranchAddress("Registry", &l0reg);
402     //
403     nevents = l0registry->GetEntries();
404     //
405     if ( nevents < 1 ) {
406     printf(" MYDETECTOR1 - ERROR: Level0 file is empty\n\n");
407     l0File->Close();
408     code = -11;
409     goto closeandexit;
410     };
411     //
412     if ( evto > nevents-1 ) {
413     printf(" MYDETECTOR1 - ERROR: too few entries in the registry tree\n");
414     l0File->Close();
415     code = -12;
416     goto closeandexit;
417     };
418     //
419     // run over all the events of the run
420     //
421     printf("\n Ready to start! \n\n Processed events: \n\n");
422     //
423     for ( re = evfrom; re <= evto; re++){
424     //
425     if ( procev%1000 == 0 && procev > 0 ) printf(" %iK \n",procev/1000);
426     //
427     l0registry->GetEntry(re);
428     //
429     // absolute time of this event
430     //
431     atime = l0reg->absTime;
432     //
433     // physics events is at entry number ei where
434     //
435     ei = l0reg->event;
436     //
437     if ( atime > runtrailtime || atime < runheadtime ) {
438     printf(" MYDETECTOR1 - WARNING: event at time outside the run time window, skipping it\n");
439     goto jumpev;
440     };
441     //
442     procev++;
443     //
444     // start processing
445     //
446     mydec = new MyDect1Level2();
447     //
448     // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
449     //
450     l0trig->GetEntry(ei);
451     S3 = 0;
452     S2 = 0;
453     S12 = 0;
454     S11 = 0;
455     S3 = trig->patterntrig[2];
456     S2 = trig->patterntrig[3];
457     S12 = trig->patterntrig[4];
458     S11 = trig->patterntrig[5];
459     if ( trig->patterntrig[0] ) tmptrigty = 2.;
460     if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
461     if ( trig->patterntrig[1] & (1<<0) || (!trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11) ) tmptrigty = 1.;
462     //
463     // store informations in the level2 class
464     //
465     mydec->myvar1 = (int)trig->patterntrig[2];
466     mydec->myvar2 = (int)trig->patterntrig[3];
467     mydec->myvar3 = tmptrigty;
468     //
469     // Fill the rootple
470     //
471     mydect1->Fill();
472     //
473     //
474     jumpev:
475     debug = false;
476     //
477     };
478     //
479     ei = 0;
480     //
481     }; // process all the runs
482     //
483     printf("\n Finished processing data \n");
484     //
485     closeandexit:
486     //
487     // 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.
488     //
489     if ( !reprocall && reproc && code >= 0 ){
490     if ( totfileentries > noaftrun ){
491     printf("\n Post-processing: copying events from the old tree after the processed run\n");
492     printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
493     printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
494     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
495     //
496     // Get entry from old tree
497     //
498     mydect1clone->GetEntry(j);
499     //
500     // copy caclone to ca
501     //
502     memcpy(&mydec,&mydecclone,sizeof(mydec));
503     //
504     // Fill entry in the new tree
505     //
506     mydect1->Fill();
507     };
508     printf(" Finished successful copying!\n");
509     };
510     };
511     //
512     // Case of no errors: close files, delete old tree(s), write and close level2 file
513     //
514     if ( code >= 0 ){
515     printf("\n Writing and closing rootple\n");
516     if ( mydect1clone && !reprocall ) mydect1clone->Delete("all");
517     if ( l0File ) l0File->Close();
518     runinfo->Close();
519     file->Write();
520     file->Close();
521     } else {
522     printf("\n MYDETECTOR1 - ERROR: an error occurred, exiting without saving\n");
523     };
524     //
525     // the end
526     //
527     printf("\n Exiting...\n");
528     return(code);
529     }
530    
531    
532    

  ViewVC Help
Powered by ViewVC 1.1.23