Select number of first entry of a file to add to a chain

Hi,

I wish to put together several ROOT files in a TChain. But, for some of these files, I wish not to include all the entries, especially the firsts ones.

For example, this is a part of my code which create the TChain. I wish that the 10.000 first entries of the ROOT file “run0004” not to be included in the TChain. How can I do that ?

Thanks,

Xavier

[code]TTree *mychain;
TTree *Analysed_Data;
TChain *allrun = new TChain(“Analysed_Data”);

allrun->Add(“run0001.root”);
allrun->GetEntries();
allrun->Add(“run0004.root”);
allrun->GetEntries();
allrun->Add(“run0010.root”);
allrun->GetEntries();
etc …

TFile *f = new TFile(“b1.251.root”,“RECREATE”);
mychain = allrun->CopyTree("","");
mychaine>SetName(“Analysed_DataC”);
mychain->Write();
f->Close();[/code]

Hi Xavier,

I don’t believe there is any way in root to automate this (but I’m not a root developer so …). I see two options:

  1. You skim all files individually removing the bad events (this is what I’d suggest) and chain the new files together.

  2. You try to design something that tells you “Oh, I don’t want this entry.” This method is dangerous and hard to implement externally. If there is some flag in the event that says “not this event”, then this is easy.

Good luck,
Charles

I was expected some analogies with histograms drawing or tree scanning, where you can just specify the number of events and the number of the first entry desired.

The first option you suggest is the best one : I have a time-dependant correction and it takes some time to be efficient, so the beginning of my files are not enough corrected. Trying to predict from when it is ok is too hazardous :slight_smile:

Thank you Charles,

Xavier

a tutorial already exists to copy a subset of a TTree. After that, I just have to make my TChain with these new files.

copytree3.C

Well, I tried to clone only my ROOT file when the entry number > event_cut. But I do not understand how “Event” works and my ROOT installation seems not to know this class. With the following code, I get this error message :

root [0] .x skim_run.C(295,100)
Treating run 295
File in : /lhome/lderkx/code/analyse/current/run0295.root
File out : /lhome/lderkx/code/analyse/current/run0295skim.root
Error: Symbol Event is not defined in current scope skim_run.C:39:
Warning: Automatic variable Event*event is allocated skim_run.C:39:
(const int)0
*** Interpreter error recovered ***

Any idea ?

[code]int skim_run(int file_number, int event_cut)
{
cout << " Treating run " << file_number<< endl;

ostringstream filein ;
filein << “/lhome/lderkx/code/analyse/current/run”;
if ( file_number / 1000 == 0 ) filein << “0” ;
if ( file_number / 100 == 0 ) filein << “0” ;
if ( file_number / 10 == 0 ) filein << “0” ;
filein << file_number << “.root” ;
TFile *in = new TFile(filein.str().c_str()) ;
cout << " File in : " << filein.str() << endl;
TTree inTree = (TTree)in->Get(in->GetListOfKeys()->At(0)->GetName());

ostringstream fileout ;
fileout << “/lhome/lderkx/code/analyse/current/run” ;
if ( file_number / 1000 == 0 ) fileout << “0” ;
if ( file_number / 100 == 0 ) fileout << “0” ;
if ( file_number / 10 == 0 ) fileout << “0” ;
fileout << file_number << “skim.root” ;
TFile *out = new TFile(fileout.str().c_str(),“recreate”);
cout << " File out : " << fileout.str() << endl;
TTree *outTree= inTree->CloneTree(0);

Event *event = 0;
inTree->SetBranchAddress(“event”,&event);

int Nevent = inTree->GetEntries();
cout << Nevent << " events in the in run" << endl;

for(int e; e<Nevent; e++)
{
inTree->GetEntry(e,1);
if (event->GetNtrack() > event_cut) outTree->Fill();
event->Clear();
}

outTree->Print();
outTree->AutoSave();

cout << outTree->GetEntries() << " events in the out run" << endl;

in->Close();
out->Write();
delete filein;
delete fileout;

return 0;
}[/code]

Cheers,
Philippe.