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.
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");
}
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.
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");
}