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

Annotation of /PadmeAmidala/src/RunGlue.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Wed Dec 6 11:01:52 2006 UTC (17 years, 11 months ago) by mocchiut
Branch: MAIN
Changes since 1.2: +2 -2 lines
use slow method

1 mocchiut 1.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 mocchiut 1.3 //fastMethod = kTRUE;
389     fastMethod = kFALSE;
390 mocchiut 1.1 //
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 mocchiut 1.2 UInt_t idr = 0;
438 mocchiut 1.1 //
439     //
440     myquery.str("");
441 mocchiut 1.2 myquery << "select ID from GL_RAW where NAME='level2 files';";
442     if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
443     //
444     pResult = dbc->Query(myquery.str().c_str());
445     if ( !pResult ){
446     printf(" ERROR QUERYING ON DB!\n");
447     return;
448     };
449     Row = pResult->Next();
450     if( Row == NULL ){
451     printf(" ERROR QUERYING THE DB!\n");
452     return;
453     } else {
454     idr = (UInt_t)atoll(Row->GetField(0));
455     };
456     //
457     myquery.str("");
458     myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
459 mocchiut 1.1 if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
460     //
461     pResult = dbc->Query(myquery.str().c_str());
462     if ( !pResult ){
463     printf(" ERROR WRITING ON DB!\n");
464     return;
465     };
466     myquery.str("");
467     myquery << "select ID from GL_ROOT where NAME='" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "';";
468     if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
469     //
470     pResult = dbc->Query(myquery.str().c_str());
471     if ( !pResult ){
472     printf(" ERROR WRITING ON DB!\n");
473     return;
474     };
475     Row = pResult->Next();
476     if( Row == NULL ){
477     printf(" ERROR QUERYING THE DB!\n");
478     return;
479     } else {
480     idl2 = (UInt_t)atoll(Row->GetField(0));
481     };
482     //
483     //
484     //
485     UInt_t drun = 0;
486     //
487     while ( (TFile*)dlist->At(ll) ){
488     donerun = "";
489     donerun = (TString)((TString)(gSystem->BaseName(((TFile*)dlist->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
490     drun = (UInt_t)(donerun.Atoi());
491     //
492     if ( DebugMode() ) printf(" Run is %s \n",donerun.Data());
493     //
494     myquery.str("");
495     myquery << "update GL_RUN set ID_ROOT_L2=" << idl2 << " where ID=" << drun << ";";
496     if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
497     //
498     pResult = dbc->Query(myquery.str().c_str());
499     if ( !pResult ){
500     printf(" ERROR WRITING ON DB!\n");
501     return;
502     };
503     //
504     ll++;
505     //
506     };
507     };
508    
509     //
510     // method stolen to ROOT's "hadd" and modified
511     //
512     void RunGlue::MergeRootfile(TList *sourcelist) {
513     //
514     merged = false;
515     //
516     TDirectory *target = Target;
517     //
518     if ( DebugMode() ) printf("\nTarget path is: %s \n",target->GetPath());
519     //
520     TString path( (char*)strstr( target->GetPath(), ":" ) );
521     path.Remove(0,2);
522     //
523     TDirectory *first_source = (TDirectory*)sourcelist->First();
524     THashList allNames;
525     //
526     while ( first_source ){
527     //
528     TDirectory *current_sourcedir = first_source->GetDirectory(path);
529     //
530     if ( !current_sourcedir ){
531     first_source = (TDirectory*)sourcelist->After(first_source);
532     continue;
533     }
534     //
535     // loop over all keys in this directory
536     //
537     TChain *globChain = 0;
538     TIter nextkey( current_sourcedir->GetListOfKeys() );
539     TKey *key, *oldkey=0;
540     TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory
541     //
542     while ( (key = (TKey*)nextkey()) ) {
543     //
544     if ( current_sourcedir == target ) break;
545     //
546     if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; //keep only the highest cycle number for each key
547     //
548     if (allNames.FindObject(key->GetName())) continue;
549     //
550     if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());
551     //
552     //
553     //
554     if ( !strcmp(key->GetName(),"Tracker") && !li->TRK2 && !li->TRK1 && !li->TRKh ) continue;
555     //
556     if ( !strcmp(key->GetName(),"Calorimeter") && !li->CAL2 && !li->CAL1 ) continue;
557     //
558     if ( !strcmp(key->GetName(),"ToF") && !li->TOF ) continue;
559     //
560     if ( !strcmp(key->GetName(),"Trigger") && !li->TRG ) continue;
561     //
562     if ( !strcmp(key->GetName(),"Anticounter") && !li->AC ) continue;
563     //
564     if ( !strcmp(key->GetName(),"S4") && !li->S4 ) continue;
565     //
566     if ( !strcmp(key->GetName(),"NeutronD") && !li->ND ) continue;
567     //
568     if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->ORB ) continue;
569     //
570     if ( !strcmp(key->GetName(),"Run") && !RUN ) continue;
571     //
572     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") ){
573     if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());
574     continue;
575     };
576     //
577     allNames.Add(new TObjString(key->GetName()));
578     //
579     // read object from first source file
580     //
581     current_sourcedir->cd();
582     TObject *obj = key->ReadObj();
583     //
584     if ( obj->IsA()->InheritsFrom("TTree") ){
585     //
586     // loop over all source files create a chain of Trees "globChain"
587     //
588     TString obj_name;
589     //
590     if ( path.Length() ) {
591     obj_name = path + "/" + obj->GetName();
592     } else {
593     obj_name = obj->GetName();
594     };
595     //
596     globChain = new TChain(obj_name);
597     //
598     globChain->Add(first_source->GetName());
599     //
600     TFile *nextsource = (TFile*)sourcelist->After( first_source );
601     //
602     while ( nextsource ) {
603     //
604     //do not add to the list a file that does not contain this Tree
605     //
606     TFile *curf = TFile::Open(nextsource->GetName());
607     //
608     if ( curf ) {
609     //
610     Bool_t mustAdd = kFALSE;
611     //
612     if (curf->FindKey(obj_name)) {
613     //
614     mustAdd = kTRUE;
615     //
616     } else {
617     //
618     //we could be more clever here. No need to import the object
619     //we are missing a function in TDirectory
620     //
621     TObject *aobj = curf->Get(obj_name);
622     //
623     if ( aobj ){
624     mustAdd = kTRUE;
625     delete aobj;
626     };
627     };
628     if (mustAdd) {
629     globChain->Add(nextsource->GetName());
630     };
631     };
632     delete curf;
633     nextsource = (TFile*)sourcelist->After(nextsource);
634     };
635     //
636     } else {
637     //
638     // object is of no type that we know or can handle
639     //
640     if ( DebugMode() ) cout << "Unknown object type, name: "
641     << obj->GetName() << " title: " << obj->GetTitle() << endl;
642     };
643     //
644     // now write the merged histogram (which is "in" obj) to the target file
645     // note that this will just store obj in the current directory level,
646     // which is not persistent until the complete directory itself is stored
647     // by "target->Write()" below
648     //
649     if ( obj ){
650     //
651     target->cd();
652     //
653     if( obj->IsA()->InheritsFrom("TTree") ) {
654     //
655     //
656     if ( fastMethod ){
657     globChain->Merge(target->GetFile(),0,"keep fast");
658     } else {
659     globChain->Merge(target->GetFile(),0,"keep");
660     };
661     //
662     merged = true;
663     //
664     if ( DebugMode() ) globChain->ls();
665     //
666     delete globChain;
667     //
668     } else {
669     //
670     // obj->Write( key->GetName() ); // <==================================================================================
671     //
672     };
673     };
674     oldkey = key;
675     };
676     //
677     first_source = (TDirectory*)sourcelist->After(first_source);
678     //
679     };
680     // save modifications to target file
681     target->SaveSelf(kTRUE);
682     //
683     }

  ViewVC Help
Powered by ViewVC 1.1.23