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

Diff of /PadmeAmidala/src/RunGlue.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by mocchiut, Wed Dec 6 11:01:52 2006 UTC revision 1.23 by mocchiut, Fri Oct 17 14:04:04 2014 UTC
# Line 11  Line 11 
11  #include <TFile.h>  #include <TFile.h>
12  #include <TTree.h>  #include <TTree.h>
13  #include <TTimeStamp.h>  #include <TTimeStamp.h>
14    #include <TTreeCloner.h>
15  #include <RunGlue.h>  #include <RunGlue.h>
16  //  //
17  ClassImp(RunGlue);  ClassImp(RunGlue);
# Line 24  RunGlue::RunGlue() { Line 25  RunGlue::RunGlue() {
25  RunGlue::RunGlue(TSQLServer *da, UInt_t ru, TString di, TString wrkdi) {  RunGlue::RunGlue(TSQLServer *da, UInt_t ru, TString di, TString wrkdi) {
26    fDBG = false;    fDBG = false;
27    li = new PamLevel2();    li = new PamLevel2();
28      //  li = 0;
29    this->Clear();    this->Clear();
30    dbc = da;    dbc = da;
31    run = ru;    run = ru;
32    dir = di;    dir = di;
33    //    //
34    TString wd = gSystem->WorkingDirectory();      wd = gSystem->WorkingDirectory();  
35    //    //
36    outdir = wrkdi;    outdir = wrkdi;
37    if ( !strcmp(outdir.Data(),"") ) outdir = wd;    if ( !strcmp(outdir.Data(),"") ) outdir = wd;
# Line 40  RunGlue::RunGlue(TSQLServer *da, UInt_t Line 42  RunGlue::RunGlue(TSQLServer *da, UInt_t
42      runmode = false;      runmode = false;
43      createlist = true;      createlist = true;
44    };    };
45      fUpgrade = true;
46    //    //
47    //  lList = new TList();    //  lList = new TList();
48    //    //
49  };  };
50    
   
51  void RunGlue::Clear() {  void RunGlue::Clear() {
52    fEnd = false;    fEnd = false;
53    run = 0;    run = 0;
# Line 74  void RunGlue::SetDList(TString list) { Line 76  void RunGlue::SetDList(TString list) {
76    if (list.Contains("+ALL", TString::kIgnoreCase)) RUN = true;    if (list.Contains("+ALL", TString::kIgnoreCase)) RUN = true;
77    //    //
78    fDList = list;    fDList = list;
79    li->SetWhichTrees(fDList);    //  li->SetWhichTrees(fDList);
80    //    //
81    if ( DebugMode() ) printf(" Detector list is %s \n",fDList.Data());    if ( DebugMode() ) printf(" Detector list is %s \n",fDList.Data());
82    //    //
83  };  };
84    
85  TList *RunGlue::GetRunList(){  TList *RunGlue::GetRunList(){
86    //    //  
87    lList = new TList();    lList = new TList();
88    lList->Clear();    lList->Clear();
89    TString thisrun;    TString thisrun;
# Line 113  TList *RunGlue::GetRunList(){ Line 115  TList *RunGlue::GetRunList(){
115      TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);      TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
116      //      //
117      UInt_t lowerlimit = llim->GetSec();      UInt_t lowerlimit = llim->GetSec();
118      UInt_t upperlimit = lowerlimit + 86401;      UInt_t upperlimit = lowerlimit + 86400;
     //  
     stringstream myquery;  
     myquery.str("");  
     myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit  
             << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";  
     //  
     pResult = dbc->Query(myquery.str().c_str());  
     for( UInt_t r=0; r < 1000; r++){  
       Row = pResult->Next();        
       if( Row == NULL ) break;  
       if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));  
       //  
       thisrun=dir+(TString)Row->GetField(0)+".Level2.root";        
       //  
       if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());  
       //  
       su = TFile::Open(thisrun);  
       if ( li->CheckLevel2File(thisrun) ){  
         lList->Add(su);  
       } else {  
         if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));  
       };  
       //  
     };  
