Add 3 TH1F and save to new TH1F

Dear ROOTers,

Firstly and foremost, thanks so much for the time to read my query.

Here’s what I want to do:

I have 3 .root files, each of them containing several TH1F histograms. I want to create a new .root file which will in turn save TH1F histograms. These new histograms I want to create by simply adding the corresponding histos in the 3 original .root files. Say:

histo4 = histo1 + (histo3 - histo4)/2,

where histo"i" corresponds to the histogram saved on the ith .root file.

I have a script which I hoped would do this for me:

void subt_TH1F(const char* file1, const char* file2, 
	       const char* file3, const char* histo, const char* rootfile)
{
  // ===== File 1 =====
  const TFile* f1 = new TFile(file1);
  const TH1F*  h1 = (TH1F*) f1->Get(histo);
  h1->SetName("hist1");

  // ===== File 2 =====
  const TFile* f2 = new TFile(file2);
  const TH1F*  h2 = (TH1F*) f2->Get(histo);
  h2->SetName("hist2");

  // ===== File 3 =====
  const TFile* f3 = new TFile(file3);
  const TH1F*  h3 = (TH1F*) f3->Get(histo);
  h3->SetName("hist3");

  // ===== New histogram =====
  TH1F*  HN = h1->Add(*h2, 0.5);
  HN->Add(*h3, -0.5);

  // ===== New file =====
  TFile* FN = new TFile(rootfile, "RECREATE");
  FN->cd();
  HN->Write();
  FN->Close();
}

However, I get an error message:

Error: Can't call TH1F::Add(*h2,1) in current scope subt_histos.C:44:
Possible candidates are...
(in TH1F)
(in TH1)
/Software/ROOT/install/lib/libHist.so  -1:-1   0 public: virtual Bool_t TH1::Add(TF1* h1,Double_t c1=1,Option_t* option="");
/Software/ROOT/install/lib/libHist.so  -1:-1   0 public: virtual Bool_t TH1::Add(const TH1* h1,Double_t c1=1);
/Software/ROOT/install/lib/libHist.so  -1:-1   0 public: virtual Bool_t TH1::Add(const TH1* h,const TH1* h2,Double_t c1=1,Double_t c2=1); //*MENU*
*** Interpreter error recovered ***

I have confirmed that the histograms I want to add have the same number of bins, upper and lower limits, and that ROOT is loading properly my files and looking for named histogram. Also, the same histogram exists in all .root files, but with different bin contents, that’s why I use it as an argument for the 3 original files.

I’m running ROOT v5.34/34.

Any chance for a hint?

Kind regards,

Oscar.

I do not see TH1F::Add(*h2,1) in your code …

Hi Olivier,

Thanks so much for your reply. After modifying the script to:

void subt_TH1F(const char* file1, const char* file2, 
	       const char* file3, const char* histo, const char* rootfile)
{
  // ===== File 1 =====
  const TFile* f1 = new TFile(file1);
  const TH1F*  h1 = (TH1F*) f1->Get(histo);
  h1->SetName("hist1");

  // ===== File 2 =====
  const TFile* f2 = new TFile(file2);
  const TH1F*  h2 = (TH1F*) f2->Get(histo);
  h2->SetName("hist2");

  // ===== File 3 =====
  const TFile* f3 = new TFile(file3);
  const TH1F*  h3 = (TH1F*) f3->Get(histo);
  h3->SetName("hist3");

  // ===== New histogram =====
  TH1F*  HN = h1->TH1F::Add(*h2, 0.5);
  //*HN->Add(*h3, -0.5);

  // ===== New file =====
  TFile* FN = new TFile(rootfile, "RECREATE");
  FN->cd();
  HN->Write();
  FN->Close();
}

and commenting out one line just to check what is going on, I got:

Error: illegal pointer to class object HN 0x0 353  subt_histos.C:51:
*** Interpreter error recovered ***

My best guess here is that since f3 was the last file stored in memory, ROOT is looking for HN somewhere inside f3 (?) . Here, line 51 corresponds to HN->Write();

Thanks!

Can you provide a running macro showing the problem ?