/[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.5 - (hide annotations) (download)
Tue Sep 5 14:49:42 2006 UTC (18 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.4: +47 -62 lines
_NOT_ COMPILING yet, fixed AC, ND, S4, RunInfo, OrbitalInfo and Trigger

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

  ViewVC Help
Powered by ViewVC 1.1.23