Get name of NTuple

Hi Rooters,

I have ntuples (with more than 50 leafs) and I want to pass each event to other functions. I am not sure how to do this from ntuple. So, I thought to write ntuples in the form of class so that I can pass events to function and do some calculation.

While doing that I encountered a problem: My program read one file first using TFile and then get the name of the NTuple and leafs, and put all the NTuples in chain. My program do not know the name of the ntuple and leafs. Program has to get the name of the ntuple and name of all the leafs from the root file. How can I do that?

Thanks.

see TTree::MakeClass

Rene

Thanks Rene.

To use TTree::MakeClass, program has to know the name of the tree, I guess. But program does not know this. It knows the root file name only.

Your suggestion gave me idea to look in hadd.h file. I copied it made some changes and it is working.

Thanks. :smiley:

Hi,

I am stuck again. I used following lines of code (working code) to get name of the ntuple and leaves.

TFile *file = new TFile("test.root");
TDirectory *currentDir = gDirectory;
TList *list = (TList*) currentDir->GetListOfKeys();
TKey *key;

for(int  i=0;  i<list->GetEntries();  i++){
    key = (TKey*) list->At(i);
    TObject *obj = key->ReadObj();
    cout << obj->GetName() << endl;    // print name of ntuple
    if(key->GetCycle() > 1) continue;

    if ( obj->IsA()->InheritsFrom( TTree::Class() ) ) {
        const char* obj_name = obj->GetName();
        TChain *globChain = new TChain(obj_name);
        globChain->Add("test.root");
        TObjArray *array = globChain->GetListOfLeaves();
        for(int  j=0;  j<array->GetEntries();  j++) {
              cout << array->At(i)->GetName() << endl;  // print name of leaves
        } 
    }
}

Now I want to get the data type of the leaf, i.e., Float_t or Int_t or other. How do I get that?
Here, Int_t type is written by defining a structure of integer types and writing the structure in ntuple.

Thanks. :question:

Hi,

that’s encoded in the inherited type, see root.cern.ch/root/html/TLeaf#TLeaf:Class_Charts i.e. TLeafF contains floats. You can compare using e.g. TLeafF::Class() add leaf->IsA(). You can also use TLeaf::GetTypeName().

Cheers, Axel.