ROOT logo
#include "hntuplemap.h"
#include "TObjArray.h"
#include "TBranch.h"
#include "TLeaf.h"
#include "TROOT.h"
#include "TSystem.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
#include <string>
#include <cstdlib>

using namespace std;

ClassImp(HNtupleMap)

//_HADES_CLASS_DESCRIPTION
////////////////////////////////////////////////////////////////////////////
// HNtupleMap
//
// This Class is designed to make the use of existing TNuples
// more comfortable. TFile, and TNtuple are puplic to allow
// to access the full functionality of this classes. In createMap(...)
// the specified root file will be opened and the pointer to the ntuple
// created. A STL map c is created which allows to access the ntuple members
// by name. The map will be used later to index the data pointer
// of Float_t* data = ntuple->GetArgs();
// data[c["x"]] will give back the value of variable "x" of the ntuple
// with c["x"] the by this function created map.
// CAUTION: DUE TO THE USE OF STL MAP YOU HAVE TO PAY A PERFORMANCE PENALTY!
//
// example :
//
// void testNtupleMap()
// {
//    HNtupleMap a;
//    a.createMap("myfile.root", "myntuple");
//    a.printMap();
//
//    Int_t n = a.ntuple->GetEntries();
//    for(Int_t i = 0; i < n ; i ++){
//	a.ntuple->GetEntry(i);
//	cout<<a["some_member"]<<" "<<a["some_other_member"]<<" "<<endl;
//    }
// }
////////////////////////////////////////////////////////////////////////////

HNtupleMap::HNtupleMap()
{
    ntuple = NULL;
    file   = NULL;
    a      = NULL;
}

HNtupleMap::~HNtupleMap()
{

    if(ntuple) {
	delete ntuple;
	ntuple = NULL;
    }
    if(file) {
	file ->Close();
	delete file;
	file   = NULL;
    }
    a = NULL;
}

void HNtupleMap::createMap(TString in, TString ntupleName)
{
    // create a STL map with the key  == leaf name of the ntuple
    // the mapped value stores the index of the leave in the list
    // of leaves.
    file = new TFile(in.Data(),"READ");
    if(!file)  { Error("createMap()","File %s not found!",in.Data()); exit(1);}

    ntuple = (TNtuple*) file->Get(ntupleName.Data());
    if(!ntuple) { Error("createMap()","Ntuple %s not found!",ntupleName.Data()); exit(1);}

    TObjArray* li = ntuple->GetListOfLeaves();
    if(li){
	for(Int_t i = 0; i < li->GetLast() + 1; i ++){
	    TLeaf* leaf = (TLeaf*) li->At(i);
	    c.insert(make_pair(leaf->GetName(),i));
	}
	a = ntuple->GetArgs();
	if(!a) { Error("createMap()","Could not get Argument pointer!"); exit(1);}
    } else { Error("createMap()","Could not get list of leaves!"); exit(1);}
}

Float_t& HNtupleMap::operator[](const std::string& val) {
    if(!a) { Error("operator[]","map not initialized!"); exit(1);}

    map<string,int>::iterator iter = c.find(val);
    if( iter == c.end() ) { Error("operator[]","key : %s not found!",val.data()); exit(1);}
    else    return a[iter->second];
}

void HNtupleMap::printMap(){
    if(ntuple){
	cout<<"-------------------------------------------------"<<endl;
	cout<<"Mapped index of ntuple : "<<ntuple->GetName()<<endl;
	map<string,Int_t>::iterator it;
	Int_t ct=0;
	for(it = c.begin(); it != c.end(); it++){
	    cout<<right<<setw(5)<<ct<<" leaf : "<<setw(20)<< it->first <<", index: "<<setw(5)<<it->second<<endl;
	    ct++;
	}
	cout<<"-------------------------------------------------"<<endl;
    } else {
	Error("printMap()","Ntuple pointer is NULL!"); exit(1);}
}
 hntuplemap.cc:1
 hntuplemap.cc:2
 hntuplemap.cc:3
 hntuplemap.cc:4
 hntuplemap.cc:5
 hntuplemap.cc:6
 hntuplemap.cc:7
 hntuplemap.cc:8
 hntuplemap.cc:9
 hntuplemap.cc:10
 hntuplemap.cc:11
 hntuplemap.cc:12
 hntuplemap.cc:13
 hntuplemap.cc:14
 hntuplemap.cc:15
 hntuplemap.cc:16
 hntuplemap.cc:17
 hntuplemap.cc:18
 hntuplemap.cc:19
 hntuplemap.cc:20
 hntuplemap.cc:21
 hntuplemap.cc:22
 hntuplemap.cc:23
 hntuplemap.cc:24
 hntuplemap.cc:25
 hntuplemap.cc:26
 hntuplemap.cc:27
 hntuplemap.cc:28
 hntuplemap.cc:29
 hntuplemap.cc:30
 hntuplemap.cc:31
 hntuplemap.cc:32
 hntuplemap.cc:33
 hntuplemap.cc:34
 hntuplemap.cc:35
 hntuplemap.cc:36
 hntuplemap.cc:37
 hntuplemap.cc:38
 hntuplemap.cc:39
 hntuplemap.cc:40
 hntuplemap.cc:41
 hntuplemap.cc:42
 hntuplemap.cc:43
 hntuplemap.cc:44
 hntuplemap.cc:45
 hntuplemap.cc:46
 hntuplemap.cc:47
 hntuplemap.cc:48
 hntuplemap.cc:49
 hntuplemap.cc:50
 hntuplemap.cc:51
 hntuplemap.cc:52
 hntuplemap.cc:53
 hntuplemap.cc:54
 hntuplemap.cc:55
 hntuplemap.cc:56
 hntuplemap.cc:57
 hntuplemap.cc:58
 hntuplemap.cc:59
 hntuplemap.cc:60
 hntuplemap.cc:61
 hntuplemap.cc:62
 hntuplemap.cc:63
 hntuplemap.cc:64
 hntuplemap.cc:65
 hntuplemap.cc:66
 hntuplemap.cc:67
 hntuplemap.cc:68
 hntuplemap.cc:69
 hntuplemap.cc:70
 hntuplemap.cc:71
 hntuplemap.cc:72
 hntuplemap.cc:73
 hntuplemap.cc:74
 hntuplemap.cc:75
 hntuplemap.cc:76
 hntuplemap.cc:77
 hntuplemap.cc:78
 hntuplemap.cc:79
 hntuplemap.cc:80
 hntuplemap.cc:81
 hntuplemap.cc:82
 hntuplemap.cc:83
 hntuplemap.cc:84
 hntuplemap.cc:85
 hntuplemap.cc:86
 hntuplemap.cc:87
 hntuplemap.cc:88
 hntuplemap.cc:89
 hntuplemap.cc:90
 hntuplemap.cc:91
 hntuplemap.cc:92
 hntuplemap.cc:93
 hntuplemap.cc:94
 hntuplemap.cc:95
 hntuplemap.cc:96
 hntuplemap.cc:97
 hntuplemap.cc:98
 hntuplemap.cc:99
 hntuplemap.cc:100
 hntuplemap.cc:101
 hntuplemap.cc:102
 hntuplemap.cc:103
 hntuplemap.cc:104
 hntuplemap.cc:105
 hntuplemap.cc:106
 hntuplemap.cc:107
 hntuplemap.cc:108
 hntuplemap.cc:109
 hntuplemap.cc:110
 hntuplemap.cc:111
 hntuplemap.cc:112
 hntuplemap.cc:113
 hntuplemap.cc:114