119      //      //
120      if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);      if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
121      //      //
# Line 172  TList *RunGlue::GetRunList(){ Line 150  TList *RunGlue::GetRunList(){
150      //      //
151      printf(" Output filename is %s \n",fFilename.Data());      printf(" Output filename is %s \n",fFilename.Data());
152      //      //
153        if ( !this->OpenFile() ){
154             fEnd = true;
155             return(0);
156        };
157        //
158        stringstream myquery;
159        myquery.str("");
160        myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
161                << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
162        //
163        pResult = dbc->Query(myquery.str().c_str());
164        for( UInt_t r=0; r < 1000; r++){
165          Row = pResult->Next();      
166          if( Row == NULL ) break;
167          if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
168          //
169          thisrun=dir+(TString)Row->GetField(0)+".Level2.root";      
170          //
171          if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
172          //
173          su = TFile::Open(thisrun);
174          if ( li->CheckLevel2File(thisrun) ){
175            lList->Add(su);
176          } else {
177                    if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
178          };
179          //
180        };
181        //
182        //
183      delete glrun;      delete glrun;
184      delete dbtime;      delete dbtime;
185      //      //
# Line 274  TList *RunGlue::GetRunList(){ Line 282  TList *RunGlue::GetRunList(){
282      TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);      TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
283      //      //
284      UInt_t lowerlimit = llim->GetSec();      UInt_t lowerlimit = llim->GetSec();
285      UInt_t upperlimit = lowerlimit + 86401;      UInt_t upperlimit = lowerlimit + 86400;
     //  
     stringstream myquery;  
     myquery.str("");  
     myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit  
             << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";  
     //  
     pResult = dbc->Query(myquery.str().c_str());  
     for( UInt_t r=0; r < 1000; r++){  
       Row = pResult->Next();        
       if( Row == NULL ) break;  
       if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));  
       //  
       thisrun=dir+(TString)Row->GetField(0)+".Level2.root";        
       //  
       if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());  
       //  
       su = TFile::Open(thisrun);  
       if ( su ){  
         TFile *su0 = TFile::Open(thisrun);  
         fDoneList->Add(su0);  
         fNlistdone++;  
       };  
       if ( li->CheckLevel2File(thisrun) ){  
         lList->Add(su);  
         if ( DebugMode() ) printf(" RUN %s ADDED \n",Row->GetField(0));  
       } else {  
         if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));  
       };  
       //  
     };  
