The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
34
35using namespace Gaudi::TestSuite;
36
38
39//--------------------------------------------------------------------
40// Register data leaf
41//--------------------------------------------------------------------
42StatusCode 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;
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}
HepRndm::Engine< HepJamesRandom > e6
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition Algorithm.h:175
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name")
void set(int val)
Set value.
Definition Counter.h:47
Essential information of the event used in examples It can be identified by "/Event".
Definition Event.h:37
void setEvent(Event *evt)
Access to event object.
Definition MyTrack.h:101
Simple class that represents a vertex for testing purposes.
Definition MyVertex.h:33
void setEvent(Event *evt)
Access to event object.
Definition MyVertex.h:72
void addCollision(Collision *vtx)
Add collision.
Definition MyVertex.h:133
void setMotherParticle(MyTrack *mother)
Set mother track.
Definition MyVertex.h:110
Based on seal::Time.
Definition Time.h:235
Data provider interface definition.
StatusCode registerObject(std::string_view fullPath, DataObject *pObject)
Register object with the data store.
iterator begin()
Retrieve start iterator.
iterator end()
Retrieve terminating iterator.
const key_type & insert(const value_type val, const key_type &kval)
Insert entry to the container with a valid key.
seq_type::iterator iterator
Parameters for the flat random number generation within boundaries [minimum, maximum].
Parameters for the Gauss random number generation.
Random number accessor This small class encapsulates the use of the random number generator.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
WriteAlg class for the RootIOExample.
Definition WriteAlg.h:24
StatusCode put(IDataProviderSvc *s, const std::string &path, DataObject *pObj)
Register data leaf.
Definition WriteAlg.cpp:42
StatusCode initialize() override
Initialize.
Definition WriteAlg.cpp:51
Gaudi::TestSuite::Counter * m_evtCount
Reference to event counter.
Definition WriteAlg.h:30
SmartIF< IDataProviderSvc > m_recordSvc
Reference to run records data service.
Definition WriteAlg.h:27
StatusCode finalize() override
Finalize.
Definition WriteAlg.cpp:64
StatusCode execute() override
Event callback.
Definition WriteAlg.cpp:78
WriteAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor: A constructor of this form must be provided.
Definition WriteAlg.h:37
KeyedContainer< MyTrack > MyTrackVector
Definition MyTrack.h:127
KeyedContainer< MyVertex > MyVertexVector
Definition MyVertex.h:104
STL namespace.