/[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.1.1.1 - (hide annotations) (download) (vendor branch)
Fri May 19 13:15:57 2006 UTC (18 years, 6 months ago) by mocchiut
Branch: DarthVader
CVS Tags: v0r01, start
Changes since 1.1: +0 -0 lines
Imported sources

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     #include <TArrayL.h>
17     #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 <RegistryEvent.h>
30     #include <PscuHeader.h>
31     #include <PscuEvent.h>
32     #include <EventHeader.h>
33     #include <RegistryEvent.h>
34     //
35     // RunInfo header
36     //
37     #include <RunInfo.h>
38     #include <GLTables.h>
39     //
40     // This program headers
41     //
42     #include <OrbitalInfo.h>
43     #include <OrbitalInfoCore.h>
44     #include <OrbitalInfoVerl2.h>
45    
46     using namespace std;
47    
48     //
49     // CORE ROUTINE
50     //
51     //
52    
53     int OrbitalInfoCore(ULong64_t run, TFile *file, TSQLServer *dbc, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){
54     Int_t i = 0;
55     //
56     TString processFolder = "OrbitalInfoFolder";
57     //
58     // Set these to true to have a very verbose output.
59     //
60     Bool_t debug = false;
61     //
62     Bool_t verbose = false;
63    
64     if ( OrbitalInfoargc > 0 ){
65     i = 0;
66     while ( i < OrbitalInfoargc ){
67     if ( !strcmp(OrbitalInfoargv[i],"-processFolder") ) {
68     if ( OrbitalInfoargc < i+1 ){
69     throw -3;
70     };
71     processFolder = (TString)OrbitalInfoargv[i+1];
72     i++;
73     };
74     if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {
75     verbose = true;
76     };
77     if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {
78     verbose = true;
79     };
80     i++;
81     };
82     };
83     //
84     const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));
85     //
86     TTree *OrbitalInfotr = 0;
87     Long64_t nevents = 0LL;
88     //
89     // variables needed to reprocess data
90     //
91     TString OrbitalInfoversion;
92     ItoRunInfo *runinfo = 0;
93     TArrayL *runlist = 0;
94     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     Long64_t totfileentries = 0ULL;
103     Long64_t idRun = 0LL;
104     //
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     TBranch *l0registry = 0;
120     pamela::RegistryEvent *l0reg=0;
121     //
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     ULong64_t atime = 0ULL;
130     Int_t ei = 0;
131     Int_t re = 0;
132     //
133     // Working filename
134     //
135     TString outputfile;
136     stringstream name;
137     name.str("");
138     name << outDir << "/";
139     //
140     // temporary file and folder
141     //
142     TFile *tempfile = 0;
143     TTree *tempOrbitalInfo = 0;
144     stringstream tempname;
145     stringstream OrbitalInfofolder;
146     tempname.str("");
147     tempname << outDir;
148     tempname << "/" << processFolder.Data();
149     OrbitalInfofolder.str("");
150     OrbitalInfofolder << tempname.str().c_str();
151     gSystem->MakeDirectory(OrbitalInfofolder.str().c_str());
152     tempname << "/OrbitalInfotree_run";
153     tempname << run << ".root";
154     //
155     // DB classes
156     //
157     GL_ROOT *glroot = new GL_ROOT();
158     //
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     if ( run == 0ULL ) reproc = true;
166     //
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     if ( run == 0ULL ){
223     if (verbose) printf(" OrbitalInfo - WARNING: you are reprocessing data but OrbitalInfo tree does not exist!\n");
224     }
225     if ( runinfo->IsReprocessing() && run != 0ULL ) {
226     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     if ( run == 0ULL ){
238     //
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     if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing run number %llu \n",run);
253     //
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     printf(" Copying %u events in the file which are before the beginning of the run %llu \n",nobefrun,run);
289     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     orbitalinfo = new OrbitalInfo();
298     memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
299     //
300     // Fill entry in the new tree
301     //
302     OrbitalInfotr->Fill();
303     //
304     };
305     if (verbose) printf(" Finished successful copying!\n");
306     };
307     };
308     //
309     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
310     //
311     runlist = runinfo->GetRunList();
312     //
313     // Loop over the run to be processed
314     //
315     for (UInt_t irun=0; irun < numbofrun; irun++){
316     //
317     // retrieve the first run ID to be processed using the RunInfo list
318     //
319     idRun = runlist->At(irun);
320     if (verbose){
321     printf("\n\n\n ####################################################################### \n");
322     printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
323     printf(" ####################################################################### \n\n\n");
324     }
325     //
326     runinfo->ID_REG_RUN = 0ULL;
327     //
328     // store in the runinfo class the GL_RUN variables for our run
329     //
330     sgnl = 0;
331     sgnl = runinfo->GetRunInfo(idRun);
332     if ( sgnl ){
333     //printf("\n OrbitalInfo - ERROR: RunInfo exited with non-zero status\n");
334     code = sgnl;
335     goto closeandexit;
336     } else {
337     sgnl = 0;
338     };
339     //
340     // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN
341     //
342     if ( runinfo->ID_REG_RUN == 0 ){
343     //printf("\n OrbitalInfo - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun);
344     code = -5;
345     goto closeandexit;
346     };
347     //
348     // Search in the DB the path and name of the LEVEL0 file to be processed.
349     //
350     glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc);
351     //
352     ftmpname.str("");
353     ftmpname << glroot->PATH.Data() << "/";
354     ftmpname << glroot->NAME.Data();
355     fname = ftmpname.str().c_str();
356     //
357     // print out informations
358     //
359     totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1;
360     if (verbose){
361     printf("\n LEVEL0 data file: %s \n",fname.Data());
362     printf(" RUN HEADER absolute time is: %llu \n",runinfo->RUNHEADER_TIME);
363     printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME);
364     printf(" %i events to be processed for run %llu: from %i to %i (reg entries)\n\n",totevent,idRun,runinfo->EV_REG_PHYS_FROM,runinfo->EV_REG_PHYS_TO);
365     }//
366     // Open Level0 file
367     l0File = new TFile(fname.Data());
368     if ( !l0File ) {
369     //printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");
370     code = -6;
371     goto closeandexit;
372     };
373     l0tr = (TTree*)l0File->Get("Physics");
374     if ( !l0tr ) {
375     //printf(" OrbitalInfo - ERROR: no Physics tree in Level0 file\n");
376     l0File->Close();
377     code = -7;
378     goto closeandexit;
379     };
380     l0registry = l0tr->GetBranch("Registry");
381     if ( !l0registry ) {
382     //printf(" OrbitalInfo - ERROR: no Registry branch in Level0 tree\n");
383     l0File->Close();
384     code = -9;
385     goto closeandexit;
386     };
387     //
388     l0tr->SetBranchAddress("Registry", &l0reg);
389     //
390     nevents = l0registry->GetEntries();
391     //
392     if ( nevents < 1 ) {
393     //printf(" OrbitalInfo - ERROR: Level0 file is empty\n\n");
394     l0File->Close();
395     code = -11;
396     goto closeandexit;
397     };
398     //
399     if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) {
400     //printf(" OrbitalInfo - ERROR: too few entries in the registry tree\n");
401     l0File->Close();
402     code = -12;
403     goto closeandexit;
404     };
405     //
406     // run over all the events of the run
407     //
408     if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
409     //
410     for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){
411     //
412     if ( procev%1000 == 0 && procev > 0 && verbose) printf(" %iK \n",procev/1000);
413     //
414     l0registry->GetEntry(re);
415     //
416     // absolute time of this event
417     //
418     atime = l0reg->absTime;
419     //
420     // physics events is at entry number ei where
421     //
422     ei = l0reg->event;
423     //
424     // paranoid check
425     //
426     if ( (atime > runinfo->RUNTRAILER_TIME) || (atime < runinfo->RUNHEADER_TIME) ) {
427     if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");
428     goto jumpev;
429     };
430     //
431     procev++;
432     //
433     // start processing
434     //
435     orbitalinfo = new OrbitalInfo();
436     orbitalinfo->absTime = l0reg->absTime;
437     OrbitalInfotr->Fill();
438     //
439     //
440     jumpev:
441     debug = false;
442     //
443     };
444     //
445     // Here you may want to clear some variables before processing another run
446     //
447     ei = 0;
448     }; // process all the runs
449     //
450     if (verbose) printf("\n Finished processing data \n");
451     //
452     closeandexit:
453     //
454     // 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.
455     //
456     if ( !reprocall && reproc && code >= 0 ){
457     if ( totfileentries > noaftrun ){
458     if (verbose){
459     printf("\n Post-processing: copying events from the old tree after the processed run\n");
460     printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
461     printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
462     }
463     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
464     //
465     // Get entry from old tree
466     //
467     OrbitalInfotrclone->GetEntry(j);
468     //
469     // copy orbitalinfoclone to OrbitalInfo
470     //
471     orbitalinfo = new OrbitalInfo();
472     memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
473     //
474     // Fill entry in the new tree
475     //
476     OrbitalInfotr->Fill();
477     };
478     if (verbose) printf(" Finished successful copying!\n");
479     };
480     };
481     //
482     // Close files, delete old tree(s), write and close level2 file
483     //
484     if ( l0File ) l0File->Close();
485     if ( tempfile ) tempfile->Close();
486     gSystem->Unlink(tempname.str().c_str());
487    
488     //if ( code < 0 ) printf("\n OrbitalInfo - ERROR: an error occurred, try to save anyway...\n");
489     //printf("\n Writing and closing rootple\n");
490     if ( runinfo ) runinfo->Close();
491     if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");
492     if ( file ){
493     file->cd();
494     file->Write();
495     };
496     //
497     gSystem->Unlink(OrbitalInfofolder.str().c_str());
498     //
499     // the end
500     //
501     if (verbose) printf("\n Exiting...\n");
502     if(OrbitalInfotr)OrbitalInfotr->Delete();
503     if(code < 0) throw code;
504     return(code);
505     }
506    

  ViewVC Help
Powered by ViewVC 1.1.23