Filling one 2D histogram with TTree data in all rootfiles by using TChain

Hi All,

I try to draw one histogram 2D with two TTree data.

Each rootfile(in the attachment) has two Tleaves that one is (TLeaf*)GAIN650 and the other is (TLeaf*)GAIN700, and each one has 48 bins. Why does the script draw only 96 bins ? Is this correct ? If so, why then is the statistics not correct ? The histogram shows only the statistics of the X axis of the (TLeaf*)GAIN650 and that of the Y axis of (TLeaf*)GAIN700 for the rootfile 2983_Ntuple.root as if there was no the statistics of 2987_NTuple.root file ?

The script to try to run as in the following ; it runs with rootfiles in the attachment…

PS : to run,please, try this one : root -l Chain.C

#include <iostream>
#include <fstream>

using namespace std;
int runno;

void Chain() {

  gStyle->SetOptStat("menrMR");
  TCanvas *c1 = new TCanvas("GAIN Correlation","GAIN Correlation",600,400);
  TH2F *hGAIN = new TH2F("GAINCorr","GAINCorr",96,0,2700000,96,0,2700000);

  ifstream myin;
  myin.open("deneme.txt",ios::in);
  if(!(myin.good())){ cout<<"There is no available deneme.txt"<<endl;}

  while(myin>>runno) {

    TChain *chain = new TChain("TreeofGAINsforHVs");
    chain->AddFile(Form("/Users/Erkurt/Desktop/!!!THESIS!!!/BURNIN Thesis/%d_Ntuples.root",runno));

//    chain->ls();


                   Int_t nentries = (Int_t)chain->GetEntries();
                   cout<<"entries   "<<nentries<<endl;
                   TLeaf *GAIN650 = TreeofGAINsforHVs->GetLeaf("GAIN650");
                   TLeaf *GAIN700 = TreeofGAINsforHVs->GetLeaf("GAIN700");

                   for (int i=0; i<48; i++) {

                     GAIN650->GetBranch()->GetEntry(i);
                     GAIN700->GetBranch()->GetEntry(i);
                     hGAIN->Fill(GAIN650->GetValue(),GAIN700->GetValue());
                     hGAIN->SetMarkerStyle(8);
                     hGAIN->SetMarkerColor(kRed);
                     hGAIN->GetXaxis()->SetTitle("GAIN650");
                     hGAIN->GetYaxis()->SetTitle("GAIN700");

                     }

  }
  hGAIN->Draw();

}

Thanks for your help,
Cheers,
Ersel
deneme.txt (10 Bytes)
2987_Ntuples.root (215 KB)
2983_Ntuples.root (215 KB)

Hi Ersel,

I am not sure I am getting your problem. You have two files with each 48 entries, so I would naturally expect 96 entries in the combined result.

However, your code had a few logical bugs. The code to set up the chain would need to look like this

  TChain *chain = new TChain("TreeofGAINsforHVs");
  while(myin>>runno) {
    chain->AddFile(Form("%d_Ntuples.root",runno));
  }

  Int_t nentries = (Int_t)chain->GetEntries();
  cout<<"entries   "<<nentries<<endl;
  TLeaf *GAIN650 = chain->GetLeaf("GAIN650");
  TLeaf *GAIN700 = chain->GetLeaf("GAIN700");
  ...

So set up the entire chain before you start looping. At the moment you re-create a one-file chain over and over again and get almost by chance the proper looping.

In addition you should better say

for (int i=0; i<nentries; i++)

instead of hard-coding the 48.

Hope that helps,
Benedikt

A more logical version of your macro is:

#include <iostream>
#include <fstream>

using namespace std;
int runno;

void Chain() {
   gStyle->SetOptStat("menrMR");
   TCanvas *c1 = new TCanvas("GAIN Correlation","GAIN Correlation",600,400);
   TH2F *hGAIN = new TH2F("GAINCorr","GAINCorr",96,0,2700000,96,0,2700000);
   hGAIN->SetMarkerStyle(8);
   hGAIN->SetMarkerColor(kRed);
   hGAIN->GetXaxis()->SetTitle("GAIN650");
   hGAIN->GetYaxis()->SetTitle("GAIN700");

   ifstream myin;
   myin.open("deneme.txt",ios::in);
   if(!(myin.good())){ cout<<"There is no available deneme.txt"<<endl;}

   TChain *chain = new TChain("TreeofGAINsforHVs");
   while(myin>>runno) chain->AddFile(Form("%d_Ntuples.root",runno));

   Int_t nentries = (Int_t)chain->GetEntries();
   cout<<"entries   "<<nentries<<endl;
   TLeaf *GAIN650 = chain->GetLeaf("GAIN650");
   TLeaf *GAIN700 = chain->GetLeaf("GAIN700");

   for (int i=0; i<nentries; i++) {
      GAIN650->GetBranch()->GetEntry(i);
      GAIN700->GetBranch()->GetEntry(i);
      hGAIN->Fill(GAIN650->GetValue(),GAIN700->GetValue());
   }

   hGAIN->Draw();
}

Ok.Thanks for your help.

I made some changes on script which is as in the following;

#include <iostream>
#include <fstream>

using namespace std;
int runno;

void HFLEDGainCorrelation() {

  TFile *f = new TFile("HFLEDGainCorrelation.root","RECREATE");

  gStyle->SetOptStat("menrMR");
  TCanvas *c1 = new TCanvas("GAIN Correlation","GAIN Correlation",600,400);

  TH2F *hGAIN = new TH2F("GAINCorr","GAINCorr",1728,0,2700000,1728,0,2700000);
  hGAIN->SetMarkerStyle(8);
  hGAIN->SetMarkerColor(kRed);
  hGAIN->GetXaxis()->SetTitle("GAIN650");
  hGAIN->GetYaxis()->SetTitle("GAIN700");

  ifstream myin;
  myin.open("led_runlist.txt",ios::in);
  if(!(myin.good())){ cout<<"There is no available deneme.txt"<<endl;}

  TChain *chain = new TChain("TreeofGAINsforHVs");

  while(myin>>runno)
    {
      chain->AddFile(Form("%d_Ntuples.root",runno));

      int nentries = (int)chain->GetEntries(0);
      cout<<"entries   "<<nentries<<endl;

      TLeaf *GAIN650 = chain->GetLeaf("GAIN650");
      TLeaf *GAIN700 = chain->GetLeaf("GAIN700");

      int count = 0;

        for (int i=0; i<48; i++) {
        
          count++;
          GAIN650->GetBranch()->GetEntry(i);
          GAIN700->GetBranch()->GetEntry(i);
          hGAIN->Fill(GAIN650->GetValue(0),GAIN700->GetValue(0));
          cout<<count<<"_"<<GAIN650->GetValue(0)<<endl;
          cout<<count<<"_"<<GAIN700->GetValue(0)<<endl;

        }
      
  c1->cd();
      hGAIN->Draw();

    }
  f->cd();
  c1->Write();

  f->Write();
}

Cheers,
Ersel