/[PAMELA software]/DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp
ViewVC logotype

Annotation of /DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.23