/[PAMELA software]/DarthVader/NDLevel2/src/NDCore.cpp
ViewVC logotype

Contents of /DarthVader/NDLevel2/src/NDCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Fri May 19 13:15:26 2006 UTC (18 years, 6 months ago) by mocchiut
Branch: DarthVader
CVS Tags: v0r01, v0r02, start, v1r00
Changes since 1.1: +0 -0 lines
Imported sources

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

  ViewVC Help
Powered by ViewVC 1.1.23