/[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.11 by mocchiut, Fri Oct 19 09:02:48 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" );    ifstream myfile;
386    //    myfile.open((this->GetFilename()).Data());
387    if ( Target ){    if ( myfile ){
388      Target->Close();      //  Target = TFile::Open((this->GetFilename()).Data(), "READ" );
389        //
390        //  if ( Target ){
391        //    Target->Close();
392        myfile.close();
393      printf("Error opening target file, %s  already exist!\n",(this->GetFilename()).Data());      printf("Error opening target file, %s  already exist!\n",(this->GetFilename()).Data());
394      return(false);      return(false);
395    } else {    } else {
396      //      //
397      printf(" Output file does not exist, creating it\n");      printf(" Output file does not exist, creating it\n");
398      //      //
399      Long64_t maxsize = 10000000000LL;        Long64_t maxsize = 99900000000LL;  
400      //      //
401      Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );      Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );
402      //fastMethod = kTRUE;      //fastMethod = kTRUE;
# Line 395  Bool_t RunGlue::OpenFile(){ Line 409  Bool_t RunGlue::OpenFile(){
409      }      }
410      //      //
411      TTree::SetMaxTreeSize(maxsize);      TTree::SetMaxTreeSize(maxsize);
412        Target->SetCompressionLevel(2);
413      //      //
414        fOpen = true;
415      return(true);      return(true);
416      //      //
417    };    };
# Line 455  void RunGlue::UpdateDB(TList *dlist){ Line 471  void RunGlue::UpdateDB(TList *dlist){
471    };    };
472    //    //
473    myquery.str("");    myquery.str("");
474    myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";    //  myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
475      myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (" << idr << ",'$PAM_L2','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
476    if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());    if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
477    //    //
478    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
# Line 534  void RunGlue::MergeRootfile(TList *sourc Line 551  void RunGlue::MergeRootfile(TList *sourc
551      //      //
552      // loop over all keys in this directory      // loop over all keys in this directory
553      //      //
554      TChain *globChain = 0;      TChain *globChain = 0;    
555      TIter nextkey( current_sourcedir->GetListOfKeys() );      TIter nextkey( current_sourcedir->GetListOfKeys() );
556      TKey *key, *oldkey=0;      TKey *key = 0;
557        TKey *oldkey = 0;
558      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
559      //      //
560      while ( (key = (TKey*)nextkey()) ) {      while ( (key = (TKey*)nextkey()) ) {
561        //        //
562          //      printf(" target ls \n");
563          //      Target->ls();
564          //
565        if ( current_sourcedir == target ) break;        if ( current_sourcedir == target ) break;
566        //        //
567        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
568        //        //
569        if (allNames.FindObject(key->GetName())) continue;        if ( allNames.FindObject(key->GetName()) ) continue;
570        //        //
571        if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());        if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());
572        //        //
573        //        //
574        //        //
575        if ( !strcmp(key->GetName(),"Tracker") && !li->TRK2 && !li->TRK1 && !li->TRKh ) continue;        if ( !strcmp(key->GetName(),"Tracker") && !li->IsTRK2() && !li->IsTRK1() && !li->IsTRKh() ) continue;
576        //        //
577        if ( !strcmp(key->GetName(),"Calorimeter") && !li->CAL2 && !li->CAL1 ) continue;        if ( !strcmp(key->GetName(),"Calorimeter") && !li->IsCAL2() && !li->IsCAL1() ) continue;
578        //        //
579        if ( !strcmp(key->GetName(),"ToF") && !li->TOF ) continue;        if ( !strcmp(key->GetName(),"ToF") && !li->IsTOF() ) continue;
580        //        //
581        if ( !strcmp(key->GetName(),"Trigger") && !li->TRG ) continue;        if ( !strcmp(key->GetName(),"Trigger") && !li->IsTRG() ) continue;
582        //        //
583        if ( !strcmp(key->GetName(),"Anticounter") && !li->AC ) continue;        if ( !strcmp(key->GetName(),"Anticounter") && !li->IsAC() ) continue;
584        //        //
585        if ( !strcmp(key->GetName(),"S4") && !li->S4 ) continue;        if ( !strcmp(key->GetName(),"S4") && !li->IsS4() ) continue;
586        //        //
587        if ( !strcmp(key->GetName(),"NeutronD") && !li->ND ) continue;        if ( !strcmp(key->GetName(),"NeutronD") && !li->IsND() ) continue;
588        //        //
589        if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->ORB ) continue;        if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->IsORB() ) continue;
590        //        //
591        if ( !strcmp(key->GetName(),"Run") && !RUN ) continue;        if ( !strcmp(key->GetName(),"Run") && !li->IsRUN() ) continue;
592        //        //
593        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 ( 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") ){
594          if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());          if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());
# Line 595  void RunGlue::MergeRootfile(TList *sourc Line 616  void RunGlue::MergeRootfile(TList *sourc
616          //          //
617          globChain = new TChain(obj_name);          globChain = new TChain(obj_name);
618          //          //
619            globChain->SetCacheSize(0);
620            //
621          globChain->Add(first_source->GetName());          globChain->Add(first_source->GetName());
622          //          //
623          TFile *nextsource = (TFile*)sourcelist->After( first_source );          TFile *nextsource = (TFile*)sourcelist->After( first_source );
# Line 633  void RunGlue::MergeRootfile(TList *sourc Line 656  void RunGlue::MergeRootfile(TList *sourc
656            nextsource = (TFile*)sourcelist->After(nextsource);            nextsource = (TFile*)sourcelist->After(nextsource);
657          };          };
658          //          //
659            delete nextsource;
660            //
661        } else {        } else {
662          //          //
663          // 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 677  void RunGlue::MergeRootfile(TList *sourc
677          //          //
678          if( obj->IsA()->InheritsFrom("TTree") ) {          if( obj->IsA()->InheritsFrom("TTree") ) {
679            //            //
680            //            Long64_t nfiles = 0;
681            if ( fastMethod ){            if ( fastMethod ){
682              globChain->Merge(target->GetFile(),0,"keep fast");              //      globChain->Merge(target->GetFile(),0,"C keep fast");
683                nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep fast");
684            } else {            } else {
685              globChain->Merge(target->GetFile(),0,"keep");              //globChain->Merge(target->GetFile(),0,"C keep");
686                nfiles=this->Mergy((TChain*)globChain,target->GetFile(),0,"C keep");
687            };            };
688            //            //
689            merged = true;            merged = true;
690            //            //
691            if ( DebugMode() ) globChain->ls();            if ( DebugMode() ){
692                printf(" Merged %i files\n",(int)nfiles);
693                globChain->ls();
694              };
695            //            //
696            delete globChain;            delete globChain;
697              //      return;
698            //            //
699          } else {          } else {
700            //            //
# Line 671  void RunGlue::MergeRootfile(TList *sourc Line 702  void RunGlue::MergeRootfile(TList *sourc
702            //            //
703          };          };
704        };        };
705          delete obj;
706        oldkey = key;        oldkey = key;
707      };      };
708      //      //
# Line 678  void RunGlue::MergeRootfile(TList *sourc Line 710  void RunGlue::MergeRootfile(TList *sourc
710      //      //
711    };    };
712    // save modifications to target file    // save modifications to target file
713      //
714    target->SaveSelf(kTRUE);    target->SaveSelf(kTRUE);
715    //    //
716    };
717    
718    
719    Long64_t RunGlue::Mergy(TChain *mychain, TFile* file, Int_t basketsize, Option_t* option){
720       // We must have been passed a file, we will use it
721       // later to reset the compression level of the branches.
722       if (!file) {
723          // FIXME: We need an error message here.
724         return 0;
725       }
726    
727       // Options
728       Bool_t fastClone = kFALSE;
729       TString opt = option;
730       opt.ToLower();
731       if (opt.Contains("fast")) {
732          fastClone = kTRUE;
733       }
734    
735       // The chain tree must have a list of branches
736       // because we may try to change their basket
737       // size later.
738       TObjArray* lbranches = mychain->GetListOfBranches();
739       if (!lbranches) {
740          // FIXME: We need an error message here.
741          return 0;
742       }
743    
744       // The chain must have a current tree because
745       // that is the one we will clone.
746       //   if (!fTree) {
747          // -- LoadTree() has not yet been called, no current tree.
748          // FIXME: We need an error message here.
749       //      return 0;
750       //   }
751    
752       // Copy the chain's current tree without
753       // copying any entries, we will do that later.
754       TTree* newTree = mychain->CloneTree(0);
755       if (!newTree) {
756          // FIXME: We need an error message here.
757          return 0;
758       }
759    
760       // Strip out the (potential) directory name.
761       // FIXME: The merged chain may or may not have the
762       //        same name as the original chain.  This is
763       //        bad because the chain name determines the
764       //        names of the trees in the chain by default.
765       newTree->SetName(gSystem->BaseName(mychain->GetName()));
766    
767       // FIXME: Why do we do this?
768       //   newTree->SetAutoSave(-1);
769       newTree->SetAutoSave(900000000000000LL);
770    
771       // Circularity is incompatible with merging, it may
772       // force us to throw away entries, which is not what
773       // we are supposed to do.
774       newTree->SetCircular(0);
775    
776       // Reset the compression level of the branches.
777       if (opt.Contains("c")) {
778          TBranch* branch = 0;
779          TIter nextb(newTree->GetListOfBranches());
780          while ((branch = (TBranch*) nextb())) {
781             branch->SetCompressionLevel(file->GetCompressionLevel());
782          }
783       }
784    
785       // Reset the basket size of the branches.
786       if (basketsize > 1000) {
787          TBranch* branch = 0;
788          TIter nextb(newTree->GetListOfBranches());
789          while ((branch = (TBranch*) nextb())) {
790             branch->SetBasketSize(basketsize);
791          }
792       }
793    
794       Long64_t nentries = mychain->GetEntriesFast();
795    
796       // Copy the entries.
797       if (fastClone) {
798          // For each tree in the chain.
799          for (Long64_t i = 0; i < nentries; i += mychain->GetTree()->GetEntries()) {
800             if (mychain->LoadTree(i) < 0) {
801                break;
802             }
803             TTreeCloner cloner(mychain->GetTree(), newTree, option);
804             if (cloner.IsValid()) {
805                newTree->SetEntries(newTree->GetEntries() + mychain->GetTree()->GetEntries());
806                cloner.Exec();
807             } else {
808                if (mychain->GetFile()) {
809                  printf("Merge Skipped file %s\n", mychain->GetFile()->GetName());
810                   //            } else {
811                   //               Warning("Merge", "Skipped file number %d\n", fTreeNumber);
812                }
813             }
814          }
815       } else {
816          for (Long64_t i = 0; i < nentries; i++) {
817             if (mychain->GetEntry(i) <= 0) {
818                break;
819             }
820             newTree->Fill();
821          }
822       }
823    
824       // Write the new tree header.
825       newTree->Write();
826    
827       // Get our return value.
828       Int_t nfiles = newTree->GetFileNumber() + 1;
829    
830       // Close and delete the current file of the new tree.
831       if (!opt.Contains("keep")) {
832          // FIXME: What happens to fDirectory in newTree here?
833          delete newTree->GetCurrentFile();
834       }
835       return nfiles;
836  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.23