Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
WriteAlg.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ====================================================================
12 // WriteAlg.cpp
13 // --------------------------------------------------------------------
14 //
15 // Package : GaudiTestSuite/Example3
16 //
17 // Author : Markus Frank
18 //
19 // ====================================================================
20 // Framework include files
23 
26 
27 // Example related include files
28 #include "WriteAlg.h"
29 
30 // Event Model related classes
31 #include <GaudiTestSuite/Counter.h>
32 #include <GaudiTestSuite/Event.h>
33 #include <GaudiTestSuite/MyTrack.h>
34 
35 using namespace Gaudi::TestSuite;
36 
38 
39 //--------------------------------------------------------------------
40 // Register data leaf
41 //--------------------------------------------------------------------
42 StatusCode WriteAlg::put( IDataProviderSvc* s, const std::string& path, DataObject* pObj ) {
43  StatusCode sc = s->registerObject( path, pObj );
44  if ( sc.isFailure() ) { error() << "Unable to register object " << path << endmsg; }
45  return sc;
46 }
47 
48 //--------------------------------------------------------------------
49 // Initialize
50 //--------------------------------------------------------------------
52  if ( auto sc = Algorithm::initialize(); !sc ) return sc;
53  m_recordSvc = service( "FileRecordDataSvc", true );
54  if ( !m_recordSvc ) {
55  error() << "Unable to retrieve run records service" << endmsg;
56  return StatusCode::FAILURE;
57  }
58  return put( m_recordSvc.get(), "/FileRecords/EvtCount", m_evtCount = new Counter() );
59 }
60 
61 //--------------------------------------------------------------------
62 // Finalize
63 //--------------------------------------------------------------------
65  Counter* pObj = new Counter();
66  pObj->set( 123456 );
67  auto sc = put( m_recordSvc.get(), "/FileRecords/SumCount", pObj );
68  m_recordSvc.reset();
69  m_evtCount = nullptr;
70  // if any sc is not success, report that one
71  if ( auto sc2 = Algorithm::finalize(); sc ) sc = sc2;
72  return sc;
73 }
74 
75 //--------------------------------------------------------------------
76 // Execute
77 //--------------------------------------------------------------------
79  StatusCode sc;
80 
81  static int evtnum = 0;
82  static int runnum = 999;
83 
84  Rndm::Numbers rndmflat( randSvc(), Rndm::Flat( 0., 1. ) );
85  Rndm::Numbers rndmgauss( randSvc(), Rndm::Gauss( 10., 1. ) );
86 
87  m_evtCount->increment();
88 
89  // Create the Event header and set it as "root" of the event store
90  Event* evt = new Event();
91  evt->setEvent( ++evtnum );
92  evt->setRun( runnum );
93  evt->setTime( Gaudi::Time( 1577836800 + evtnum, evtnum * 1e6 ) );
94 
95  sc = eventSvc()->registerObject( "/Event", "Header", evt );
96  if ( sc.isFailure() ) {
97  error() << "Unable to register Event Header" << endmsg;
98  return sc;
99  }
100 
101  Collision* coll0 = new Collision( 0 );
102  Collision* coll1 = new Collision( 1 );
103  Collision* coll2 = new Collision( 2 );
104 
105  sc = eventSvc()->registerObject( "/Event", "Collision_0", coll0 );
106  if ( sc.isFailure() ) {
107  error() << "Unable to register Collision 0" << endmsg;
108  return sc;
109  }
110  sc = put( eventSvc(), "/Event/Collision_1", coll1 );
111  if ( sc.isFailure() ) return sc;
112  sc = put( eventSvc(), "/Event/Collision_2", coll2 );
113  if ( sc.isFailure() ) return sc;
114 
115  evt->addCollision( coll0 );
116  evt->addCollision( coll1 );
117  evt->addCollision( coll2 );
118 
119  // Create the collection of tracks and register them in the event store
120  int n = (int)( rndmflat() * 100. );
121  MyTrackVector* myTracks = new MyTrackVector();
122  for ( int i = 0; i < n; i++ ) {
123  // Create new track
124  double c = rndmgauss();
125  double b = rndmgauss();
126  double a = rndmgauss();
127  MyTrack* track = new MyTrack( float( a ), float( b ), float( c ) );
128  // the following line has been replace by the previous one since
129  // the order of evaluation of the rndgauss() call is unspecified
130  // in the C++ standard. Don't do that.
131  // MyTrack* track = new MyTrack(rndmgauss(),rndmgauss(),rndmgauss());
132 
133  // set Link to event object
134  track->setEvent( evt );
135  // And add the stuff to the container
136  myTracks->insert( track );
137  }
138 
139  // Create vertex container
140  int m = (int)( rndmflat() * 100. ) + 1;
141  MyVertexVector* myVertices = new MyVertexVector();
142  for ( int j = 0; j < m; j++ ) {
143  // Create new track
144  double c = rndmgauss();
145  double b = rndmgauss();
146  double a = rndmgauss();
147  MyVertex* vtx = new MyVertex( float( a ) / 100.0F, float( b ) / 100.0F, float( c ) / 100.0F );
148  // the following line has been replace by the previous one since
149  // the order of evaluation of the rndgauss() call is unspecified
150  // in the C++ standard. Don't do that.
151  // MyVertex* vtx = new MyVertex(rndmgauss()/100.0,
152  // rndmgauss()/100.0,
153  // rndmgauss()/100.0);
154 
155  // set Link to event object
156  vtx->setEvent( evt );
157  vtx->addCollision( coll0 );
158  vtx->addCollision( coll1 );
159  vtx->addCollision( coll2 );
160  // And add the stuff to the container
161  myVertices->insert( vtx );
162  }
163  // Now connect vertices and tracks
164  for ( MyTrackVector::iterator k = myTracks->begin(); k != myTracks->end(); ++k ) {
165  int org = (int)( rndmflat() * double( m ) );
166  MyVertex* orgVtx = *( myVertices->begin() + org );
167  ( *k )->setOriginVertex( orgVtx );
168  int dec1 = (int)( rndmflat() * double( m ) );
169  int dec2 = (int)( rndmflat() * double( m ) );
170  int tmp = dec1;
171  dec1 = ( tmp < dec2 ) ? tmp : dec2;
172  dec2 = ( tmp > dec2 ) ? tmp : dec2;
173  for ( int l = dec1; l < dec2; ++l ) {
174  MyVertex* decVtx = *( myVertices->begin() + l );
175  ( *k )->addDecayVertex( decVtx );
176  decVtx->setMotherParticle( *k );
177  }
178  }
179 
180  sc = put( eventSvc(), "/Event/MyTracks", myTracks );
181  if ( sc.isFailure() ) return sc;
182  sc = put( eventSvc(), "/Event/Collision_0/MyVertices", myVertices );
183  if ( sc.isFailure() ) return sc;
184  // All done
185  return StatusCode::SUCCESS;
186 }
IOTest.evt
evt
Definition: IOTest.py:107
KeyedContainer::end
iterator end()
Retrieve terminating iterator.
Definition: KeyedContainer.h:323
Event.h
Gaudi::TestSuite::Counter
Definition: Counter.h:30
RndmGenerators.h
WriteAlg::initialize
StatusCode initialize() override
Initialize.
Definition: WriteAlg.cpp:51
KeyedContainer::begin
iterator begin()
Retrieve start iterator.
Definition: KeyedContainer.h:319
Gaudi::TestSuite::MyVertex::setEvent
void setEvent(Event *evt)
Access to event object.
Definition: MyVertex.h:72
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
MyTrack.h
Gaudi::TestSuite::MyVertex
Definition: MyVertex.h:33
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
Gaudi::TestSuite::MyTrack
Definition: MyTrack.h:45
gaudirun.c
c
Definition: gaudirun.py:525
Rndm::Flat
Parameters for the flat random number generation within boundaries [minimum, maximum].
Definition: RndmGenerators.h:253
Gaudi::TestSuite::MyVertex::setMotherParticle
void setMotherParticle(MyTrack *mother)
Set mother track.
Definition: MyVertex.h:110
Gaudi::TestSuite::Collision
Definition: Collision.h:26
Gaudi::TestSuite::Event
Definition: Event.h:37
IDataProviderSvc.h
Gaudi::Time
Definition: Time.h:241
StatusCode
Definition: StatusCode.h:65
Rndm::Gauss
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:32
Rndm::Numbers
Random number accessor This small class encapsulates the use of the random number generator.
Definition: RndmGenerators.h:359
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
ProduceConsume.j
j
Definition: ProduceConsume.py:104
Gaudi::TestSuite::Counter::set
void set(int val)
Set value.
Definition: Counter.h:47
SmartDataPtr.h
KeyedContainer
template class KeyedContainer, KeyedContainer.h
Definition: KeyedContainer.h:74
Gaudi::TestSuite
Definition: ConditionAccessorHolder.h:21
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
KeyedContainer::insert
const key_type & insert(const value_type val, const key_type &kval)
Insert entry to the container with a valid key.
Definition: KeyedContainer.h:533
Gaudi::TestSuite::MyTrack::setEvent
void setEvent(Event *evt)
Access to event object.
Definition: MyTrack.h:101
cpluginsvc.n
n
Definition: cpluginsvc.py:234
KeyedContainer::iterator
seq_type::iterator iterator
Sequential access: iterator type used in sequential container.
Definition: KeyedContainer.h:100
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
WriteAlg
Definition: WriteAlg.h:25
Gaudi::Algorithm::finalize
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:184
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
gaudirun.l
dictionary l
Definition: gaudirun.py:583
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Gaudi::TestSuite::MyVertexVector
KeyedContainer< MyVertex > MyVertexVector
Definition: MyVertex.h:104
WriteAlg::finalize
StatusCode finalize() override
Finalize.
Definition: WriteAlg.cpp:64
Gaudi::TestSuite::MyTrackVector
KeyedContainer< MyTrack > MyTrackVector
Definition: MyTrack.h:127
DataObject
Definition: DataObject.h:36
Gaudi::TestSuite::MyVertex::addCollision
void addCollision(Collision *vtx)
Add collision.
Definition: MyVertex.h:133
HepRndm::Engine
Definition: HepRndmEngine.h:35
IDataProviderSvc
Definition: IDataProviderSvc.h:53
WriteAlg.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Functional::details::put
auto put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
Definition: details.h:162
WriteAlg::execute
StatusCode execute() override
Event callback.
Definition: WriteAlg.cpp:78
IDataManagerSvc.h
Counter.h