The Gaudi Framework  master (37c0b60a)
EvtCollectionWrite.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 // EvtCollection.Write.cpp
13 // --------------------------------------------------------------------
14 //
15 // Author : Markus Frank
16 //
17 // ====================================================================
18 #define EVTCOLLECTION_WRITE_CPP
19 
20 // Framework include files
21 #include <GaudiKernel/INTupleSvc.h>
22 #include <GaudiKernel/IRegistry.h>
23 #include <GaudiKernel/MsgStream.h>
25 
26 // Example related include files
27 #include "EvtCollectionWrite.h"
28 #include <GaudiTestSuite/Event.h>
29 #include <GaudiTestSuite/MyTrack.h>
30 
31 #include <cmath>
32 
33 using namespace Gaudi::TestSuite;
34 
35 DECLARE_COMPONENT( EvtCollectionWrite )
36 
37 StatusCode EvtCollectionWrite::initialize() {
38  StatusCode status = m_evtTupleSvc.retrieve();
39  if ( status.isSuccess() ) {
40  NTuplePtr nt( m_evtTupleSvc.get(), "/NTUPLES/EvtColl/Dir1/Dir2/Dir3/Collection" );
41  if ( !nt ) { // Check if already booked
42  nt = m_evtTupleSvc->book( "/NTUPLES/EvtColl/Dir1/Dir2/Dir3/Collection", CLID_ColumnWiseTuple, "Hello World" );
43  if ( nt ) {
44  // Add an index column
45  status = nt->addItem( "Ntrack", m_ntrkColl, 0, 5000 );
46  status = nt->addItem( "Energy", m_eneColl );
47  status = nt->addItem( "Track", m_trackItem );
48  status = nt->addItem( "Addr", m_evtAddrColl );
49  status = nt->addItem( "TrkMom", m_ntrkColl, m_trkMom );
50  status = nt->addItem( "TrkMomFix", 100, m_trkMomFixed );
51  status = nt->addItem( "Address", m_evtAddrCollEx );
52  } else { // did not manage to book the N tuple....
53  return StatusCode::FAILURE;
54  }
55  } else { // Just reconnect to existing items
56  status = nt->item( "Ntrack", m_ntrkColl );
57  status = nt->item( "Energy", m_eneColl );
58  status = nt->item( "TrkMom", m_trkMom );
59  status = nt->item( "Track", m_trackItem );
60  status = nt->item( "TrkMomFix", m_trkMomFixed );
61  status = nt->item( "Addr", m_evtAddrColl );
62  status = nt->item( "Address", m_evtAddrCollEx );
63  }
64  }
65  return status;
66 }
67 
68 // Event callback
70  auto& log = msgStream();
71  SmartDataPtr<DataObject> evtRoot( eventSvc(), "/Event" );
72  SmartDataPtr<Event> evt( eventSvc(), "/Event/Header" );
73  if ( evt ) {
74  int evt_num = evt->event();
75  SmartDataPtr<MyTrackVector> trkCont( eventSvc(), "/Event/MyTracks" );
76  if ( trkCont != 0 ) {
77  // Force an object update since now the original tracks should be
78  // present and the local pointers can be updated!
79  m_evtAddrCollEx = evtRoot->registry()->address();
80  m_evtAddrColl = evtRoot->registry()->address();
81  m_ntrkColl = trkCont->size();
82  m_eneColl = 0.f;
83  log << MSG::DEBUG << " ->Track:";
84  for ( size_t j = 0; j < 100; ++j ) { m_trkMomFixed[j] = 0; }
85  int cnt = 0;
86  for ( const auto& i : *trkCont ) {
87  float p = sqrt( i->px() * i->px() + i->py() * i->py() + i->pz() * i->pz() );
88  if ( cnt < 5000 ) m_trkMom[cnt] = p;
89  if ( cnt < 5 ) m_trkMomFixed[cnt] = p;
90  m_eneColl += p;
91  ++cnt;
92  }
93  m_trackItem = ( 0 == m_ntrkColl ) ? 0 : ( *trkCont->begin() );
94  if ( evt_num < 10 || evt_num % 500 == 0 ) {
95  log << endmsg;
96  log << MSG::INFO << "================ EVENT:" << evt->event() << " RUN:" << evt->run()
97  << " ====== N(Track)=" << m_ntrkColl;
98  if ( m_ntrkColl < m_nMCcut.value() ) {
99  log << " FAILED selection (<" << m_nMCcut.value() << ") ============" << endmsg;
100  } else {
101  log << " PASSED selection (>=" << m_nMCcut.value() << ") ============" << endmsg;
102  return m_evtTupleSvc->writeRecord( "/NTUPLES/EvtColl/Dir1/Dir2/Dir3/Collection" );
103  }
104  }
105  return StatusCode::SUCCESS;
106  }
107  }
108  error() << "Unable to retrieve Event Header object" << endmsg;
109  return StatusCode::FAILURE;
110 }
IOTest.evt
evt
Definition: IOTest.py:107
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Event.h
Gaudi::Accumulators::sqrt
auto sqrt(std::chrono::duration< Rep, Period > d)
sqrt for std::chrono::duration
Definition: Counters.h:34
Gaudi.Configuration.log
log
Definition: Configuration.py:28
MyTrack.h
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
EvtCollectionWrite::execute
StatusCode execute() override
Event callback.
Definition: EvtCollectionWrite.cpp:69
EvtCollectionWrite.h
StatusCode
Definition: StatusCode.h:65
KeyedContainer::size
size_type size() const
Number of objects in the container.
Definition: KeyedContainer.h:285
ProduceConsume.j
j
Definition: ProduceConsume.py:104
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
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
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
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:78
INTupleSvc.h
MsgStream.h