/[PAMELA software]/DarthVader/TrackerLevel2/src/TrkCore.cpp
ViewVC logotype

Annotation of /DarthVader/TrackerLevel2/src/TrkCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (hide annotations) (download)
Fri Aug 31 14:56:51 2007 UTC (17 years, 3 months ago) by pam-fi
Branch: MAIN
Changes since 1.26: +1 -0 lines
new variables added to TrkTrack + other changes

1 mocchiut 1.1 /**
2     * \file TrkCore.cpp
3     * \author Elena Vannuccini
4     */
5     // .........................................................
6     // ROOT header files
7     // .........................................................
8     #include <TTree.h>
9     #include <TClassEdit.h>
10     #include <TObject.h>
11     #include <TList.h>
12     #include <TSystem.h>
13     #include <TSystemDirectory.h>
14     #include <TString.h>
15     #include <TFile.h>
16     #include <TClass.h>
17     #include <TCanvas.h>
18     #include <TH1.h>
19     #include <TH1F.h>
20     #include <TH2D.h>
21     #include <TLatex.h>
22     #include <TPad.h>
23     #include <TSQLServer.h>
24     #include <TSQLRow.h>
25     #include <TSQLResult.h>
26 pam-fi 1.11 #include <TBenchmark.h>
27    
28 mocchiut 1.1 // .........................................................
29     // other general header files
30     // .........................................................
31     #include <fstream>
32     #include <iostream>
33     // .........................................................
34     // files in the inc directory
35     // .........................................................
36     #include <RunInfo.h>
37     #include <GLTables.h>
38     #include <TrkVerl2.h>
39     #include <TrkProcess.h>
40 pam-fi 1.2 //#include <TrkStruct.h>
41 mocchiut 1.1 #include <TrkLevel2.h>
42 pam-fi 1.6 #include <TrkLevel1.h>
43 mocchiut 1.1 #include <TrkLevel0.h>
44 pam-fi 1.15 #include <TrkHough.h>
45 mocchiut 1.1 // .........................................................
46     // YODA header files
47     // .........................................................
48     #include <PamelaRun.h>
49 pam-fi 1.7 //#include <RegistryEvent.h>
50 mocchiut 1.1 #include <physics/tracker/TrackerEvent.h>
51     #include <CalibTrk1Event.h>
52     #include <CalibTrk2Event.h>
53 pam-fi 1.2
54 mocchiut 1.1 // .........................................................
55     // namespaces
56     // .........................................................
57     using namespace std;
58     using namespace pamela;
59     // ================================================================================
60     //
61     //
62     // ================================================================================
63     /**
64     * \brief Tracker data reduction routine.
65     *
66     * It performs data reduction LEVEL0->LEVEL1->LEVEL2, producing LEVEL1 and or LEVEL2 output, in ROOT or HBOOK format.
67     * Input parameters:
68     * @param run id of the run to be processed (if run=0 a whole file is reprocessed)
69     * @param dbc pointer to BD server
70     * @param file1 output LEVEL1 file name (null if no LEVEL1 output is required)
71     * @param file2 output LEVEL2 file name (null if no LEVEL2 output is required)
72     * @param standalone (flag to run the program in standalone mode, that is without reading RunInfo)
73     */
74     //short int TrkCore(Int_t run, TSQLServer *dbc, TString file1, TString file2, Bool_t standalone)
75 pam-fi 1.7 int TrkCore(UInt_t run, TFile *f2, TSQLServer *dbc, int ncustom, char*vcustom[])
76 mocchiut 1.1 {
77     // ---------------------------
78     // Define some basic variables
79     // ---------------------------
80    
81     TString filename = 0;
82 pam-fi 1.7 Long64_t nentries = 0LL;
83 pam-fi 1.2
84 mocchiut 1.1 // LEVEL0 input
85     TFile *f0 = 0;
86     TTree *physicsTree = 0;
87     TBranch *b_trk = 0;
88     TBranch *b_header = 0;
89     EventHeader *header = 0;
90     PscuHeader *pscu = 0;
91 pam-fi 1.22 TrkLevel0 *l0_event = new TrkLevel0();
92 mocchiut 1.1 // RunInfo
93     ItoRunInfo *runinfo = 0;
94 pam-fi 1.7 TArrayI *runlist = 0;
95     UInt_t from_run;
96     UInt_t to_run;
97 mocchiut 1.1 Bool_t reprocessing = false;
98     //LEVEL2 output - ROOT
99     TTree *t_level2 = 0;
100     TTree *t_clone = 0;
101     TrkLevel2 *l2_event = new TrkLevel2();
102     TrkLevel2 *l2_clone = new TrkLevel2();
103 pam-fi 1.13 //LEVEL1 output - ROOT
104     TrkLevel1 *l1_event = new TrkLevel1();
105 pam-fi 1.15 TrkHough *lh_event = new TrkHough();
106 mocchiut 1.1 // -----------------------
107     // -----------------------
108     // Handle input parameters
109     // (data)
110     //
111     // - open/create output files, determining the environment root/hbook from the estension
112     // - create the level1/level2 tree+branch/nt-uple
113     // -----------------------
114     // -----------------------
115     TrkProcess *p = new TrkProcess(run,f2);
116 pam-fi 1.26 if( p->HandleCustomPar(ncustom,vcustom) )return(p->ostatus);;
117 pam-fi 1.25 if( TrkParams::VerboseMode() )p->Dump();
118 mocchiut 1.1
119     // ===================================
120     // Open/Create level2 output file
121     // ===================================
122     if(p->get2){
123 pam-fi 1.13 //-------------------------------------------
124     // read RunInfo
125     //-------------------------------------------
126     if(!(p->standalone)){
127     // Open "RunInfo" tree and get list of run
128     runinfo = new ItoRunInfo(f2);
129     char *trkversion = TrkInfo(false);
130     int runinfo_error = runinfo->Update(run,"TRK",trkversion);
131     if( runinfo_error ) throw runinfo_error;
132     runlist = runinfo->GetRunList();
133     reprocessing = runinfo->IsReprocessing();//????
134 pam-fi 1.25 if(TrkParams::VerboseMode()){
135 pam-fi 1.13 cout << "#events "<< runinfo->GetFileEntries() << endl;// #events in the file
136     cout << "#runs "<< runinfo->GetRunEntries() << endl;// #runs in the file
137     cout << "1-st run "<< runlist->At(0) << endl;
138     cout << "last run "<< runlist->At(runinfo->GetRunEntries()-1) << endl;
139     cout << "1-st event "<< runinfo->GetFirstEntry() << endl;// first event of our run
140     cout << "last event+1 "<< runinfo->GetLastEntry() << endl;// first event AFTER the last event of our run
141     cout << "reprocessing "<< runinfo->IsReprocessing() << endl << endl;
142     };
143     };
144     //-------------------------------------------
145     //
146     //-------------------------------------------
147     // Take (if present) the old Tracker tree
148     t_clone = (TTree*)f2->Get("Tracker");
149     if( t_clone != NULL ) t_clone->SetBranchAddress("TrkLevel2",&l2_clone);
150     // Create NEW Tracker tree
151     t_level2 = new TTree("Tracker","PAMELA tracker level2 data ");
152 pam-fi 1.21 l2_event->Set(); // ****NBNBNBN*****
153 pam-fi 1.13 t_level2->Branch("TrkLevel2","TrkLevel2",&l2_event);
154     if(p->get1){
155 pam-fi 1.25 if(TrkParams::VerboseMode())cout << endl << "Requested LEVEL1 output" << endl;
156 pam-fi 1.21 l1_event->Set(); // ****NBNBNBN*****
157 pam-fi 1.13 t_level2->Branch("TrkLevel1","TrkLevel1",&l1_event);
158 pam-fi 1.16 }
159     if(p->geth){
160 pam-fi 1.25 if(TrkParams::VerboseMode())cout << endl << "Requested Hough-Transform output" << endl;
161 pam-fi 1.16 t_level2->Branch("TrkHough","TrkHough",&lh_event);
162 pam-fi 1.7 };
163 pam-fi 1.13 };
164 mocchiut 1.1
165    
166     // -------------------------------------------
167     // define runs to be processed/reprocessed
168     // -------------------------------------------
169     if(run == 0){
170 pam-fi 1.13 // reprocessing ALL runs
171     if(p->standalone)throw -298; // reprocessing not implemented
172     from_run = runlist->At(0);
173     to_run = runlist->At(runinfo->GetRunEntries()-1);
174 mocchiut 1.1 }else{
175     // processing/reprocessing ONE single run
176 pam-fi 1.13 from_run = run;
177     to_run = run;
178 mocchiut 1.1 };
179    
180     //
181     // init event counter
182     //
183     Int_t ev_count =0;
184     //
185     // create query-results objects
186     //
187 pam-fi 1.7 GL_RUN q1 = GL_RUN();
188 mocchiut 1.1 GL_TRK_CALIB q2 = GL_TRK_CALIB();
189     GL_ROOT q3 = GL_ROOT();
190     GL_PARAM q4 = GL_PARAM();
191    
192     // ------------------------------------------------------------
193     // if reprocessing one run, copy all the events BEFORE the run
194     // ------------------------------------------------------------
195     if( !(p->standalone) ){
196 pam-fi 1.13 for(UInt_t i=0; i<runinfo->GetFirstEntry(); i++){
197     t_clone->GetEntry(i);
198     *l2_event = *l2_clone;
199     t_level2->Fill();
200     l2_event->Clear();
201     // COPY COPY COPY
202     };
203 mocchiut 1.1 };
204     // ------------------------------------------------------------
205     // ------------------------------------------------------------
206 pam-fi 1.25 // START LOOP OVER RUNS TO PROCESS/REPROCESS
207 mocchiut 1.1 // ------------------------------------------------------------
208     // ------------------------------------------------------------
209 pam-fi 1.7 for(UInt_t idRun = from_run; idRun <= to_run; idRun++){
210 mocchiut 1.1
211 pam-fi 1.25 if(TrkParams::VerboseMode()) cout << endl<<" ========================= Run: "<< idRun << endl;
212 pam-fi 1.7 UInt_t runheadtime = 0;
213     UInt_t runtrailtime = 0;
214 mocchiut 1.1 UInt_t evfrom = 0;
215     UInt_t evto = 0;
216 pam-fi 1.7 UInt_t nevents = 0;
217     Int_t id_root_l0 =-1;
218 mocchiut 1.1 Int_t trk_calib_used = 0;
219 pam-fi 1.7
220 mocchiut 1.1 if(p->standalone){
221     // ==============================
222     // first query: retrieve run info
223     // ==============================
224 pam-fi 1.7 if (q1.Query_GL_RUN(idRun,dbc) )throw -50;
225     id_root_l0 = q1.ID_ROOT_L0;
226 mocchiut 1.1 runheadtime = q1.RUNHEADER_TIME;
227     runtrailtime = q1.RUNTRAILER_TIME;
228 pam-fi 1.7 evfrom = q1.EV_FROM;
229     evto = q1.EV_TO;
230 pam-fi 1.13 nevents = q1.NEVENTS;
231     trk_calib_used = q1.TRK_CALIB_USED;
232 mocchiut 1.1 }else{
233     // ==============================
234     // get run info from RunInfo tree
235     // ==============================
236 pam-fi 1.3 int runinfo_error = runinfo->GetRunInfo(idRun);
237 pam-fi 1.7 if( runinfo_error ) throw runinfo_error;
238     id_root_l0 = runinfo->ID_ROOT_L0;
239 mocchiut 1.1 runheadtime = runinfo->RUNHEADER_TIME;
240     runtrailtime = runinfo->RUNTRAILER_TIME;
241 pam-fi 1.7 evfrom = runinfo->EV_FROM;
242     evto = runinfo->EV_TO;
243 pam-fi 1.13 nevents = runinfo->NEVENTS;
244     trk_calib_used = runinfo->TRK_CALIB_USED;
245 pam-fi 1.24
246 mocchiut 1.1 };
247 pam-fi 1.24
248 mocchiut 1.1 //
249 pam-fi 1.25 if(TrkParams::VerboseMode()){
250 pam-fi 1.7 cout << "ROOT file ID "<< id_root_l0 << endl;
251 mocchiut 1.1 cout << "RunHeader time "<< runheadtime << endl;
252     cout << "RunTrailer time "<< runtrailtime << endl;
253 pam-fi 1.7 cout << " from event "<< evfrom << endl;
254     cout << " to event "<< evto << endl;
255 pam-fi 1.24 cout << " num. events "<< nevents << endl;
256     cout << "trk-calibration used "<< trk_calib_used << endl;
257 mocchiut 1.1 };
258     // ========================================================
259     // second query: search the LEVEL0 file that contains idRun
260     // ========================================================
261     TString lastfilename = filename;
262 pam-fi 1.7 if( q3.Query_GL_ROOT(id_root_l0,dbc) )throw -51;
263 mocchiut 1.1 filename = q3.PATH + q3.NAME;
264     // ========================================================
265     // Open the input LEVEL0 data file
266     // ========================================================
267     if(filename.CompareTo(lastfilename)){
268     if(!lastfilename.IsNull())f0->Close();
269     //if( debug ) cout << "Opening LEVEL0 file: "<< filename << endl;
270 pam-fi 1.25 if(TrkParams::VerboseMode()) cout << "Opening LEVEL0 file: "<< filename << endl;
271 pam-fi 1.2 FileStat_t t;
272     if( gSystem->GetPathInfo(filename.Data(),t) )throw -6;
273 mocchiut 1.1 f0 = new TFile(filename);
274     if ( !f0 ) throw -6;
275     physicsTree = (TTree*)f0->Get("Physics"); if(!physicsTree) throw -7;
276     b_header = physicsTree ->GetBranch("Header"); if(!b_header ) throw -8;
277     b_trk = physicsTree ->GetBranch("Tracker"); if(!b_trk ) throw -203;
278 pam-fi 1.22 l0_event->Set();
279     physicsTree->SetBranchAddress("Tracker" ,l0_event->GetPointerToTrackerEvent());
280 mocchiut 1.1 physicsTree->SetBranchAddress("Header",&header);
281    
282 pam-fi 1.7 nentries = physicsTree->GetEntries();
283     if ( nentries < 1 ) throw -11;
284     if ( nentries < (evto+1)) throw -12;
285    
286 mocchiut 1.1 };
287    
288 pam-fi 1.7 GL_TIMESYNC *dbtime = new GL_TIMESYNC(id_root_l0,"ID",dbc);
289    
290 pam-fi 1.24
291     // =============================================================
292     // retrieve information about parameters to process LEVEL2
293     // =============================================================
294    
295     TrkParams::Set(runinfo->GetGL_RUN(),dbc);
296 pam-fi 1.25 for(int i=0; i<p->npar; i++){
297     if(TrkParams::VerboseMode())cout<<" ((( force parameters from input path )))"<<endl;
298     TrkParams::Set(p->parpath[i],p->partype[i]);
299     }
300    
301 pam-fi 1.24 TrkParams::Load();
302     if( !TrkParams::IsLoaded() )throw -52;
303    
304     // =============================================================
305     // retrieve calibration file needed to reduce data
306     // =============================================================
307    
308     TrkParams::SetCalib(runinfo->GetGL_RUN(),dbc);
309     TrkParams::LoadCalib( );
310     if( !TrkParams::CalibIsLoaded() )throw -53;
311    
312 pam-fi 1.11 TBenchmark *reduction = new TBenchmark();
313 pam-fi 1.25 if(TrkParams::VerboseMode())reduction->Start("reduction");
314 pam-fi 1.11 Int_t ntrk=0;
315 mocchiut 1.1 // ====================================================
316     // start looping on events cointained in the data file
317     // ====================================================
318 pam-fi 1.7 for (UInt_t re = evfrom; re < evfrom+nevents; re++){
319 mocchiut 1.1
320     ev_count++;
321    
322 pam-fi 1.25 // if ( TrkParams::DebugMode() && re%100 == 0 && re > 0 ) cout << ".";
323 mocchiut 1.1
324 pam-fi 1.13 b_trk->GetEntry(re);
325     b_header->GetEntry(re);
326     pscu = header->GetPscuHeader();
327 pam-fi 1.23
328 pam-fi 1.25 if( TrkParams::DebugMode() )cout << ">>> "<<ev_count-1<<" @ OBT "<<pscu->GetOrbitalTime()<<endl;
329 pam-fi 1.23
330 pam-fi 1.13 if ( dbtime->DBabsTime(pscu->GetOrbitalTime()) > runtrailtime || dbtime->DBabsTime(pscu->GetOrbitalTime()) < runheadtime) {
331 pam-fi 1.7
332 pam-fi 1.25 if (TrkParams::VerboseMode()){
333 pam-fi 1.14 printf(" TrkCore - WARNING: event outside the run time window, skipping it\n");
334     cout << " OBT "<<pscu->GetOrbitalTime()<<" ABS-time "<<dbtime->DBabsTime(pscu->GetOrbitalTime())<<" RUN "<<runheadtime<<"-"<<runtrailtime<<endl;
335     };
336 pam-fi 1.13 }else{
337 pam-fi 1.25 if ( TrkParams::DebugMode() )
338 pam-fi 1.23 printf("\n-------------------------------\nevent %d\n",re-evfrom);
339 pam-fi 1.16
340 pam-fi 1.13 p->ProcessEvent(l0_event);
341 pam-fi 1.7
342 pam-fi 1.13 // ----------------
343     // LEVEL1 output
344     // ----------------
345     if(p->get1){
346     if(p->ifroot1){ // root
347     l1_event->Clear();
348 pam-fi 1.22 // l1_event->SetFromLevel1Struct(&level1event_,p->full1);
349     l1_event->SetFromLevel1Struct(p->full1);
350 pam-fi 1.13 // t_level1->Fill();
351 pam-fi 1.23 }else{ // hbook
352 pam-fi 1.13 throw -299;
353     };
354     };
355     // ----------------
356     // HOUGH output
357     // ----------------
358     if(p->geth){
359     if(p->ifrooth){ // root
360 pam-fi 1.23 lh_event->Delete();
361     lh_event->SetFromHoughStruct(&houghevent_);
362 pam-fi 1.13 }else{ // hbook
363     throw -299;
364     };
365     };
366     // ----------------
367     // LEVEL2 output
368     // ----------------
369     if(p->get2){
370 pam-fi 1.23 l2_event->Clear();
371     if(p->get1){
372     l2_event->SetFromLevel2Struct(&level2event_,l1_event);//set references to level1
373     }else{
374     l2_event->SetFromLevel2Struct(&level2event_);
375     }
376 pam-fi 1.10 // l2_event->Dump();
377 pam-fi 1.23 t_level2->Fill();
378     if( l2_event->ntrk()>0 )ntrk++;
379 pam-fi 1.27 if(TrkParams::VerboseMode())l2_event->Dump();
380 mocchiut 1.1 };
381 pam-fi 1.13 };
382 mocchiut 1.1 }; // end loop on events
383 pam-fi 1.25 if(TrkParams::VerboseMode()){
384 pam-fi 1.13 cout << " >>> processed "<< ev_count <<" events"<< endl;
385     if(ev_count)cout << ntrk << " events with at least one track ("<<(Int_t)(100*ntrk/ev_count)<<"%)\n";
386     reduction->Show("reduction");
387 pam-fi 1.11 }
388     delete reduction;
389 pam-fi 1.7
390     delete dbtime;
391    
392 pam-fi 1.13 }; // end loop on runs
393    
394    
395 mocchiut 1.1 // ------------------------------------------------------------
396     // if reprocessing one run, copy all the events AFTER the run
397     // ------------------------------------------------------------
398     if( !(p->standalone) ){
399     for(UInt_t i=runinfo->GetLastEntry()+1; i<runinfo->GetFileEntries(); i++){
400     t_clone->GetEntry(i);
401     *l2_event = *l2_clone;
402     t_level2->Fill();
403     l2_event->Clear();
404     // COPY COPY COPY
405     };
406     };
407     // ---------------
408     // close the files
409     // ---------------
410     if(p->get2){
411 pam-fi 1.13 if( t_clone )t_clone->Delete("all");//delete old tree from file
412     if( !(p->standalone) )runinfo->Close();
413     f2->Write("Tracker");
414     if( t_level2 )t_level2->Delete(); //delete new tree from memory
415    
416     };
417 pam-fi 1.6
418 pam-fi 1.24 if(f0) if(f0->IsOpen()) f0->Close();
419     // if( f0_c->IsOpen() )f0_c->Close();
420 pam-fi 1.13
421     // lh_event->Delete();
422     l1_event->Delete();
423     l2_event->Delete();
424     l2_clone->Delete();
425 pam-fi 1.2
426     return(p->ostatus);
427 mocchiut 1.1 }
428    

  ViewVC Help
Powered by ViewVC 1.1.23