The Gaudi Framework  master (e68eea06)
Loading...
Searching...
No Matches
ReadAlg.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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
41#include <TROOT.h>
42
43using namespace Gaudi::TestSuite;
44
46
47//--------------------------------------------------------------------
48// Initialize
49//--------------------------------------------------------------------
51 // make root work in multithreaded mode. This triggers some changes in particular in
52 // the way Streamers are used which need to be tested (see test_mtread)
53 ROOT::EnableThreadSafety();
54
55 auto sc = Algorithm::initialize();
56 if ( !sc ) return sc;
57
58 m_recordSvc = service( "FileRecordDataSvc", true );
59 if ( !m_recordSvc ) {
60 error() << "Unable to retrieve run records service" << endmsg;
62 }
63
64 if ( m_incidentName.empty() ) {
65 auto prp = m_recordSvc.as<IProperty>();
66
67 if ( ( sc = setPropertyRepr( "IncidentName", prp->getProperty( "IncidentName" ).toString() ) ).isFailure() ) {
68 error() << "Failed to copy FileRecordDataSvc.IncidentName (" << prp->getProperty( "IncidentName" ).toString()
69 << ')' << endmsg;
70 return sc;
71 }
72 }
73
74 m_incidentSvc = service( "IncidentSvc", true );
75 if ( !m_incidentSvc ) {
76 error() << "Failed to access IncidentSvc." << endmsg;
78 }
79 m_incidentSvc->addListener( this, m_incidentName );
80
81 return sc;
82}
83
84//--------------------------------------------------------------------
85// Finalize
86//--------------------------------------------------------------------
88 if ( m_incidentSvc ) m_incidentSvc->removeListener( this );
89 m_incidentSvc.reset();
90 m_recordSvc.reset();
91 return Algorithm::finalize();
92}
93
94//--------------------------------------------------------------------
95// IIncidentListener override: Inform that a new incident has occured
96//--------------------------------------------------------------------
97void ReadAlg::handle( const Incident& incident ) {
98 // full serialization for thread safety
99 std::scoped_lock lock( m_mutex );
100 always() << "Got incident: " << incident.type() << " Source:" << incident.source() << endmsg;
101 if ( m_incidentName == incident.type() ) {
102 std::string n = incident.source();
103 always() << "Received incident:" << incident.type() << ": " << n << endmsg;
104 SmartDataPtr<Counter> evt_cnt( m_recordSvc.get(), n + "/EvtCount" );
105 if ( evt_cnt != 0 )
106 always() << "Incident: FileInfo record: " << n << "/EvtCount=" << evt_cnt->value() << endmsg;
107 else
108 always() << "Incident: NO FileInfo record EvtCounter for " << n << "/EvtCount" << endmsg;
109
110 SmartDataPtr<Counter> sum_cnt( m_recordSvc.get(), n + "/SumCount" );
111 if ( sum_cnt != 0 )
112 always() << "Incident: FileInfo record: " << n << "/SumCount=" << sum_cnt->value() << endmsg;
113 else
114 always() << "Incident: NO FileInfo record SumCounter for " << n << "/SumCount" << endmsg;
115 }
116}
117
118//--------------------------------------------------------------------
119// Execute
120//--------------------------------------------------------------------
122 // full serialization for thread safety
123 std::scoped_lock lock( m_mutex );
124 // This just makes the code below a bit easier to read (and type)
125 SmartDataPtr<Event> evt( eventSvc(), "/Event/Header" );
126
127 if ( evt != 0 ) {
128 int evt_num = evt->event();
129 if ( evt_num <= 10 || evt_num % 100 == 0 ) {
130 auto& log = info();
131 log << "========= EVENT:" << evt->event() << " RUN:" << evt->run() << " TIME:" << evt->time();
132 for ( size_t nc = 0; nc < evt->collisions().size(); ++nc ) {
133 if ( evt->collisions()[nc] ) {
134 log << " " << evt->collisions()[nc]->collision();
135 log << " (" << evt->collisions()[nc].hintID() << ")";
136 } else {
137 log << " ===";
138 }
139 }
140 log << endmsg;
141 }
142 SmartDataPtr<MyTrackVector> myTracks( eventSvc(), "/Event/MyTracks" );
143 SmartDataPtr<MyVertexVector> myVtx( eventSvc(), "/Event/Collision_0/MyVertices" );
144 if ( myTracks != 0 ) {
145 IOpaqueAddress* pAddr = myTracks->registry()->address();
146 static std::string fname = "";
147 int count = 0;
148 if ( pAddr ) {
149 std::string new_fname = pAddr->par()[0];
150 if ( fname != new_fname ) {
151 fname = new_fname;
152 SmartDataPtr<Counter> evt_cnt( m_recordSvc.get(), new_fname + "/EvtCount" );
153 if ( evt_cnt != 0 )
154 always() << "FileInfo record: " << new_fname << "/EvtCount=" << evt_cnt->value() << endmsg;
155 else
156 always() << "NO FileInfo record EvtCounter for " << fname << endmsg;
157
158 SmartDataPtr<Counter> sum_cnt( m_recordSvc.get(), new_fname + "/SumCount" );
159 if ( sum_cnt != 0 )
160 always() << "FileInfo record: " << new_fname << "/SumCount=" << sum_cnt->value() << endmsg;
161 else
162 always() << "NO FileInfo record SumCounter for " << fname << endmsg;
163 }
164 }
165
166 for ( MyTrackVector::iterator i = myTracks->begin(); i != myTracks->end() && count++ < 5; ++i ) {
167 try {
168 if ( evt->event() < 10 || evt->event() % 500 == 0 ) {
169 auto& log = debug();
170 log << "Evt:";
171 if ( ( *i )->event() ) {
172 log << ( *i )->event()->event();
173 } else {
174 log << "Unknown";
175 }
176 log << " Track:";
177 log.width( 12 );
178 log << ( *i )->px();
179 log.width( 12 );
180 log << ( *i )->py();
181 log.width( 12 );
182 log << ( *i )->pz();
183 log << " Org:";
184 if ( ( *i )->originVertex() ) {
185 log.width( 4 );
186 log << ( *i )->originVertex()->index();
187 log.width( 10 );
188 log << ( *i )->originVertex()->x();
189 log.width( 10 );
190 log << ( *i )->originVertex()->y();
191 log.width( 10 );
192 log << ( *i )->originVertex()->z();
193 } else {
194 log << "No Origin Vertex!";
195 }
196 log << endmsg << " Decays:";
197 log.width( 4 );
198 log << ( *i )->decayVertices().size();
199 log << endmsg;
200 for ( size_t id = 0; id < ( *i )->decayVertices().size(); ++id ) {
201 const MyVertex* v = ( *i )->decayVertices()[id];
202 if ( v ) {
203 log << " ";
204 log.width( 10 );
205 log << v->x();
206 log.width( 10 );
207 log << v->y();
208 log.width( 10 );
209 log << v->z() << " Collisions:";
210 for ( size_t ic = 0; ic < v->collisions().size(); ++ic ) {
211 log.width( 3 );
212 if ( v->collisions()[ic] ) {
213 log << v->collisions()[ic]->collision();
214 log << " (" << v->collisions()[ic].hintID() << ")";
215 } else {
216 log << "-";
217 log << " (" << v->collisions()[ic].hintID() << ")";
218 }
219 }
220 } else {
221 log << "No decay vertex!";
222 }
223 log << endmsg;
224 }
225 }
226 } catch ( ... ) { error() << "Exception occurred!" << endmsg; }
227 }
228 } else
229 warning() << "No tracks found, Event " << evt->event() << endmsg;
230 return StatusCode::SUCCESS;
231 }
232 error() << "Unable to retrieve Event Header object" << endmsg;
233 return StatusCode::FAILURE;
234}
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:29
StatusCode execute() override
Event callback.
Definition ReadAlg.cpp:121
SmartIF< IIncidentSvc > m_incidentSvc
Reference to incident service.
Definition ReadAlg.h:33
SmartIF< IDataProviderSvc > m_recordSvc
Reference to run records data service.
Definition ReadAlg.h:31
std::mutex m_mutex
protection for thread safe usage
Definition ReadAlg.h:36
Gaudi::Property< std::string > m_incidentName
Definition ReadAlg.h:34
StatusCode finalize() override
Finalize.
Definition ReadAlg.cpp:87
void handle(const Incident &incident) override
IIncidentListener override: Inform that a new incident has occured.
Definition ReadAlg.cpp:97
StatusCode initialize() override
Initialize.
Definition ReadAlg.cpp:50
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