The Gaudi Framework  master (37c0b60a)
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
26 #include <GaudiKernel/IRegistry.h>
28 #include <GaudiKernel/MsgStream.h>
30 
31 // Example related include files
32 #include "ReadAlg.h"
33 
34 // Event Model related classes
35 #include <GaudiTestSuite/Counter.h>
36 #include <GaudiTestSuite/Event.h>
37 #include <GaudiTestSuite/MyTrack.h>
38 
39 #include <GaudiKernel/System.h>
40 
41 using namespace Gaudi::TestSuite;
42 
43 DECLARE_COMPONENT( ReadAlg )
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;
55  return StatusCode::FAILURE;
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;
71  return StatusCode::FAILURE;
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 //--------------------------------------------------------------------
91 void 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 }
IOTest.evt
evt
Definition: IOTest.py:107
Event.h
ReadAlg::finalize
StatusCode finalize() override
Finalize.
Definition: ReadAlg.cpp:81
ReadAlg::initialize
StatusCode initialize() override
Initialize.
Incident::source
const std::string & source() const
Access to the source of the incident.
Definition: Incident.h:54
std::string
STL class.
DataStoreItem.h
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
Gaudi.Configuration.log
log
Definition: Configuration.py:28
MyTrack.h
System.h
Gaudi::TestSuite::MyVertex
Definition: MyVertex.h:33
IOpaqueAddress
Definition: IOpaqueAddress.h:33
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
ReadAlg.h
ReadAlg::execute
StatusCode execute() override
Event callback.
GaudiPartProp.tests.id
id
Definition: tests.py:111
Gaudi::TestSuite::Counter::value
int value() const
Retrieve counter value.
Definition: Counter.h:41
IDataProviderSvc.h
IIncidentSvc.h
IProperty
Definition: IProperty.h:33
IProperty::getProperty
virtual StatusCode getProperty(Gaudi::Details::PropertyBase *p) const =0
Get the property by property.
StatusCode
Definition: StatusCode.h:65
IOpaqueAddress.h
SmartDataPtr.h
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
IRegistry.h
IOpaqueAddress::registry
virtual IRegistry * registry() const =0
Update branch name.
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
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
ReadAlg::handle
void handle(const Incident &incident) override
IIncidentListener override: Inform that a new incident has occured.
Definition: ReadAlg.cpp:91
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
SmartDataPtr
A small class used to access easily (and efficiently) data items residing in data stores.
Definition: SmartDataPtr.h:57
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
std::count
T count(T... args)
Properties.v
v
Definition: Properties.py:122
Incident::type
const std::string & type() const
Access to the incident type.
Definition: Incident.h:48
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Incident
Definition: Incident.h:27
MsgStream.h
Counter.h