The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
ReadAlg.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// ReadAlg.cpp
13// --------------------------------------------------------------------
14//
15// Package : GaudiTestSuite/Example3
16//
17// Author : Markus Frank
18//
19// ====================================================================
20
21// Framework include files
30
31// Example related include files
32#include "ReadAlg.h"
33
34// Event Model related classes
38
39#include <GaudiKernel/System.h>
40
41using namespace Gaudi::TestSuite;
42
44
45//--------------------------------------------------------------------
46// Initialize
47//--------------------------------------------------------------------
49 auto sc = Algorithm::initialize();
50 if ( !sc ) return sc;
51
52 m_recordSvc = service( "FileRecordDataSvc", true );
53 if ( !m_recordSvc ) {
54 error() << "Unable to retrieve run records service" << endmsg;
56 }
57
58 if ( m_incidentName.empty() ) {
59 auto prp = m_recordSvc.as<IProperty>();
60
61 if ( ( sc = setPropertyRepr( "IncidentName", prp->getProperty( "IncidentName" ).toString() ) ).isFailure() ) {
62 error() << "Failed to copy FileRecordDataSvc.IncidentName (" << prp->getProperty( "IncidentName" ).toString()
63 << ')' << endmsg;
64 return sc;
65 }
66 }
67
68 m_incidentSvc = service( "IncidentSvc", true );
69 if ( !m_incidentSvc ) {
70 error() << "Failed to access IncidentSvc." << endmsg;
72 }
73 m_incidentSvc->addListener( this, m_incidentName );
74
75 return sc;
76}
77
78//--------------------------------------------------------------------
79// Finalize
80//--------------------------------------------------------------------
82 if ( m_incidentSvc ) m_incidentSvc->removeListener( this );
83 m_incidentSvc.reset();
84 m_recordSvc.reset();
85 return Algorithm::finalize();
86}
87
88//--------------------------------------------------------------------
89// IIncidentListener override: Inform that a new incident has occured
90//--------------------------------------------------------------------
91void ReadAlg::handle( const Incident& incident ) {
92 always() << "Got incident: " << incident.type() << " Source:" << incident.source() << endmsg;
93 if ( m_incidentName == incident.type() ) {
94 std::string n = incident.source();
95 always() << "Received incident:" << incident.type() << ": " << n << endmsg;
96 SmartDataPtr<Counter> evt_cnt( m_recordSvc.get(), n + "/EvtCount" );
97 if ( evt_cnt != 0 )
98 always() << "Incident: FileInfo record: " << n << "/EvtCount=" << evt_cnt->value() << endmsg;
99 else
100 always() << "Incident: NO FileInfo record EvtCounter for " << n << "/EvtCount" << endmsg;
101
102 SmartDataPtr<Counter> sum_cnt( m_recordSvc.get(), n + "/SumCount" );
103 if ( sum_cnt != 0 )
104 always() << "Incident: FileInfo record: " << n << "/SumCount=" << sum_cnt->value() << endmsg;
105 else
106 always() << "Incident: NO FileInfo record SumCounter for " << n << "/SumCount" << endmsg;
107 }
108}
109
110//--------------------------------------------------------------------
111// Execute
112//--------------------------------------------------------------------
114 // This just makes the code below a bit easier to read (and type)
115 SmartDataPtr<Event> evt( eventSvc(), "/Event/Header" );
116
117 if ( evt != 0 ) {
118 int evt_num = evt->event();
119 if ( evt_num <= 10 || evt_num % 100 == 0 ) {
120 auto& log = info();
121 log << "========= EVENT:" << evt->event() << " RUN:" << evt->run() << " TIME:" << evt->time();
122 for ( size_t nc = 0; nc < evt->collisions().size(); ++nc ) {
123 if ( evt->collisions()[nc] ) {
124 log << " " << evt->collisions()[nc]->collision();
125 log << " (" << evt->collisions()[nc].hintID() << ")";
126 } else {
127 log << " ===";
128 }
129 }
130 log << endmsg;
131 }
132 SmartDataPtr<MyTrackVector> myTracks( eventSvc(), "/Event/MyTracks" );
133 SmartDataPtr<MyVertexVector> myVtx( eventSvc(), "/Event/Collision_0/MyVertices" );
134 if ( myTracks != 0 ) {
135 IOpaqueAddress* pAddr = myTracks->registry()->address();
136 static std::string fname = "";
137 int count = 0;
138 if ( pAddr ) {
139 std::string new_fname = pAddr->par()[0];
140 if ( fname != new_fname ) {
141 fname = new_fname;
142 SmartDataPtr<Counter> evt_cnt( m_recordSvc.get(), new_fname + "/EvtCount" );
143 if ( evt_cnt != 0 )
144 always() << "FileInfo record: " << new_fname << "/EvtCount=" << evt_cnt->value() << endmsg;
145 else
146 always() << "NO FileInfo record EvtCounter for " << fname << endmsg;
147
148 SmartDataPtr<Counter> sum_cnt( m_recordSvc.get(), new_fname + "/SumCount" );
149 if ( sum_cnt != 0 )
150 always() << "FileInfo record: " << new_fname << "/SumCount=" << sum_cnt->value() << endmsg;
151 else
152 always() << "NO FileInfo record SumCounter for " << fname << endmsg;
153 }
154 }
155
156 for ( MyTrackVector::iterator i = myTracks->begin(); i != myTracks->end() && count++ < 5; ++i ) {
157 try {
158 if ( evt->event() < 10 || evt->event() % 500 == 0 ) {
159 auto& log = debug();
160 log << "Evt:";
161 if ( ( *i )->event() ) {
162 log << ( *i )->event()->event();
163 } else {
164 log << "Unknown";
165 }
166 log << " Track:";
167 log.width( 12 );
168 log << ( *i )->px();
169 log.width( 12 );
170 log << ( *i )->py();
171 log.width( 12 );
172 log << ( *i )->pz();
173 log << " Org:";
174 if ( ( *i )->originVertex() ) {
175 log.width( 4 );
176 log << ( *i )->originVertex()->index();
177 log.width( 10 );
178 log << ( *i )->originVertex()->x();
179 log.width( 10 );
180 log << ( *i )->originVertex()->y();
181 log.width( 10 );
182 log << ( *i )->originVertex()->z();
183 } else {
184 log << "No Origin Vertex!";
185 }
186 log << endmsg << " Decays:";
187 log.width( 4 );
188 log << ( *i )->decayVertices().size();
189 log << endmsg;
190 for ( size_t id = 0; id < ( *i )->decayVertices().size(); ++id ) {
191 const MyVertex* v = ( *i )->decayVertices()[id];
192 if ( v ) {
193 log << " ";
194 log.width( 10 );
195 log << v->x();
196 log.width( 10 );
197 log << v->y();
198 log.width( 10 );
199 log << v->z() << " Collisions:";
200 for ( size_t ic = 0; ic < v->collisions().size(); ++ic ) {
201 log.width( 3 );
202 if ( v->collisions()[ic] ) {
203 log << v->collisions()[ic]->collision();
204 log << " (" << v->collisions()[ic].hintID() << ")";
205 } else {
206 log << "-";
207 log << " (" << v->collisions()[ic].hintID() << ")";
208 }
209 }
210 } else {
211 log << "No decay vertex!";
212 }
213 log << endmsg;
214 }
215 }
216 } catch ( ... ) { error() << "Exception occurred!" << endmsg; }
217 }
218 } else
219 warning() << "No tracks found, Event " << evt->event() << endmsg;
220 return StatusCode::SUCCESS;
221 }
222 error() << "Unable to retrieve Event Header object" << endmsg;
223 return StatusCode::FAILURE;
224}
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)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
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< 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")
Simple class that represents a vertex for testing purposes.
Definition MyVertex.h:33
Opaque address interface definition.
virtual const std::string * par() const =0
Retrieve String parameters.
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition IProperty.h:32
Base class for all Incidents (computing events).
Definition Incident.h:24
const std::string & type() const
Access to the incident type.
Definition Incident.h:43
const std::string & source() const
Access to the source of the incident.
Definition Incident.h:49
seq_type::iterator iterator
ReadAlg class for the RootIOExample.
Definition ReadAlg.h:27
StatusCode execute() override
Event callback.
Definition ReadAlg.cpp:113
SmartIF< IIncidentSvc > m_incidentSvc
Reference to incident service.
Definition ReadAlg.h:31
SmartIF< IDataProviderSvc > m_recordSvc
Reference to run records data service.
Definition ReadAlg.h:29
Gaudi::Property< std::string > m_incidentName
Definition ReadAlg.h:32
StatusCode finalize() override
Finalize.
Definition ReadAlg.cpp:81
void handle(const Incident &incident) override
IIncidentListener override: Inform that a new incident has occured.
Definition ReadAlg.cpp:91
StatusCode initialize() override
Initialize.
Definition ReadAlg.cpp:48
A small class used to access easily (and efficiently) data items residing in data stores.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100