CMS 3D CMS Logo

ThinnedAssociationsHelper.cc
Go to the documentation of this file.
4 #include <algorithm>
5 
6 namespace edm {
7 
9 
11  BranchID const& association,
12  BranchID const& thinned)
13  : parent_(parent), association_(association), thinned_(thinned) {}
14 
16 
17  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::begin() const {
18  return vThinnedAssociationBranches_.begin();
19  }
20 
21  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::end() const {
22  return vThinnedAssociationBranches_.end();
23  }
24 
25  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::parentBegin(
26  BranchID const& parent) const {
30  target,
32  return x.parent() < y.parent();
33  });
34  }
35 
36  std::vector<ThinnedAssociationBranches>::const_iterator ThinnedAssociationsHelper::parentEnd(
37  BranchID const& parent) const {
41  target,
43  return x.parent() < y.parent();
44  });
45  }
46 
48  std::sort(vThinnedAssociationBranches_.begin(),
51  return x.parent() < y.parent() ? true
52  : y.parent() < x.parent() ? false : x.association() < y.association();
53  });
54  }
55 
57  BranchID const& association,
58  BranchID const& thinned) {
60  }
61 
63  vThinnedAssociationBranches_.push_back(branches);
64  }
65 
66  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*> >
68  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*> > temp;
69  for (auto const& item : vThinnedAssociationBranches_) {
70  temp.push_back(std::make_pair(item.association(), &item));
71  }
72  std::sort(temp.begin(),
73  temp.end(),
74  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
75  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
76  return temp;
77  }
78 
80  std::vector<BranchDescription const*> const& associationDescriptions,
81  std::set<BranchID> const& keptProductsInEvent,
82  std::map<BranchID, bool>& keepAssociation) const {
83  keepAssociation.clear();
84  // Copy the elements in vThinnedAssociationBranches_ into a vector sorted on
85  // the association BranchID so we can do searches on that BranchID faster.
86  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*> > assocToBranches = associationToBranches();
87 
88  for (auto association : associationDescriptions) {
89  if (association
90  ->isAlias()) { // There is no reason to configure an association product with an EDAlias (ignore and drop them if they exist)
91  keepAssociation.insert(std::make_pair(association->branchID(), false));
92  } else {
93  std::set<BranchID> branchesInRecursion;
95  association->branchID(), assocToBranches, branchesInRecursion, keptProductsInEvent, keepAssociation);
96  }
97  }
98  }
99 
101  BranchID const& association,
102  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*> > const& associationToBranches,
103  std::set<BranchID>& branchesInRecursion,
104  std::set<BranchID> const& keptProductsInEvent,
105  std::map<BranchID, bool>& keepAssociation) const {
106  // If we already decided to keep or drop this one, then
107  // return the same decision.
108  auto decision = keepAssociation.find(association);
109  if (decision != keepAssociation.end()) {
110  return decision->second;
111  }
112 
113  // Be careful not to fall into an infinite loop because
114  // of a circular recursion.
115  if (!branchesInRecursion.insert(association).second) {
116  return false;
117  }
118 
119  // If the thinned collection is being kept then keep the association
120  auto branches = std::lower_bound(
121  associationToBranches.begin(),
122  associationToBranches.end(),
123  std::make_pair(association, static_cast<ThinnedAssociationBranches const*>(nullptr)),
124  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
125  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
126  // This should never happen
127  if (branches == associationToBranches.end() || branches->first != association) {
129  "ThinnedAssociationHelper::shouldKeepAssociation could not find branches information, "
130  "contact Framework developers");
131  }
132  BranchID const& thinnedCollection = branches->second->thinned();
133  if (keptProductsInEvent.find(thinnedCollection) != keptProductsInEvent.end()) {
134  keepAssociation.insert(std::make_pair(association, true));
135  return true;
136  }
137  // otherwise loop over any associations where the thinned collection
138  // is also a parent collection and recursively examine those to see
139  // if their thinned collections are being kept.
140  auto iterEnd = parentEnd(thinnedCollection);
141  for (auto match = parentBegin(thinnedCollection); match != iterEnd; ++match) {
143  match->association(), associationToBranches, branchesInRecursion, keptProductsInEvent, keepAssociation)) {
144  keepAssociation.insert(std::make_pair(association, true));
145  return true;
146  }
147  }
148  // drop the association
149  keepAssociation.insert(std::make_pair(association, false));
150  return false;
151  }
152 
154  bool foundMatch = false;
155  for (auto entry = parentBegin(input.parent()), iEnd = parentEnd(input.parent()); entry != iEnd; ++entry) {
156  if (entry->association() == input.association() && entry->thinned() == input.thinned()) {
157  foundMatch = true;
158  break;
159  }
160  }
161  if (!foundMatch) {
162  throw edm::Exception(
164  "ThinnedAssociationHelper::requireMatch, Illegal attempt to merge files with different ThinnedAssociations");
165  }
166  }
167 
169  if (vThinnedAssociationBranches_.empty()) {
171  return;
172  }
173  std::vector<ThinnedAssociationBranches> const& inputData = helper.data();
174  for (auto const& inputEntry : inputData) {
175  requireMatch(inputEntry);
176  }
177  }
178 
180  std::vector<BranchID> const& associationsFromSecondary) {
181  if (associationsFromSecondary.empty())
182  return;
183 
184  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*> > assocToBranches =
185  helper.associationToBranches();
186 
187  for (BranchID const& association : associationsFromSecondary) {
188  auto branches = std::lower_bound(
189  assocToBranches.begin(),
190  assocToBranches.end(),
191  std::make_pair(association, static_cast<ThinnedAssociationBranches const*>(nullptr)),
192  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
193  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
194  // This should never happen
195  if (branches == assocToBranches.end() || branches->first != association) {
197  "ThinnedAssociationHelper::initAssociationsFromSecondary could not find branches "
198  "information, contact Framework developers");
199  }
200  requireMatch(*(branches->second));
201  }
202  }
203 
205  ThinnedAssociationsHelper const& parentThinnedAssociationsHelper,
206  std::map<BranchID, bool> const& keepAssociation,
207  std::map<BranchID::value_type, BranchID::value_type> const& droppedBranchIDToKeptBranchID) {
208  clear();
209  for (auto const& associationBranches : parentThinnedAssociationsHelper.data()) {
210  auto keep = keepAssociation.find(associationBranches.association());
211  if (keep != keepAssociation.end() && keep->second) {
212  BranchID parent = associationBranches.parent();
213  auto iter = droppedBranchIDToKeptBranchID.find(parent.id());
214  if (iter != droppedBranchIDToKeptBranchID.end()) {
215  parent = BranchID(iter->second);
216  }
217  BranchID thinned = associationBranches.thinned();
218  iter = droppedBranchIDToKeptBranchID.find(thinned.id());
219  if (iter != droppedBranchIDToKeptBranchID.end()) {
220  thinned = BranchID(iter->second);
221  }
222  addAssociation(parent, associationBranches.association(), thinned);
223  }
224  }
225  sort();
226  }
227 
229  std::vector<BranchID> const& associationsFromSecondary, ThinnedAssociationsHelper const& fileAssociationsHelper) {
230  if (associationsFromSecondary.empty())
231  return;
232 
233  std::vector<std::pair<BranchID, ThinnedAssociationBranches const*> > assocToBranches =
234  fileAssociationsHelper.associationToBranches();
235 
236  for (BranchID const& association : associationsFromSecondary) {
237  auto branches = std::lower_bound(
238  assocToBranches.begin(),
239  assocToBranches.end(),
240  std::make_pair(association, static_cast<ThinnedAssociationBranches const*>(nullptr)),
241  [](std::pair<BranchID, ThinnedAssociationBranches const*> const& x,
242  std::pair<BranchID, ThinnedAssociationBranches const*> const& y) { return x.first < y.first; });
243  // This should never happen
244  if (branches == assocToBranches.end() || branches->first != association) {
246  "ThinnedAssociationHelper::initAssociationsFromSecondary could not find branches "
247  "information, contact Framework developers");
248  }
249  addAssociation(*(branches->second));
250  }
251  sort();
252  }
253 } // namespace edm
ThinnedAssociationsHelper.h
edm::errors::MismatchedInputFiles
Definition: EDMException.h:52
input
static const std::string input
Definition: EdmProvDump.cc:48
funct::false
false
Definition: Factorize.h:34
edm::errors::LogicError
Definition: EDMException.h:37
edm
HLT enums.
Definition: AlignableModifier.h:19
mps_splice.entry
entry
Definition: mps_splice.py:68
edm::ThinnedAssociationsHelper::clear
void clear()
Definition: ThinnedAssociationsHelper.h:51
edm::ThinnedAssociationsHelper::shouldKeepAssociation
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
Definition: ThinnedAssociationsHelper.cc:100
edm::ThinnedAssociationsHelper::parentEnd
std::vector< ThinnedAssociationBranches >::const_iterator parentEnd(BranchID const &) const
Definition: ThinnedAssociationsHelper.cc:36
edm::ThinnedAssociationsHelper::data
std::vector< ThinnedAssociationBranches > const & data() const
Definition: ThinnedAssociationsHelper.h:57
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
edm::BranchID::id
unsigned int id() const
Definition: BranchID.h:21
EDMException.h
cuda_std::upper_bound
__host__ constexpr __device__ RandomIt upper_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
Definition: cudastdAlgorithm.h:45
edm::BranchID
Definition: BranchID.h:14
edm::ThinnedAssociationsHelper::updateFromPrimaryInput
void updateFromPrimaryInput(ThinnedAssociationsHelper const &)
Definition: ThinnedAssociationsHelper.cc:168
edm::ThinnedAssociationsHelper::selectAssociationProducts
void selectAssociationProducts(std::vector< BranchDescription const * > const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
Definition: ThinnedAssociationsHelper.cc:79
edm::ThinnedAssociationBranches
Definition: ThinnedAssociationsHelper.h:18
edm::ThinnedAssociationsHelper::requireMatch
void requireMatch(ThinnedAssociationBranches const &input) const
Definition: ThinnedAssociationsHelper.cc:153
edm::ThinnedAssociationsHelper::vThinnedAssociationBranches_
std::vector< ThinnedAssociationBranches > vThinnedAssociationBranches_
Definition: ThinnedAssociationsHelper.h:81
cuda_std::lower_bound
__host__ constexpr __device__ RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
Definition: cudastdAlgorithm.h:27
funct::true
true
Definition: Factorize.h:173
edm::ThinnedAssociationsHelper
Definition: ThinnedAssociationsHelper.h:35
BranchDescription.h
edm::ThinnedAssociationsHelper::updateFromSecondaryInput
void updateFromSecondaryInput(ThinnedAssociationsHelper const &, std::vector< BranchID > const &associationsFromSecondary)
Definition: ThinnedAssociationsHelper.cc:179
helper
Definition: helper.py:1
edm::ThinnedAssociationsHelper::end
std::vector< ThinnedAssociationBranches >::const_iterator end() const
Definition: ThinnedAssociationsHelper.cc:21
edm::ThinnedAssociationsHelper::ThinnedAssociationsHelper
ThinnedAssociationsHelper()
Definition: ThinnedAssociationsHelper.cc:15
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
edm::ThinnedAssociationsHelper::updateFromParentProcess
void updateFromParentProcess(ThinnedAssociationsHelper const &parentThinnedAssociationsHelper, std::map< BranchID, bool > const &keepAssociation, std::map< BranchID::value_type, BranchID::value_type > const &droppedBranchIDToKeptBranchID)
Definition: ThinnedAssociationsHelper.cc:204
TrackValidation_cff.association
association
Definition: TrackValidation_cff.py:286
edm::match
std::string match(BranchDescription const &a, BranchDescription const &b, std::string const &fileName)
Definition: BranchDescription.cc:351
edm::ThinnedAssociationsHelper::parentBegin
std::vector< ThinnedAssociationBranches >::const_iterator parentBegin(BranchID const &) const
Definition: ThinnedAssociationsHelper.cc:25
Exception
Definition: hltDiff.cc:246
edm::ThinnedAssociationsHelper::sort
void sort()
Definition: ThinnedAssociationsHelper.cc:47
edm::ThinnedAssociationsHelper::initAssociationsFromSecondary
void initAssociationsFromSecondary(std::vector< BranchID > const &, ThinnedAssociationsHelper const &)
Definition: ThinnedAssociationsHelper.cc:228
filterCSVwithJSON.target
target
Definition: filterCSVwithJSON.py:32
edm::ThinnedAssociationsHelper::associationToBranches
std::vector< std::pair< BranchID, ThinnedAssociationBranches const * > > associationToBranches() const
Definition: ThinnedAssociationsHelper.cc:67
edm::ThinnedAssociationBranches::ThinnedAssociationBranches
ThinnedAssociationBranches()
Definition: ThinnedAssociationsHelper.cc:8
keep
const int keep
Definition: GenParticlePruner.cc:48
class-composition.parent
parent
Definition: class-composition.py:88
edm::ThinnedAssociationsHelper::begin
std::vector< ThinnedAssociationBranches >::const_iterator begin() const
Definition: ThinnedAssociationsHelper.cc:17
edm::ThinnedAssociationsHelper::addAssociation
void addAssociation(BranchID const &, BranchID const &, BranchID const &)
Definition: ThinnedAssociationsHelper.cc:56