/[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.9 - (hide annotations) (download)
Thu Jan 11 14:33:42 2007 UTC (17 years, 10 months ago) by mocchiut
Branch: MAIN
Changes since 1.8: +8 -0 lines
Incremented SetAutoSave variable for all the trees

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

  ViewVC Help
Powered by ViewVC 1.1.23