/[PAMELA software]/DarthVader/NDLevel2/src/NDCore.cpp
ViewVC logotype

Annotation of /DarthVader/NDLevel2/src/NDCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Fri Aug 4 10:31:28 2006 UTC (18 years, 4 months ago) by mocchiut
Branch: MAIN
Changes since 1.2: +7 -2 lines
Small memory leaks fixed

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