ATLAS Offline Software
HistogramFillerTree.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef AthenaMonitoringKernel_HistogramFiller_HistogramFillerTree_h
6 #define AthenaMonitoringKernel_HistogramFiller_HistogramFillerTree_h
7 
8 #include "TTree.h"
9 
11 #include <boost/algorithm/string.hpp>
12 
13 namespace Monitored {
14  template <typename T> void scalarFillerFunc(TBranch* branch, const IMonitoredVariable& var);
15  template <> void scalarFillerFunc<std::string>(TBranch* branch, const IMonitoredVariable& var);
16  template <typename T> void vectorFillerFunc(TBranch* branch, const IMonitoredVariable& var);
17  template <> void vectorFillerFunc<std::string>(TBranch* branch, const IMonitoredVariable& var);
18 
23  public:
24  HistogramFillerTree(const HistogramDef& definition, std::shared_ptr<IHistogramProvider> provider)
25  : HistogramFiller(definition, provider) {
27  }
28 
29  virtual unsigned fill( const HistogramFiller::VariablesPack& vars ) const override {
30  // handling of the cutmask
31  auto cutMaskValuePair = getCutMaskFunc(vars.cut);
32  if (cutMaskValuePair.first == 0) { return 0; }
33  if (ATH_UNLIKELY(cutMaskValuePair.first > 1)) {
34  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
35  log << MSG::WARNING << "HistogramFillerTree (" << m_histDef->alias
36  << ") does not support more than a single entry being filled at a time\n"
37  << "so a cut mask with > 1 entry doesn't make sense. Using first entry only." << endmsg;
38  if (! cutMaskValuePair.second(0)) { return 0; }
39  }
40 
41  if (ATH_UNLIKELY(vars.size() != m_branchDefs.size())) {
42  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
43  log << MSG::ERROR << "Mismatch of passed variables and expected variables for " << m_histDef->alias
44  << "(" << vars.size() << ", " << m_branchDefs.size() << ")" << endmsg;
45  return 0;
46  }
47 
48  auto cutMaskAccessor = cutMaskValuePair.second;
49 
50  auto tree = this->histogram<TTree>();
51  if (tree->GetListOfBranches()->GetEntries() == 0) {
53  }
54  auto branchList = tree->GetListOfBranches();
55  // following logic allows us to skip branches that were badly-defined
56  size_t idx = 0, idxgood = 0;
57  for (const auto& brdef : m_branchDefs) {
58  if (ATH_UNLIKELY(brdef.second == "IGNORE")) {
59  ++idx; continue;
60  }
61  TBranch* branch = static_cast<TBranch*>(branchList->At(idxgood));
63  ++idx; ++idxgood;
64  }
65  for (Int_t i = 0; i < branchList->GetEntries(); ++i) {
66 
67 
68  }
69  tree->SetEntries(tree->GetEntries() + 1);
70  return 1;
71  }
72 
73  private:
74  std::vector<std::pair<std::string, std::string>> m_branchDefs;
75  std::vector<std::function<void(TBranch*, const IMonitoredVariable&)>> m_fillerFunctions;
76 
77  void parseDefinition() {
78  std::vector<std::string> tokenized;
79  boost::split(tokenized, m_histDef->treeDef, [](char c){ return c ==':'; });
80  for (const auto& token : tokenized) {
81  auto ipart = token.find('/');
82  if (ipart == std::string::npos) {
83  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
84  log << MSG::ERROR << "Tree " << m_histDef->alias << ": Badly formed variable definition " << token
85  << "; skipping" << endmsg;
86  continue;
87  }
88  auto branch = token.substr(0, ipart);
89  auto type = token.substr(ipart+1);
90  std::function<void(TBranch*, const IMonitoredVariable&)> fillerFunc;
91  // scalar
92  if (type == "B") fillerFunc = scalarFillerFunc<Char_t>;
93  else if (type == "b") fillerFunc = scalarFillerFunc<UChar_t>;
94  else if (type == "S") fillerFunc = scalarFillerFunc<Short_t>;
95  else if (type == "s") fillerFunc = scalarFillerFunc<UShort_t>;
96  else if (type == "I") fillerFunc = scalarFillerFunc<Int_t>;
97  else if (type == "i") fillerFunc = scalarFillerFunc<UInt_t>;
98  else if (type == "F") fillerFunc = scalarFillerFunc<Float_t>;
99  else if (type == "f") fillerFunc = scalarFillerFunc<Float16_t>;
100  else if (type == "D") fillerFunc = scalarFillerFunc<Double_t>;
101  else if (type == "d") fillerFunc = scalarFillerFunc<Double32_t>;
102  else if (type == "L") fillerFunc = scalarFillerFunc<Long64_t>;
103  else if (type == "l") fillerFunc = scalarFillerFunc<ULong64_t>;
104  else if (type == "O") fillerFunc = scalarFillerFunc<Bool_t>;
105  else if (type == "string") fillerFunc = scalarFillerFunc<std::string>;
106  else if (type == "vector<char>") fillerFunc = vectorFillerFunc<char>;
107  else if (type == "vector<unsigned char>") fillerFunc = vectorFillerFunc<unsigned char>;
108  else if (type == "vector<int>") fillerFunc = vectorFillerFunc<int>;
109  else if (type == "vector<unsigned int>") fillerFunc = vectorFillerFunc<unsigned int>;
110  else if (type == "vector<float>") fillerFunc = vectorFillerFunc<float>;
111  else if (type == "vector<double>") fillerFunc = vectorFillerFunc<double>;
112  else if (type == "vector<long>") fillerFunc = vectorFillerFunc<long>;
113  else if (type == "vector<unsigned long>") fillerFunc = vectorFillerFunc<unsigned long>;
114  else if (type == "vector<string>") fillerFunc = vectorFillerFunc<std::string>;
115  else if (type == "C") {
116  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
117  log << MSG::ERROR << "Tree " << m_histDef->alias << ": Branch type \"C\" not supported for branch" << branch
118  << "; please use \"string\"" << endmsg;
119  type = "IGNORE";
120  } else {
121  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
122  log << MSG::ERROR << "Tree " << m_histDef->alias << ": Unrecognized branch type " << type << " for branch " << branch
123  << "; ignoring branch " << endmsg;
124  type = "IGNORE";
125  }
126  m_branchDefs.emplace_back(branch, type);
127  m_fillerFunctions.push_back(fillerFunc);
128  }
129  }
130 
131  template <typename T>
132  void branchHelper(TTree* tree, const std::pair<std::string, std::string>& brdef) const {
133  std::vector<T> dummy;
134  std::vector<T>* dummyptr = &dummy;
135  // we only need dummy object to exist through the end of this method
136  tree->Branch(brdef.first.c_str(), &dummyptr);
137  }
138 
139  void createBranches(TTree* tree) const {
140  for (const auto& brdef : m_branchDefs) {
141  if (brdef.second == "vector<char>") branchHelper<char>(tree, brdef);
142  else if (brdef.second == "vector<unsigned char>") branchHelper<unsigned char>(tree, brdef);
143  else if (brdef.second == "vector<int>") branchHelper<int>(tree, brdef);
144  else if (brdef.second == "vector<unsigned int>") branchHelper<unsigned int>(tree, brdef);
145  else if (brdef.second == "vector<float>") branchHelper<float>(tree, brdef);
146  else if (brdef.second == "vector<double>") branchHelper<double>(tree, brdef);
147  else if (brdef.second == "vector<long>") branchHelper<long>(tree, brdef);
148  else if (brdef.second == "vector<unsigned long>") branchHelper<unsigned long>(tree, brdef);
149  else if (brdef.second == "vector<string>") branchHelper<std::string>(tree, brdef);
150  else if (brdef.second == "string") {
151  std::string dummy; tree->Branch(brdef.first.c_str(), &dummy);
152  } else if (brdef.second != "IGNORE") {
153  tree->Branch(brdef.first.c_str(), (void*) nullptr, (brdef.first+"/"+brdef.second).c_str());
154  }
155  }
156  }
157  };
158 
159  // helper functions for filling branches
160  template <typename T>
162  if (ATH_UNLIKELY(var.size() == 0)) {
163  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
164  log << MSG::WARNING << "Tree " << branch->GetTree()->GetName() << ": Empty value passed to scalar branch fill for"
165  << branch->GetName() << endmsg;
166  return;
167  }
168  T tofill = var.get(0);
169  T* ptr = &tofill;
170  branch->SetAddress(ptr);
171  branch->Fill();
172  }
173 
174  // specialization for string
175  template <>
176  void scalarFillerFunc<std::string>(TBranch* branch, const IMonitoredVariable& var) {
177  if (ATH_UNLIKELY(var.size() == 0)) {
178  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
179  log << MSG::WARNING << "Tree " << branch->GetTree()->GetName() << ": Empty value passed to scalar branch fill for"
180  << branch->GetName() << endmsg;
181  return;
182  }
183  std::string tofill{var.getString(0)};
184  branch->SetObject(&tofill);
185  //*static_cast<std::string*>(branch->GetObject()) = tofill;
186  branch->Fill();
187  }
188 
189  template <typename T>
191  std::vector<T> tofill;
192  tofill.reserve(var.size());
193  for (size_t i = 0; i < var.size(); i++) {
194  tofill.push_back(var.get(i));
195  }
196  std::vector<T>* tofillptr = &tofill;
197  branch->SetAddress(&tofillptr);
198  branch->Fill();
199  }
200 
201  // specialization for string
202  template <>
203  void vectorFillerFunc<std::string>(TBranch* branch, const IMonitoredVariable& var) {
204  std::vector<std::string> tofill;
205  tofill.reserve(var.size());
206  for (size_t i = 0; i < var.size(); i++) {
207  tofill.push_back(var.getString(i));
208  }
209  auto* tofillptr = &tofill;
210  branch->SetAddress(&tofillptr);
211  branch->Fill();
212  }
213 }
214 
215 #endif // AthenaMonitoringKernel_HistogramFiller_HistogramFillerTree_h
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
Monitored::HistogramFiller::m_histDef
std::shared_ptr< HistogramDef > m_histDef
Definition: HistogramFiller.h:196
tree
TChain * tree
Definition: tile_monitor.h:30
Monitored::HistogramFiller::VariablesPack::cut
const Monitored::IMonitoredVariable * cut
pointer to cut mask variable, typically absent
Definition: HistogramFiller.h:115
Monitored::HistogramFillerTree
Filler for TTrees.
Definition: HistogramFillerTree.h:22
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
Monitored::HistogramDef
the internal class used to keep parsed Filler properties
Definition: HistogramDef.h:15
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
Monitored::vectorFillerFunc
void vectorFillerFunc(TBranch *branch, const IMonitoredVariable &var)
Definition: HistogramFillerTree.h:190
Monitored::HistogramFiller::VariablesPack
helper class to pass variables to fillers
Definition: HistogramFiller.h:71
Monitored::IMonitoredVariable
Definition: IMonitoredVariable.h:14
Monitored::HistogramFillerTree::fill
virtual unsigned fill(const HistogramFiller::VariablesPack &vars) const override
Method that actually fills the ROOT object.
Definition: HistogramFillerTree.h:29
lumiFormat.i
int i
Definition: lumiFormat.py:92
Monitored
Generic monitoring tool for athena components.
Definition: GenericMonitoringTool.h:30
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
Monitored::HistogramFiller::VariablesPack::size
size_t size() const
number of variables in the pack ( not counting the weight and mask )
Definition: HistogramFiller.h:79
Monitored::HistogramFillerTree::m_fillerFunctions
std::vector< std::function< void(TBranch *, const IMonitoredVariable &)> > m_fillerFunctions
Definition: HistogramFillerTree.h:75
Monitored::HistogramFillerTree::HistogramFillerTree
HistogramFillerTree(const HistogramDef &definition, std::shared_ptr< IHistogramProvider > provider)
Definition: HistogramFillerTree.h:24
Monitored::HistogramFillerTree::m_branchDefs
std::vector< std::pair< std::string, std::string > > m_branchDefs
Definition: HistogramFillerTree.h:74
python.xAODType.dummy
dummy
Definition: xAODType.py:4
Monitored::scalarFillerFunc
void scalarFillerFunc(TBranch *branch, const IMonitoredVariable &var)
Definition: HistogramFillerTree.h:161
Monitored::HistogramFillerTree::branchHelper
void branchHelper(TTree *tree, const std::pair< std::string, std::string > &brdef) const
Definition: HistogramFillerTree.h:132
HistogramFiller.h
Monitored::HistogramFiller
Base class for all histogram fillers.
Definition: HistogramFiller.h:43
Monitored::HistogramFillerTree::parseDefinition
void parseDefinition()
Definition: HistogramFillerTree.h:77
Monitored::HistogramFiller::VariablesPack::var
std::vector< const Monitored::IMonitoredVariable * > var
storage for variables, default size of 4, serves all histograming uses
Definition: HistogramFiller.h:113
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
Monitored::HistogramFiller::getCutMaskFunc
std::pair< size_t, std::function< bool(size_t)> > getCutMaskFunc(const Monitored::IMonitoredVariable *mask) const
Definition: HistogramFiller.h:155
Monitored::HistogramFillerTree::createBranches
void createBranches(TTree *tree) const
Definition: HistogramFillerTree.h:139
python.compressB64.c
def c
Definition: compressB64.py:93
Trk::split
@ split
Definition: LayerMaterialProperties.h:38