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
 

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, std::move(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 134 of file HistogramFillerTree.h.

134  {
135  std::vector<T> dummy;
136  std::vector<T>* dummyptr = &dummy;
137  // we only need dummy object to exist through the end of this method
138  tree->Branch(brdef.first.c_str(), &dummyptr);
139  }

◆ createBranches()

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

Definition at line 141 of file HistogramFillerTree.h.

141  {
142  for (const auto& brdef : m_branchDefs) {
143  if (brdef.second == "vector<char>") branchHelper<char>(tree, brdef);
144  else if (brdef.second == "vector<unsigned char>") branchHelper<unsigned char>(tree, brdef);
145  else if (brdef.second == "vector<int>") branchHelper<int>(tree, brdef);
146  else if (brdef.second == "vector<unsigned int>") branchHelper<unsigned int>(tree, brdef);
147  else if (brdef.second == "vector<float>") branchHelper<float>(tree, brdef);
148  else if (brdef.second == "vector<double>") branchHelper<double>(tree, brdef);
149  else if (brdef.second == "vector<long>") branchHelper<long>(tree, brdef);
150  else if (brdef.second == "vector<unsigned long>") branchHelper<unsigned long>(tree, brdef);
151  else if (brdef.second == "vector<string>") branchHelper<std::string>(tree, brdef);
152  else if (brdef.second == "string") {
153  std::string dummy; tree->Branch(brdef.first.c_str(), &dummy);
154  } else if (brdef.second != "IGNORE") {
155  tree->Branch(brdef.first.c_str(), (void*) nullptr, (brdef.first+"/"+brdef.second).c_str());
156  }
157  }
158  }

◆ 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 
31  if (vars.cut) {
32  const size_t maskSize = vars.cut->size();
33  // Abort if no cut entries or first (and only) entry is false
34  if (maskSize == 0 || (maskSize == 1 && !vars.cut->get(0))) { return 0; }
35 
36  if (ATH_UNLIKELY(maskSize > 1)) {
37  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
38  log << MSG::WARNING << "HistogramFillerTree (" << m_histDef->alias
39  << ") does not support more than a single entry being filled at a time\n"
40  << "so a cut mask with > 1 entry doesn't make sense. Using first entry only." << endmsg;
41  if (!vars.cut->get(0)) { return 0; }
42  }
43  }
44 
45  if (ATH_UNLIKELY(vars.size() != m_branchDefs.size())) {
46  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
47  log << MSG::ERROR << "Mismatch of passed variables and expected variables for " << m_histDef->alias
48  << "(" << vars.size() << ", " << m_branchDefs.size() << ")" << endmsg;
49  return 0;
50  }
51 
52  auto tree = this->histogram<TTree>();
53  if (tree->GetListOfBranches()->GetEntries() == 0) {
55  }
56  auto branchList = tree->GetListOfBranches();
57  // following logic allows us to skip branches that were badly-defined
58  size_t idx = 0, idxgood = 0;
59  for (const auto& brdef : m_branchDefs) {
60  if (ATH_UNLIKELY(brdef.second == "IGNORE")) {
61  ++idx; continue;
62  }
63  TBranch* branch = static_cast<TBranch*>(branchList->At(idxgood));
64  m_fillerFunctions[idx](branch, *vars[idx]);
65  ++idx; ++idxgood;
66  }
67  for (Int_t i = 0; i < branchList->GetEntries(); ++i) {
68 
69 
70  }
71  tree->SetEntries(tree->GetEntries() + 1);
72  return 1;
73  }

◆ 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 168 of file HistogramFiller.h.

168  {
169  auto hist = this->histogram<H>();
170 
171  size_t i = 0;
172  for (; i < std::max({m.size()...}); ++i ) {
173  if ( cut(i) ) {
174  detail::doFill(hist, weight, i, m...);
175  }
176  }
177  return i;
178  }

◆ getLock()

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

Definition at line 147 of file HistogramFiller.h.

147  {
148  return std::unique_lock(m_lock);
149  }

◆ histogram()

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

Definition at line 153 of file HistogramFiller.h.

153  {
154  return static_cast<H*>(m_histogramProvider->histogram());
155  }

◆ histogramCutMaskName()

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

Definition at line 143 of file HistogramFiller.h.

143  {
144  return m_histDef->cutMask;
145  }

◆ histogramVariablesNames()

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

Definition at line 135 of file HistogramFiller.h.

135  {
136  return m_histDef->name;
137  }

◆ histogramWeightName()

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

Definition at line 139 of file HistogramFiller.h.

139  {
140  return m_histDef->weight;
141  }

◆ parseDefinition()

void Monitored::HistogramFillerTree::parseDefinition ( )
inlineprivate

Definition at line 79 of file HistogramFillerTree.h.

