Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

#include<vector>
#include "TROOT.h"
#include "TFile.h"
#include "TFileMerger.h"
#include "TDirectory.h"
#include "TChain.h"
#include "TObject.h"
#include "TCanvas.h"
#include "TMath.h"
#include "TLegend.h"
#include "TGraph.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TTree.h"
#include "TF1.h"
#include "TPaveText.h"



TObject* GetObjectFromPath(TDirectory* File, const char* Path);
void merge_core(TDirectory* Output, TDirectory* Input);

void merge(char* InputFilePath,char* OutputFilePath)
{
   gROOT->Reset();
   char outname[2048];
   char name[2048];

   unsigned int PackN    = 1;
   unsigned int PackSize = 120;

   for(unsigned int P=0;P<PackN;P++){
        TFileMerger merger(kFALSE);
        if(PackN>1){
        sprintf(outname,"tmp_%s_%i.root",OutputFilePath,P);
        }else{
        sprintf(outname,"%s",OutputFilePath);
        }
        merger.OutputFile(outname);
        merger.PrintFiles("true");        
        merger.SetFastMethod();
        for(unsigned int i=0;i<PackSize;i++){
           sprintf(name,InputFilePath,P*PackSize+i);
           merger.AddFile(name);
        }
        merger.Merge();
   }

   if(PackN>1){
      TFileMerger merger(kFALSE);
      merger.OutputFile(OutputFilePath);
      merger.PrintFiles("true");        
      merger.SetFastMethod();
      for(unsigned int P=0;P<PackN;P++){
           sprintf(outname,"tmp_%s_%i.root",OutputFilePath,P);
           merger.AddFile(outname);
      }
      merger.Merge();

      for(unsigned int P=0;P<PackN;P++){
           sprintf(outname,"tmp_%s_%i.root",OutputFilePath,P);
           remove(outname);
      }
   }
}
        

/*
	TFile* Output = new TFile("out.root","RECREATE");
	TFile* Input;
	for(unsigned int i=0;i<100;i++){
		char name[1024];
		sprintf(name,"file:SingleMu_Discrim_%04i.root",i);
		printf("Oppening %s\n",name);
		Input =  new TFile(name);
		if(Input==NULL || Input->IsZombie() ){printf("### Bug With File %s\n### File will be skipped \n",name); continue;}
		merge_core(Output, Input );
		Input->Close();
		delete Input;		
	}

	Output->Write();

}	Output->Close();
*/




void merge_core(TDirectory* Output, TDirectory* Input)
{ 
   TH1::AddDirectory(kTRUE);

   if(Input==NULL) return;
   TList* input_list = Input->GetListOfKeys();
   if(input_list==NULL){cout <<"LIST IS NULL\n";return;}

   TObject* input_it = input_list->First();  

   while(input_it!=NULL){
	if(input_it->IsFolder()){
//	   printf("Enter in %s\n",input_it->GetName());
	   TDirectory* in_dir_tmp  = (TDirectory*)Input ->Get(input_it->GetName());

	   TDirectory* out_dir_tmp = (TDirectory*)Output->Get(input_it->GetName());
	   if(out_dir_tmp==NULL){
//		printf("Directory do not exist\n");
		out_dir_tmp = Output->mkdir(input_it->GetName());
	   }
	   merge_core(out_dir_tmp, in_dir_tmp);
	}else{
	   TH1* in_hist_tmp  = (TH1*)Input ->Get(input_it->GetName());
	   TH1* out_hist_tmp = (TH1*)Output->Get(input_it->GetName());
	   if(out_hist_tmp==NULL){
//		printf("Creation of a new TH1*\n");
		Output->cd();
		out_hist_tmp = (TH1*)in_hist_tmp->Clone();	
	   }else{
//		printf("Summing Histograms\n");
		out_hist_tmp->Add(in_hist_tmp,1);
	   }
	}
	
	input_it = input_list->After(input_it);
   }

}


TObject* GetObjectFromPath(TDirectory* File, const char* Path)
{
   string str(Path);
   size_t pos = str.find("/"); 

   if(pos < 256){
      string firstPart = str.substr(0,pos);
      string endPart   = str.substr(pos+1,str.length());
      TDirectory* TMP = (TDirectory*)File->Get(firstPart.c_str());
      if(TMP!=NULL)return GetObjectFromPath(TMP,endPart.c_str());
      
      printf("BUG\n");
      return NULL;
   }else{
      return File->Get(Path);
   }

}