I am getting the following errors while accessing the dictionary branch using c++ syntax.
Error in TChain::SetBranchAddress: The class requested (map<string,bool>) for the branch “HLT_BPH_isFired” is an instance of an stl collection and does not have a compiled CollectionProxy. Please generate the dictionary for this collection (map<string, bool>) to avoid writing corrupted data.
*** Break *** segmentation violation
Please help me accessing the branch. I have attached the root file for ref. The branch name is “HLT_BPH_isFired” flatntuple_MC_.root (382.4 KB)
.
Thanks for your reply. Actually i am not generating any dictionary. It is already present in my root file, I am trying to access it. I can’t plot or print any other variables too until the BranchStatus of the “HLT_BPH_isFired” (dictionary) is turned off.
ROOT does not know how to do I/O of map<string, bool> without dictionaries. They should come with the data. If not, you can produce them. Otherwise, you can probably still read the individual elements of the map but you might have problems reading the full map<string, bool> object.
Actually i am not generating any dictionary. It is already present in my root file,
The “dictionaries” are piece of C++ code that make the connection between C++ object (map<string, bool> in your case) and the data on file, as such they are not (and can not be) “in” the file. What is present in the file is only a data and a description of the data (i.e. that it is a map<string, bool>).
To connect the data to a C++ object (i.e. what you attempt with SetBranchAddress) you must have a dictionary for the class/type of that object.
However, as Enrico, mentions there is also way to access the data without relying on the C++ objects. For example you can read you data without a dictionary with TTree::Draw or RDataFrame.
Following the interactive way (ACLiC), the dictionary I have created. But I am unable to print them with normal cpp syntax. When I tried to make cout statement (Screenshot is attached) unusual sign appeared. Sorry but Is it not possible to print in the terminal with cpp syntax ?
As I have told i have already tried via ACLiC, plotsAll_cpp.d, plotsAll_cpp.so files are already generated. I am unable access now also after following your example.
Alright this is the shortest demonstrator I could put together:
#include <TFile.h>
#include <TTree.h>
#include <map>
#include <string>
#include <iostream>
#pragma link C++ class std::map<std::string, bool>+;
int read_map_branch() {
TFile f("flatntuple_MC_.root");
f.ls();
f.cd("ntuplizer");
f.ls();
auto t = f.Get<TTree>("ntuplizer/tree");
std::map<std::string, bool> *m = nullptr;
t->SetBranchAddress("HLT_BPH_isFired", &m);
for (Long64_t i = 0ll; i < t->GetEntries(); ++i) {
t->GetEntry(i);
for (auto p : *m)
std::cout << p.first << ": " << p.second << '\n';
}
return 0;
}
that you can execute with root read_map_branch.C+.
ACLiC will produce the dictionaries as per the #pragma link ..., and then use them to read the std::map<std::string, bool>.
Variations on that theme will also work: you can produce the dictionaries first and then load them before opening the file, you can produce the dictionaries with the rootcling command line and then compile them together with your source, etc., as per GitHub - eguiraud/root_dictionaries_tutorial: A tutorial on creating ROOT dictionaries to perform I/O of custom C++ classes .