# Using MakeClass and Chains

**URL:** <https://root-forum.cern.ch/t/using-makeclass-and-chains/35384>\
**Category:** Newbie\
**Created:** [July 29, 2019, 10:22am UTC](https://root-forum.cern.ch/t/using-makeclass-and-chains/35384 "2019-07-29T10:22:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KatR](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/d9b06d/32.png) [@KatR](https://root-forum.cern.ch/u/KatR)\
**Post date:** [July 29, 2019, 10:22am UTC](https://root-forum.cern.ch/t/using-makeclass-and-chains/35384/1 "2019-07-29T10:22:51Z")

</div>

Hi everyone! I’ve been trying to run a macro over multiple root files. I used MakeClass on one of these trees to generate a template and then tried to change it to read in a Chain. When I try to run the macro though, it says there are zero events and produces empty histograms. I can’t find where I’ve gone wrong though, I’d really appreciate any help! This is my code:

```auto
    #define MakeHistograms_cxx
#include "MakeHistograms.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <iostream>
#include "TFileCollection.h"
#include "THashList.h"
#include "TList.h"
void MakeHistograms::Loop()
{
  //Stop ROOT opening every canvas as it's created
  gROOT->SetBatch(kTRUE);

  //Auto-generated stuff
  //Long64_t nentries = fChain->GetEntriesFast();

  Long64_t nbytes = 0, nb = 0;

  Long64_t N = fChain->GetEntries();
   
  //Print the number of events
  std::cout << "Number of entries is " << N << std::endl;
   

  // Create write histogram before creating histograms 

  TFile hfile("hfile.root","RECREATE");
  //Make useful histograms, jet pt and NTrack
  TH1F *h = new TH1F("h", "Jet pT", 100, 50, 2500.);
  TH1F *g = new TH1F("g", "NTrack", 100, 0, 120);

  //Number of pT slices
  const Int_t nHists = 9;

  std::cout << "STAGE 2 Number of entries is " << N << std::endl;

  Double_t edges[nHists+1] = {400,500,650,800,1000,1200,1500,2000,3000,6000};

      //Array of histograms
  TH1F *m[nHists];

      //Use these to give the histograms unique names       
  char name[20];
  char title[100];
  char buffer[50];

  for (Int_t i=0;i<nHists;i++) {
    sprintf(name,"m%d",i);
    sprintf(title,"m%d pT between %5.0f and %5.0f GeV",i,edges[i],edges[i+1]);
    m[i] = new TH1F(name,title,120,-0.5,119.5);
    m[i]->Print();
  }

 std::cout << "STAGE 2 Number of entries is " << N << std::endl;

  
  //Fill useful histograms
  for (Long64_t i = 0; i < N; i++) {
    //cout << i << endl;
    fChain->GetEntry(i);
    for (unsigned long j = 0; j <jet_pt->size(); j++){
      h->Fill(jet_pt->at(j),1.);
      g->Fill(jet_NumTrkPt1000PV->at(j),1.);
      
      for(int k=0; k<nHists;k++){
        double minPt = edges[k];
        double maxPt = edges[k+1];
        if (jet_pt->at(j)<maxPt){
          m[k]->Fill(jet_NumTrkPt1000PV->at(j),1.);
          break;
        }
      }

    }
  }
  

  

  //Print other useful info
  Double_t Min = h->GetMinimum();
  Double_t Max = h->GetMaximum();
  std::cout << "Maximum value is " << Max << std::endl;
  std::cout << "Minimum value is " << Min << std::endl;
     
  hfile.Write();
  hfile.Close();    
}

      

/* To run use root -l, .L MakeHistograms.C, run() */
int run(){

  /*TTree *tree;
  TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("T.root");
  if (!f || !f->IsOpen()) {
    f = new TFile("T.root");
  }
  f->GetObject("outTree",tree);

  */

  // name of tree is the argument
  TChain *tree = new TChain("outTree");
  // New is a place holder.... 
  TFileCollection fc("New");

  // add the names of the trees to files_signal.txt
  
  fc.AddFromFile("files_signal.txt");
  fc.Print();
  fc.GetList()->Print();
  tree->AddFileInfoList(fc.GetList(),TChain::kBigNumber);

   MakeHistograms m(tree);
   
   m.Loop();
   return 0;
   }                                                                                                                                                                                                                                                                                                           

```

And I have three files in files\_signal.txt containing only the three lines:

```auto
user.krybacki.18636682._000001.tree.root
user.krybacki.18636682._000002.tree.root
user.krybacki.18636682._000003.tree.root

```

Removing everything but the last three lines in run() gives a macro that works fine on a single file. Thanks again for any help! 🙂

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [July 31, 2019, 7:30am UTC](https://root-forum.cern.ch/t/using-makeclass-and-chains/35384/2 "2019-07-31T07:30:33Z")

</div>

Hi @KatR,

So when you print

```auto
  std::cout << "Number of entries is " << N << std::endl;

```

It says the chain has zero entries?

Can you try to construct the chain differently? Instead of using `AddFileInfoList`, can you try adding one by one the files to the chain with:  
[https://root.cern.ch/doc/master/classTChain.html#a4d491db32262125e6cb77a8f7a6bfd93](https://root.cern.ch/doc/master/classTChain.html#a4d491db32262125e6cb77a8f7a6bfd93)

and check immediately the number of entries?

---

<div class="post-metadata">

**Author:** ![KatR](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/d9b06d/32.png) [@KatR](https://root-forum.cern.ch/u/KatR)\
**Post date:** [July 31, 2019, 9:21am UTC](https://root-forum.cern.ch/t/using-makeclass-and-chains/35384/3 "2019-07-31T09:21:54Z")

</div>

That works!!! Yes, previously that line was returning 0 entries, but making the change to AddFile fixed it. Thank you, I was so stuck with that!
