1 #ifndef DataFormats_Common_getThinned_implementation_h
2 #define DataFormats_Common_getThinned_implementation_h
66 template <
typename F1,
typename F2,
typename F3>
71 F2 getThinnedAssociation,
78 if (associatedBranches == iEnd) {
81 bool const slimmedAllowed = (associatedBranches + 1 == iEnd);
82 if (slimmedAllowed and associatedBranches->isSlimmed()) {
84 ThinnedAssociation const* slimmedAssociation = getThinnedAssociation(associatedBranches->association());
85 if (slimmedAssociation ==
nullptr or
92 if (slimmedIndex.has_value()) {
100 for (; associatedBranches != iEnd; ++associatedBranches) {
101 if (associatedBranches->isSlimmed()) {
105 ThinnedAssociation const* thinnedAssociation = getThinnedAssociation(associatedBranches->association());
106 if (thinnedAssociation ==
nullptr)
109 if (associatedBranches->parent() != pidToBid(thinnedAssociation->
parentCollectionID())) {
115 if (not thinnedIndex.has_value()) {
121 WrapperBase const* thinnedCollection = getByProductID(thinnedCollectionPID);
122 if (thinnedCollection !=
nullptr) {
130 thinnedAssociationsHelper,
132 getThinnedAssociation,
134 if (thinnedOrSlimmed.hasThinned()
or (slimmedAllowed and thinnedOrSlimmed.hasSlimmed())) {
135 return thinnedOrSlimmed;
143 std::vector<WrapperBase const*>
const& foundContainers,
145 unsigned const nKeys =
keys.size();
148 for (
unsigned k = 0;
k < nKeys; ++
k) {
150 if (foundContainers[
k] !=
nullptr) {
158 if (
auto thinnedIndex = thinnedAssociation->
getThinnedIndex(
keys[
k]); thinnedIndex.has_value()) {
159 thinnedIndexes[
k] = *thinnedIndex;
163 return std::tuple(
std::move(thinnedIndexes), hasAny);
186 template <
typename F1,
typename F2,
typename F3>
191 F2 getThinnedAssociation,
193 std::vector<WrapperBase const*>& foundContainers,
194 std::vector<unsigned int>&
keys) {
200 if (associatedBranches == iEnd) {
203 bool const slimmedAllowed = associatedBranches + 1 == iEnd;
204 if (slimmedAllowed and associatedBranches->isSlimmed()) {
206 ThinnedAssociation const* slimmedAssociation = getThinnedAssociation(associatedBranches->association());
207 if (slimmedAssociation ==
nullptr or
215 return std::tuple(slimmedAssociation,
std::move(slimmedIndexes));
222 for (; associatedBranches != iEnd; ++associatedBranches) {
223 if (associatedBranches->isSlimmed()) {
227 ThinnedAssociation const* thinnedAssociation = getThinnedAssociation(associatedBranches->association());
228 if (thinnedAssociation ==
nullptr)
231 if (associatedBranches->parent() != pidToBid(thinnedAssociation->
parentCollectionID())) {
242 WrapperBase const* thinnedCollection = getByProductID(thinnedCollectionPID);
243 unsigned const nKeys =
keys.size();
244 if (thinnedCollection ==
nullptr) {
248 thinnedAssociationsHelper,
250 getThinnedAssociation,
254 if (slimmedAllowed and slimmed.has_value()) {
257 for (
unsigned k = 0;
k < nKeys; ++
k) {
258 if (foundContainers[
k] ==
nullptr)
262 keys[
k] = thinnedIndexes[
k];
265 for (
unsigned k = 0;
k < nKeys; ++
k) {
268 keys[
k] = thinnedIndexes[
k];
269 foundContainers[
k] = thinnedCollection;
286 template <
typename F1,
typename F2,
typename F3>
292 F2 getThinnedAssociation,
295 pid,
key, thinnedAssociationsHelper, pidToBid, getThinnedAssociation, getByProductID);
297 if (thinnedOrSlimmed.hasThinned()) {
298 return thinnedOrSlimmed.thinnedProduct();
299 }
else if (thinnedOrSlimmed.hasSlimmed()) {
300 auto [slimmedAssociation, slimmedIndex] = thinnedOrSlimmed.slimmedAssociation();
301 ProductID const& slimmedCollectionPID = slimmedAssociation->thinnedCollectionID();
302 WrapperBase const* slimmedCollection = getByProductID(slimmedCollectionPID);
303 if (slimmedCollection ==
nullptr) {
308 thinnedAssociationsHelper,
310 getThinnedAssociation,
313 return std::tuple(slimmedCollection, slimmedIndex);
333 template <
typename F1,
typename F2,
typename F3>
337 F2 getThinnedAssociation,
339 std::vector<WrapperBase const*>& foundContainers,
340 std::vector<unsigned int>&
keys) {
342 pid, thinnedAssociationsHelper, pidToBid, getThinnedAssociation, getByProductID, foundContainers,
keys);
343 if (slimmed.has_value()) {
345 auto [slimmedAssociation, slimmedIndexes] =
std::move(*slimmed);
346 ProductID const& slimmedCollectionPID = slimmedAssociation->thinnedCollectionID();
347 WrapperBase const* slimmedCollection = getByProductID(slimmedCollectionPID);
348 unsigned const nKeys =
keys.size();
349 if (slimmedCollection ==
nullptr) {
351 thinnedAssociationsHelper,
353 getThinnedAssociation,
357 for (
unsigned k = 0;
k < nKeys; ++
k) {
358 if (foundContainers[
k] ==
nullptr)
362 keys[
k] = slimmedIndexes[
k];
365 for (
unsigned k = 0;
k < nKeys; ++
k) {
368 keys[
k] = slimmedIndexes[
k];
369 foundContainers[
k] = slimmedCollection;
393 template <
typename F>
401 F&& getThinnedAssociation) {
408 return [parentID]() {
410 <<
"Parent collection with ProductID " << parentID <<
" has not been thinned";
414 bool foundParent =
false;
415 std::vector<ThinnedAssociation const*> thinnedAssociationParentage;
416 while (not foundParent) {
418 auto branchesToThinned = std::find_if(
419 thinnedAssociationsHelper.
begin(), thinnedAssociationsHelper.
end(), [&thinned](
auto& associatedBranches) {
420 return associatedBranches.thinned() == thinned;
422 if (branchesToThinned == thinnedAssociationsHelper.
end()) {
423 return [parentID, thinnedID, thinnedIsThinned = not thinnedAssociationParentage.empty()]() {
425 ex <<
"Requested thinned collection with ProductID " << thinnedID
426 <<
" is not thinned from the parent collection with ProductID " << parentID
427 <<
" or from any collection thinned from it.";
428 if (not thinnedIsThinned) {
429 ex <<
" In fact, the collection " << thinnedID
430 <<
" passed in as a 'thinned' collection has not been thinned at all.";
436 ThinnedAssociation const* thinnedAssociation = getThinnedAssociation(branchesToThinned->association());
437 if (thinnedAssociation ==
nullptr) {
439 if (thinnedAssociationParentage.empty()) {
440 ex <<
"ThinnedAssociation corresponding to thinned collection with ProductID " << thinnedID
443 ex <<
"Intermediate ThinnedAssociation between the requested thinned ProductID " << thinnedID
444 <<
" and parent " << parentID <<
" not found.";
446 ex <<
" This should not happen.\nPlease contact the core framework developers.";
450 thinnedAssociationParentage.push_back(thinnedAssociation);
451 if (branchesToThinned->parent() ==
parent) {
455 thinned = branchesToThinned->parent();
461 unsigned int thinnedIndex =
key;
462 for (
auto iAssociation = thinnedAssociationParentage.rbegin(), iEnd = thinnedAssociationParentage.rend();
463 iAssociation != iEnd;
465 auto optIndex = (*iAssociation)->getThinnedIndex(thinnedIndex);
467 thinnedIndex = *optIndex;
469 return std::monostate{};