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

  ViewVC Help
Powered by ViewVC 1.1.23