79  {
80  std::vector<std::string> tokenized;
81  boost::split(tokenized, m_histDef->treeDef, [](char c){ return c ==':'; });
82  for (const auto& token : tokenized) {
83  auto ipart = token.find('/');
84  if (ipart == std::string::npos) {
85  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
86  log << MSG::ERROR << "Tree " << m_histDef->alias << ": Badly formed variable definition " << token
87  << "; skipping" << endmsg;
88  continue;
89  }
90  auto branch = token.substr(0, ipart);
91  auto type = token.substr(ipart+1);
92  std::function<void(TBranch*, const IMonitoredVariable&)> fillerFunc;
93  // scalar
94  if (type == "B") fillerFunc = scalarFillerFunc<Char_t>;
95  else if (type == "b") fillerFunc = scalarFillerFunc<UChar_t>;
96  else if (type == "S") fillerFunc = scalarFillerFunc<Short_t>;
97  else if (type == "s") fillerFunc = scalarFillerFunc<UShort_t>;
98  else if (type == "I") fillerFunc = scalarFillerFunc<Int_t>;
99  else if (type == "i") fillerFunc = scalarFillerFunc<UInt_t>;
100  else if (type == "F") fillerFunc = scalarFillerFunc<Float_t>;
101  else if (type == "f") fillerFunc = scalarFillerFunc<Float16_t>;
102  else if (type == "D") fillerFunc = scalarFillerFunc<Double_t>;
103  else if (type == "d") fillerFunc = scalarFillerFunc<Double32_t>;
104  else if (type == "L") fillerFunc = scalarFillerFunc<Long64_t>;
105  else if (type == "l") fillerFunc = scalarFillerFunc<ULong64_t>;
106  else if (type == "O") fillerFunc = scalarFillerFunc<Bool_t>;
107  else if (type == "string") fillerFunc = scalarFillerFunc<std::string>;
108  else if (type == "vector<char>") fillerFunc = vectorFillerFunc<char>;
109  else if (type == "vector<unsigned char>") fillerFunc = vectorFillerFunc<unsigned char>;
110  else if (type == "vector<int>") fillerFunc = vectorFillerFunc<int>;
111  else if (type == "vector<unsigned int>") fillerFunc = vectorFillerFunc<unsigned int>;
112  else if (type == "vector<float>") fillerFunc = vectorFillerFunc<float>;
113  else if (type == "vector<double>") fillerFunc = vectorFillerFunc<double>;
114  else if (type == "vector<long>") fillerFunc = vectorFillerFunc<long>;
115  else if (type == "vector<unsigned long>") fillerFunc = vectorFillerFunc<unsigned long>;
116  else if (type == "vector<string>") fillerFunc = vectorFillerFunc<std::string>;
117  else if (type == "C") {
118  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
119  log << MSG::ERROR << "Tree " << m_histDef->alias << ": Branch type \"C\" not supported for branch" << branch
120  << "; please use \"string\"" << endmsg;
121  type = "IGNORE";
122  } else {
123  MsgStream log(Athena::getMessageSvc(), "HistogramFillerTree");
124  log << MSG::ERROR << "Tree " << m_histDef->alias << ": Unrecognized branch type " << type << " for branch " << branch
125  << "; ignoring branch " << endmsg;
126  type = "IGNORE";
127  }
128  m_branchDefs.emplace_back(branch, type);
129  m_fillerFunctions.push_back(std::move(fillerFunc));
130  }
131  }

◆ touch()

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

Ensure histogram exists.

Definition at line 130 of file HistogramFiller.h.

130  {
131  histogram<void>();
132  }

Member Data Documentation

◆ m_branchDefs

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

Definition at line 76 of file HistogramFillerTree.h.

◆ m_fillerFunctions

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

Definition at line 77 of file HistogramFillerTree.h.

◆ m_histDef

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

Definition at line 180 of file HistogramFiller.h.

◆ m_histogramProvider

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

Definition at line 181 of file HistogramFiller.h.

◆ m_lock

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

Definition at line 182 of file HistogramFiller.h.


The documentation for this class was generated from the following file:
Monitored::HistogramFiller::m_histDef
std::shared_ptr< HistogramDef > m_histDef
Definition: HistogramFiller.h:180
Monitored::HistogramFiller::m_histogramProvider
std::shared_ptr< IHistogramProvider > m_histogramProvider
Definition: HistogramFiller.h:181
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
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
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:190
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:85
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:77
CalibDbCompareRT.dummy
dummy
Definition: CalibDbCompareRT.py:59
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:76
Monitored::HistogramFiller::m_lock
std::mutex m_lock
Definition: HistogramFiller.h:182
Monitored::HistogramFillerTree::parseDefinition
void parseDefinition()
Definition: HistogramFillerTree.h:79
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
Monitored::HistogramFillerTree::createBranches
void createBranches(TTree *tree) const
Definition: HistogramFillerTree.h:141
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:51
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.SystemOfUnits.m
float m
Definition: SystemOfUnits.py:106