CMS 3D CMS Logo

ThinnedAssociationsHelper.cc
Go to the documentation of this file.
4 #include <algorithm>
5 
6 #include <fmt/format.h>
7 
8 namespace edm {
9 
11 
13  BranchID const& association,
14  BranchID const& thinned,
15  bool slimmed)
16  : parent_(parent), association_(association), thinned_(thinned), slimmed_(slimmed) {}
17 
19 
20  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::begin() const {
21  return vThinnedAssociationBranches_.begin();
22  }
23 
24  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::end() const {
25  return vThinnedAssociationBranches_.end();
26  }
27 
28  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::parentBegin(
29  BranchID const& parent) const {
33  target,
35  return x.parent() < y.parent();
36  });
37  }
38 
39  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::parentEnd(
40  BranchID const& parent) const {
44  target,
46  return x.parent() < y.parent();
47  });
48  }
49 
50  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::lower_bound(
51  ThinnedAssociationBranches const& branches) const {
52  return std::lower_bound(
55  branches,
57  return x.parent() < y.parent() ? true : y.parent() < x.parent() ? false : x.association() < y.association();
58  });
59  }
60 
62  BranchID const& association,
63  BranchID const& thinned,
64  bool slimmed) {
66  }
67 
69  vThinnedAssociationBranches_.insert(lower_bound(branches), branches);
70  if (branches.isSlimmed()) {
71  try {
73  } catch (edm::Exception& ex) {
74  ex.addContext("Calling ThinnedAssociationsHelper::addAssociation()");
75  ex.addAdditionalInfo(fmt::format("When adding a slimmed collection with BranchID {}", branches.thinned().id()));
76  throw ex;
77  }
78  }
79  }
80 } // namespace edm
81 
82 namespace {
83  struct SlimmedCount {
85  int slimmedChildrenCount;
86  };
87 
88  int countSlimmingChildren(edm::ThinnedAssociationsHelper const& helper,
89  edm::BranchID const& parent,
90  std::vector<SlimmedCount> const& counts);
91  void addSlimmingChildrenCount(edm::ThinnedAssociationsHelper const& helper,
92  edm::ThinnedAssociationBranches const& branches,
93  std::vector<SlimmedCount> const& counts,
94  int& slimmedCount) {
95  int slimmingChildren = countSlimmingChildren(helper, branches.thinned(), counts);
96  if (slimmingChildren > 1) {
98  << "Encountered a parent collection with BranchID " << branches.thinned().id()
99  << " that has more than one thinned children that are either themselves slimmed, or have further thinned "
100  "children that are slimmed. This is not allowed, but this particular check should not fire (it should "
101  "have been caught earlier). Please contact framework developers.";
102  }
103 
104  if (slimmingChildren == 0 and branches.isSlimmed()) {
105  ++slimmingChildren;
106  }
107 
108  slimmedCount += slimmingChildren;
109  if (slimmedCount > 1) {
111  << "Encountered a parent collection with BranchID " << branches.parent().id()
112  << " that has more than one thinned children that are either themselves slimmed, or have further thinned "
113  "children that are slimmed. This is not allowed. In the thinning parentage tree, any parent may have "
114  "slimmed collections in at most one path to any leaf thinned collections of that parent.";
115  }
116  }
117 
118  int countSlimmingChildren(edm::ThinnedAssociationsHelper const& helper,
119  edm::BranchID const& parent,
120  std::vector<SlimmedCount> const& counts) {
121  auto begin = helper.parentBegin(parent);
122  auto end = helper.parentEnd(parent);
123  if (begin == end)
124  return 0;
125 
126  // if already visited, can just return the count
127  auto pos =
128  std::lower_bound(counts.begin(), counts.end(), parent, [](SlimmedCount const& c, edm::BranchID const& b) {
129  return c.parent < b;
130  });
131  if (pos != counts.end() && pos->parent == parent) {
132  return pos->slimmedChildrenCount;
133  }
134 
135  int slimmedCount = 0;
136  for (auto iItem = begin; iItem != end; ++iItem) {
137  addSlimmingChildrenCount(helper, *iItem, counts, slimmedCount);
138  }
139  return slimmedCount;
140  }
141 } // namespace
142 
143 namespace edm {
145  // ThinnedAssociationBranches defines an edge between a parent and
146  // a thinned collection in the parentage tree of the thinned
147  // collections. It is required that for a given parentage tree
148  // there is at most one path from the root node to any leaf nodes
149  // that has slimming edges.
150 
151  std::vector<::SlimmedCount> counts;
152  BranchID prevParent;
153  std::vector<SlimmedCount>::iterator currentCount;
154  for (auto iItem = begin(), iEnd = end(); iItem != iEnd; ++iItem) {
155  if (iItem->parent() == BranchID()) {
156  continue;
157  }
158  if (iItem->parent() == prevParent) {
159  addSlimmingChildrenCount(*this, *iItem, counts, currentCount->slimmedChildrenCount);
160  } else {
161  currentCount = std::lower_bound(
162  counts.begin(), counts.end(), iItem->parent(), [](SlimmedCount const& c, BranchID const& b) {
163  return c.parent < b;
164  });
165  // has the tree with iItem->parent() as root node already been counted?
166  if (currentCount != counts.end() && currentCount->parent == iItem->parent()) {
167  continue;
168  }
169  currentCount = counts.insert(currentCount, ::SlimmedCount{iItem->parent(), 0});
170  addSlimmingChildrenCount(*this, *iItem, counts, currentCount->slimmedChildrenCount);
171  prevParent = iItem->parent();
172  }
173  }
174  }
175 
176  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*>> ThinnedAssociationsHelper::associationToBranches()
177  const {
178  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*>> temp;
179  for (auto const& item : vThinnedAssociationBranches_) {
180  temp.push_back(std::make_pair(item.association(), &item));
181  }
182  std::sort(temp.begin(),
183  temp.end(),
184  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
185  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
186  return temp;
187  }
188 
190  std::vector<BranchDescription const*> const& associationDescriptions,
191  std::set<BranchID> const& keptProductsInEvent,
192  std::map<BranchID, bool>& keepAssociation) const {
193  keepAssociation.clear();
194  // Copy the elements in vThinnedAssociationBranches_ into a vector sorted on
195  // the association BranchID so we can do searches on that BranchID faster.
196  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*>> assocToBranches = associationToBranches();
197 
198  for (auto association : associationDescriptions) {
199  if (association
200  ->isAlias()) { // There is no reason to configure an association product with an EDAlias (ignore and drop them if they exist)
201  keepAssociation.insert(std::make_pair(association->branchID(), false));
202  } else {
203  std::set<BranchID> branchesInRecursion;
205  association->branchID(), assocToBranches, branchesInRecursion, keptProductsInEvent, keepAssociation);
206  }
207  }
208  }
209 
211  BranchID const& association,
212  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*>> const& associationToBranches,
213  std::set<BranchID>& branchesInRecursion,
214  std::set<BranchID> const& keptProductsInEvent,
215  std::map<BranchID, bool>& keepAssociation) const {
216  // If we already decided to keep or drop this one, then
217  // return the same decision.
218  auto decision = keepAssociation.find(association);
219  if (decision != keepAssociation.end()) {
220  return decision->second;
221  }
222 
223  // Be careful not to fall into an infinite loop because
224  // of a circular recursion.
225  if (!branchesInRecursion.insert(association).second) {
226  return false;
227  }
228 
229  // If the thinned collection is being kept then keep the association
230  auto branches = std::lower_bound(
231  associationToBranches.begin(),
232  associationToBranches.end(),
233  std::make_pair(association, static_cast<ThinnedAssociationBranches const*>(nullptr)),
234  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
235  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
236  // This should never happen
237  if (branches == associationToBranches.end() || branches->first != association) {
239  "ThinnedAssociationHelper::shouldKeepAssociation could not find branches information, "
240  "contact Framework developers");
241  }
242  BranchID const& thinnedCollection = branches->second->thinned();
243  if (keptProductsInEvent.find(thinnedCollection) != keptProductsInEvent.end()) {
244  keepAssociation.insert(std::make_pair(association, true));
245  return true;
246  }
247  // otherwise loop over any associations where the thinned collection
248  // is also a parent collection and recursively examine those to see
249  // if their thinned collections are being kept.
250  auto iterEnd = parentEnd(thinnedCollection);
251  for (auto match = parentBegin(thinnedCollection); match != iterEnd; ++match) {
253  match->association(), associationToBranches, branchesInRecursion, keptProductsInEvent, keepAssociation)) {
254  keepAssociation.insert(std::make_pair(association, true));
255  return true;
256  }
257  }
258  // drop the association
259  keepAssociation.insert(std::make_pair(association, false));
260  return false;
261  }
262 
264  bool foundMatch = false;
265  for (auto entry = parentBegin(input.parent()), iEnd = parentEnd(input.parent()); entry != iEnd; ++entry) {
266  if (entry->association() == input.association() && entry->thinned() == input.thinned()) {
267  foundMatch = true;
268  break;
269  }
270  }
271  if (!foundMatch) {
272  throw edm::Exception(
274  "ThinnedAssociationHelper::requireMatch, Illegal attempt to merge files with different ThinnedAssociations");
275  }
276  }
277 
279  if (vThinnedAssociationBranches_.empty()) {
281  return;
282  }
283  std::vector<ThinnedAssociationBranches> const& inputData = helper.data();
284  for (auto const& inputEntry : inputData) {
285  requireMatch(inputEntry);
286  }
287  }
288 
290  std::vector<BranchID> const& associationsFromSecondary) {
291  if (associationsFromSecondary.empty())
292  return;
293 
294  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*>> assocToBranches =
295  helper.associationToBranches();
296 
297  for (BranchID const& association : associationsFromSecondary) {
298  auto branches = std::lower_bound(
299  assocToBranches.begin(),
300  assocToBranches.end(),
301  std::make_pair(association, static_cast<ThinnedAssociationBranches const*>(nullptr)),
302  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
303  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
304  // This should never happen
305  if (branches == assocToBranches.end() || branches->first != association) {
307  "ThinnedAssociationHelper::initAssociationsFromSecondary could not find branches "
308  "information, contact Framework developers");
309  }
310  requireMatch(*(branches->second));
311  }
312  }
313 
315  ThinnedAssociationsHelper const& parentThinnedAssociationsHelper,
316  std::map<BranchID, bool> const& keepAssociation,
317  std::map<BranchID::value_type, BranchID::value_type> const& droppedBranchIDToKeptBranchID) {
318  clear();
319  for (auto const& associationBranches : parentThinnedAssociationsHelper.data()) {
320  auto keep = keepAssociation.find(associationBranches.association());
321  if (keep != keepAssociation.end() && keep->second) {
322  BranchID parent = associationBranches.parent();
323  auto iter = droppedBranchIDToKeptBranchID.find(parent.id());
324  if (iter != droppedBranchIDToKeptBranchID.end()) {
325  parent = BranchID(iter->second);
326  }
327  BranchID thinned = associationBranches.thinned();
328  iter = droppedBranchIDToKeptBranchID.find(thinned.id());
329  if (iter != droppedBranchIDToKeptBranchID.end()) {
330  thinned = BranchID(iter->second);
331  }
332  addAssociation(parent, associationBranches.association(), thinned, associationBranches.isSlimmed());
333  }
334  }
335  }
336 
338  std::vector<BranchID> const& associationsFromSecondary, ThinnedAssociationsHelper const& fileAssociationsHelper) {
339  if (associationsFromSecondary.empty())
340  return;
341 
342  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*>> assocToBranches =
343  fileAssociationsHelper.associationToBranches();
344 
345  for (BranchID const& association : associationsFromSecondary) {
346  auto branches = std::lower_bound(
347  assocToBranches.begin(),
348  assocToBranches.end(),
349  std::make_pair(association, static_cast<ThinnedAssociationBranches const*>(nullptr)),
350  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
351  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
352  // This should never happen
353  if (branches == assocToBranches.end() || branches->first != association) {
355  "ThinnedAssociationHelper::initAssociationsFromSecondary could not find branches "
356  "information, contact Framework developers");
357  }
358  addAssociation(*(branches->second));
359  }
360  }
361 } // namespace edm
void initAssociationsFromSecondary(std::vector< BranchID > const &, ThinnedAssociationsHelper const &)
std::vector< ThinnedAssociationBranches >::const_iterator parentEnd(BranchID const &) const
Definition: helper.py:1
std::vector< ThinnedAssociationBranches >::const_iterator lower_bound(ThinnedAssociationBranches const &branches) const
std::vector< std::pair< BranchID, ThinnedAssociationBranches const * > > associationToBranches() const
void updateFromPrimaryInput(ThinnedAssociationsHelper const &)
static std::string const input
Definition: EdmProvDump.cc:50
std::tuple< layerClusterToCaloParticle, caloParticleToLayerCluster > association
std::vector< ThinnedAssociationBranches > const & data() const
unsigned int id() const
Definition: BranchID.h:21
std::vector< ThinnedAssociationBranches >::const_iterator begin() const
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:169
std::vector< ThinnedAssociationBranches >::const_iterator end() const
void addAssociation(BranchID const &, BranchID const &, BranchID const &, bool slimmed)
void updateFromSecondaryInput(ThinnedAssociationsHelper const &, std::vector< BranchID > const &associationsFromSecondary)
std::vector< ThinnedAssociationBranches > vThinnedAssociationBranches_
void requireMatch(ThinnedAssociationBranches const &input) const
double b
Definition: hdecay.h:118
bool shouldKeepAssociation(BranchID const &association, std::vector< std::pair< BranchID, ThinnedAssociationBranches const *> > const &associationToBranches, std::set< BranchID > &branchesInRecursion, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
void addContext(std::string const &context)
Definition: Exception.cc:165
std::vector< ThinnedAssociationBranches >::const_iterator parentBegin(BranchID const &) const
void updateFromParentProcess(ThinnedAssociationsHelper const &parentThinnedAssociationsHelper, std::map< BranchID, bool > const &keepAssociation, std::map< BranchID::value_type, BranchID::value_type > const &droppedBranchIDToKeptBranchID)
HLT enums.
void selectAssociationProducts(std::vector< BranchDescription const *> const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
float x
std::string match(BranchDescription const &a, BranchDescription const &b, std::string const &fileName)