The Gaudi Framework  master (e68eea06)
Loading...
Searching...
No Matches
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
25
26// Example related include files
27#include "EvtCollectionWrite.h"
30
31#include <cmath>
32
33using namespace Gaudi::TestSuite;
34
36
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}
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 & msgStream() const
Return an uninitialized MsgStream.
User example objects: EvtCollectionWrite.
ServiceHandle< INTupleSvc > m_evtTupleSvc
StatusCode execute() override
Event callback.
NTuple::Item< IOpaqueAddress * > m_evtAddrCollEx
Gaudi::Property< int > m_nMCcut
StatusCode initialize() override
Initialize.
NTuple::Item< int > m_ntrkColl
NTuple::Array< float > m_trkMom
NTuple::Item< Gaudi::TestSuite::MyTrack * > m_trackItem
NTuple::Item< IOpaqueAddress * > m_evtAddrColl
NTuple::Array< float > m_trkMomFixed
NTuple::Item< float > m_eneColl
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
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
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
@ DEBUG
Definition IMessageSvc.h:22
@ INFO
Definition IMessageSvc.h:22