ATLAS Offline Software
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
Monitored::HistogramFillerTree Class Reference

Filler for TTrees. More...

#include <HistogramFillerTree.h>

Inheritance diagram for Monitored::HistogramFillerTree:
Collaboration diagram for Monitored::HistogramFillerTree:

Public Member Functions

 HistogramFillerTree (const HistogramDef &definition, std::shared_ptr< IHistogramProvider > provider)
 
virtual unsigned fill (const HistogramFiller::VariablesPack &vars) const override
 Method that actually fills the ROOT object. More...
 
void touch () const
 Ensure histogram exists. More...
 
const std::vector< std::string > & histogramVariablesNames () const
 
const std::string & histogramWeightName () const
 
const std::string & histogramCutMaskName () const
 
const std::unique_lock< std::mutex > getLock () const
 

Protected Member Functions

template<class H , typename W , typename C , typename ... Ms>
unsigned fill (W weight, C cut, const Ms &... m) const
 Fill histogram from IMonitoredVariable. More...
 
template<class H >
H * histogram () const
 
std::pair< size_t, std::function< bool(size_t)> > getCutMaskFunc (const Monitored::IMonitoredVariable *mask) const
 

Protected Attributes

std::shared_ptr< HistogramDefm_histDef
 
std::shared_ptr< IHistogramProviderm_histogramProvider
 
std::mutex m_lock
 

Private Member Functions

void parseDefinition ()
 
template<typename T >
void branchHelper (TTree *tree, const std::pair< std::string, std::string > &brdef) const
 
void createBranches (TTree *tree) const
 

Private Attributes

std::vector< std::pair< std::string, std::string > > m_branchDefs
 
std::vector< std::function< void(TBranch *, const IMonitoredVariable &)> > m_fillerFunctions
 

Detailed Description

Filler for TTrees.

Definition at line 22 of file HistogramFillerTree.h.

Constructor & Destructor Documentation

◆ HistogramFillerTree()

Monitored::HistogramFillerTree::HistogramFillerTree ( const HistogramDef definition,
std::shared_ptr< IHistogramProvider provider 
)
inline

Definition at line 24 of file HistogramFillerTree.h.

25  : HistogramFiller(definition, provider) {
27  }

Member Function Documentation

◆ branchHelper()

template<typename T >
void Monitored::HistogramFillerTree::branchHelper ( TTree *  tree,
const std::pair< std::string, std::string > &  brdef 
) const
inlineprivate

Definition at line 132 of file HistogramFillerTree.h.

132  {
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  }

◆ createBranches()

void Monitored::HistogramFillerTree::createBranches ( TTree *  tree) const
inlineprivate

Definition at line 139 of file HistogramFillerTree.h.

139  {
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  }

◆ fill() [1/2]

virtual unsigned Monitored::HistogramFillerTree::fill ( const HistogramFiller::VariablesPack ) const
inlineoverridevirtual

Method that actually fills the ROOT object.

Returns
number of fills performed

Implements Monitored::HistogramFiller.

Definition at line 29 of file HistogramFillerTree.h.

29  {
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));
62  m_fillerFunctions[idx](branch, *vars.var[idx]);
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  }

◆ fill() [2/2]

template<class H , typename W , typename C , typename ... Ms>
unsigned Monitored::HistogramFiller::fill ( weight,
cut,
const Ms &...  m 
) const
inlineprotectedinherited

Fill histogram from IMonitoredVariable.

Supports arbitrary dimensions and double/string representation.

Template Parameters
Hhistogram type (TH1, TH2, ...)
Parameters
weightweight accessor (use detail::noWeight if not needed)
cutcut mask accessor (use detail::noCut if not needed)
m...IMonitoredVariable list to fill from

Definition at line 184 of file HistogramFiller.h.

184  {
185  auto hist = this->histogram<H>();
186 
187  size_t i = 0;
188  for (; i < std::max({m.size()...}); ++i ) {
189  if ( cut(i) ) {
190  detail::doFill(hist, weight, i, m...);
191  }
192  }
193  return i;
194  }

◆ getCutMaskFunc()

std::pair<size_t, std::function<bool(size_t)> > Monitored::HistogramFiller::getCutMaskFunc ( const Monitored::IMonitoredVariable mask) const
inlineprotectedinherited

Definition at line 155 of file HistogramFiller.h.

155  {
156  std::function<bool(size_t)> cutMaskValue = [] (size_t){ return true; }; // default is true
157  size_t maskSize = 1;
158  if ( mask != nullptr ) {
159  maskSize = mask->size();
160  if (maskSize == 1) {
161  if (!mask->get(0)) {
162  // globally fails cut; zero first argument is a signal that one can abort
163  return std::make_pair(0, [](size_t){ return false; });
164  // otherwise, default cutMaskValue is sufficient
165  }
166  } else {
167  return std::make_pair(maskSize, [mask](size_t i){ return static_cast<bool>(mask->get(i)); });
168  }
169  }
170  return std::make_pair(maskSize, cutMaskValue);
171  }

