/[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.6 by mocchiut, Wed Jan 24 16:24:51 2007 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 115  TList *RunGlue::GetRunList(){ Line 116  TList *RunGlue::GetRunList(){
116      UInt_t lowerlimit = llim->GetSec();      UInt_t lowerlimit = llim->GetSec();
117      UInt_t upperlimit = lowerlimit + 86401;      UInt_t upperlimit = lowerlimit + 86401;
118      //      //
     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      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);
120      //      //
121      YY -= 2000;      YY -= 2000;
# Line 172  TList *RunGlue::GetRunList(){ Line 149  TList *RunGlue::GetRunList(){
149      //      //
150      printf(" Output filename is %s \n",fFilename.Data());      printf(" Output filename is %s \n",fFilename.Data());
151      //      //
152        if ( !this->OpenFile() ){
153             fEnd = true;
154             return(0);
155        };
156        //
157        stringstream myquery;
158        myquery.str("");
159        myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
160                << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
161        //
162        pResult = dbc->Query(myquery.str().c_str());
163        for( UInt_t r=0; r < 1000; r++){
164          Row = pResult->Next();      
165          if( Row == NULL ) break;
166          if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
167          //
168          thisrun=dir+(TString)Row->GetField(0)+".Level2.root";      
169          //
170          if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
171          //
172          su = TFile::Open(thisrun);
173          if ( li->CheckLevel2File(thisrun) ){
174            lList->Add(su);
175          } else {
176            if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
177          };
178          //
179        };
180        //
181        //
182      delete glrun;      delete glrun;
183      delete dbtime;      delete dbtime;
184      //      //
# Line 276  TList *RunGlue::GetRunList(){ Line 283  TList *RunGlue::GetRunList(){
283      UInt_t lowerlimit = llim->GetSec();      UInt_t lowerlimit = llim->GetSec();
284      UInt_t upperlimit = lowerlimit + 86401;      UInt_t upperlimit = lowerlimit + 86401;
285      //      //
     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      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);
287      //      //
288      YY -= 2000;      YY -= 2000;
# Line 339  TList *RunGlue::GetRunList(){ Line 316  TList *RunGlue::GetRunList(){
316      //      //
317      printf(" Output filename is %s \n",fFilename.Data());      printf(" Output filename is %s \n",fFilename.Data());
318      //      //
319        if ( !this->OpenFile() ) return(0);
320        //
321        stringstream myquery;
322        myquery.str("");
323        myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
324                << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
325        //
326        pResult = dbc->Query(myquery.str().c_str());
327        for( UInt_t r=0; r < 1000; r++){
328          Row = pResult->Next();      
329          if( Row == NULL ) break;
330          if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
331          //
332          thisrun=dir+(TString)Row->GetField(0)+".Level2.root";      
333          //
334          if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
335          //
336          su = TFile::Open(thisrun);
337          if ( su ){
338            TFile *su0 = TFile::Open(thisrun);
339            fDoneList->Add(su0);
340            fNlistdone++;
341          };
342          if ( li->CheckLevel2File(thisrun) ){
343            lList->Add(su);
344            if ( DebugMode() ) printf(" RUN %s ADDED \n",Row->GetField(0));
345          } else {
346            if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
347          };
348          //
349        };
350        //
351      if ( DebugMode() ){      if ( DebugMode() ){
352        UInt_t ll = 0;        UInt_t ll = 0;
353        while ( (TFile*)lList->At(ll) ){        while ( (TFile*)lList->At(ll) ){
# Line 371  TList *RunGlue::GetRunList(){ Line 380  TList *RunGlue::GetRunList(){
380    
381  Bool_t RunGlue::OpenFile(){  Bool_t RunGlue::OpenFile(){
382    //    //
383      fOpen = false;
384    printf(" Check if output file already exists \n");    printf(" Check if output file already exists \n");
385    Target = TFile::Open((this->GetFilename()).Data(), "READ" );    Target = TFile::Open((this->GetFilename()).Data(), "READ" );
386    //    //
# Line 382  Bool_t RunGlue::OpenFile(){ Line 392  Bool_t RunGlue::OpenFile(){
392      //      //
393      printf(" Output file does not exist, creating it\n");      printf(" Output file does not exist, creating it\n");
394      //      //
395      Long64_t maxsize = 10000000000LL;        Long64_t maxsize = 99900000000LL;  
396      //      //
397      Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );      Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );
398      //fastMethod = kTRUE;      //fastMethod = kTRUE;
# Line 395  Bool_t RunGlue::OpenFile(){ Line 405  Bool_t RunGlue::OpenFile(){
405      }      }
406      //      //
407      TTree::SetMaxTreeSize(maxsize);      TTree::SetMaxTreeSize(maxsize);
408        Target->SetCompressionLevel(2);
409      //      //
410        fOpen = true;
411      return(true);      return(true);
412      //      //
413    };    };
# Line 534  void RunGlue::MergeRootfile(TList *sourc Line 546  void RunGlue::MergeRootfile(TList *sourc
546      //      //
547      // loop over all keys in this directory      // loop over all keys in this directory
548      //      //
549      TChain *globChain = 0;      TChain *globChain = 0;    
550      TIter nextkey( current_sourcedir->GetListOfKeys() );      TIter nextkey( current_sourcedir->GetListOfKeys() );
551      TKey *key, *oldkey=0;      TKey *key = 0;
552        TKey *oldkey = 0;
553      TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory      TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory
554      //      //
555      while ( (key = (TKey*)nextkey()) ) {      while ( (key = (TKey*)nextkey()) ) {
556        //        //
557          //      printf(" target ls \n");
558          //      Target->ls();
559          //
560        if ( current_sourcedir == target ) break;        if ( current_sourcedir == target ) break;
561        //        //
562        if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; //keep only the highest cycle number for each key        if ( oldkey && !strcmp(oldkey->GetName(),key->GetName()) ) continue; //keep only the highest cycle number for each key
563        //        //
564        if (allNames.FindObject(key->GetName())) continue;        if ( allNames.FindObject(key->GetName()) ) continue;
565        //        //
566        if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());        if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());
567        //        //
# Line 595  void RunGlue::MergeRootfile(TList *sourc Line 611  void RunGlue::MergeRootfile(TList *sourc
611          //          //
612          globChain = new TChain(obj_name);          globChain = new TChain(obj_name);
613          //          //
614            globChain->SetCacheSize(0);
615            //
616          globChain->Add(first_source->GetName());          globChain->Add(first_source->GetName());
617          //          //
618          TFile *nextsource = (TFile*)sourcelist->After( first_source );          TFile *nextsource = (TFile*)sourcelist->After( first_source );
# Line 633  void RunGlue::MergeRootfile(TList *sourc Line 651  void RunGlue::MergeRootfile(TList *sourc
651            nextsource = (TFile*)sourcelist->After(nextsource);            nextsource = (TFile*)sourcelist->After(nextsource);
652          };          };
653          //          //
654            delete nextsource;
655            //
656        } else {        } else {
657          //          //
658          // object is of no type that we know or can handle          // object is of no type that we know or can handle
# Line 652  void RunGlue::MergeRootfile(TList *sourc Line 672  void RunGlue::MergeRootfile(TList *sourc
672          //          //
673          if( obj->IsA()->InheritsFrom("TTree") ) {          if( obj->IsA()->InheritsFrom("TTree") ) {
674            //            //
675            //            Long64_t nfiles = 0;
676            if ( fastMethod ){            if ( fastMethod ){
677              globChain->Merge(target->GetFile(),0,"keep fast");              //      globChain->Merge(target->GetFile(),0,"C keep fast");
678                nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep fast");
679            } else {            } else {
680              globChain->Merge(target->GetFile(),0,"keep");              //globChain->Merge(target->GetFile(),0,"C keep");
681                nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep");
682            };            };
683            //            //
684            merged = true;            merged = true;
685            //            //
686            if ( DebugMode() ) globChain->ls();            if ( DebugMode() ){
687                printf(" Merged %i files\n",(int)nfiles);
688                globChain->ls();
689              };
690            //            //
691            delete globChain;            delete globChain;
692              //      return;
693            //            //
694          } else {          } else {
695            //            //
# Line 671  void RunGlue::MergeRootfile(TList *sourc Line 697  void RunGlue::MergeRootfile(TList *sourc
697            //            //
698          };          };
699        };        };
700          delete obj;
701        oldkey = key;        oldkey = key;
702      };      };
703      //      //
# Line 678  void RunGlue::MergeRootfile(TList *sourc Line 705  void RunGlue::MergeRootfile(TList *sourc
705      //      //
706    };    };
707    // save modifications to target file    // save modifications to target file
708      //
709    target->SaveSelf(kTRUE);    target->SaveSelf(kTRUE);
710    //    //
711    };
712    
713    
714    Long64_t RunGlue::Mergy(TChain *mychain, TFile* file, Int_t basketsize, Option_t* option){
715       // We must have been passed a file, we will use it
716       // later to reset the compression level of the branches.
717       if (!file) {
718          // FIXME: We need an error message here.
719         return 0;
720       }
721    
722       // Options
723       Bool_t fastClone = kFALSE;
724       TString opt = option;
725       opt.ToLower();
726       if (opt.Contains("fast")) {
727          fastClone = kTRUE;
728       }
729    
730       // The chain tree must have a list of branches
731       // because we may try to change their basket
732       // size later.
733       TObjArray* lbranches = mychain->GetListOfBranches();
734       if (!lbranches) {
735          // FIXME: We need an error message here.
736          return 0;
737       }
738    
739       // The chain must have a current tree because
740       // that is the one we will clone.
741       //   if (!fTree) {
742          // -- LoadTree() has not yet been called, no current tree.
743          // FIXME: We need an error message here.
744       //      return 0;
745       //   }
746    
747       // Copy the chain's current tree without
748       // copying any entries, we will do that later.
749       TTree* newTree = mychain->CloneTree(0);
750       if (!newTree) {
751          // FIXME: We need an error message here.
752          return 0;
753       }
754    
755       // Strip out the (potential) directory name.
756       // FIXME: The merged chain may or may not have the
757       //        same name as the original chain.  This is
758       //        bad because the chain name determines the
759       //        names of the trees in the chain by default.
760       newTree->SetName(gSystem->BaseName(mychain->GetName()));
761    
762       // FIXME: Why do we do this?
763       //   newTree->SetAutoSave(-1);
764       newTree->SetAutoSave(900000000000000LL);
765    
766       // Circularity is incompatible with merging, it may
767       // force us to throw away entries, which is not what
768       // we are supposed to do.
769       newTree->SetCircular(0);
770    
771       // Reset the compression level of the branches.
772       if (opt.Contains("c")) {
773          TBranch* branch = 0;
774          TIter nextb(newTree->GetListOfBranches());
775          while ((branch = (TBranch*) nextb())) {
776             branch->SetCompressionLevel(file->GetCompressionLevel());
777          }
778       }
779    
780       // Reset the basket size of the branches.
781       if (basketsize > 1000) {
782          TBranch* branch = 0;
783          TIter nextb(newTree->GetListOfBranches());
784          while ((branch = (TBranch*) nextb())) {
785             branch->SetBasketSize(basketsize);
786          }
787       }
788    
789       Long64_t nentries = mychain->GetEntriesFast();
790    
791       // Copy the entries.
792       if (fastClone) {
793          // For each tree in the chain.
794          for (Long64_t i = 0; i < nentries; i += mychain->GetTree()->GetEntries()) {
795             if (mychain->LoadTree(i) < 0) {
796                break;
797             }
798             TTreeCloner cloner(mychain->GetTree(), newTree, option);
799             if (cloner.IsValid()) {
800                newTree->SetEntries(newTree->GetEntries() + mychain->GetTree()->GetEntries());
801                cloner.Exec();
802             } else {
803                if (mychain->GetFile()) {
804                  printf("Merge Skipped file %s\n", mychain->GetFile()->GetName());
805                   //            } else {
806                   //               Warning("Merge", "Skipped file number %d\n", fTreeNumber);
807                }
808             }
809          }
810       } else {
811          for (Long64_t i = 0; i < nentries; i++) {
812             if (mychain->GetEntry(i) <= 0) {
813                break;
814             }
815             newTree->Fill();
816          }
817       }
818    
819       // Write the new tree header.
820       newTree->Write();
821    
822       // Get our return value.
823       Int_t nfiles = newTree->GetFileNumber() + 1;
824    
825       // Close and delete the current file of the new tree.
826       if (!opt.Contains("keep")) {
827          // FIXME: What happens to fDirectory in newTree here?
828          delete newTree->GetCurrentFile();
829       }
830       return nfiles;
831  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.23