# Add files to a chain

**URL:** <https://root-forum.cern.ch/t/add-files-to-a-chain/26045>\
**Category:** ROOT\
**Created:** [August 28, 2017, 2:43pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045 "2017-08-28T14:43:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![naikal\_mc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/bc8723/32.png) [@naikal\_mc](https://root-forum.cern.ch/u/naikal_mc)\
**Post date:** [August 28, 2017, 2:43pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/1 "2017-08-28T14:43:11Z")

</div>

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.

```auto
 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:

```auto
/// 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?

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 28, 2017, 2:49pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/2 "2017-08-28T14:49:27Z")

</div>

Just FYI:

> [@Getting the same histograms after cuts](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/2):
>
> Remark on the way to post code snippets: When you post pieces of code on the forum enclose it with ``` . That makes your post much more readable (I systematically edit your posts that way) . So do: ``` // this is a C++ piece of code. ``` And it will display as: // this is a C++ piece of code.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [August 28, 2017, 2:50pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/3 "2017-08-28T14:50:47Z")

</div>

Hi,

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

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 28, 2017, 2:55pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/4 "2017-08-28T14:55:31Z")

</div>

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

```auto
{
   //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");
}

```

---

<div class="post-metadata">

**Author:** ![naikal\_mc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/bc8723/32.png) [@naikal\_mc](https://root-forum.cern.ch/u/naikal_mc)\
**Post date:** [August 28, 2017, 2:56pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/5 "2017-08-28T14:56:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 28, 2017, 2:58pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/6 "2017-08-28T14:58:48Z")

</div>

Modified example to demonstrate adding multiple files to the chain:

```auto
{
   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");
}

```

---

<div class="post-metadata">

**Author:** ![naikal\_mc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/bc8723/32.png) [@naikal\_mc](https://root-forum.cern.ch/u/naikal_mc)\
**Post date:** [August 28, 2017, 2:59pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/7 "2017-08-28T14:59:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![naikal\_mc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/bc8723/32.png) [@naikal\_mc](https://root-forum.cern.ch/u/naikal_mc)\
**Post date:** [August 28, 2017, 3:00pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/8 "2017-08-28T15:00:58Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 28, 2017, 3:02pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/9 "2017-08-28T15:02:24Z")

</div>

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

```auto
{
   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");
}

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 11, 2017, 3:02pm UTC](https://root-forum.cern.ch/t/add-files-to-a-chain/26045/10 "2017-09-11T15:02:33Z")

</div>

Closed on September 11, 2017, 3:02pm UTC

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