ATLAS Offline Software
xAODCutFlowHelpers.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 // This functions header
8 
9 // STL includes
10 #include <string>
11 #include <vector>
12 
13 // ROOT includes
14 #include <TH1.h>
15 
16 // Local include(s):
18 
19 
20 
23  const std::string& histName,
24  int minCycle, int maxCycle ) {
25  // Lets first make some checks
26  if (!cbkCont) return nullptr;
27 
28  // Get the maximum cycle number that we need to worry about
29  const int cbkMaxCycle = cbkCont->maxCycle();
30  if (maxCycle<0) maxCycle = cbkMaxCycle;
31 
32  // Now, find all xAOD::CutBookkeepers in the container that we care about
33  std::vector<const xAOD::CutBookkeeper*> cbks;
34  cbks.reserve(cbkCont->size());
35  for ( const xAOD::CutBookkeeper* cbk : *cbkCont ){
36  const int cycle = cbk->cycle();
37  if ( cycle >= minCycle && cycle <= maxCycle ){ cbks.push_back(cbk); }
38  }
39  const std::size_t nCBK = cbks.size();
40 
41  // Create the histogram:
42  const std::string histTitle = histName + ", minCycle=" + std::to_string(minCycle)
43  + ", maxCycle=" + std::to_string(maxCycle);
44  ::TH1* resultHist = new ::TH1D( histName.c_str(), histTitle.c_str(),
45  nCBK, 0.0, nCBK );
46  // Set some properties of the histogram
47  resultHist->GetXaxis()->SetTitle("CutBookkeeper Name");
48  resultHist->GetYaxis()->SetTitle("nAcceptedEvents");
49 
50  // Now, set the bin content for all bins
51  for (std::size_t i=0; i<nCBK; ++i){
52  const xAOD::CutBookkeeper* cbk = cbks[0];
53  const double nEvents = static_cast<double>(cbk->nAcceptedEvents());
54  const double error = std::sqrt(nEvents);
55  const std::string& name = cbk->name();
56  const int binIdx = static_cast<int>(i+1);
57  resultHist->SetBinContent(binIdx,nEvents);
58  resultHist->SetBinError(binIdx,error);
59  resultHist->GetXaxis()->SetBinLabel(binIdx,name.c_str());
60  }
61 
62  // Done
63  return resultHist;
64 }
65 
66 
68 namespace
69 {
70 
71 xAOD::CutBookkeeper *resolveLink( const xAOD::CutBookkeeper *old,
72  xAOD::CutBookkeeperContainer &contToUpdate,
73  const xAOD::CutBookkeeperContainer &otherCont,
74  const std::vector<size_t> &otherIndexMap )
75 {
76  {
78  std::find(contToUpdate.begin(), contToUpdate.end(), old);
79  if (matchIter != contToUpdate.end()) {
80  return *matchIter;
81  }
82  }
83 
84  {
86  std::find(otherCont.begin(), otherCont.end(), old);
87  if (matchIter != contToUpdate.end()) {
88  std::size_t pos = matchIter - otherCont.begin();
89  return contToUpdate[otherIndexMap[pos]];
90  }
91  }
92 
93  // If we didn't find it, we need to add it
94  xAOD::CutBookkeeper *newEBK = new xAOD::CutBookkeeper(); // will be owned by the container
95  if (newEBK->usingPrivateStore()) { newEBK->releasePrivateStore(); }
96  newEBK->makePrivateStore(old);
97  contToUpdate.push_back(newEBK);
98  return newEBK;
99 }
100 
101 } // unnamed namespace
102 
103 
106  const xAOD::CutBookkeeperContainer *otherCont )
107 {
108  std::size_t origSize = contToUpdate->size();
109 
110  // Vector of indices in contToUpdate of elements in otherCont.
111  std::vector<std::size_t> otherIndexMap(otherCont->size());
112  // Loop through otherCont.
113  // If element already in contToUpdate, update event counts, otherwise create new element
114  for (std::size_t i = 0; i < otherCont->size(); ++i) {
115  const xAOD::CutBookkeeper *otherEBK = otherCont->at(i);
116 
117  // Loop through the container to be updated (contToUpdate) and see if we find a match
118  bool foundEBKToUpdate{false};
119  for (std::size_t j = 0; j < contToUpdate->size(); ++j) {
120  xAOD::CutBookkeeper *ebkToUpdate = contToUpdate->at(j);
121  // Check if they are identical, if so, update; else add otherEBK
122  if (otherEBK->isEqualTo(ebkToUpdate)) {
123  ebkToUpdate->setPayload(ebkToUpdate->payload() + otherEBK->payload());
124  otherIndexMap[i] = j;
125  foundEBKToUpdate = true;
126  break;
127  }
128  } // End: Inner loop over contToUpdate
129  if (!foundEBKToUpdate) {
130  xAOD::CutBookkeeper *newEBK = new xAOD::CutBookkeeper(); // will be owned by the container
131  if (newEBK->usingPrivateStore()) { newEBK->releasePrivateStore(); }
132  newEBK->makePrivateStore(otherEBK);
133  contToUpdate->push_back(newEBK);
134  std::size_t ebIdx = newEBK->index();
135  otherIndexMap[i] = ebIdx;
136  }
137  } // End: Outer loop over contToUpdate
138 
139  // Now, we still need to fix the cross-referencing of the newly added CutBookkkeepers
140  for (std::size_t i = origSize; i < contToUpdate->size(); ++i) {
141  xAOD::CutBookkeeper *ebkToModify = contToUpdate->at(i);
142 
143  // Parent check
144  if (ebkToModify->hasParent()) {
145  const xAOD::CutBookkeeper *oldParent = ebkToModify->parent();
146  const xAOD::CutBookkeeper *newParent = resolveLink (oldParent,
147  *contToUpdate,
148  *otherCont,
149  otherIndexMap);
150  ebkToModify->setParent (newParent);
151  } // Done fixing parent
152 
153  // Children check
154  std::vector<xAOD::CutBookkeeper *> newChildren;
155  for ( std::size_t oldIdx = 0; oldIdx < ebkToModify->nChildren(); ++oldIdx) {
156  const xAOD::CutBookkeeper *oldEBK = ebkToModify->child(oldIdx);
157  newChildren.push_back(resolveLink(oldEBK,
158  *contToUpdate,
159  *otherCont,
160  otherIndexMap));
161  } // Done fixing children
162  ebkToModify->setChildren(newChildren);
163 
164  // Used others check
165  std::vector<xAOD::CutBookkeeper *> newOthers;
166  for (std::size_t oldIdx = 0; oldIdx < ebkToModify->nUsedOthers(); ++oldIdx) {
167  const xAOD::CutBookkeeper *oldEBK = ebkToModify->usedOther(oldIdx);
168  newOthers.push_back(resolveLink(oldEBK,
169  *contToUpdate,
170  *otherCont,
171  otherIndexMap));
172  } // Done fixing used others
173  ebkToModify->setUsedOthers(newOthers);
174 
175  // Siblings check
176  std::vector<xAOD::CutBookkeeper*> newSiblings;
177  for (std::size_t oldIdx = 0; oldIdx < ebkToModify->nSiblings(); ++oldIdx) {
178  const xAOD::CutBookkeeper *oldEBK = ebkToModify->sibling(oldIdx);
179  newSiblings.push_back(resolveLink(oldEBK,
180  *contToUpdate,
181  *otherCont,
182  otherIndexMap));
183  } // Done fixing siblings
184  ebkToModify->setSiblings(newSiblings);
185  } // Done fixing all cross references
186 }
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
AddEmptyComponent.histName
string histName
Definition: AddEmptyComponent.py:64
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
xAOD::CutBookkeeper_v1::sibling
const xAOD::CutBookkeeper_v1 * sibling(std::size_t i) const
Get the sibling number i.
Definition: CutBookkeeper_v1.cxx:608
xAOD::CutBookkeeper_v1
Description of the class that is used to keep track of event counts.
Definition: CutBookkeeper_v1.h:29
cycle
double cycle(double a, double b)
Definition: SiHitCollectionCnv_p2.cxx:38
xAOD::CutBookkeeper_v1::nUsedOthers
std::size_t nUsedOthers() const
Check if this CutBookkeeper has used others.
Definition: CutBookkeeper_v1.cxx:500
xAOD::CutBookkeeper_v1::setUsedOthers
void setUsedOthers(const std::vector< CutBookkeeper_v1 * > &usedOthers)
Set all CutBookkeeper that are used by this one in one go.
Definition: CutBookkeeper_v1.cxx:535
xAOD::CutBookkeeper_v1::payload
Payload payload() const
Get the whole payload object (which contains all counters) in one go.
Definition: CutBookkeeper_v1.cxx:112
xAOD::CutBookkeeper_v1::nSiblings
std::size_t nSiblings() const
Check if this CutBookkeeper has siblings.
Definition: CutBookkeeper_v1.cxx:587
xAOD::CutBookkeeper_v1::hasParent
bool hasParent() const
Check if there is a parent CutBookkeeper of this CutBookkeeper.
Definition: CutBookkeeper_v1.cxx:359
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::AuxElement::usingPrivateStore
bool usingPrivateStore() const
Test to see if this object is currently using a private store.
Definition: AuxElement.cxx:260
xAOD::CutFlowHelpers::nAcceptedEvents
::TH1 * nAcceptedEvents(const xAOD::CutBookkeeperContainer *cbkCont, const std::string &histName, int minCycle=0, int maxCycle=-1)
Make a histogram of the number of accepted events from a container.
Definition: xAODCutFlowHelpers.cxx:22
xAOD::CutBookkeeperContainer_v1::maxCycle
int maxCycle() const
Get the maximum cycle number of any xAOD::CutBookkeepers in the container.
Definition: CutBookkeeperContainer_v1.cxx:200
SG::AuxElement::releasePrivateStore
void releasePrivateStore()
Release and free any private store associated with this object.
Definition: AuxElement.cxx:190
CutBookkeeperContainer.h
xAOD::CutBookkeeper_v1::setParent
void setParent(const CutBookkeeper_v1 *parentEB)
Set the parent CutBookkeeper of this CutBookkeeper.
Definition: CutBookkeeper_v1.cxx:372
nEvents
int nEvents
Definition: fbtTestBasics.cxx:79
xAOD::CutBookkeeperContainer_v1
Container that holds the Container of all CutBookkeepers.
Definition: CutBookkeeperContainer_v1.h:27
xAOD::CutBookkeeper_v1::setChildren
void setChildren(const std::vector< CutBookkeeper_v1 * > &childrenEB)
Set all children of this CutBookkeeper in one go.
Definition: CutBookkeeper_v1.cxx:422
xAOD::CutFlowHelpers::updateContainer
void updateContainer(xAOD::CutBookkeeperContainer *contToUpdate, const xAOD::CutBookkeeperContainer *otherCont)
Helper function to update a container with information from another one.
Definition: xAODCutFlowHelpers.cxx:105
xAOD::CutBookkeeper_v1::isEqualTo
bool isEqualTo(const CutBookkeeper_v1 *eb) const
Test for the equality of this CutBookkeeper with another one.
Definition: CutBookkeeper_v1.cxx:348
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
xAOD::CutBookkeeper_v1::nAcceptedEvents
uint64_t nAcceptedEvents() const
Get the number of accepted events that this CutBookkeeper has seen.
Definition: CutBookkeeper_v1.cxx:291
xAOD::CutBookkeeper_v1::child
const xAOD::CutBookkeeper_v1 * child(std::size_t i) const
Get the child at position i.
Definition: CutBookkeeper_v1.cxx:408
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
xAOD::CutBookkeeper
CutBookkeeper_v1 CutBookkeeper
Define the latest version of the CutBookkeeper class.
Definition: CutBookkeeper.h:17
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::AuxElement::makePrivateStore
void makePrivateStore()
Create a new (empty) private store for this object.
Definition: AuxElement.cxx:172
xAOD::CutBookkeeper_v1::name
const std::string & name() const
Get the name of this CutBookkeeper.
Definition: CutBookkeeper_v1.cxx:156
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
xAOD::CutBookkeeper_v1::usedOther
const xAOD::CutBookkeeper_v1 * usedOther(std::size_t i) const
Get the usedOther at position i.
Definition: CutBookkeeper_v1.cxx:521
xAODCutFlowHelpers.h
CSV_InDetExporter.old
old
Definition: CSV_InDetExporter.py:145
xAOD::CutBookkeeper_v1::setPayload
void setPayload(const Payload &payload)
Set the whole payload object (which contains all counters) in one go.
Definition: CutBookkeeper_v1.cxx:121
xAOD::CutBookkeeper_v1::nChildren
std::size_t nChildren() const
Get the number of children CutBookkeepers of this CutBookkeeper.
Definition: CutBookkeeper_v1.cxx:387
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
xAOD::CutBookkeeper_v1::setSiblings
void setSiblings(const std::vector< CutBookkeeper_v1 * > &siblings)
Set all CutBookkeeper that are siblings to this one in one go.
Definition: CutBookkeeper_v1.cxx:622
error
Definition: IImpactPoint3dEstimator.h:70
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
xAOD::CutBookkeeper_v1::parent
const xAOD::CutBookkeeper_v1 * parent() const
Get the parent CutBookkeeper.
Definition: CutBookkeeper_v1.cxx:364