Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:43

0001 
0002 #include<vector>
0003 #include "TROOT.h"
0004 #include "TFile.h"
0005 #include "TFileMerger.h"
0006 #include "TDirectory.h"
0007 #include "TChain.h"
0008 #include "TObject.h"
0009 #include "TCanvas.h"
0010 #include "TMath.h"
0011 #include "TLegend.h"
0012 #include "TGraph.h"
0013 #include "TH1.h"
0014 #include "TH2.h"
0015 #include "TH3.h"
0016 #include "TTree.h"
0017 #include "TF1.h"
0018 #include "TPaveText.h"
0019 
0020 
0021 
0022 TObject* GetObjectFromPath(TDirectory* File, const char* Path);
0023 void merge_core(TDirectory* Output, TDirectory* Input);
0024 
0025 void merge(char* InputFilePath,char* OutputFilePath)
0026 {
0027    gROOT->Reset();
0028    char outname[2048];
0029    char name[2048];
0030 
0031    unsigned int PackN    = 1;
0032    unsigned int PackSize = 120;
0033 
0034    for(unsigned int P=0;P<PackN;P++){
0035         TFileMerger merger(kFALSE);
0036         if(PackN>1){
0037         sprintf(outname,"tmp_%s_%i.root",OutputFilePath,P);
0038         }else{
0039         sprintf(outname,"%s",OutputFilePath);
0040         }
0041         merger.OutputFile(outname);
0042         merger.PrintFiles("true");        
0043         merger.SetFastMethod();
0044         for(unsigned int i=0;i<PackSize;i++){
0045            sprintf(name,InputFilePath,P*PackSize+i);
0046            merger.AddFile(name);
0047         }
0048         merger.Merge();
0049    }
0050 
0051    if(PackN>1){
0052       TFileMerger merger(kFALSE);
0053       merger.OutputFile(OutputFilePath);
0054       merger.PrintFiles("true");        
0055       merger.SetFastMethod();
0056       for(unsigned int P=0;P<PackN;P++){
0057            sprintf(outname,"tmp_%s_%i.root",OutputFilePath,P);
0058            merger.AddFile(outname);
0059       }
0060       merger.Merge();
0061 
0062       for(unsigned int P=0;P<PackN;P++){
0063            sprintf(outname,"tmp_%s_%i.root",OutputFilePath,P);
0064            remove(outname);
0065       }
0066    }
0067 }
0068         
0069 
0070 /*
0071     TFile* Output = new TFile("out.root","RECREATE");
0072     TFile* Input;
0073     for(unsigned int i=0;i<100;i++){
0074         char name[1024];
0075         sprintf(name,"file:SingleMu_Discrim_%04i.root",i);
0076         printf("Oppening %s\n",name);
0077         Input =  new TFile(name);
0078         if(Input==NULL || Input->IsZombie() ){printf("### Bug With File %s\n### File will be skipped \n",name); continue;}
0079         merge_core(Output, Input );
0080         Input->Close();
0081         delete Input;       
0082     }
0083 
0084     Output->Write();
0085 
0086 }   Output->Close();
0087 */
0088 
0089 
0090 
0091 
0092 void merge_core(TDirectory* Output, TDirectory* Input)
0093 { 
0094    TH1::AddDirectory(kTRUE);
0095 
0096    if(Input==NULL) return;
0097    TList* input_list = Input->GetListOfKeys();
0098    if(input_list==NULL){cout <<"LIST IS NULL\n";return;}
0099 
0100    TObject* input_it = input_list->First();  
0101 
0102    while(input_it!=NULL){
0103     if(input_it->IsFolder()){
0104 //     printf("Enter in %s\n",input_it->GetName());
0105        TDirectory* in_dir_tmp  = (TDirectory*)Input ->Get(input_it->GetName());
0106 
0107        TDirectory* out_dir_tmp = (TDirectory*)Output->Get(input_it->GetName());
0108        if(out_dir_tmp==NULL){
0109 //      printf("Directory do not exist\n");
0110         out_dir_tmp = Output->mkdir(input_it->GetName());
0111        }
0112        merge_core(out_dir_tmp, in_dir_tmp);
0113     }else{
0114        TH1* in_hist_tmp  = (TH1*)Input ->Get(input_it->GetName());
0115        TH1* out_hist_tmp = (TH1*)Output->Get(input_it->GetName());
0116        if(out_hist_tmp==NULL){
0117 //      printf("Creation of a new TH1*\n");
0118         Output->cd();
0119         out_hist_tmp = (TH1*)in_hist_tmp->Clone();  
0120        }else{
0121 //      printf("Summing Histograms\n");
0122         out_hist_tmp->Add(in_hist_tmp,1);
0123        }
0124     }
0125     
0126     input_it = input_list->After(input_it);
0127    }
0128 
0129 }
0130 
0131 
0132 TObject* GetObjectFromPath(TDirectory* File, const char* Path)
0133 {
0134    string str(Path);
0135    size_t pos = str.find("/"); 
0136 
0137    if(pos < 256){
0138       string firstPart = str.substr(0,pos);
0139       string endPart   = str.substr(pos+1,str.length());
0140       TDirectory* TMP = (TDirectory*)File->Get(firstPart.c_str());
0141       if(TMP!=NULL)return GetObjectFromPath(TMP,endPart.c_str());
0142       
0143       printf("BUG\n");
0144       return NULL;
0145    }else{
0146       return File->Get(Path);
0147    }
0148 
0149 }
0150