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

Annotation of /PadmeAmidala/src/RunGlue.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations) (download)
Fri Sep 7 13:26:11 2007 UTC (17 years, 2 months ago) by mocchiut
Branch: MAIN
CVS Tags: v2r00
Changes since 1.9: +42 -17 lines
New feature: it can write directly on CASTOR via RFIO

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 <RunGlue.h>
9     //
10     ClassImp(RunGlue);
11     //
12     using namespace std;
13    
14     RunGlue::RunGlue() {
15     this->Clear();
16     }
17    
18 mocchiut 1.10 RunGlue::RunGlue(TSQLServer *da, UInt_t ru, TString di, TString wrkdi, Bool_t fcas) {
19 mocchiut 1.1 fDBG = false;
20 mocchiut 1.10 castor = fcas;
21 mocchiut 1.1 li = new PamLevel2();
22     this->Clear();
23     dbc = da;
24     run = ru;
25     dir = di;
26     //
27     TString wd = gSystem->WorkingDirectory();
28     //
29     outdir = wrkdi;
30     if ( !strcmp(outdir.Data(),"") ) outdir = wd;
31     fList = 0;
32     if ( run ){
33     runmode = true;
34     } else {
35     runmode = false;
36     createlist = true;
37     };
38     //
39     // lList = new TList();
40     //
41     };
42    
43    
44     void RunGlue::Clear() {
45     fEnd = false;
46     run = 0;
47     dir = "";
48     outdir = "";
49     fFilename = "";
50     };
51    
52     void RunGlue::Clean() {
53     if ( !merged ) gSystem->Unlink(fFilename);
54     lList->Delete();
55     delete Target;
56     };
57    
58     void RunGlue::SetDebug(Bool_t dbg) {
59     fDBG = dbg;
60     };
61    
62     void RunGlue::SetDList(TString list) {
63     //
64     RUN = true;
65     //
66     if (list.Contains("-RUN", TString::kIgnoreCase)) RUN = false;
67     if (list.Contains("-ALL", TString::kIgnoreCase)) RUN = false;
68     if (list.Contains("+RUN", TString::kIgnoreCase)) RUN = true;
69     if (list.Contains("+ALL", TString::kIgnoreCase)) RUN = true;
70     //
71     fDList = list;
72     li->SetWhichTrees(fDList);
73     //
74     if ( DebugMode() ) printf(" Detector list is %s \n",fDList.Data());
75     //
76     };
77    
78     TList *RunGlue::GetRunList(){
79     //
80     lList = new TList();
81     lList->Clear();
82     TString thisrun;
83 mocchiut 1.10 // TFile *su;
84 mocchiut 1.1 TFile *su;
85     //
86     TSQLResult *pResult;
87     TSQLRow *Row;
88     //
89     if ( run && runmode ){
90     //
91     // Qurey the GL_RUN table to obtain RUN infos
92     //
93     GL_RUN *glrun = new GL_RUN();
94     glrun->Query_GL_RUN(run,dbc);
95     //
96     // convert RUNHEADER_TIME into a UTC date
97     //
98     GL_TIMESYNC *dbtime = new GL_TIMESYNC();
99     //
100     TString UTC="UTC";
101     TString rhdate=dbtime->ConvertTime(UTC,glrun->RUNHEADER_TIME);
102     //
103     TDatime ti = TDatime(rhdate.Data());
104     //
105     YY=(UInt_t)ti.GetYear();
106     MM=(UInt_t)ti.GetMonth();
107     DD=(UInt_t)ti.GetDay();
108     //
109     TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
110     //
111     UInt_t lowerlimit = llim->GetSec();
112     UInt_t upperlimit = lowerlimit + 86401;
113     //
114     if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
115     //
116     YY -= 2000;
117     stringstream tmpf;
118     tmpf.str("");
119     tmpf << "L2PAM";
120     if ( YY < 10 ){
121     tmpf<< "0" << YY;
122     } else {
123     tmpf<< YY;
124     };
125     if ( MM < 10 ){
126     tmpf<< "0" << MM;
127     } else {
128     tmpf<< MM;
129     };
130     if ( DD < 10 ){
131     tmpf << "0" << DD << ".root";
132     } else {
133     tmpf << DD << ".root";
134     };
135     TString fpath = "";
136     if ( !strcmp(outdir,"") ){
137     fpath="./";
138     } else {
139     fpath += outdir;
140     fpath += "/";
141     };
142     //
143     fFilename = fpath + tmpf.str().c_str();
144     //
145     printf(" Output filename is %s \n",fFilename.Data());
146     //
147 mocchiut 1.4 if ( !this->OpenFile() ){
148     fEnd = true;
149     return(0);
150     };
151     //
152     stringstream myquery;
153     myquery.str("");
154     myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
155     << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
156     //
157     pResult = dbc->Query(myquery.str().c_str());
158     for( UInt_t r=0; r < 1000; r++){
159     Row = pResult->Next();
160     if( Row == NULL ) break;
161     if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
162     //
163     thisrun=dir+(TString)Row->GetField(0)+".Level2.root";
164     //
165     if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
166     //
167 mocchiut 1.10 //su = TFile::Open(thisrun);
168 mocchiut 1.4 su = TFile::Open(thisrun);
169     if ( li->CheckLevel2File(thisrun) ){
170     lList->Add(su);
171     } else {
172     if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
173     };
174     //
175     };
176     //
177     //
178 mocchiut 1.1 delete glrun;
179     delete dbtime;
180     //
181     };
182     //
183     if ( strcmp(dir.Data(),"" )){
184     //
185     if ( createlist ){
186     fList = new TList();
187     fDoneList = new TList();
188     fNlist = 0;
189     fNlistdone = 0;
190     //
191     // read files in the directory
192     //
193     if ( DebugMode() ) printf("\n No input run given\n Check for existing root files. \n");
194     //
195     TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(dir),dir);
196     TList *temp = datadir->GetListOfFiles();
197     //
198     TIter next(temp);
199     TSystemFile *questo = 0;
200     //
201     while ( (questo = (TSystemFile*)next()) ) {
202     TString name = questo->GetName();
203     if( name.EndsWith(".Level2.root") ){
204     char *fullpath0;
205     gSystem->IsFileInIncludePath(name,&fullpath0);
206     TString fullpath = fullpath0;
207     //
208     if ( DebugMode() ) printf(" fullpath = %s name %s \n",fullpath.Data(),name.Data());
209     //
210     //
211 mocchiut 1.10 //su = TFile::Open(fullpath);
212 mocchiut 1.1 su = TFile::Open(fullpath);
213     if ( li->CheckLevel2File((TString)fullpath) ){
214     fList->Add(su);
215     fNlist++;
216     } else {
217     if ( DebugMode() ) printf(" RUN %s DISCARDED \n",fullpath.Data());
218     };
219     //
220     };
221     };
222     createlist = false;
223     nrun = 0;
224     };
225     //
226     //
227     //
228     Bool_t stop = true;
229     UInt_t drun = 0;
230     while ( nrun < fNlist && stop ){
231     TString myrun = (TString)((TString)(gSystem->BaseName(((TFile*)fList->At(nrun))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
232     if ( DebugMode() ) printf(" nrun %u myrun is %s \n",nrun,(myrun.ReplaceAll(".Level2.root",12,"",0)).Data());
233     run = (UInt_t)(myrun.Atoi());
234     nrun++;
235     //
236     TString donerun = "";
237     for (UInt_t ll=0; ll<fNlistdone; ll++){
238     donerun = "";
239     donerun = (TString)((TString)(gSystem->BaseName(((TFile*)fDoneList->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
240     if ( DebugMode() ) printf(" donerun is %s \n",(donerun.ReplaceAll(".Level2.root",12,"",0)).Data());
241     drun = (UInt_t)(donerun.Atoi());
242     //
243     if ( run == drun ) goto jump;
244     //
245     };
246     //
247     stop = false;
248     //
249     jump:
250     drun = 0;
251     //
252     };
253     //
254     if ( nrun >= fNlist && stop ){
255     fEnd = true;
256     return(0);
257     };
258     //
259     // Qurey the GL_RUN table to obtain RUN infos
260     //
261     printf(" PROCESSING RUN %u AND ITS FRIENDS\n",run);
262     GL_RUN *glrun = new GL_RUN();
263     glrun->Query_GL_RUN(run,dbc);
264     //
265     // convert RUNHEADER_TIME into a UTC date
266     //
267     GL_TIMESYNC *dbtime = new GL_TIMESYNC();
268     //
269     TString UTC="UTC";
270     TString rhdate=dbtime->ConvertTime(UTC,glrun->RUNHEADER_TIME);
271     //
272     TDatime ti = TDatime(rhdate.Data());
273     //
274     YY=(UInt_t)ti.GetYear();
275     MM=(UInt_t)ti.GetMonth();
276     DD=(UInt_t)ti.GetDay();
277     //
278     TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
279     //
280     UInt_t lowerlimit = llim->GetSec();
281     UInt_t upperlimit = lowerlimit + 86401;
282     //
283     if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
284     //
285     YY -= 2000;
286     stringstream tmpf;
287     tmpf.str("");
288     tmpf << "L2PAM";
289     if ( YY < 10 ){
290     tmpf<< "0" << YY;
291     } else {
292     tmpf<< YY;
293     };
294     if ( MM < 10 ){
295     tmpf<< "0" << MM;
296     } else {
297     tmpf<< MM;
298     };
299     if ( DD < 10 ){
300     tmpf << "0" << DD << ".root";
301     } else {
302     tmpf << DD << ".root";
303     };
304     TString fpath = "";
305     if ( !strcmp(outdir,"") ){
306     fpath="./";
307     } else {
308     fpath += outdir;
309     fpath += "/";
310     };
311     //
312     fFilename = fpath + tmpf.str().c_str();
313     //
314     printf(" Output filename is %s \n",fFilename.Data());
315     //
316 mocchiut 1.4 if ( !this->OpenFile() ) return(0);
317     //
318     stringstream myquery;
319     myquery.str("");
320     myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
321     << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
322     //
323     pResult = dbc->Query(myquery.str().c_str());
324     for( UInt_t r=0; r < 1000; r++){
325     Row = pResult->Next();
326     if( Row == NULL ) break;
327     if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
328     //
329     thisrun=dir+(TString)Row->GetField(0)+".Level2.root";
330     //
331     if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
332     //
333     su = TFile::Open(thisrun);
334     if ( su ){
335 mocchiut 1.10 // TFile *su0 = TFile::Open(thisrun);
336 mocchiut 1.4 TFile *su0 = TFile::Open(thisrun);
337     fDoneList->Add(su0);
338     fNlistdone++;
339     };
340     if ( li->CheckLevel2File(thisrun) ){
341     lList->Add(su);
342     if ( DebugMode() ) printf(" RUN %s ADDED \n",Row->GetField(0));
343     } else {
344     if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
345     };
346     //
347     };
348     //
349 mocchiut 1.1 if ( DebugMode() ){
350     UInt_t ll = 0;
351     while ( (TFile*)lList->At(ll) ){
352     TString donerun = "";
353     donerun = (TString)((TString)(gSystem->BaseName(((TFile*)lList->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
354     printf(" XXX file is %s \n",donerun.Data());
355     ll++;
356     //
357     };
358     };
359     //
360     delete glrun;
361     delete dbtime;
362     //
363     };
364     //
365     // is it the end?
366     //
367     if ( runmode ){
368     fEnd = true;
369     } else {
370     //
371     // did we use all the run in the directory? if so exit
372     //
373     if ( nrun >= fNlist ) fEnd = true;
374     };
375     //
376     return(lList);
377     };
378    
379     Bool_t RunGlue::OpenFile(){
380     //
381 mocchiut 1.10 ifstream myfile;
382     TString thename;
383     //
384     Bool_t exi = false;
385 mocchiut 1.4 fOpen = false;
386 mocchiut 1.10 thename = Form("rfio://%s",(this->GetFilename()).Data());
387     Target = 0;
388     if ( castor ){
389     Target = new TRFIOFile(thename.Data(),"READ","",1);
390     if ( !Target->IsZombie()) exi = true;
391     } else {
392     // thename = Form("rfio://%s",(this->GetFilename()).Data());
393     printf(" Check if output file already exists \n");
394     myfile.open((this->GetFilename()).Data());
395     if ( myfile ) exi = true;
396     };
397     if ( exi ){
398 mocchiut 1.7 // Target = TFile::Open((this->GetFilename()).Data(), "READ" );
399     //
400     // if ( Target ){
401     // Target->Close();
402 mocchiut 1.10 if ( myfile ) myfile.close();
403     //
404     if ( Target ) Target->Close();
405     //
406     printf("Error opening target file, %s already exist!\n",thename.Data());
407 mocchiut 1.1 return(false);
408 mocchiut 1.10 //
409 mocchiut 1.1 } else {
410     //
411     printf(" Output file does not exist, creating it\n");
412     //
413 mocchiut 1.5 Long64_t maxsize = 99900000000LL;
414 mocchiut 1.1 //
415 mocchiut 1.10 Target = new TRFIOFile(thename.Data(), "RECREATE", "", 2 );
416     // Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );
417     //
418 mocchiut 1.3 //fastMethod = kTRUE;
419     fastMethod = kFALSE;
420 mocchiut 1.1 //
421     //
422     if ( !Target || Target->IsZombie()) {
423 mocchiut 1.10 printf("Error opening target file (does %s exist?)\n",thename.Data());
424 mocchiut 1.1 exit(1);
425     }
426     //
427     TTree::SetMaxTreeSize(maxsize);
428 mocchiut 1.5 Target->SetCompressionLevel(2);
429 mocchiut 1.1 //
430 mocchiut 1.4 fOpen = true;
431 mocchiut 1.1 return(true);
432     //
433     };
434     //
435     return(true);
436     //
437     };
438    
439    
440     void RunGlue::DeleteRunFiles(TList *dlist){
441     //
442     TString donerun = "";
443     //
444     printf(" Deleting runs: \n");
445     //
446     UInt_t ll = 0;
447     //
448     while ( (TFile*)dlist->At(ll) ){
449     donerun = (TString)(((TFile*)dlist->At(ll))->GetName());
450     gSystem->Unlink(donerun);
451     printf(" Deleted file is %s \n",donerun.Data());
452     ll++;
453     //
454     };
455     };
456    
457     void RunGlue::UpdateDB(TList *dlist){
458     //
459     TSQLResult *pResult;
460     TSQLRow *Row;
461     stringstream myquery;
462     //
463     TString donerun = "";
464     //
465     if ( DebugMode() ) printf(" Updating GL_RUN: \n");
466     //
467     UInt_t ll = 0;
468     UInt_t idl2 = 0;
469 mocchiut 1.2 UInt_t idr = 0;
470 mocchiut 1.1 //
471     //
472     myquery.str("");
473 mocchiut 1.2 myquery << "select ID from GL_RAW where NAME='level2 files';";
474     if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
475     //
476     pResult = dbc->Query(myquery.str().c_str());
477     if ( !pResult ){
478     printf(" ERROR QUERYING ON DB!\n");
479     return;
480     };
481     Row = pResult->Next();
482     if( Row == NULL ){
483     printf(" ERROR QUERYING THE DB!\n");
484     return;
485     } else {
486     idr = (UInt_t)atoll(Row->GetField(0));
487     };
488     //
489     myquery.str("");
490 mocchiut 1.9 // myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
491     myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'$PAM_L2','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
492 mocchiut 1.1 if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
493     //
494     pResult = dbc->Query(myquery.str().c_str());
495     if ( !pResult ){
496     printf(" ERROR WRITING ON DB!\n");
497     return;
498     };
499     myquery.str("");
500     myquery << "select ID from GL_ROOT where NAME='" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "';";
501     if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
502     //
503     pResult = dbc->Query(myquery.str().c_str());
504     if ( !pResult ){
505     printf(" ERROR WRITING ON DB!\n");
506     return;
507     };
508     Row = pResult->Next();
509     if( Row == NULL ){
510     printf(" ERROR QUERYING THE DB!\n");
511     return;
512     } else {
513     idl2 = (UInt_t)atoll(Row->GetField(0));
514     };
515     //
516     //
517     //
518     UInt_t drun = 0;
519     //
520     while ( (TFile*)dlist->At(ll) ){
521     donerun = "";
522     donerun = (TString)((TString)(gSystem->BaseName(((TFile*)dlist->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
523     drun = (UInt_t)(donerun.Atoi());
524     //
525     if ( DebugMode() ) printf(" Run is %s \n",donerun.Data());
526     //
527     myquery.str("");
528     myquery << "update GL_RUN set ID_ROOT_L2=" << idl2 << " where ID=" << drun << ";";
529     if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
530     //
531     pResult = dbc->Query(myquery.str().c_str());
532     if ( !pResult ){
533     printf(" ERROR WRITING ON DB!\n");
534     return;
535     };
536     //
537     ll++;
538     //
539     };
540     };
541    
542     //
543     // method stolen to ROOT's "hadd" and modified
544     //
545     void RunGlue::MergeRootfile(TList *sourcelist) {
546     //
547     merged = false;
548     //
549     TDirectory *target = Target;
550     //
551     if ( DebugMode() ) printf("\nTarget path is: %s \n",target->GetPath());
552     //
553 mocchiut 1.10 TString path;
554     //if ( castor ){
555     path = TString(":/");
556     // } else {
557     // path = TString( (char*)strstr( target->GetPath(), ":" ) );
558     // };
559 mocchiut 1.1 path.Remove(0,2);
560 mocchiut 1.10 if ( DebugMode() ) printf("path is %s \n",path.Data());
561 mocchiut 1.1 //
562     TDirectory *first_source = (TDirectory*)sourcelist->First();
563     THashList allNames;
564     //
565     while ( first_source ){
566     //
567     TDirectory *current_sourcedir = first_source->GetDirectory(path);
568     //
569     if ( !current_sourcedir ){
570     first_source = (TDirectory*)sourcelist->After(first_source);
571     continue;
572     }
573     //
574     // loop over all keys in this directory
575     //
576 mocchiut 1.6 TChain *globChain = 0;
577 mocchiut 1.1 TIter nextkey( current_sourcedir->GetListOfKeys() );
578 mocchiut 1.5 TKey *key = 0;
579     TKey *oldkey = 0;
580 mocchiut 1.1 TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory
581     //
582     while ( (key = (TKey*)nextkey()) ) {
583     //
584 mocchiut 1.5 // printf(" target ls \n");
585     // Target->ls();
586     //
587 mocchiut 1.1 if ( current_sourcedir == target ) break;
588     //
589 mocchiut 1.5 if ( oldkey && !strcmp(oldkey->GetName(),key->GetName()) ) continue; //keep only the highest cycle number for each key
590 mocchiut 1.1 //
591 mocchiut 1.5 if ( allNames.FindObject(key->GetName()) ) continue;
592 mocchiut 1.1 //
593     if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());
594     //
595     //
596     //
597 pamela 1.8 if ( !strcmp(key->GetName(),"Tracker") && !li->IsTRK2() && !li->IsTRK1() && !li->IsTRKh() ) continue;
598 mocchiut 1.1 //
599 pamela 1.8 if ( !strcmp(key->GetName(),"Calorimeter") && !li->IsCAL2() && !li->IsCAL1() ) continue;
600 mocchiut 1.1 //
601 pamela 1.8 if ( !strcmp(key->GetName(),"ToF") && !li->IsTOF() ) continue;
602 mocchiut 1.1 //
603 pamela 1.8 if ( !strcmp(key->GetName(),"Trigger") && !li->IsTRG() ) continue;
604 mocchiut 1.1 //
605 pamela 1.8 if ( !strcmp(key->GetName(),"Anticounter") && !li->IsAC() ) continue;
606 mocchiut 1.1 //
607 pamela 1.8 if ( !strcmp(key->GetName(),"S4") && !li->IsS4() ) continue;
608 mocchiut 1.1 //
609 pamela 1.8 if ( !strcmp(key->GetName(),"NeutronD") && !li->IsND() ) continue;
610 mocchiut 1.1 //
611 pamela 1.8 if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->IsORB() ) continue;
612 mocchiut 1.1 //
613 pamela 1.8 if ( !strcmp(key->GetName(),"Run") && !li->IsRUN() ) continue;
614 mocchiut 1.1 //
615     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") ){
616     if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());
617     continue;
618     };
619     //
620     allNames.Add(new TObjString(key->GetName()));
621     //
622     // read object from first source file
623     //
624     current_sourcedir->cd();
625     TObject *obj = key->ReadObj();
626     //
627     if ( obj->IsA()->InheritsFrom("TTree") ){
628     //
629     // loop over all source files create a chain of Trees "globChain"
630     //
631     TString obj_name;
632     //
633     if ( path.Length() ) {
634     obj_name = path + "/" + obj->GetName();
635     } else {
636     obj_name = obj->GetName();
637     };
638     //
639     globChain = new TChain(obj_name);
640     //
641 mocchiut 1.6 globChain->SetCacheSize(0);
642     //
643 mocchiut 1.1 globChain->Add(first_source->GetName());
644     //
645 mocchiut 1.10 // TFile *nextsource = (TFile*)sourcelist->After( first_source );
646 mocchiut 1.1 TFile *nextsource = (TFile*)sourcelist->After( first_source );
647     //
648     while ( nextsource ) {
649     //
650     //do not add to the list a file that does not contain this Tree
651     //
652     TFile *curf = TFile::Open(nextsource->GetName());
653 mocchiut 1.10 // TFile *curf = TFile::Open(nextsource->GetName());
654 mocchiut 1.1 //
655     if ( curf ) {
656     //
657     Bool_t mustAdd = kFALSE;
658     //
659     if (curf->FindKey(obj_name)) {
660     //
661     mustAdd = kTRUE;
662     //
663     } else {
664     //
665     //we could be more clever here. No need to import the object
666     //we are missing a function in TDirectory
667     //
668     TObject *aobj = curf->Get(obj_name);
669     //
670     if ( aobj ){
671     mustAdd = kTRUE;
672     delete aobj;
673     };
674     };
675     if (mustAdd) {
676     globChain->Add(nextsource->GetName());
677     };
678     };
679     delete curf;
680     nextsource = (TFile*)sourcelist->After(nextsource);
681     };
682     //
683 mocchiut 1.5 delete nextsource;
684     //
685 mocchiut 1.1 } else {
686     //
687     // object is of no type that we know or can handle
688     //
689     if ( DebugMode() ) cout << "Unknown object type, name: "
690     << obj->GetName() << " title: " << obj->GetTitle() << endl;
691     };
692     //
693     // now write the merged histogram (which is "in" obj) to the target file
694     // note that this will just store obj in the current directory level,
695     // which is not persistent until the complete directory itself is stored
696     // by "target->Write()" below
697     //
698     if ( obj ){
699     //
700     target->cd();
701     //
702     if( obj->IsA()->InheritsFrom("TTree") ) {
703     //
704 mocchiut 1.5 Long64_t nfiles = 0;
705 mocchiut 1.1 if ( fastMethod ){
706 mocchiut 1.5 // globChain->Merge(target->GetFile(),0,"C keep fast");
707     nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep fast");
708 mocchiut 1.1 } else {
709 mocchiut 1.5 //globChain->Merge(target->GetFile(),0,"C keep");
710     nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep");
711 mocchiut 1.1 };
712     //
713     merged = true;
714     //
715 mocchiut 1.5 if ( DebugMode() ){
716     printf(" Merged %i files\n",(int)nfiles);
717     globChain->ls();
718     };
719 mocchiut 1.1 //
720     delete globChain;
721 mocchiut 1.5 // return;
722 mocchiut 1.1 //
723     } else {
724     //
725     // obj->Write( key->GetName() ); // <==================================================================================
726     //
727     };
728     };
729 mocchiut 1.5 delete obj;
730 mocchiut 1.1 oldkey = key;
731     };
732     //
733     first_source = (TDirectory*)sourcelist->After(first_source);
734     //
735     };
736     // save modifications to target file
737 mocchiut 1.5 //
738 mocchiut 1.1 target->SaveSelf(kTRUE);
739     //
740 mocchiut 1.5 };
741    
742    
743 mocchiut 1.10 //Long64_t RunGlue::Mergy(TChain *mychain, TFile* file, Int_t basketsize, Option_t* option){
744 mocchiut 1.5 Long64_t RunGlue::Mergy(TChain *mychain, TFile* file, Int_t basketsize, Option_t* option){
745     // We must have been passed a file, we will use it
746     // later to reset the compression level of the branches.
747     if (!file) {
748     // FIXME: We need an error message here.
749     return 0;
750     }
751    
752     // Options
753     Bool_t fastClone = kFALSE;
754     TString opt = option;
755     opt.ToLower();
756     if (opt.Contains("fast")) {
757     fastClone = kTRUE;
758     }
759    
760     // The chain tree must have a list of branches
761     // because we may try to change their basket
762     // size later.
763     TObjArray* lbranches = mychain->GetListOfBranches();
764     if (!lbranches) {
765     // FIXME: We need an error message here.
766     return 0;
767     }
768    
769     // The chain must have a current tree because
770     // that is the one we will clone.
771     // if (!fTree) {
772     // -- LoadTree() has not yet been called, no current tree.
773     // FIXME: We need an error message here.
774     // return 0;
775     // }
776    
777     // Copy the chain's current tree without
778     // copying any entries, we will do that later.
779     TTree* newTree = mychain->CloneTree(0);
780     if (!newTree) {
781     // FIXME: We need an error message here.
782     return 0;
783     }
784    
785     // Strip out the (potential) directory name.
786     // FIXME: The merged chain may or may not have the
787     // same name as the original chain. This is
788     // bad because the chain name determines the
789     // names of the trees in the chain by default.
790     newTree->SetName(gSystem->BaseName(mychain->GetName()));
791    
792     // FIXME: Why do we do this?
793     // newTree->SetAutoSave(-1);
794     newTree->SetAutoSave(900000000000000LL);
795    
796     // Circularity is incompatible with merging, it may
797     // force us to throw away entries, which is not what
798     // we are supposed to do.
799     newTree->SetCircular(0);
800    
801     // Reset the compression level of the branches.
802     if (opt.Contains("c")) {
803     TBranch* branch = 0;
804     TIter nextb(newTree->GetListOfBranches());
805     while ((branch = (TBranch*) nextb())) {
806     branch->SetCompressionLevel(file->GetCompressionLevel());
807     }
808     }
809    
810     // Reset the basket size of the branches.
811     if (basketsize > 1000) {
812     TBranch* branch = 0;
813     TIter nextb(newTree->GetListOfBranches());
814     while ((branch = (TBranch*) nextb())) {
815     branch->SetBasketSize(basketsize);
816     }
817     }
818    
819     Long64_t nentries = mychain->GetEntriesFast();
820    
821     // Copy the entries.
822     if (fastClone) {
823     // For each tree in the chain.
824     for (Long64_t i = 0; i < nentries; i += mychain->GetTree()->GetEntries()) {
825     if (mychain->LoadTree(i) < 0) {
826     break;
827     }
828     TTreeCloner cloner(mychain->GetTree(), newTree, option);
829     if (cloner.IsValid()) {
830     newTree->SetEntries(newTree->GetEntries() + mychain->GetTree()->GetEntries());
831     cloner.Exec();
832     } else {
833     if (mychain->GetFile()) {
834     printf("Merge Skipped file %s\n", mychain->GetFile()->GetName());
835     // } else {
836     // Warning("Merge", "Skipped file number %d\n", fTreeNumber);
837     }
838     }
839     }
840     } else {
841     for (Long64_t i = 0; i < nentries; i++) {
842     if (mychain->GetEntry(i) <= 0) {
843     break;
844     }
845     newTree->Fill();
846     }
847     }
848    
849     // Write the new tree header.
850     newTree->Write();
851    
852     // Get our return value.
853     Int_t nfiles = newTree->GetFileNumber() + 1;
854    
855     // Close and delete the current file of the new tree.
856     if (!opt.Contains("keep")) {
857     // FIXME: What happens to fDirectory in newTree here?
858     delete newTree->GetCurrentFile();
859     }
860     return nfiles;
861 mocchiut 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.23