Hi All,
I have been trying to find a way to store branches that I want to access from my root file in a std::map<> format. My goal is to use this so that I can access different branches for each event that I need with a string and without using many if statements.
Thus far, I have tried the following (I haven’t seen anything like this on the forums, so I apologize if I missed something!):
[code]#include “TROOT.h”
#include “TFile.h”
#include “TTree.h”
#include “TH1.h”
#include “TSystem.h”
#include “TMath.h”
#include “TTreeReader.h”
#include “TTreeReaderValue.h”
#include “TTreeReaderArray.h”
#include
#include
#include <stdio.h>
#include
#include
// – Load ROOT File
auto file = TFile::Open(“data.root”); // simple root file
// – Create a TTreeReader for the “nominal” tree
TTreeReader myReader(“nominal”, file);
// – Create the map and load variables
std::vectorstd::string variables = {“ht”}; // variables I want to store in the map
std::map<std::string,TTreeReaderValue > branch_map;
branch_map[“ht”] = {myReader, “HT”}; // store “HT” in the map
TTreeReaderValue lepton_pt(myReader, “lepton_pt”);
TTreeReaderValue met(myReader, “met_met”);
// TTreeReaderValue met = {myReader, “met_met”}; // this also works
while (myReader.Next()) {
std::cout << " lepton pT = " << *met << std::endl; // this works
std::cout << " met = " << *lepton_pt << std::endl; // this works
for (auto variable : variables){
std::cout << " >>> Value = " << *branch_map.at(variable) << std::endl;
}
}[/code]
But, I get the following error:
[code]Error in <Get()>: Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?
*** Break *** segmentation violation
Value = Generating stack trace…[/code]
I hope this is a simple c++ issue I’m experiencing.
I’m using ROOT 6.04/14 and I execute this with:
$ g++ -o example example.cxx `root-config --cflags --glibs` -lTreePlayer
$ ./example
Any help or tips would be greatly appreciated!
Dan