ATLAS Offline Software
Loading...
Searching...
No Matches
WriteThinnedData.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// WriteThinnedData.cxx
8// Implementation file for class WriteThinnedData
9// Author: S.Binet<binet@cern.ch>
11
12
13// STL includes
14#include <vector>
15#include <sstream>
16
17// FrameWork includes
18#include "Gaudi/Property.h"
19#include "GaudiKernel/SystemOfUnits.h"
20
21// StoreGate
25
26// DataModel includes
29
30// AthExThinning includes
35
36
37using namespace AthExThinning;
38
39// Constructors
41WriteThinnedData::WriteThinnedData( const std::string& name,
42 ISvcLocator* pSvcLocator ) :
43 ::AthAlgorithm( name, pSvcLocator )
44{
45 //
46 // Property declaration
47 //
48 //declareProperty( "Property", m_nProperty );
49
50 declareProperty( "Particles",
51 m_particlesName = "Particles",
52 "Input location of particles (to be thinned)" );
53
54 declareProperty( "Filter",
55 m_filter,
56 "Filter to apply on Particles (true == keep element)" );
57 std::vector<bool> filter( 10, false );
58 m_filter.set( filter );
59
60}
61
62// Destructor
66
67// Athena Algorithm's Hooks
70{
71 ATH_MSG_INFO("Initializing {}...", name());
72
73 for (int i = 0; i < 3; i++) {
74 std::ostringstream ss;
75 std::print (ss, "_test{}", (i+1));
76 m_elephantinoKeys.emplace_back (m_elephantinoName.value() + ss.str());
77 m_decayKeys.emplace_back (m_decayName.value() + ss.str());
78 m_iparticlesKeys.emplace_back (m_particlesName.value() + ss.str());
79 }
80 ATH_CHECK( m_elephantinoKeys.initialize() );
81 ATH_CHECK( m_decayKeys.initialize() );
82 ATH_CHECK( m_iparticlesKeys.initialize() );
83
84 m_particlesKey1 = m_particlesName.value() + "_test1";
85 m_particlesKey2 = m_particlesName.value() + "_test2";
86 ATH_CHECK( m_particlesKey1.initialize ("StreamUSR_0") );
87 ATH_CHECK( m_particlesKey2.initialize ("StreamUSR_0") );
88
89 m_iparticlesKey3 = m_particlesName.value() + "_test3";
90 ATH_CHECK( m_iparticlesKey3.initialize ("StreamUSR_0") );
91
92 return StatusCode::SUCCESS;
93}
94
96{
97 ATH_MSG_INFO("Finalizing {}...", name());
98 return StatusCode::SUCCESS;
99}
100
101StatusCode WriteThinnedData::execute(const EventContext& ctx)
102{
103 ATH_MSG_DEBUG("Executing {}...", name());
104
105 bool allGood = true;
106 if ( !test( ctx, 0, "test1" ).isSuccess() ) {
107 ATH_MSG_WARNING("Could not perform 'thinning test1' !!");
108 allGood = false;
109 }
110
111 if ( !test( ctx, 1, "test2" ).isSuccess() ) {
112 ATH_MSG_WARNING("Could not perform 'thinning test2' !!");
113 allGood = false;
114 }
115
116 if ( !test( ctx, 2, "test3" ).isSuccess() ) {
117 ATH_MSG_WARNING("Could not perform 'thinning test3' !!");
118 allGood = false;
119 }
120
121 return allGood ? StatusCode::SUCCESS : StatusCode::FAILURE;
122}
123
124StatusCode WriteThinnedData::test( const EventContext& ctx,
125 int testNum, const std::string& testName )
126{
127 const std::string& test = testName;
128 ATH_MSG_INFO("Performing thinning test [{}]...", test);
129
130 // fetch Particles
131 const std::string particlesName = m_particlesName.value() + "_" + test;
132 const AthExParticles * particles = 0;
133 if ( !evtStore()->retrieve(particles, particlesName).isSuccess() ||
134 0 == particles ) {
136 ("Could not fetch particles at [{}] !!", particlesName);
137 return StatusCode::RECOVERABLE;
138 }
139
140 // fetch IParticles
141 SG::ReadHandle<AthExIParticles> iparticles (m_iparticlesKeys[testNum], ctx);
142
143 if ( iparticles->size() != particles->size() ||
144 iparticles->at(0)->px() != particles->at(0)->px() ) {
145 ATH_MSG_WARNING ("symlinked containers are corrupted: ");
146 ATH_MSG_WARNING (" #iparticles: {}", iparticles->size());
147 ATH_MSG_WARNING (" # particles: {}", particles->size());
148 ATH_MSG_WARNING (" ipx[0] = {}", iparticles->at(0)->px());
149 ATH_MSG_WARNING (" px[0] = {}", particles->at(0)->px());
150 return StatusCode::RECOVERABLE;
151 }
152
153 // fetch Decay
154 SG::ReadHandle<AthExDecay> decay (m_decayKeys[testNum], ctx);
155
156 // fetch Elephantino
157 SG::ReadHandle<AthExElephantino> elephantino (m_elephantinoKeys[testNum], ctx);
158
159 const double igev = 1. / Gaudi::Units::GeV;
160 ATH_MSG_INFO("IN particles: {}", particles->size());
161 ATH_MSG_INFO("IN decay: ");
162 ATH_MSG_INFO(" p1: px= {}", decay->p1()->px() * igev);
163 ATH_MSG_INFO(" p2: px= {}", decay->p2()->px() * igev);
164 ATH_MSG_INFO(" l1: px= {}", decay->l1()->px() * igev);
165 ATH_MSG_INFO(" l2: px= {}", decay->l2()->px() * igev);
166
167 ATH_MSG_INFO("IN elephantino: ");
168 ATH_MSG_INFO(" leg1: px= {}", elephantino->leg1()->px() * igev);
169 ATH_MSG_INFO(" leg2: px= {}", elephantino->leg2()->px() * igev);
170 ATH_MSG_INFO(" leg3: px= {}", elephantino->leg3()->px() * igev);
171 ATH_MSG_INFO(" leg4: px= {}", elephantino->leg4()->px() * igev);
172 ATH_MSG_INFO(" ear1: px= {}", elephantino->ear1()->px() * igev);
173 ATH_MSG_INFO(" ear2: px= {}", elephantino->ear2()->px() * igev);
174
177 if ( test == "test1" ) {
178 if ( !doThinningTest1(ctx, m_particlesKey1).isSuccess() ) {
179 ATH_MSG_WARNING("Could not exercise Thinning !!");
180 }
181 } else if ( test == "test2" ) {
182 if ( !doThinningTest2(ctx, m_particlesKey2).isSuccess() ) {
183 ATH_MSG_WARNING("Could not exercise Thinning !!");
184 }
185 } else if ( test == "test3" ) {
186 if ( !doThinningTest3(ctx, m_iparticlesKey3).isSuccess() ) {
187 ATH_MSG_WARNING("Could not exercise Thinning !!");
188 }
189 } else {
190 ATH_MSG_ERROR("Unknown test: [{}]", test);
191 return StatusCode::FAILURE;
192 }
194
195 ATH_MSG_INFO("Decay is now:");
196 ATH_MSG_INFO(" p1: px= {}", decay->p1()->px() * igev);
197 ATH_MSG_INFO(" p2: px= {}", decay->p2()->px() * igev);
198 ATH_MSG_INFO(" l1: px= {}", decay->l1()->px() * igev);
199 ATH_MSG_INFO(" l2: px= {}", decay->l2()->px() * igev);
200
201 ATH_MSG_INFO("Elephantino is now: ");
202 ATH_MSG_INFO(" leg1: px= {}", elephantino->leg1()->px() * igev);
203 ATH_MSG_INFO(" leg2: px= {}", elephantino->leg2()->px() * igev);
204 ATH_MSG_INFO(" leg3: px= {}", elephantino->leg3()->px() * igev);
205 ATH_MSG_INFO(" leg4: px= {}", elephantino->leg4()->px() * igev);
206 ATH_MSG_INFO(" ear1: px= {}", elephantino->ear1()->px() * igev);
207 ATH_MSG_INFO(" ear2: px= {}", elephantino->ear2()->px() * igev);
208
209
210 ATH_MSG_INFO("[{}] has been performed.", test);
211 return StatusCode::SUCCESS;
212}
213
214
215StatusCode WriteThinnedData::doThinningTest1( const EventContext& ctx,
216 const SG::ThinningHandleKey<AthExParticles>& particlesKey ) const
217{
218 SG::ThinningHandle<AthExParticles> particles (particlesKey, ctx);
219 std::vector<bool> filter = m_filter.value();
220
221 const double igev = 1. / Gaudi::Units::GeV;
222 ATH_MSG_INFO("Particles | filter :");
223 for ( unsigned int i = 0; i != particles->size(); ++i ) {
224 const std::string kr = filter[i] ? "keep" : "remove";
225 ATH_MSG_INFO("{:9} | {}", (*particles)[i]->px() * igev, kr);
226 }
227 ATH_MSG_INFO("===================");
228
229 std::fill( filter.begin() + (filter.size() / 2),
230 filter.end(),
231 true );
232 ATH_MSG_INFO("Filter {}", filter);
233
234 ATH_MSG_INFO("... Processing [pre-thinning] ...");
235 particles.keep (filter);
236 ATH_MSG_INFO("======== Index table =========");
237 {
238 SG::ThinningDecisionBase tmp = particles.decision();
239 tmp.buildIndexMap();
240 for ( std::size_t i = 0; i != particles->size(); ++i ) {
241 std::size_t newIdx = tmp.index( i );
242 ATH_MSG_INFO(" idx {} -> {}",
243 i,
244 newIdx == SG::ThinningDecision::RemovedIdx ? "-" : std::to_string(newIdx));
245 }
246 }
247
248
249 filter = m_filter.value();
250 std::fill( filter.begin(),
251 filter.begin() + (filter.size() / 2),
252 true );
253
254 ATH_MSG_INFO("Filter {}", filter);
255
256 ATH_MSG_INFO("... Processing [thinning] ...");
257 particles.keep( filter, SG::ThinningDecisionBase::Op::And );
258
259 ATH_MSG_INFO("======== Index table =========");
260 {
261 SG::ThinningDecisionBase tmp = particles.decision();
262 tmp.buildIndexMap();
263 for ( std::size_t i = 0; i != particles->size(); ++i ) {
264 std::size_t newIdx = tmp.index( i );
265 ATH_MSG_INFO(" idx {} -> {}",
266 i,
267 newIdx == SG::ThinningDecision::RemovedIdx ? "-" : std::to_string(newIdx));
268 }
269 }
270
271 return StatusCode::SUCCESS;
272}
273
274
275StatusCode WriteThinnedData::doThinningTest2( const EventContext& ctx,
276 const SG::ThinningHandleKey<AthExParticles>& particlesKey ) const
277{
278 SG::ThinningHandle<AthExParticles> particles (particlesKey, ctx);
279 std::vector<bool> filter = m_filter.value();
280
281 const double igev = 1. / Gaudi::Units::GeV;
282 ATH_MSG_INFO( "Particles | filter :");
283 for ( unsigned int i = 0; i != particles->size(); ++i ) {
284 const std::string kr = filter[i] ? "keep" : "remove";
285 ATH_MSG_INFO("{:9} | {}",(*particles)[i]->px() * igev, kr);
286 }
287 ATH_MSG_INFO("===================");
288
289 std::fill( filter.begin() + (filter.size() / 2),
290 filter.end(),
291 false );
292 ATH_MSG_INFO("Filter {}", filter);
293
294 ATH_MSG_INFO("... Processing [pre-thinning] ...");
295 particles.keep (filter);
296 ATH_MSG_INFO("======== Index table =========");
297 {
298 SG::ThinningDecisionBase tmp = particles.decision();
299 tmp.buildIndexMap();
300 for ( std::size_t i = 0; i != particles->size(); ++i ) {
301 std::size_t newIdx = tmp.index( i );
302 ATH_MSG_INFO(" idx {} -> {}",
303 i,
304 newIdx == SG::ThinningDecision::RemovedIdx ? "-" : std::to_string(newIdx));
305 }
306 }
307
308
309 filter = m_filter.value();
310 std::fill( filter.begin(),
311 filter.begin() + (filter.size() / 2),
312 false );
313
314 ATH_MSG_INFO("Filter {}", filter);
315
316 ATH_MSG_INFO("... Processing [thinning] ...");
317 particles.keep (filter, SG::ThinningDecisionBase::Op::Or);
318
319 ATH_MSG_INFO("======== Index table =========");
320 {
321 SG::ThinningDecisionBase tmp = particles.decision();
322 tmp.buildIndexMap();
323 for ( std::size_t i = 0; i != particles->size(); ++i ) {
324 std::size_t newIdx = tmp.index( i );
325 ATH_MSG_INFO(" idx {} -> {}",
326 i,
327 newIdx == SG::ThinningDecision::RemovedIdx ? "-" : std::to_string(newIdx));
328 }
329 }
330
331 return StatusCode::SUCCESS;
332}
333
334
335StatusCode
336WriteThinnedData::doThinningTest3( const EventContext& ctx,
337 const SG::ThinningHandleKey<AthExIParticles>& iparticlesKey ) const
338{
339 SG::ThinningHandle<AthExIParticles> iparticles (iparticlesKey, ctx);
340 std::vector<bool> filter = m_filter.value();
341
342 const double igev = 1. / Gaudi::Units::GeV;
343 ATH_MSG_INFO("IParticles | filter :");
344 for ( unsigned int i = 0; i != iparticles->size(); ++i ) {
345 const std::string kr = filter[i] ? "keep" : "remove";
346 ATH_MSG_INFO("{:9} | {}", (*iparticles)[i]->px() * igev, kr);
347 }
348 ATH_MSG_INFO("===================");
349
350 std::fill( filter.begin() + (filter.size() / 2),
351 filter.end(),
352 true );
353 ATH_MSG_INFO("Filter {}", filter);
354
355 ATH_MSG_INFO("... Processing [pre-thinning] ...");
356 iparticles.keep (filter);
357 ATH_MSG_INFO("======== Index table =========");
358 {
359 SG::ThinningDecisionBase tmp = iparticles.decision();
360 tmp.buildIndexMap();
361 for ( std::size_t i = 0; i != iparticles->size(); ++i ) {
362 std::size_t newIdx = tmp.index( i );
363 ATH_MSG_INFO(" idx {} -> {}",
364 i,
365 newIdx == SG::ThinningDecision::RemovedIdx ? "-" : std::to_string(newIdx));
366 }
367 }
368
369
370 filter = m_filter.value();
371 std::fill( filter.begin(),
372 filter.begin() + (filter.size() / 2),
373 true );
374
375 ATH_MSG_INFO("Filter {}", filter);
376
377 ATH_MSG_INFO("... Processing [thinning] ...");
378 iparticles.keep (filter, SG::ThinningDecisionBase::Op::And);
379
380 ATH_MSG_INFO("======== Index table =========");
381 {
382 SG::ThinningDecisionBase tmp = iparticles.decision();
383 tmp.buildIndexMap();
384 for ( std::size_t i = 0; i != iparticles->size(); ++i ) {
385 std::size_t newIdx = tmp.index( i );
386 ATH_MSG_INFO(" idx {} -> {}",
387 i,
388 newIdx == SG::ThinningDecision::RemovedIdx ? "-" : std::to_string(newIdx));
389 }
390 }
391
392 return StatusCode::SUCCESS;
393}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_INFO(x,...)
An STL vector of pointers that by default owns its pointed-to elements.
static Double_t ss
Handle class for reading from StoreGate.
Hold thinning decisions for one container.
Handle for requesting thinning for a data object.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
StringProperty m_elephantinoName
Elephantino input location.
StatusCode test(const EventContext &ctx, int testNum, const std::string &testName)
Exercise the following thinning tests: [testName = "test1"] retrieve a AthExParticles container remov...
StatusCode doThinningTest2(const EventContext &ctx, const SG::ThinningHandleKey< AthExParticles > &particlesKey) const
Apply the real thinning.
StringProperty m_particlesName
Particles input location.
virtual StatusCode initialize() override
SG::ReadHandleKeyArray< AthExElephantino > m_elephantinoKeys
SG::ThinningHandleKey< AthExParticles > m_particlesKey1
StringProperty m_decayName
Decay input location.
WriteThinnedData(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
SG::ThinningHandleKey< AthExParticles > m_particlesKey2
SG::ReadHandleKeyArray< AthExIParticles > m_iparticlesKeys
SG::ReadHandleKeyArray< AthExDecay > m_decayKeys
SG::ThinningHandleKey< AthExIParticles > m_iparticlesKey3
BooleanArrayProperty m_filter
Filter to apply on the Particles.
StatusCode doThinningTest3(const EventContext &ctx, const SG::ThinningHandleKey< AthExIParticles > &iparticlesKey) const
Apply the real thinning.
StatusCode doThinningTest1(const EventContext &ctx, const SG::ThinningHandleKey< AthExParticles > &particlesKey) const
Apply the real thinning.
virtual ~WriteThinnedData()
Destructor:
virtual StatusCode finalize() override
Hold thinning decisions for one container.
static const std::size_t RemovedIdx
Flag used to show that an index has been thinned away.
const ThinningDecision & decision() const
Return the thinning object we're building.
void keep(size_t ndx)
Mark that index ndx in the container should be kept (not thinned away).
HandleKey object for adding thinning to an object.
Handle for requesting thinning for a data object.