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

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.23