Error in Adding same histograms from different root files


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hello there, i am a beginner, i am trying to add histograms (same) from different root files, i have gone through many posts here related to my issue, i tried many of them but none worked, couldn’t anyone here please help me. I will be grateful.
I have my root files saved in my folder.
Thanks,

Towseef Rehman

That should be quite simple.

  1. You open the first file using TFile ie:
    auto f1 = new TFile( "file1.root");

  2. You get the first histogram for f1, ie:
    TH1D *h1 = (TH1D*)f1->Get("h1");

  3. You open the second file using TFile ie:
    auto f2 = new TFile( "file2.root");

  4. You get the second histogram for f2, ie:
    TH1D *h2 = (TH1D*)f2->Get("h2");

  5. You add the two histograms, ie:
    h2->Add("1")

Of course you should adapt the names to your own case.

I don’t Have just 2 files infact i have 10 files, therefore i want to loop over files and get histograms from each file and then get them added.
Thanks@ couet

Yes you can extrapolate the steps I showed you to a loop… that should be easy.

Dear couet , i have made a macro used loop but i m still getting errors.

Can you post a macro reproducing the errors ?

yes sure, I am sorry I have an internet problem here.
Here is my code I am trying to read 5 root files, but in actual I have to read 100 files

void test(){
  
  
  
   TFile *f = new TFile ("result.root", "RECREAT");
  char InputFile[1000];
  
  Int_t n = 5;
  for(Int_t i=1; i<=n; i++){

    TFile *f1 = new TFile ("output_urqmd25centrEta100.root");
    TH1D *h1 = (TH1D*)f1->Get("homega");
    TString fFileName = "output_urqmd25centrEta100.root";
    fFileName =TString::Format("output_urqmd25centrEta%d00.root",i);
    TFile *fInputFile = new TFile(fFileName);
    TH1D *h2 = (TH1D*)fInputFile->Get("homega");
    
    TH1D *h1 = h1->Add(h2);
    
  }
  
  h1->SetDirectory(f);
  
  
  f->cd();
  
  f->Write();
  f->Close();
  
}
void test(){
   char InputFile[200];
  
   Int_t n = 5;

   TFile *f1 = new TFile ("output_urqmd25centrEta100.root");
   TH1D *h1 = (TH1D*)f1->Get("homega");
 
   for (Int_t i=1; i<=n; i++) {
      TString fFileName = "output_urqmd25centrEta100.root";
      fFileName = TString::Format("output_urqmd25centrEta%d00.root",i);
      TFile *fInputFile = new TFile(fFileName);
      TH1D *h2 = (TH1D*)fInputFile->Get("homega");    
      h1->Add(h2);   
  }
  
   TFile *f = new TFile ("result.root", "RECREATE");
   h1->SetDirectory(f);
   f->cd();
   f->Write();
   f->Close();
}

Dear Couet, i modified my code as you said but here is what it shows,

error: cannot initialize a variable of type ‘TH1D *’ with an rvalue of type ‘Bool_t’
(aka ‘bool’)
TH1D *h1 = h1->Add(h2);
^ ~~~~~~~~~~~
root [1]

Sorry I did not noticed you made a mistake there. I fixed it in the previous post.
It should be:

      h1->Add(h2);   

Thankyou so much Couet, it worked as expected.
Thanks again!

Hello Couet,
Can you please tell me if each root file contains 3 histograms how can I add all three using this code separately?

void test(){
   char InputFile[200];
  
   Int_t n = 5;

   TFile *f1 = new TFile ("output_urqmd25centrEta100.root");
   TH1D *h1 = (TH1D*)f1->Get("homega1");
   TH1D *h2 = (TH1D*)f1->Get("homega2");
   TH1D *h3 = (TH1D*)f1->Get("homage");

   for (Int_t i=1; i<=n; i++) {
      TString fFileName = "output_urqmd25centrEta100.root";
      fFileName = TString::Format("output_urqmd25centrEta%d00.root",i);
      TFile *fInputFile = new TFile(fFileName);
      TH1D *h21 = (TH1D*)fInputFile->Get("homega1");    
      h1->Add(h21); 
      TH1D *h22 = (TH1D*)fInputFile->Get("homega2");    
      h2->Add(h22);
      TH1D *h23 = (TH1D*)fInputFile->Get("homega3");    
      h3->Add(h23);     
  }
  
   TFile *f = new TFile ("result.root", "RECREATE");
   f->cd();
   f->Write();
   f->Close();
}

Thanks @couet, i did it the same way.
Thankyou