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

Contents of /PadmeAmidala/src/RunGlue.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Tue Dec 5 12:31:00 2006 UTC (18 years ago) by mocchiut
Branch: PadmeAmidala
CVS Tags: v0r01, start
Changes since 1.1: +0 -0 lines
Imported sources

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

  ViewVC Help
Powered by ViewVC 1.1.23