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

  ViewVC Help
Powered by ViewVC 1.1.23