Copying TTree entries with a TCutG

Hi again,

given a TCutG, how can I apply it to a TTree to fill another TTree?

For instance, let’s say I have a TCutG called cutg:

     TCutG *cutg = new TCutG("cutg",gc1->GetN(),gc1->GetX(),gc1->GetY());

where gc1 is some TGraph. Now it’s easy to fill a histogram with this cut, like:

    TH2D *histo = new TH2D("histo","histo", 50, -5, 5, 50, -5, 5);

but how would I go about filling an ntuple (TTree) with this cut? I.e. something like

    TNtupleD *ntSel = (TNtupleD*)ntuple->CopyTree("cutgselection");

where “cutgselection” would correspond to the selection imposed by TCutG.

If CopyTree can’t handle it, I could run a loop over the ntuple and check for every entry TCutG::IsInside(), but then how do I add a single entry (as received from GetEntry(i) to my new ntuple?

Any help is appreciated!


That’s not exactly what you are asking but something close would be to use a TEventList I guess:

Hi, TNtupleD *ntSel = (TNtupleD*)ntuple->CopyTree("cutgselection"); should work. However, note that by definition, this will copy any entry that has at least one match (i.e. if your selection is on a collection within the entry, all the elements of the collection are copied when one or more match the criterium).


Thank you for your replies.

I can vaguely imagine trying something with Olivier’s suggestion of TEventList (although it is not clear to me how to turn the TEventList back into a TTree/TNtupleD).

I am confused by Philippe’s reply. When I wrote CopyTree(“cutgselection”), the “cutgselection” was just meant as a placeholder for something that would actually give me the selection defined by the TCutG… as it stands it will most certainly NOT work! Did you perhaps misunderstand the question? If not, I would appreciate a working example (could be based on on the hist/FirstContour.C tutorial, which provides an example of a TCutG).


The parameter to CopyTree follows the same syntax as the parameter to TTree::Draw. So apriori with your earlier example, you ought to doTNtupleD *ntSel = (TNtupleD*)ntuple->CopyTree(cutg->GetName());


Wow, yes, this actually works right away!! I seriously underestimated ROOT, how silly of me! (I take comfort in the fact that Olivier did not know either… :smiley: )

Many thanks!


I know now :wink: !