286      //      //
287      if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);      if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
288      //      //
# Line 339  TList *RunGlue::GetRunList(){ Line 317  TList *RunGlue::GetRunList(){
317      //      //
318      printf(" Output filename is %s \n",fFilename.Data());      printf(" Output filename is %s \n",fFilename.Data());
319      //      //
320        if ( !this->OpenFile() ) return(0);
321        //
322        stringstream myquery;
323        myquery.str("");
324        myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
325                << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
326        //
327        pResult = dbc->Query(myquery.str().c_str());
328        for( UInt_t r=0; r < 1000; r++){
329          Row = pResult->Next();      
330          if( Row == NULL ) break;
331          if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
332          //
333          thisrun=dir+(TString)Row->GetField(0)+".Level2.root";      
334          //
335          if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
336          //
337          su = TFile::Open(thisrun);
338          if ( su ){
339            TFile *su0 = TFile::Open(thisrun);
340            fDoneList->Add(su0);
341            fNlistdone++;
342          };
343          if ( li->CheckLevel2File(thisrun) ){
344            lList->Add(su);
345            if ( DebugMode() ) printf(" RUN %s ADDED \n",Row->GetField(0));
346          } else {
347            if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
348          };
349          //
350        };
351        //
352      if ( DebugMode() ){      if ( DebugMode() ){
353        UInt_t ll = 0;        UInt_t ll = 0;
354        while ( (TFile*)lList->At(ll) ){        while ( (TFile*)lList->At(ll) ){
# Line 371  TList *RunGlue::GetRunList(){ Line 381  TList *RunGlue::GetRunList(){
381    
382  Bool_t RunGlue::OpenFile(){  Bool_t RunGlue::OpenFile(){
383    //    //
384      fOpen = false;
385    printf(" Check if output file already exists \n");    printf(" Check if output file already exists \n");
386    Target = TFile::Open((this->GetFilename()).Data(), "READ" );    ifstream myfile;
387    //    myfile.open((this->GetFilename()).Data());
388    if ( Target ){    if ( myfile ){
389      Target->Close();      //  Target = TFile::Open((this->GetFilename()).Data(), "READ" );
390        //
391        //  if ( Target ){
392        //    Target->Close();
393        myfile.close();
394      printf("Error opening target file, %s  already exist!\n",(this->GetFilename()).Data());      printf("Error opening target file, %s  already exist!\n",(this->GetFilename()).Data());
395      return(false);      return(false);
396    } else {    } else {
397      //      //
398        // try to lock tables and work on a db level to avoid multiple file simultaneous access
399        //
400        stringstream oss;
401        //  
402        if ( fUpgrade ){
403          oss.str("");
404          oss << "lock table GL_ROOT write;";
405          TSQLResult *result = 0;
406          TSQLRow *row = 0;
407          result = dbc->Query(oss.str().c_str());
408          oss.str("");
409          oss << "select ID from GL_ROOT where NAME='" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "';";
410          result = dbc->Query(oss.str().c_str());
411          row = result->Next();
412          if ( row ){
413            printf("Error opening target file (does %s exist? already in the DB? are the permissions ok?)\n",(this->GetFilename()).Data());
414            return(false);
415          } else {
416            oss.str("");
417            oss << "insert into GL_ROOT (PATH,NAME) values ('$PAM_L2','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";      
418            result = dbc->Query(oss.str().c_str());
419          };
420          oss.str("");
421          oss << "unlock tables;";
422          result = dbc->Query(oss.str().c_str());
423        };
424        //    Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );
425        Target = TFile::Open((this->GetFilename()).Data(), "NEW" );    
426        //    
427      printf(" Output file does not exist, creating it\n");      printf(" Output file does not exist, creating it\n");
428      //      //
429      Long64_t maxsize = 10000000000LL;        Long64_t maxsize = 99900000000LL;  
430        //
431        if ( !Target || Target->IsZombie()) {
432          printf("Error opening target file (does %s exist? are the permissions ok?)\n",(this->GetFilename()).Data());
433          //      exit(1);
434          return(false);
435        }
436        //
437        Target->Write();
438      //      //
     Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );  
439      //fastMethod = kTRUE;      //fastMethod = kTRUE;
440      fastMethod = kFALSE;      fastMethod = kFALSE;
441      //          //    
     //      
     if ( !Target || Target->IsZombie()) {  
       printf("Error opening target file (does %s exist?)\n",(this->GetFilename()).Data());  
       exit(1);  
     }  
442      //      //
443      TTree::SetMaxTreeSize(maxsize);      TTree::SetMaxTreeSize(maxsize);
444        //    Target->SetCompressionLevel(2);
445        Target->SetCompressionLevel(3);
446        //Target->SetCompressionLevel(5);
447      //      //
448        fOpen = true;
449      return(true);      return(true);
450      //      //
451    };    };
# Line 455  void RunGlue::UpdateDB(TList *dlist){ Line 505  void RunGlue::UpdateDB(TList *dlist){
505    };    };
506    //    //
507    myquery.str("");    myquery.str("");
   myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";  
   if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());  
   //  
   pResult = dbc->Query(myquery.str().c_str());  
   if ( !pResult ){  
     printf(" ERROR WRITING ON DB!\n");  
     return;  
   };  
   myquery.str("");  
508    myquery << "select ID from GL_ROOT where NAME='" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "';";    myquery << "select ID from GL_ROOT where NAME='" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "';";
509    if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());    if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
510    //    //
# Line 479  void RunGlue::UpdateDB(TList *dlist){ Line 520  void RunGlue::UpdateDB(TList *dlist){
520    } else {    } else {
521      idl2 = (UInt_t)atoll(Row->GetField(0));      idl2 = (UInt_t)atoll(Row->GetField(0));
522    };    };
523      myquery.str("");
524      //  myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
525      //  myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'$PAM_L2','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
526      myquery << "update GL_ROOT set ID_RAW=" << idr << " where ID=" << idl2 << ";";
527      if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
528      //
529      pResult = dbc->Query(myquery.str().c_str());
530      if ( !pResult ){
531        printf(" ERROR WRITING ON DB!\n");
532        return;
533      };
534    //    //
535    //    //
536    //    //
# Line 511  void RunGlue::UpdateDB(TList *dlist){ Line 563  void RunGlue::UpdateDB(TList *dlist){
563  //  //
564  void RunGlue::MergeRootfile(TList *sourcelist) {  void RunGlue::MergeRootfile(TList *sourcelist) {
565    //    //
566    merged = false;    merged = true;
   //  
   TDirectory *target = Target;  
   //  
   if ( DebugMode() ) printf("\nTarget path is: %s \n",target->GetPath());  
567    //    //
568    TString path( (char*)strstr( target->GetPath(), ":" ) );    //  if ( li ) li->Delete();
569    path.Remove(0,2);    PamLevel2 *nli = new PamLevel2(wd,sourcelist,fDList);
570    //    nli->SetSELLI(2);
571    TDirectory *first_source = (TDirectory*)sourcelist->First();    //
572    THashList allNames;    Target->cd();
573    //    //
574    while ( first_source ){    nli->CreateCloneTrees(Target);
575      //    //
576      TDirectory *current_sourcedir = first_source->GetDirectory(path);    /*  nli->GetCloneTree("Tracker")->SetAutoSave(900000000000000LL);
577      //    nli->GetCloneTree("Trigger")->SetAutoSave(900000000000000LL);
578      if ( !current_sourcedir ){    nli->GetCloneTree("Calorimeter")->SetAutoSave(900000000000000LL);
579        first_source = (TDirectory*)sourcelist->After(first_source);    nli->GetCloneTree("S4")->SetAutoSave(900000000000000LL);
580        continue;    nli->GetCloneTree("Anticounter")->SetAutoSave(900000000000000LL);
581      nli->GetCloneTree("NeutronD")->SetAutoSave(900000000000000LL);
582      nli->GetCloneTree("OrbitalInfo")->SetAutoSave(900000000000000LL);
583      nli->GetCloneTree("Run")->SetAutoSave(900000000000000LL);
584      nli->GetCloneTree("ToF")->SetAutoSave(900000000000000LL);*/
585      //
586      ULong64_t nevents = nli->GetEntries();        
587      printf(" NEVENTS %llu \n",nevents);
588      for(ULong64_t iev=0; iev<nevents; iev++){
589        nli->Clear();
590        if( nli->GetEntry(iev) ){
591          nli->FillCloneTrees();
592        };
593      };
594      Target->cd();
595      nli->WriteCloneTrees();
596      printf("Written file %s \n",Target->GetName());
597      //  Target->Write();
598      //  TTree *slist = (TTree*)Target->Get("SelectionList");
599      //  slist->Delete("all");
600      if ( !nli->GetCloneTree("ProcessingInfo") ){
601        TChain *P = new TChain("ProcessingInfo");
602        // loop over files and create chains
603        TIter next(sourcelist);
604        TSystemFile *questo = 0;
605        while ((questo = (TSystemFile*) next())) {
606          TString name = questo->GetName();
607          P->Add(name);
608      }      }
609      //      if ( P->GetEntries() ){
610      // loop over all keys in this directory        TTree *Pclone = P->CloneTree();
611      //  
612      TChain *globChain = 0;  
613      TIter nextkey( current_sourcedir->GetListOfKeys() );  
614      TKey *key, *oldkey=0;        ProcInfo *procinfo = new ProcInfo();
615      TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory        procinfo->runID = 0;
616      //        TTimeStamp *dt = new TTimeStamp();
617      while ( (key = (TKey*)nextkey()) ) {        procinfo->date = dt->AsString();
618        //        delete dt;
619        if ( current_sourcedir == target ) break;        TString isok;
620        //        if ( nli->HasDiscardedFiles() ){
621        if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; //keep only the highest cycle number for each key          isok = " WARNING, files were discarded while merging! ";
       //  
       if (allNames.FindObject(key->GetName())) continue;  
       //  
       if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());  
       //  
       //  
       //  
       if ( !strcmp(key->GetName(),"Tracker") && !li->TRK2 && !li->TRK1 && !li->TRKh ) continue;  
       //  
       if ( !strcmp(key->GetName(),"Calorimeter") && !li->CAL2 && !li->CAL1 ) continue;  
       //  
       if ( !strcmp(key->GetName(),"ToF") && !li->TOF ) continue;  
       //  
       if ( !strcmp(key->GetName(),"Trigger") && !li->TRG ) continue;  
       //  
       if ( !strcmp(key->GetName(),"Anticounter") && !li->AC ) continue;  
       //  
       if ( !strcmp(key->GetName(),"S4") && !li->S4 ) continue;  
       //  
       if ( !strcmp(key->GetName(),"NeutronD") && !li->ND ) continue;  
       //  
       if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->ORB ) continue;  
       //  
       if ( !strcmp(key->GetName(),"Run") && !RUN ) continue;  
       //  
       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") ){  
         if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());  
         continue;  
       };  
       //  
       allNames.Add(new TObjString(key->GetName()));  
       //  
       // read object from first source file  
       //  
       current_sourcedir->cd();  
       TObject *obj = key->ReadObj();  
       //  
       if ( obj->IsA()->InheritsFrom("TTree") ){  
         //  
         // loop over all source files create a chain of Trees "globChain"  
         //  
         TString obj_name;  
         //  
         if ( path.Length() ) {  
           obj_name = path + "/" + obj->GetName();  
         } else {  
           obj_name = obj->GetName();  
         };  
         //  
         globChain = new TChain(obj_name);  
         //  
         globChain->Add(first_source->GetName());  
         //  
         TFile *nextsource = (TFile*)sourcelist->After( first_source );  
         //  
         while ( nextsource ) {              
           //  
           //do not add to the list a file that does not contain this Tree  
           //  
           TFile *curf = TFile::Open(nextsource->GetName());  
           //  
           if ( curf ) {  
             //  
             Bool_t mustAdd = kFALSE;  
             //  
             if (curf->FindKey(obj_name)) {  
               //  
               mustAdd = kTRUE;  
               //  
             } else {  
               //  
               //we could be more clever here. No need to import the object  
               //we are missing a function in TDirectory  
               //  
               TObject *aobj = curf->Get(obj_name);  
               //  
               if ( aobj ){  
                 mustAdd = kTRUE;  
                 delete aobj;  
               };  
             };  
             if (mustAdd) {  
               globChain->Add(nextsource->GetName());  
             };  
           };  
           delete curf;  
           nextsource = (TFile*)sourcelist->After(nextsource);  
         };  
         //  
622        } else {        } else {
623          //          isok = " the level2 merging was OK!";
624          // object is of no type that we know or can handle        }
625          //        procinfo->commandLine += Form("PadmeAmidala  run: %i dir: %s wd %s. %s",run,dir.Data(),wd.Data(),isok.Data());
626          if ( DebugMode() ) cout << "Unknown object type, name: "        procinfo->outputFilename = Form("%s %s ",outdir.Data(),Target->GetName());
627               << obj->GetName() << " title: " << obj->GetTitle() << endl;        procinfo->localDir = gSystem->WorkingDirectory();
628        };        procinfo->uname = gSystem->GetFromPipe("uname -a");
629        //        procinfo->DB = Form("Host %s DB %s ",dbc->GetHost(),dbc->GetDB());
630        // now write the merged histogram (which is "in" obj) to the target file        
631        // note that this will just store obj in the current directory level,        Pclone->SetBranchAddress("ProcInfo",&procinfo);
632        // which is not persistent until the complete directory itself is stored        Pclone->Fill();
633        // by "target->Write()" below  
634        //        Target->cd();
635        if ( obj ){        Pclone->Write("ProcessingInfo",TObject::kOverwrite);
636          //        P->Delete();
637          target->cd();        P=0;
638          //      }
639          if( obj->IsA()->InheritsFrom("TTree") ) {    }
640            //    //  
641            //    Target->Close();
642            if ( fastMethod ){  
643              globChain->Merge(target->GetFile(),0,"keep fast");  //   //
644            } else {  //   TDirectory *target = Target;
645              globChain->Merge(target->GetFile(),0,"keep");  //   //
646            };  //   if ( DebugMode() ) printf("\nTarget path is: %s \n",target->GetPath());
647            //  //   //
648            merged = true;  //   TString path( (char*)strstr( target->GetPath(), ":" ) );
649            //  //   path.Remove(0,2);
650            if ( DebugMode() ) globChain->ls();  //   //
651            //  //   TDirectory *first_source = (TDirectory*)sourcelist->First();
652            delete globChain;  //   THashList allNames;
653            //  //   //
654          } else {  //   while ( first_source ){
655            //  //     //
656            //      obj->Write( key->GetName() ); // <==================================================================================  //     TDirectory *current_sourcedir = first_source->GetDirectory(path);
657            //  //     //
658          };  //     if ( !current_sourcedir ){
659        };  //       first_source = (TDirectory*)sourcelist->After(first_source);
660        oldkey = key;  //       continue;
661      };  //     }
662      //  //     //
663      first_source = (TDirectory*)sourcelist->After(first_source);  //     // loop over all keys in this directory
664      //  //     //
665    };  //     TChain *globChain = 0;    
666    // save modifications to target file  //     TIter nextkey( current_sourcedir->GetListOfKeys() );
667    target->SaveSelf(kTRUE);  //     TKey *key = 0;
668    //  //     TKey *oldkey = 0;
669  }  //     TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory
670    //     //
671    //     while ( (key = (TKey*)nextkey()) ) {
672    //       //
673    //       //      printf(" target ls \n");
674    //       //      Target->ls();
675    //       //
676    //       if ( current_sourcedir == target ) break;
677    //       //
678    //       if ( oldkey && !strcmp(oldkey->GetName(),key->GetName()) ) continue; //keep only the highest cycle number for each key
679    //       //
680    //       if ( allNames.FindObject(key->GetName()) ) continue;
681    //       //
682    //       if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());
683    //       //
684    //       //
685    //       //
686    //       if ( !strcmp(key->GetName(),"Tracker") && !li->IsTRK2() && !li->IsTRK1() && !li->IsTRKh() ) continue;
687    //       //
688    //       if ( !strcmp(key->GetName(),"Calorimeter") && !li->IsCAL2() && !li->IsCAL1() ) continue;
689    //       //
690    //       if ( !strcmp(key->GetName(),"ToF") && !li->IsTOF() ) continue;
691    //       //
692    //       if ( !strcmp(key->GetName(),"Trigger") && !li->IsTRG() ) continue;
693    //       //
694    //       if ( !strcmp(key->GetName(),"Anticounter") && !li->IsAC() ) continue;
695    //       //
696    //       if ( !strcmp(key->GetName(),"S4") && !li->IsS4() ) continue;
697    //       //
698    //       if ( !strcmp(key->GetName(),"NeutronD") && !li->IsND() ) continue;
699    //       //
700    //       if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->IsORB() ) continue;
701    //       //
702    //       if ( !strcmp(key->GetName(),"Run") && !li->IsRUN() ) continue;
703    //       //
704    //       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") ){
705    //      if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());
706    //      continue;
707    //       };
708    //       //
709    //       printf(" 1 \n");
710    //       allNames.Add(new TObjString(key->GetName()));
711    //       printf(" 2 \n");
712    //       //
713    //       // read object from first source file
714    //       //
715    //       current_sourcedir->cd();
716    //       TObject *obj = key->ReadObj();
717    //       printf(" 3 \n");
718    //       //
719    //       if ( obj->IsA()->InheritsFrom("TTree") ){
720    //      //
721    //              // loop over all source files create a chain of Trees "globChain"
722    //      //
723    //      TString obj_name;
724    //      printf(" 4 \n");
725    //      //
726    //      if ( path.Length() ) {
727    //        obj_name = path + "/" + obj->GetName();
728    //      } else {
729    //        obj_name = obj->GetName();
730    //      };
731    //      //
732    //      globChain = new TChain(obj_name);
733    //      //
734    //      globChain->SetCacheSize(0);
735    //      //
736    //      printf(" 5 \n");
737    //      globChain->Add(first_source->GetName());
738    //      printf(" 6 \n");
739    //      //
740    //      TFile *nextsource = (TFile*)sourcelist->After( first_source );
741    //      //
742    //      while ( nextsource ) {            
743    //        //
744    //        //do not add to the list a file that does not contain this Tree
745    //        //
746    //        printf(" 7 \n");
747    //        TFile *curf = TFile::Open(nextsource->GetName());
748    //        //
749    //        if ( curf ) {
750    //          //
751    //          Bool_t mustAdd = kFALSE;
752    //          //
753    //          if (curf->FindKey(obj_name)) {
754    //            //
755    //            mustAdd = kTRUE;
756    //            //
757    //          } else {
758    //            //
759    //            //we could be more clever here. No need to import the object
760    //            //we are missing a function in TDirectory
761    //            //
762    //            TObject *aobj = curf->Get(obj_name);
763    //            //
764    //            if ( aobj ){
765    //              mustAdd = kTRUE;
766    //              delete aobj;
767    //            };
768    //          };
769    //          if (mustAdd) {
770    //            printf(" 8 \n");
771    //            globChain->Add(nextsource->GetName());
772    //            printf(" 9 \n");
773    //          };
774    //        };
775    //        delete curf;
776    //        nextsource = (TFile*)sourcelist->After(nextsource);
777    //      };
778    //      //
779    //      printf(" 10 \n");      
780    //      delete nextsource;
781    //      printf(" 11 \n");
782    //      //
783    //       } else {
784    //      //
785    //      // object is of no type that we know or can handle
786    //      //
787    //      if ( DebugMode() ) cout << "Unknown object type, name: "
788    //           << obj->GetName() << " title: " << obj->GetTitle() << endl;
789    //       };
790    //       //
791    //       // now write the merged histogram (which is "in" obj) to the target file
792    //       // note that this will just store obj in the current directory level,
793    //       // which is not persistent until the complete directory itself is stored
794    //       // by "target->Write()" below
795    //       //
796    //       printf(" 12 \n");
797    //       if ( obj ){
798    //      //
799    //      target->cd();
800    //      //
801    //      if( obj->IsA()->InheritsFrom("TTree") ) {
802    //        //
803    //        Long64_t nfiles = 0;
804    //        if ( fastMethod ){
805    //          //      globChain->Merge(target->GetFile(),0,"C keep fast");
806    //          nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep fast");
807    //        } else {
808    //          //globChain->Merge(target->GetFile(),0,"C keep");
809    //          printf(" 13 %llu \n",globChain->GetEntries());          
810    //          nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep");
811    //          printf(" 14 \n");
812    //        };
813    //        //
814    //        merged = true;
815    //        //
816    //        if ( DebugMode() ){
817    //          printf(" Merged %i files\n",(int)nfiles);
818    //          globChain->ls();
819    //        };
820    //        //
821    //        delete globChain;
822    //        //      return;
823    //        //
824    //      } else {
825    //        //
826    //        //      obj->Write( key->GetName() ); // <==================================================================================
827    //        //
828    //      };
829    //       };
830    //       delete obj;
831    //       oldkey = key;
832    //     };
833    //     //
834    //     first_source = (TDirectory*)sourcelist->After(first_source);
835    //     //
836    //   };
837    //   // save modifications to target file
838    //   //
839    //   target->SaveSelf(kTRUE);
840    //   //
841    };
842    
843    
844    // Long64_t RunGlue::Mergy(TChain *mychain, TFile* file, Int_t basketsize, Option_t* option){
845    //    // We must have been passed a file, we will use it
846    //    // later to reset the compression level of the branches.
847    //    if (!file) {
848    //       // FIXME: We need an error message here.
849    //      printf(" 19 \n");
850    //      return 0;
851    //    }
852    //    printf(" 20 \n");
853    
854    //    // Options
855    //    Bool_t fastClone = kFALSE;
856    //    TString opt = option;
857    //    opt.ToLower();
858    //    if (opt.Contains("fast")) {
859    //       fastClone = kTRUE;
860    //       printf(" 21 \n");
861    //    }
862    //    // The chain tree must have a list of branches
863    //    // because we may try to change their basket
864    //    // size later.
865    //    TObjArray* lbranches = mychain->GetListOfBranches();
866    //    if (!lbranches) {
867    //       // FIXME: We need an error message here.
868    //      printf(" 22 \n");
869    //       return 0;
870    //    }
871    
872    //    //   file->cd();
873    //    // The chain must have a current tree because
874    //    // that is the one we will clone.
875    //    //   if (!fTree) {
876    //       // -- LoadTree() has not yet been called, no current tree.
877    //       // FIXME: We need an error message here.
878    //    //      return 0;
879    //    //   }
880    
881    //    // Copy the chain's current tree without
882    //    // copying any entries, we will do that later.
883    //    printf(" 23 \n");
884    //    TTree* newTree = mychain->CloneTree(0);
885    //    if (!newTree) {
886    //       // FIXME: We need an error message here.
887    //      printf(" 24 \n");
888    //       return 0;
889    //    }
890    //    printf(" 25 \n");
891    
892    
893    //    // Strip out the (potential) directory name.
894    //    // FIXME: The merged chain may or may not have the
895    //    //        same name as the original chain.  This is
896    //    //        bad because the chain name determines the
897    //    //        names of the trees in the chain by default.
898    //    printf(" 26 \n");
899    //    newTree->SetName(gSystem->BaseName(mychain->GetName()));
900    //    printf(" 27 \n");
901    
902    //    // FIXME: Why do we do this?
903    //    //   newTree->SetAutoSave(-1);
904    //    newTree->SetAutoSave(900000000000000LL);
905    
906    //    printf(" 28 \n");
907    //    // Circularity is incompatible with merging, it may
908    //    // force us to throw away entries, which is not what
909    //    // we are supposed to do.
910    //    newTree->SetCircular(0);
911    
912    //    // Reset the compression level of the branches.
913    //    if (opt.Contains("c")) {
914    //       TBranch* branch = 0;
915    //       TIter nextb(newTree->GetListOfBranches());
916    //       while ((branch = (TBranch*) nextb())) {
917    //      printf(" 29 \n");
918    //          branch->SetCompressionLevel(file->GetCompressionLevel());
919    //       }
920    //    }
921    
922    //    printf(" 30 \n");
923    //    // Reset the basket size of the branches.
924    //    if (basketsize > 1000) {
925    //       TBranch* branch = 0;
926    //       TIter nextb(newTree->GetListOfBranches());
927    //       while ((branch = (TBranch*) nextb())) {
928    //      printf(" 31 \n");
929    //          branch->SetBasketSize(basketsize);
930    //       }
931    //    }
932    
933    //    printf(" 32 \n");
934    //    Long64_t nentries = mychain->GetEntriesFast();
935    //    //Long64_t nentries = mychain->GetEntries();
936    
937    //    // Copy the entries.
938    //    if (fastClone) {
939    //       // For each tree in the chain.
940    //       for (Long64_t i = 0; i < nentries; i += mychain->GetTree()->GetEntries()) {
941    //          if (mychain->LoadTree(i) < 0) {
942    //             break;
943    //          }
944    //          TTreeCloner cloner(mychain->GetTree(), newTree, option);
945    //          if (cloner.IsValid()) {
946    //             newTree->SetEntries(newTree->GetEntries() + mychain->GetTree()->GetEntries());
947    //             cloner.Exec();
948    //          } else {
949    //             if (mychain->GetFile()) {
950    //            printf("Merge Skipped file %s\n", mychain->GetFile()->GetName());
951    //             //            } else {
952    //             //               Warning("Merge", "Skipped file number %d\n", fTreeNumber);
953    //             }
954    //          }
955    //       }
956    //    } else {
957    //      printf(" 33 %llu \n",nentries);
958    //      mychain->Print();
959    //      for (Long64_t i = 0; i < nentries; i++) {
960    //        printf(" i %llu \n",i);
961    //        // for (Long64_t i = 0; i < 1; i++) {
962    //          if (mychain->GetEntry(i) <= 0) {
963    //             break;
964    //          }
965    //          newTree->Fill();
966    //       }
967    //       printf(" 34 \n");
968    //    }
969    
970    //    // Write the new tree header.
971    //    printf(" 35 \n");
972    //    newTree->Write();
973    
974    //    printf(" 36 \n");
975    //    // Get our return value.
976    //    Int_t nfiles = newTree->GetFileNumber() + 1;
977    
978    //    // Close and delete the current file of the new tree.
979    //    if (!opt.Contains("keep")) {
980    //       // FIXME: What happens to fDirectory in newTree here?
981    //       delete newTree->GetCurrentFile();
982    //    }
983    //    return nfiles;
984    // }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.23

  ViewVC Help
Powered by ViewVC 1.1.23