Add files to a chain

Hello, everyone.
I am trying to add a huge amount of files to a chain and I would like to do so by reading the name of the root files from a .txt file.

 void {

TChain ch("reco");
ch.Add("[//machine]/C:\root_v5.34.36\crazy\crazy.1.root[\reco]");
TH2F *h1 = new TH2F("h1", "h1 title",350, -2, 2, 300, -1.1, 1.1);
reco->Draw("Var1>>h1","","colz");
}

Just like I read in the TChain class:

/// Add a new file to this chain.
/// Argument name may have either of two formats. The first:
/// [//machine]/path/file_name.root[/tree_name]

but it isn’t working, because the histogram is not drawn.
Can you help me, pls?

Just FYI:

Hi,

why not looping on the lines inside this txt file and add the single files to the chain?

Cheers,
D

Here’s a working example on how to add files to a TChain:

{
   //Create a file for the example
   TFile *f = new TFile("output.root","CREATE");
   TTree *t = new TTree("tree", "Tree");
   double value;
   t->Branch("value", &value);
   for (int i=0;i<5000;i++) {
      value = gRandom->Gaus();
      t->Fill();
   }
   t->ResetBranchAddresses();
   f->Write();
   f->Close();

   //Open for reading using a TChain
   TChain *chain = new TChain("tree");
   chain->AddFile("output.root");
   chain->Draw("value");
}

That’s exactly what I want to do, but how? I can’t find this in the user guide.

Modified example to demonstrate adding multiple files to the chain:

{
   const int numFiles = 10;
   for (int fileNum=0;fileNum < numFiles;fileNum++) {
      //Create a file for the example
      TFile *f = new TFile(Form("output_%d.root", fileNum),"CREATE");
      TTree *t = new TTree("tree", "Tree");
      double value;
      t->Branch("value", &value);
      for (int i=0;i<5000;i++) {
         value = gRandom->Gaus();
         t->Fill();
      }
      t->ResetBranchAddresses();
      f->Write();
      f->Close();
   }

   //Open for reading using a TChain
   TChain *chain = new TChain("tree");
   for (int fileNum=0;fileNum < numFiles;fileNum++) {
      chain->AddFile(Form("output_%d.root", fileNum));
   }
   chain->Draw("value");
}

Hello, ksmith. Thanks for your answer. I already have all the files and variables. But I want to add ~1000 files to the chain without using:
ch.Add(“file*.root”);
Because I don’t want to use ALL the files that I have. I want to add just a part of all my files, that I put in a different past.

I was trying to put the files in a different paste so I didn’t have to use the .txt file. But using this method is not working either,

In this example the chain only loads a subset of the files generated:

{
   const int numFiles = 10;
   for (int fileNum=0;fileNum < numFiles;fileNum++) {
      //Create a file for the example
      TFile *f = new TFile(Form("output_%d.root", fileNum),"RECREATE");
      TTree *t = new TTree("tree", "Tree");
      double value;
      t->Branch("value", &value);
      for (int i=0;i<5000;i++) {
         value = gRandom->Gaus(fileNum);
         t->Fill();
      }
      t->ResetBranchAddresses();
      f->Write();
      f->Close();
   }

   //Open for reading using a TChain
   TChain *chain = new TChain("tree");
   //Add only a subset of the files generated.
   std::vector<int> fileNums = {8,3,2};
   for (auto fileNum : fileNums) {
      chain->AddFile(Form("output_%d.root", fileNum));
   }
   chain->Draw("value");
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.