The Gaudi Framework  master (da3d77e1)
NTupleAlgorithm.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 // Include files
12 #include "NTupleAlgorithm.h"
13 
15 #include <GaudiKernel/MsgStream.h>
17 
18 #include <GaudiKernel/INTupleSvc.h>
19 
20 #include <math.h>
21 
23 
24 //------------------------------------------------------------------------------
25 NTupleAlgorithm::NTupleAlgorithm( const std::string& name, ISvcLocator* pSvcLocator )
26  : Algorithm( name, pSvcLocator )
27 //------------------------------------------------------------------------------
28 {
29  m_tuple1 = nullptr;
30  m_tuple2 = nullptr;
31 }
32 
33 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 {
37  // Book N-tuple 1
38  NTuplePtr nt1( ntupleSvc(), "MyTuples/1" );
39  if ( nt1 )
40  m_tuple1 = nt1;
41  else {
42  m_tuple1 = ntupleSvc()->book( "MyTuples/1", CLID_RowWiseTuple, "Row-wise N-Tuple example" );
43  if ( m_tuple1 ) {
44  m_tuple1->addItem( "Ntrack", m_ntrk ).ignore();
45  m_tuple1->addItem( "Energy", m_energy ).ignore();
46  } else { // did not manage to book the N tuple....
47  error() << " Cannot book N-tuple:" << long( m_tuple1 ) << endmsg;
48  return StatusCode::FAILURE;
49  }
50  }
51 
52  // Book N-tuple 2
53  NTuplePtr nt2( ntupleSvc(), "MyTuples/simple/2" );
54  if ( nt2 )
55  m_tuple2 = nt2;
56  else {
57  m_tuple2 = ntupleSvc()->book( "MyTuples/simple/2", CLID_ColumnWiseTuple, "Col-Wise NTuple examples" );
58  if ( m_tuple2 ) {
59  m_tuple2->addItem( "N", m_n, 0, 100 ).ignore();
60  m_tuple2->addItem( "FNumbers", m_n, m_fNumbers ).ignore();
61  m_tuple2->addItem( "INumbers", m_n, m_iNumbers ).ignore();
62  } else { // did not manage to book the N tuple....
63  error() << " Cannot book N-tuple:" << long( m_tuple2 ) << endmsg;
64  return StatusCode::FAILURE;
65  }
66  }
67  info() << "Finished booking NTuples" << endmsg;
68  return StatusCode::SUCCESS;
69 }
70 
71 //------------------------------------------------------------------------------
73 //------------------------------------------------------------------------------
74 {
75  StatusCode status;
76 
77  static int n = 0;
78 
79  m_ntrk = long( sin( double( n ) ) * 52. + 50. );
80  m_energy = float( sin( double( n ) ) * 52. + 50. );
81 
82  m_n = long( abs( ( 12345 * ( n + 1 ) ) % 100 ) );
83 
84  for ( int i = 0; i < m_n; i++ ) {
85  m_fNumbers[i] = float( cos( double( 2 * n ) ) * 52. + 50. );
86  m_iNumbers[i] = long( cos( double( 2 * n ) ) * 52. + 50. );
87  }
88  n++;
89 
90  status = m_tuple1->write();
91  if ( status.isFailure() ) {
92  error() << " Cannot fill N-tuple:" << long( m_tuple1 ) << endmsg;
93  return StatusCode::FAILURE;
94  }
95  status = m_tuple2->write();
96  if ( status.isFailure() ) {
97  error() << " Cannot fill N-tuple:" << long( m_tuple2 ) << endmsg;
98  return StatusCode::FAILURE;
99  }
100 
101  return StatusCode::SUCCESS;
102 }
103 
104 //------------------------------------------------------------------------------
106 //------------------------------------------------------------------------------
107 {
108  info() << "Finalizing..." << endmsg;
109  return StatusCode::SUCCESS;
110 }
NTupleAlgorithm::m_tuple2
NTuple::Tuple * m_tuple2
Definition: NTupleAlgorithm.h:33
NTupleAlgorithm
Definition: NTupleAlgorithm.h:20
NTupleAlgorithm::m_ntrk
NTuple::Item< long > m_ntrk
Definition: NTupleAlgorithm.h:35
NTupleAlgorithm::m_energy
NTuple::Item< float > m_energy
Definition: NTupleAlgorithm.h:36
ISvcLocator
Definition: ISvcLocator.h:46
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
NTupleAlgorithm::finalize
StatusCode finalize() override
Definition: NTupleAlgorithm.cpp:105
NTupleAlgorithm::m_n
NTuple::Item< long > m_n
Definition: NTupleAlgorithm.h:40
std::abs
Gaudi::ParticleID abs(const Gaudi::ParticleID &p)
Return the absolute value for a PID.
Definition: ParticleID.h:191
NTupleAlgorithm.h
StatusCode
Definition: StatusCode.h:65
INTuple::write
virtual StatusCode write()=0
Write record of the NTuple (Shortcut of writeRecord)
NTupleAlgorithm::initialize
StatusCode initialize() override
Definition: NTupleAlgorithm.cpp:34
SmartDataPtr.h
NTupleAlgorithm::m_iNumbers
NTuple::Array< long > m_iNumbers
Definition: NTupleAlgorithm.h:38
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
NTuple::Tuple::addItem
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:516
cpluginsvc.n
n
Definition: cpluginsvc.py:234
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
NTupleAlgorithm::m_tuple1
NTuple::Tuple * m_tuple1
Definition: NTupleAlgorithm.h:32
SmartDataPtr
A small class used to access easily (and efficiently) data items residing in data stores.
Definition: SmartDataPtr.h:57
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
NTupleAlgorithm::execute
StatusCode execute() override
Definition: NTupleAlgorithm.cpp:72
Gaudi::Algorithm::ntupleSvc
SmartIF< INTupleSvc > & ntupleSvc() const
The standard N tuple service.
Definition: Algorithm.cpp:562
IHistogramSvc.h
INTupleSvc.h
MsgStream.h
NTupleAlgorithm::m_fNumbers
NTuple::Array< float > m_fNumbers
Definition: NTupleAlgorithm.h:39