ATLAS Offline Software
Loading...
Searching...
No Matches
WriteThinnedData.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2020 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 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() ) {
146 ("symlinked containers are corrupted: " << endmsg
147 << " #iparticles: " << iparticles->size() << endmsg
148 << " # particles: " << particles->size() << endmsg
149 << " ipx[0] = " << iparticles->at(0)->px() << endmsg
150 << " px[0] = " << particles->at(0)->px());
151 return StatusCode::RECOVERABLE;
152 }
153
154 // fetch Decay
155 SG::ReadHandle<AthExDecay> decay (m_decayKeys[testNum], ctx);
156
157 // fetch Elephantino
158 SG::ReadHandle<AthExElephantino> elephantino (m_elephantinoKeys[testNum], ctx);
159
160 const double igev = 1. / Gaudi::Units::GeV;
161 ATH_MSG_INFO("IN particles: " << particles->size() << endmsg
162 << "IN decay: " << endmsg
163 << " p1: px= " << decay->p1()->px() * igev << endmsg
164 << " p2: px= " << decay->p2()->px() * igev << endmsg
165 << " l1: px= " << decay->l1()->px() * igev << endmsg
166 << " l2: px= " << decay->l2()->px() * igev);
167
168 ATH_MSG_INFO("IN elephantino: " << endmsg
169 << " leg1: px= " << elephantino->leg1()->px() * igev << endmsg
170 << " leg2: px= " << elephantino->leg2()->px() * igev << endmsg
171 << " leg3: px= " << elephantino->leg3()->px() * igev << endmsg
172 << " leg4: px= " << elephantino->leg4()->px() * igev << endmsg
173 << " ear1: px= " << elephantino->ear1()->px() * igev << endmsg
174 << " ear2: px= " << elephantino->ear2()->px() * igev);
175
178 if ( test == "test1" ) {
179 if ( !doThinningTest1(ctx, m_particlesKey1).isSuccess() ) {
180 ATH_MSG_WARNING("Could not exercise Thinning !!");
181 }
182 } else if ( test == "test2" ) {
183 if ( !doThinningTest2(ctx, m_particlesKey2).isSuccess() ) {
184 ATH_MSG_WARNING("Could not exercise Thinning !!");
185 }
186 } else if ( test == "test3" ) {
187 if ( !doThinningTest3(ctx, m_iparticlesKey3).isSuccess() ) {
188 ATH_MSG_WARNING("Could not exercise Thinning !!");
189 }
190 } else {
191 ATH_MSG_ERROR("Unknown test: [" << test << "]");
192 return StatusCode::FAILURE;
193 }
195
197 ("Decay is now: " << endmsg
198 << " p1: px= " << decay->p1()->px() * igev << endmsg
199 << " p2: px= " << decay->p2()->px() * igev << endmsg
200 << " l1: px= " << decay->l1()->px() * igev << endmsg
201 << " l2: px= " << decay->l2()->px() * igev);
202
204 ("Elephantino is now: " << endmsg
205 << " leg1: px= " << elephantino->leg1()->px() * igev << endmsg
206 << " leg2: px= " << elephantino->leg2()->px() * igev << endmsg
207 << " leg3: px= " << elephantino->leg3()->px() * igev << endmsg
208 << " leg4: px= " << elephantino->leg4()->px() * igev << endmsg
209 << " ear1: px= " << elephantino->ear1()->px() * igev << endmsg
210 << " ear2: px= " << elephantino->ear2()->px() * igev);
211
212
213 ATH_MSG_INFO("[" << test << "] has been performed.");
214 return StatusCode::SUCCESS;
215}
216
217
218StatusCode WriteThinnedData::doThinningTest1( const EventContext& ctx,
219 const SG::ThinningHandleKey<AthExParticles>& particlesKey ) const
220{
221 SG::ThinningHandle<AthExParticles> particles (particlesKey, ctx);
222 std::vector<bool> filter = m_filter.value();
223
224 const double igev = 1. / Gaudi::Units::GeV;
225 msg(MSG::INFO) << "Particles | filter :" << endmsg;
226 for ( unsigned int i = 0; i != particles->size(); ++i ) {
227 const std::string kr = filter[i] ? "keep" : "remove";
228 msg(MSG::INFO)
229 << std::setw(9) << (*particles)[i]->px() * igev
230 << " | " << kr
231 << endmsg;
232 }
233 msg(MSG::INFO) << "===================" << endmsg;
234
235 std::fill( filter.begin() + (filter.size() / 2),
236 filter.end(),
237 true );
238 msg(MSG::INFO) << "Filter [" << std::boolalpha;
239 std::copy( filter.begin(), filter.end(),
240 std::ostream_iterator<bool>(msg(MSG::INFO).stream(), " ") );
241 msg(MSG::INFO) << "]" << endmsg;
242
243 msg(MSG::INFO) << "... Processing [pre-thinning] ..." << endmsg;
244 particles.keep (filter);
245 msg(MSG::INFO) << "======== Index table =========" << endmsg;
246 {
247 SG::ThinningDecisionBase tmp = particles.decision();
248 tmp.buildIndexMap();
249 for ( std::size_t i = 0; i != particles->size(); ++i ) {
250 std::size_t newIdx = tmp.index( i );
251 std::stringstream newIdxStr;
252 newIdxStr << newIdx;
253 msg(MSG::INFO)
254 << " idx " << i
255 << " -> " << (newIdx == SG::ThinningDecision::RemovedIdx
256 ? "-"
257 : newIdxStr.str() )
258 << endmsg;
259 }
260 }
261
262
263 filter = m_filter.value();
264 std::fill( filter.begin(),
265 filter.begin() + (filter.size() / 2),
266 true );
267
268 msg(MSG::INFO) << "Filter [" << std::boolalpha;
269 std::copy( filter.begin(), filter.end(),
270 std::ostream_iterator<bool>(msg(MSG::INFO).stream(), " ") );
271 msg(MSG::INFO) << "]" << endmsg;
272
273 msg(MSG::INFO) << "... Processing [thinning] ..." << endmsg;
274 particles.keep( filter, SG::ThinningDecisionBase::Op::And );
275
276 msg(MSG::INFO) << "======== Index table =========" << endmsg;
277 {
278 SG::ThinningDecisionBase tmp = particles.decision();
279 tmp.buildIndexMap();
280 for ( std::size_t i = 0; i != particles->size(); ++i ) {
281 std::size_t newIdx = tmp.index( i );
282 std::stringstream newIdxStr;
283 newIdxStr << newIdx;
284 msg(MSG::INFO)
285 << " idx " << i
286 << " -> " << (newIdx == SG::ThinningDecision::RemovedIdx
287 ? "-"
288 : newIdxStr.str() )
289 << endmsg;
290 }
291 }
292
293 return StatusCode::SUCCESS;
294}
295
296
297StatusCode WriteThinnedData::doThinningTest2( const EventContext& ctx,
298 const SG::ThinningHandleKey<AthExParticles>& particlesKey ) const
299{
300 SG::ThinningHandle<AthExParticles> particles (particlesKey, ctx);
301 std::vector<bool> filter = m_filter.value();
302
303 const double igev = 1. / Gaudi::Units::GeV;
304 msg(MSG::INFO) << "Particles | filter :" << endmsg;
305 for ( unsigned int i = 0; i != particles->size(); ++i ) {
306 const std::string kr = filter[i] ? "keep" : "remove";
307 msg(MSG::INFO)
308 << std::setw(9) << (*particles)[i]->px() * igev
309 << " | " << kr
310 << endmsg;
311 }
312 msg(MSG::INFO) << "===================" << endmsg;
313
314 std::fill( filter.begin() + (filter.size() / 2),
315 filter.end(),
316 false );
317 msg(MSG::INFO) << "Filter [" << std::boolalpha;
318 std::copy( filter.begin(), filter.end(),
319 std::ostream_iterator<bool>(msg(MSG::INFO).stream(), " ") );
320 msg(MSG::INFO) << "]" << endmsg;
321
322 msg(MSG::INFO) << "... Processing [pre-thinning] ..." << endmsg;
323 particles.keep (filter);
324 msg(MSG::INFO) << "======== Index table =========" << endmsg;
325 {
326 SG::ThinningDecisionBase tmp = particles.decision();
327 tmp.buildIndexMap();
328 for ( std::size_t i = 0; i != particles->size(); ++i ) {
329 std::size_t newIdx = tmp.index( i );
330 std::stringstream newIdxStr;
331 newIdxStr << newIdx;
332 msg(MSG::INFO)
333 << " idx " << i
334 << " -> " << (newIdx == SG::ThinningDecision::RemovedIdx
335 ? "-"
336 : newIdxStr.str() )
337 << endmsg;
338 }
339 }
340
341
342 filter = m_filter.value();
343 std::fill( filter.begin(),
344 filter.begin() + (filter.size() / 2),
345 false );
346
347 msg(MSG::INFO) << "Filter [" << std::boolalpha;
348 std::copy( filter.begin(), filter.end(),
349 std::ostream_iterator<bool>(msg(MSG::INFO).stream(), " ") );
350 msg(MSG::INFO) << "]" << endmsg;
351
352 msg(MSG::INFO) << "... Processing [thinning] ..." << endmsg;
353 particles.keep (filter, SG::ThinningDecisionBase::Op::Or);
354
355 msg(MSG::INFO) << "======== Index table =========" << endmsg;
356 {
357 SG::ThinningDecisionBase tmp = particles.decision();
358 tmp.buildIndexMap();
359 for ( std::size_t i = 0; i != particles->size(); ++i ) {
360 std::size_t newIdx = tmp.index( i );
361 std::stringstream newIdxStr;
362 newIdxStr << newIdx;
363 msg(MSG::INFO)
364 << " idx " << i
365 << " -> " << (newIdx == SG::ThinningDecision::RemovedIdx
366 ? "-"
367 : newIdxStr.str() )
368 << endmsg;
369 }
370 }
371
372 return StatusCode::SUCCESS;
373}
374
375
376StatusCode
377WriteThinnedData::doThinningTest3( const EventContext& ctx,
378 const SG::ThinningHandleKey<AthExIParticles>& iparticlesKey ) const
379{
380 SG::ThinningHandle<AthExIParticles> iparticles (iparticlesKey, ctx);
381 std::vector<bool> filter = m_filter.value();
382
383 const double igev = 1. / Gaudi::Units::GeV;
384 msg(MSG::INFO) << "IParticles | filter :" << endmsg;
385 for ( unsigned int i = 0; i != iparticles->size(); ++i ) {
386 const std::string kr = filter[i] ? "keep" : "remove";
387 msg(MSG::INFO)
388 << std::setw(9) << (*iparticles)[i]->px() * igev
389 << " | " << kr
390 << endmsg;
391 }
392 msg(MSG::INFO) << "===================" << endmsg;
393
394 std::fill( filter.begin() + (filter.size() / 2),
395 filter.end(),
396 true );
397 msg(MSG::INFO) << "Filter [" << std::boolalpha;
398 std::copy( filter.begin(), filter.end(),
399 std::ostream_iterator<bool>(msg(MSG::INFO).stream(), " ") );
400 msg(MSG::INFO) << "]" << endmsg;
401
402 msg(MSG::INFO) << "... Processing [pre-thinning] ..." << endmsg;
403 iparticles.keep (filter);
404 msg(MSG::INFO) << "======== Index table =========" << endmsg;
405 {
406 SG::ThinningDecisionBase tmp = iparticles.decision();
407 tmp.buildIndexMap();
408 for ( std::size_t i = 0; i != iparticles->size(); ++i ) {
409 std::size_t newIdx = tmp.index( i );
410 std::stringstream newIdxStr;
411 newIdxStr << newIdx;
412 msg(MSG::INFO)
413 << " idx " << i
414 << " -> " << (newIdx == SG::ThinningDecision::RemovedIdx
415 ? "-"
416 : newIdxStr.str() )
417 << endmsg;
418 }
419 }
420
421
422 filter = m_filter.value();
423 std::fill( filter.begin(),
424 filter.begin() + (filter.size() / 2),
425 true );
426
427 msg(MSG::INFO) << "Filter [" << std::boolalpha;
428 std::copy( filter.begin(), filter.end(),
429 std::ostream_iterator<bool>(msg(MSG::INFO).stream(), " ") );
430 msg(MSG::INFO) << "]" << endmsg;
431
432 msg(MSG::INFO) << "... Processing [thinning] ..." << endmsg;
433 iparticles.keep (filter, SG::ThinningDecisionBase::Op::And);
434
435 msg(MSG::INFO) << "======== Index table =========" << endmsg;
436 {
437 SG::ThinningDecisionBase tmp = iparticles.decision();
438 tmp.buildIndexMap();
439 for ( std::size_t i = 0; i != iparticles->size(); ++i ) {
440 std::size_t newIdx = tmp.index( i );
441 std::stringstream newIdxStr;
442 newIdxStr << newIdx;
443 msg(MSG::INFO)
444 << " idx " << i
445 << " -> " << (newIdx == SG::ThinningDecision::RemovedIdx
446 ? "-"
447 : newIdxStr.str() )
448 << endmsg;
449 }
450 }
451
452 return StatusCode::SUCCESS;
453}
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(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.