/[PAMELA software]/PadmeAmidala/src/RunGlue.cpp
ViewVC logotype

Contents of /PadmeAmidala/src/RunGlue.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Thu Dec 7 11:53:26 2006 UTC (18 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.3: +64 -54 lines
*** empty log message ***

1 /**
2 * \file RunGlue.cpp
3 * \author Emiliano Mocchiutti
4 *
5 * The file contains implementation of the methods to query the DB.
6 */
7 //
8 #include <sstream>
9 #include <iostream>
10 //
11 #include <TFile.h>
12 #include <TTree.h>
13 #include <TTimeStamp.h>
14 #include <RunGlue.h>
15 //
16 ClassImp(RunGlue);
17 //
18 using namespace std;
19
20 RunGlue::RunGlue() {
21 this->Clear();
22 }
23
24 RunGlue::RunGlue(TSQLServer *da, UInt_t ru, TString di, TString wrkdi) {
25 fDBG = false;
26 li = new PamLevel2();
27 this->Clear();
28 dbc = da;
29 run = ru;
30 dir = di;
31 //
32 TString wd = gSystem->WorkingDirectory();
33 //
34 outdir = wrkdi;
35 if ( !strcmp(outdir.Data(),"") ) outdir = wd;
36 fList = 0;
37 if ( run ){
38 runmode = true;
39 } else {
40 runmode = false;
41 createlist = true;
42 };
43 //
44 // lList = new TList();
45 //
46 };
47
48
49 void RunGlue::Clear() {
50 fEnd = false;
51 run = 0;
52 dir = "";
53 outdir = "";
54 fFilename = "";
55 };
56
57 void RunGlue::Clean() {
58 if ( !merged ) gSystem->Unlink(fFilename);
59 lList->Delete();
60 delete Target;
61 };
62
63 void RunGlue::SetDebug(Bool_t dbg) {
64 fDBG = dbg;
65 };
66
67 void RunGlue::SetDList(TString list) {
68 //
69 RUN = true;
70 //
71 if (list.Contains("-RUN", TString::kIgnoreCase)) RUN = false;
72 if (list.Contains("-ALL", TString::kIgnoreCase)) RUN = false;
73 if (list.Contains("+RUN", TString::kIgnoreCase)) RUN = true;
74 if (list.Contains("+ALL", TString::kIgnoreCase)) RUN = true;
75 //
76 fDList = list;
77 li->SetWhichTrees(fDList);
78 //
79 if ( DebugMode() ) printf(" Detector list is %s \n",fDList.Data());
80 //
81 };
82
83 TList *RunGlue::GetRunList(){
84 //
85 lList = new TList();
86 lList->Clear();
87 TString thisrun;
88 TFile *su;
89 //
90 TSQLResult *pResult;
91 TSQLRow *Row;
92 //
93 if ( run && runmode ){
94 //
95 // Qurey the GL_RUN table to obtain RUN infos
96 //
97 GL_RUN *glrun = new GL_RUN();
98 glrun->Query_GL_RUN(run,dbc);
99 //
100 // convert RUNHEADER_TIME into a UTC date
101 //
102 GL_TIMESYNC *dbtime = new GL_TIMESYNC();
103 //
104 TString UTC="UTC";
105 TString rhdate=dbtime->ConvertTime(UTC,glrun->RUNHEADER_TIME);
106 //
107 TDatime ti = TDatime(rhdate.Data());
108 //
109 YY=(UInt_t)ti.GetYear();
110 MM=(UInt_t)ti.GetMonth();
111 DD=(UInt_t)ti.GetDay();
112 //
113 TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
114 //
115 UInt_t lowerlimit = llim->GetSec();
116 UInt_t upperlimit = lowerlimit + 86401;
117 //
118 if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
119 //
120 YY -= 2000;
121 stringstream tmpf;
122 tmpf.str("");
123 tmpf << "L2PAM";
124 if ( YY < 10 ){
125 tmpf<< "0" << YY;
126 } else {
127 tmpf<< YY;
128 };
129 if ( MM < 10 ){
130 tmpf<< "0" << MM;
131 } else {
132 tmpf<< MM;
133 };
134 if ( DD < 10 ){
135 tmpf << "0" << DD << ".root";
136 } else {
137 tmpf << DD << ".root";
138 };
139 TString fpath = "";
140 if ( !strcmp(outdir,"") ){
141 fpath="./";
142 } else {
143 fpath += outdir;
144 fpath += "/";
145 };
146 //
147 fFilename = fpath + tmpf.str().c_str();
148 //
149 printf(" Output filename is %s \n",fFilename.Data());
150 //
151 if ( !this->OpenFile() ){
152 fEnd = true;
153 return(0);
154 };
155 //
156 stringstream myquery;
157 myquery.str("");
158 myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
159 << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
160 //
161 pResult = dbc->Query(myquery.str().c_str());
162 for( UInt_t r=0; r < 1000; r++){
163 Row = pResult->Next();
164 if( Row == NULL ) break;
165 if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
166 //
167 thisrun=dir+(TString)Row->GetField(0)+".Level2.root";
168 //
169 if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
170 //
171 su = TFile::Open(thisrun);
172 if ( li->CheckLevel2File(thisrun) ){
173 lList->Add(su);
174 } else {
175 if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
176 };
177 //
178 };
179 //
180 //
181 delete glrun;
182 delete dbtime;
183 //
184 };
185 //
186 if ( strcmp(dir.Data(),"" )){
187 //
188 if ( createlist ){
189 fList = new TList();
190 fDoneList = new TList();
191 fNlist = 0;
192 fNlistdone = 0;
193 //
194 // read files in the directory
195 //
196 if ( DebugMode() ) printf("\n No input run given\n Check for existing root files. \n");
197 //
198 TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(dir),dir);
199 TList *temp = datadir->GetListOfFiles();
200 //
201 TIter next(temp);
202 TSystemFile *questo = 0;
203 //
204 while ( (questo = (TSystemFile*)next()) ) {
205 TString name = questo->GetName();
206 if( name.EndsWith(".Level2.root") ){
207 char *fullpath0;
208 gSystem->IsFileInIncludePath(name,&fullpath0);
209 TString fullpath = fullpath0;
210 //
211 if ( DebugMode() ) printf(" fullpath = %s name %s \n",fullpath.Data(),name.Data());
212 //
213 //
214 su = TFile::Open(fullpath);
215 if ( li->CheckLevel2File((TString)fullpath) ){
216 fList->Add(su);
217 fNlist++;
218 } else {
219 if ( DebugMode() ) printf(" RUN %s DISCARDED \n",fullpath.Data());
220 };
221 //
222 };
223 };
224 createlist = false;
225 nrun = 0;
226 };
227 //
228 //
229 //
230 Bool_t stop = true;
231 UInt_t drun = 0;
232 while ( nrun < fNlist && stop ){
233 TString myrun = (TString)((TString)(gSystem->BaseName(((TFile*)fList->At(nrun))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
234 if ( DebugMode() ) printf(" nrun %u myrun is %s \n",nrun,(myrun.ReplaceAll(".Level2.root",12,"",0)).Data());
235 run = (UInt_t)(myrun.Atoi());
236 nrun++;
237 //
238 TString donerun = "";
239 for (UInt_t ll=0; ll<fNlistdone; ll++){
240 donerun = "";
241 donerun = (TString)((TString)(gSystem->BaseName(((TFile*)fDoneList->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
242 if ( DebugMode() ) printf(" donerun is %s \n",(donerun.ReplaceAll(".Level2.root",12,"",0)).Data());
243 drun = (UInt_t)(donerun.Atoi());
244 //
245 if ( run == drun ) goto jump;
246 //
247 };
248 //
249 stop = false;
250 //
251 jump:
252 drun = 0;
253 //
254 };
255 //
256 if ( nrun >= fNlist && stop ){
257 fEnd = true;
258 return(0);
259 };
260 //
261 // Qurey the GL_RUN table to obtain RUN infos
262 //
263 printf(" PROCESSING RUN %u AND ITS FRIENDS\n",run);
264 GL_RUN *glrun = new GL_RUN();
265 glrun->Query_GL_RUN(run,dbc);
266 //
267 // convert RUNHEADER_TIME into a UTC date
268 //
269 GL_TIMESYNC *dbtime = new GL_TIMESYNC();
270 //
271 TString UTC="UTC";
272 TString rhdate=dbtime->ConvertTime(UTC,glrun->RUNHEADER_TIME);
273 //
274 TDatime ti = TDatime(rhdate.Data());
275 //
276 YY=(UInt_t)ti.GetYear();
277 MM=(UInt_t)ti.GetMonth();
278 DD=(UInt_t)ti.GetDay();
279 //
280 TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
281 //
282 UInt_t lowerlimit = llim->GetSec();
283 UInt_t upperlimit = lowerlimit + 86401;
284 //
285 if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
286 //
287 YY -= 2000;
288 stringstream tmpf;
289 tmpf.str("");
290 tmpf << "L2PAM";
291 if ( YY < 10 ){
292 tmpf<< "0" << YY;
293 } else {
294 tmpf<< YY;
295 };
296 if ( MM < 10 ){
297 tmpf<< "0" << MM;
298 } else {
299 tmpf<< MM;
300 };
301 if ( DD < 10 ){
302 tmpf << "0" << DD << ".root";
303 } else {
304 tmpf << DD << ".root";
305 };
306 TString fpath = "";
307 if ( !strcmp(outdir,"") ){
308 fpath="./";
309 } else {
310 fpath += outdir;
311 fpath += "/";
312 };
313 //
314 fFilename = fpath + tmpf.str().c_str();
315 //
316 printf(" Output filename is %s \n",fFilename.Data());
317 //
318 if ( !this->OpenFile() ) return(0);
319 //
320 stringstream myquery;
321 myquery.str("");
322 myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
323 << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
324 //
325 pResult = dbc->Query(myquery.str().c_str());
326 for( UInt_t r=0; r < 1000; r++){
327 Row = pResult->Next();
328 if( Row == NULL ) break;
329 if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
330 //
331 thisrun=dir+(TString)Row->GetField(0)+".Level2.root";
332 //
333 if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
334 //
335 su = TFile::Open(thisrun);
336 if ( su ){
337 TFile *su0 = TFile::Open(thisrun);
338 fDoneList->Add(su0);
339 fNlistdone++;
340 };
341 if ( li->CheckLevel2File(thisrun) ){
342 lList->Add(su);
343 if ( DebugMode() ) printf(" RUN %s ADDED \n",Row->GetField(0));
344 } else {
345 if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
346 };
347 //
348 };
349 //
350 if ( DebugMode() ){
351 UInt_t ll = 0;
352 while ( (TFile*)lList->At(ll) ){
353 TString donerun = "";
354 donerun = (TString)((TString)(gSystem->BaseName(((TFile*)lList->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
355 printf(" XXX file is %s \n",donerun.Data());
356 ll++;
357 //
358 };
359 };
360 //
361 delete glrun;
362 delete dbtime;
363 //
364 };
365 //
366 // is it the end?
367 //
368 if ( runmode ){
369 fEnd = true;
370 } else {
371 //
372 // did we use all the run in the directory? if so exit
373 //
374 if ( nrun >= fNlist ) fEnd = true;
375 };
376 //
377 return(lList);
378 };
379
380 Bool_t RunGlue::OpenFile(){
381 //
382 fOpen = false;
383 printf(" Check if output file already exists \n");
384 Target = TFile::Open((this->GetFilename()).Data(), "READ" );
385 //
386 if ( Target ){
387 Target->Close();
388 printf("Error opening target file, %s already exist!\n",(this->GetFilename()).Data());
389 return(false);
390 } else {
391 //
392 printf(" Output file does not exist, creating it\n");
393 //
394 Long64_t maxsize = 10000000000LL;
395 //
396 Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );
397 //fastMethod = kTRUE;
398 fastMethod = kFALSE;
399 //
400 //
401 if ( !Target || Target->IsZombie()) {
402 printf("Error opening target file (does %s exist?)\n",(this->GetFilename()).Data());
403 exit(1);
404 }
405 //
406 TTree::SetMaxTreeSize(maxsize);
407 //
408 fOpen = true;
409 return(true);
410 //
411 };
412 //
413 return(true);
414 //
415 };
416
417
418 void RunGlue::DeleteRunFiles(TList *dlist){
419 //
420 TString donerun = "";
421 //
422 printf(" Deleting runs: \n");
423 //
424 UInt_t ll = 0;
425 //
426 while ( (TFile*)dlist->At(ll) ){
427 donerun = (TString)(((TFile*)dlist->At(ll))->GetName());
428 gSystem->Unlink(donerun);
429 printf(" Deleted file is %s \n",donerun.Data());
430 ll++;
431 //
432 };
433 };
434
435 void RunGlue::UpdateDB(TList *dlist){
436 //
437 TSQLResult *pResult;
438 TSQLRow *Row;
439 stringstream myquery;
440 //
441 TString donerun = "";
442 //
443 if ( DebugMode() ) printf(" Updating GL_RUN: \n");
444 //
445 UInt_t ll = 0;
446 UInt_t idl2 = 0;
447 UInt_t idr = 0;
448 //
449 //
450 myquery.str("");
451 myquery << "select ID from GL_RAW where NAME='level2 files';";
452 if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
453 //
454 pResult = dbc->Query(myquery.str().c_str());
455 if ( !pResult ){
456 printf(" ERROR QUERYING ON DB!\n");
457 return;
458 };
459 Row = pResult->Next();
460 if( Row == NULL ){
461 printf(" ERROR QUERYING THE DB!\n");
462 return;
463 } else {
464 idr = (UInt_t)atoll(Row->GetField(0));
465 };
466 //
467 myquery.str("");
468 myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
469 if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
470 //
471 pResult = dbc->Query(myquery.str().c_str());
472 if ( !pResult ){
473 printf(" ERROR WRITING ON DB!\n");
474 return;
475 };
476 myquery.str("");
477 myquery << "select ID from GL_ROOT where NAME='" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "';";
478 if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
479 //
480 pResult = dbc->Query(myquery.str().c_str());
481 if ( !pResult ){
482 printf(" ERROR WRITING ON DB!\n");
483 return;
484 };
485 Row = pResult->Next();
486 if( Row == NULL ){
487 printf(" ERROR QUERYING THE DB!\n");
488 return;
489 } else {
490 idl2 = (UInt_t)atoll(Row->GetField(0));
491 };
492 //
493 //
494 //
495 UInt_t drun = 0;
496 //
497 while ( (TFile*)dlist->At(ll) ){
498 donerun = "";
499 donerun = (TString)((TString)(gSystem->BaseName(((TFile*)dlist->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
500 drun = (UInt_t)(donerun.Atoi());
501 //
502 if ( DebugMode() ) printf(" Run is %s \n",donerun.Data());
503 //
504 myquery.str("");
505 myquery << "update GL_RUN set ID_ROOT_L2=" << idl2 << " where ID=" << drun << ";";
506 if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
507 //
508 pResult = dbc->Query(myquery.str().c_str());
509 if ( !pResult ){
510 printf(" ERROR WRITING ON DB!\n");
511 return;
512 };
513 //
514 ll++;
515 //
516 };
517 };
518
519 //
520 // method stolen to ROOT's "hadd" and modified
521 //
522 void RunGlue::MergeRootfile(TList *sourcelist) {
523 //
524 merged = false;
525 //
526 TDirectory *target = Target;
527 //
528 if ( DebugMode() ) printf("\nTarget path is: %s \n",target->GetPath());
529 //
530 TString path( (char*)strstr( target->GetPath(), ":" ) );
531 path.Remove(0,2);
532 //
533 TDirectory *first_source = (TDirectory*)sourcelist->First();
534 THashList allNames;
535 //
536 while ( first_source ){
537 //
538 TDirectory *current_sourcedir = first_source->GetDirectory(path);
539 //
540 if ( !current_sourcedir ){
541 first_source = (TDirectory*)sourcelist->After(first_source);
542 continue;
543 }
544 //
545 // loop over all keys in this directory
546 //
547 TChain *globChain = 0;
548 TIter nextkey( current_sourcedir->GetListOfKeys() );
549 TKey *key, *oldkey=0;
550 TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory
551 //
552 while ( (key = (TKey*)nextkey()) ) {
553 //
554 if ( current_sourcedir == target ) break;
555 //
556 if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; //keep only the highest cycle number for each key
557 //
558 if (allNames.FindObject(key->GetName())) continue;
559 //
560 if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());
561 //
562 //
563 //
564 if ( !strcmp(key->GetName(),"Tracker") && !li->TRK2 && !li->TRK1 && !li->TRKh ) continue;
565 //
566 if ( !strcmp(key->GetName(),"Calorimeter") && !li->CAL2 && !li->CAL1 ) continue;
567 //
568 if ( !strcmp(key->GetName(),"ToF") && !li->TOF ) continue;
569 //
570 if ( !strcmp(key->GetName(),"Trigger") && !li->TRG ) continue;
571 //
572 if ( !strcmp(key->GetName(),"Anticounter") && !li->AC ) continue;
573 //
574 if ( !strcmp(key->GetName(),"S4") && !li->S4 ) continue;
575 //
576 if ( !strcmp(key->GetName(),"NeutronD") && !li->ND ) continue;
577 //
578 if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->ORB ) continue;
579 //
580 if ( !strcmp(key->GetName(),"Run") && !RUN ) continue;
581 //
582 if ( strcmp(key->GetName(),"Calorimeter") && strcmp(key->GetName(),"Tracker") && strcmp(key->GetName(),"ToF") && strcmp(key->GetName(),"Trigger") && strcmp(key->GetName(),"Anticounter") && strcmp(key->GetName(),"S4") && strcmp(key->GetName(),"NeutronD") && strcmp(key->GetName(),"OrbitalInfo") && strcmp(key->GetName(),"Run") && strcmp(key->GetName(),"ProcessID0") ){
583 if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());
584 continue;
585 };
586 //
587 allNames.Add(new TObjString(key->GetName()));
588 //
589 // read object from first source file
590 //
591 current_sourcedir->cd();
592 TObject *obj = key->ReadObj();
593 //
594 if ( obj->IsA()->InheritsFrom("TTree") ){
595 //
596 // loop over all source files create a chain of Trees "globChain"
597 //
598 TString obj_name;
599 //
600 if ( path.Length() ) {
601 obj_name = path + "/" + obj->GetName();
602 } else {
603 obj_name = obj->GetName();
604 };
605 //
606 globChain = new TChain(obj_name);
607 //
608 globChain->Add(first_source->GetName());
609 //
610 TFile *nextsource = (TFile*)sourcelist->After( first_source );
611 //
612 while ( nextsource ) {
613 //
614 //do not add to the list a file that does not contain this Tree
615 //
616 TFile *curf = TFile::Open(nextsource->GetName());
617 //
618 if ( curf ) {
619 //
620 Bool_t mustAdd = kFALSE;
621 //
622 if (curf->FindKey(obj_name)) {
623 //
624 mustAdd = kTRUE;
625 //
626 } else {
627 //
628 //we could be more clever here. No need to import the object
629 //we are missing a function in TDirectory
630 //
631 TObject *aobj = curf->Get(obj_name);
632 //
633 if ( aobj ){
634 mustAdd = kTRUE;
635 delete aobj;
636 };
637 };
638 if (mustAdd) {
639 globChain->Add(nextsource->GetName());
640 };
641 };
642 delete curf;
643 nextsource = (TFile*)sourcelist->After(nextsource);
644 };
645 //
646 } else {
647 //
648 // object is of no type that we know or can handle
649 //
650 if ( DebugMode() ) cout << "Unknown object type, name: "
651 << obj->GetName() << " title: " << obj->GetTitle() << endl;
652 };
653 //
654 // now write the merged histogram (which is "in" obj) to the target file
655 // note that this will just store obj in the current directory level,
656 // which is not persistent until the complete directory itself is stored
657 // by "target->Write()" below
658 //
659 if ( obj ){
660 //
661 target->cd();
662 //
663 if( obj->IsA()->InheritsFrom("TTree") ) {
664 //
665 //
666 if ( fastMethod ){
667 globChain->Merge(target->GetFile(),0,"keep fast");
668 } else {
669 globChain->Merge(target->GetFile(),0,"keep");
670 };
671 //
672 merged = true;
673 //
674 if ( DebugMode() ) globChain->ls();
675 //
676 delete globChain;
677 //
678 } else {
679 //
680 // obj->Write( key->GetName() ); // <==================================================================================
681 //
682 };
683 };
684 oldkey = key;
685 };
686 //
687 first_source = (TDirectory*)sourcelist->After(first_source);
688 //
689 };
690 // save modifications to target file
691 target->SaveSelf(kTRUE);
692 //
693 }

  ViewVC Help
Powered by ViewVC 1.1.23