ATLAS Offline Software
copyThinned.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /**
6  * @file AthContainers/tools/copyThinned.icc
7  * @author scott snyder <snyder@bnl.gov>
8  * @date Jul, 2014
9  * @brief Helper to copy an object while applying thinning.
10  */
11 
12 
13 namespace SG {
14 
15 
16 /**
17  * @brief Helper to copy an object while applying thinning.
18  * @param orig The object to copy.
19  * @param dummy Dummy argument for overload resolution.
20  * @param info Thinning information for this object (or nullptr).
21  *
22  * This is the generic version of @c copyThinned, which matches types
23  * for which there is not a more specific overload. It simply makes
24  * a copy of @c orig using the copy constructor.
25  */
26 template <class CONTAINER>
27 std::unique_ptr<CONTAINER>
28 copyThinned1 (const CONTAINER& orig,
29  const void* /*dummy*/,
30  const SG::ThinningInfo* /*info*/)
31 {
32  return std::make_unique<CONTAINER> (orig);
33 }
34 
35 
36 /**
37  * @brief Helper to copy an object while applying thinning.
38  * @param orig The object to copy.
39  * @param dummy Dummy argument for overload resolution.
40  * @param info Thinning information for this object (or nullptr).
41  *
42  * This overload handles @c DataVector types. It returns a view container
43  * copy of @c orig, from which any thinned elements are removed.
44  */
45 template <class CONTAINER>
46 std::unique_ptr<CONTAINER>
47 copyThinned1 (CONTAINER& orig,
48  DataVector<typename CONTAINER::base_value_type>* /*dummy*/,
49  const SG::ThinningInfo* info)
50 {
51  const ThinningDecisionBase* dec = info ? info->m_decision : nullptr;
52  size_t size = orig.size();
53  if (size > 0 && dec) {
54  auto newcont = std::make_unique<CONTAINER>();
55  // Avoid some ambiguities with clear().
56  auto* dv = static_cast<DataVector<typename CONTAINER::base_value_type>*> (newcont.get());
57  dv->clear (SG::VIEW_ELEMENTS);
58  newcont->reserve (size);
59 
60  for (size_t i = 0; i < size; i++) {
61  if (!(dec && dec->thinned (i))) {
62  newcont->push_back (orig[i]);
63  }
64  }
65 
66  return newcont;
67  }
68 
69  return std::make_unique<CONTAINER> (orig);
70 }
71 
72 
73 /**
74  * @brief Helper to copy an object while applying thinning.
75  * @param orig The object to copy.
76  * @param dummy Dummy argument for overload resolution.
77  * @param info Thinning information for this object (or nullptr).
78  *
79  * This overload handles @c DataVector types. It returns a view container
80  * copy of @c orig, from which any thinned elements are removed.
81  */
82 template <class CONTAINER>
83 std::unique_ptr<const CONTAINER>
84 copyThinned1 (const CONTAINER& orig,
85  const DataVector<typename CONTAINER::base_value_type>* /*dummy*/,
86  const SG::ThinningInfo* info)
87 {
88  const ThinningDecisionBase* dec = info ? info->m_decision : nullptr;
89  size_t size = orig.size();
90  if (size > 0 && dec) {
91  auto newcont = std::make_unique<ConstDataVector<CONTAINER> >();
92  newcont->clear (SG::VIEW_ELEMENTS);
93  newcont->reserve (size);
94 
95  for (size_t i = 0; i < size; i++) {
96  if (!(dec && dec->thinned (i)))
97  {
98  newcont->push_back (orig[i]);
99  }
100  }
101 
102  return std::unique_ptr<const CONTAINER>(newcont.release()->asDataVector());
103  }
104 
105  return std::make_unique<CONTAINER> (orig);
106 }
107 
108 
109 /**
110  * @brief Helper to copy an object while applying thinning.
111  * @param orig The object to copy.
112  * @param dummy Dummy argument for overload resolution.
113  * @param info Thinning information for this object (or nullptr).
114  *
115  * This overload handles @c IAuxStore types. It returns a new copy
116  * of the store, with any thinned elements removed.
117  */
118 template <class CONTAINER>
119 std::unique_ptr<CONTAINER>
120 copyThinned1 (const CONTAINER& orig,
121  const SG::IAuxStore* /*dummy*/,
122  const SG::ThinningInfo* info)
123 {
124  // T->P conversion is done from within copyAuxStoreThinned,
125  // so we need to call it regardless of whether or not
126  // thinning is actually done. Variable selection happens there
127  // as well, so need to call it even if the container is empty.
128  // (Actually, in that case, all dynamic variables will be removed,
129  // but that's ok.)
130  auto newcont = std::make_unique<CONTAINER>();
131  copyAuxStoreThinned (orig, *newcont, info);
132  return newcont;
133 }
134 
135 
136 /**
137  * @brief Helper to copy an object while applying thinning.
138  * @param orig The object to copy.
139  * @param info Thinning information for this object (or nullptr).
140  *
141  * Returns a new copy of @c orig with elements removed according to the
142  * thinning defined in @c svc. Ownership of the new object is passed
143  * back to the caller.
144  *
145  * The code here handles @c IAuxStore objects.
146  * (For a @c DataVector object, use @c copyThinnedConst.)
147  * Support for additional object types may be added by adding
148  * overloads for @c copyThinned1.
149  */
150 template <class CONTAINER>
151 std::unique_ptr<CONTAINER>
152 copyThinned (CONTAINER& orig,
153  const SG::ThinningInfo* info)
154 {
155  return copyThinned1 (orig, &orig, info);
156 }
157 
158 
159 /**
160  * @brief Helper to copy an object while applying thinning, const version.
161  * @param orig The object to copy.
162  * @param info Thinning information for this object (or nullptr).
163  *
164  * Returns a new copy of @c orig with elements removed according to the
165  * thinning defined in @c svc. Ownership of the new object is passed
166  * back to the caller.
167  *
168  * The code here handles @c DataVector and @c IAuxStore objects.
169  * Support for additional object types may be added by adding
170  * overloads for @c copyThinned1.
171  */
172 template <class CONTAINER>
173 std::unique_ptr<const CONTAINER>
174 copyThinnedConst (const CONTAINER& orig,
175  const SG::ThinningInfo* info)
176 {
177  return copyThinned1 (orig, &orig, info);
178 }
179 
180 
181 } // namespace SG