◆ getLock()

const std::unique_lock<std::mutex> Monitored::HistogramFiller::getLock ( ) const
inlineinherited

Definition at line 144 of file HistogramFiller.h.

144  {
145  return std::unique_lock(m_lock);
146  }

◆ histogram()

template<class H >
H* Monitored::HistogramFiller::histogram ( ) const
inlineprotectedinherited

Definition at line 150 of file HistogramFiller.h.

150  {
151  return static_cast<H*>(m_histogramProvider->histogram());
152  }

◆ histogramCutMaskName()

const std::string& Monitored::HistogramFiller::histogramCutMaskName ( ) const
inlineinherited

Definition at line 140 of file HistogramFiller.h.

140  {
141  return m_histDef->cutMask;
142  }

◆ histogramVariablesNames()

const std::vector<std::string>& Monitored::HistogramFiller::histogramVariablesNames ( ) const
inlineinherited

Definition at line 132 of file HistogramFiller.h.

132  {
133  return m_histDef->name;
134  }

◆ histogramWeightName()

const std::string& Monitored::HistogramFiller::histogramWeightName ( ) const
inlineinherited

Definition at line 136 of file HistogramFiller.h.

136  {
137  return m_histDef->weight;
138  }

◆ parseDefinition()

void Monitored::HistogramFillerTree::parseDefinition ( )
inlineprivate

Definition at line 77 of file HistogramFillerTree.h.

77  {
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  }

◆ touch()

void Monitored::HistogramFiller::touch ( ) const
inlineinherited

Ensure histogram exists.

Definition at line 127 of file HistogramFiller.h.

127  {
128  histogram<void>();
129  }

Member Data Documentation

◆ m_branchDefs

std::vector<std::pair<std::string, std::string> > Monitored::HistogramFillerTree::m_branchDefs
private

Definition at line 74 of file HistogramFillerTree.h.

◆ m_fillerFunctions

std::vector<std::function<void(TBranch*, const IMonitoredVariable&)> > Monitored::HistogramFillerTree::m_fillerFunctions
private

Definition at line 75 of file HistogramFillerTree.h.

◆ m_histDef

std::shared_ptr<HistogramDef> Monitored::HistogramFiller::m_histDef
protectedinherited

Definition at line 196 of file HistogramFiller.h.

◆ m_histogramProvider

std::shared_ptr<IHistogramProvider> Monitored::HistogramFiller::m_histogramProvider
protectedinherited

Definition at line 197 of file HistogramFiller.h.

◆ m_lock

std::mutex Monitored::HistogramFiller::m_lock
mutableprotectedinherited

Definition at line 198 of file HistogramFiller.h.


The documentation for this class was generated from the following file:
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
max
#define max(a, b)
Definition: cfImp.cxx:41
Monitored::HistogramFiller::m_histDef
std::shared_ptr< HistogramDef > m_histDef
Definition: HistogramFiller.h:196
Monitored::HistogramFiller::m_histogramProvider
std::shared_ptr< IHistogramProvider > m_histogramProvider
Definition: HistogramFiller.h:197
plotmaker.hist
hist
Definition: plotmaker.py:148
tree
TChain * tree
Definition: tile_monitor.h:30
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
Monitored::detail::doFill
void doFill(H *hist, W weight, size_t i, const M &m1, const Ms &... m)
Perform (arbitrary dimension) histogram fill with weight.
Definition: HistogramFillerUtils.h:164
H
#define H(x, y, z)
Definition: MD5.cxx:114
ROBbits::maskSize
const uint32_t maskSize
Definition: TrigMonROBData.cxx:17
lumiFormat.i
int i
Definition: lumiFormat.py:92
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
Monitored::HistogramFillerTree::m_fillerFunctions
std::vector< std::function< void(TBranch *, const IMonitoredVariable &)> > m_fillerFunctions
Definition: HistogramFillerTree.h:75
BindingsTest.cut
cut
This script demonstrates how to call a C++ class from Python Also how to use PyROOT is shown.
Definition: BindingsTest.py:13
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::HistogramFiller::m_lock
std::mutex m_lock
Definition: HistogramFiller.h:198
Monitored::HistogramFillerTree::parseDefinition
void parseDefinition()
Definition: HistogramFillerTree.h:77
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
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
Monitored::HistogramFillerTree::createBranches
void createBranches(TTree *tree) const
Definition: HistogramFillerTree.h:139
python.compressB64.c
def c
Definition: compressB64.py:93
Monitored::HistogramFiller::HistogramFiller
HistogramFiller(const HistogramDef &histDef, std::shared_ptr< IHistogramProvider > histogramProvider)
Default constructor.
Definition: HistogramFiller.h:50
Trk::split
@ split
Definition: LayerMaterialProperties.h:38