The Gaudi Framework  master (c30e4d23)
Loading...
Searching...
No Matches
WideNTupleAlgorithm.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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\***********************************************************************************/
14#include <GaudiKernel/NTuple.h>
15
16#include <cmath>
17#include <memory>
18#include <string>
19#include <vector>
20
33public:
35
36 StatusCode initialize() override;
37 StatusCode execute() override;
38
39private:
40 Gaudi::Property<int> m_nTuples{ this, "NTuples", 4, "number of tuples to book" };
41 Gaudi::Property<int> m_nColumns{ this, "Columns", 64, "approximate number of columns per tuple" };
42 Gaudi::Property<int> m_entries{ this, "EntriesPerTuple", 10, "entries written to each tuple" };
43 Gaudi::Property<int> m_maxArray{ this, "MaxArraySize", 8, "declared maximum of the variable columns" };
44 Gaudi::Property<std::string> m_dir{ this, "Directory", "MyTuples", "top-level directory" };
45
47 struct Block {
48 NTuple::Tuple* tuple = nullptr;
51 std::vector<NTuple::Item<double>> d;
52 std::vector<NTuple::Item<float>> f;
53 std::vector<NTuple::Item<int>> i;
54 std::vector<NTuple::Item<bool>> b;
55 std::vector<NTuple::Array<double>> var;
56 std::vector<NTuple::Array<int>> var2;
57 std::vector<NTuple::Array<float>> fixed;
58 std::vector<NTuple::Matrix<double>> mat;
59 std::vector<NTuple::Matrix<float>> fmat;
60 };
61 std::vector<std::unique_ptr<Block>> m_blocks;
62 int m_event = 0;
63};
64
66
69 if ( sc.isFailure() ) return sc;
70
71 // Split the requested width evenly over the column kinds
72 const int nEach = std::max( 1, m_nColumns / 9 );
73
74 for ( int t = 0; t < m_nTuples; ++t ) {
75 auto blk = std::make_unique<Block>();
76 std::string path = m_dir.value() + "/t" + std::to_string( t );
77 blk->tuple = ntupleSvc()->book( path, CLID_ColumnWiseTuple, "wide tuple " + std::to_string( t ) );
78 if ( !blk->tuple ) {
79 error() << "cannot book " << path << endmsg;
81 }
82 blk->d.resize( nEach );
83 blk->f.resize( nEach );
84 blk->i.resize( nEach );
85 blk->b.resize( nEach );
86 blk->var.resize( nEach );
87 blk->var2.resize( nEach );
88 blk->fixed.resize( nEach );
89 blk->mat.resize( nEach );
90 blk->fmat.resize( nEach );
91
92 // The index has to be declared before the columns it counts: ROOT resolves a
93 // leaf count by name against the leaves already in the tree.
94 if ( blk->tuple->addItem( "n", blk->n, 0, m_maxArray.value() ).isFailure() ) return StatusCode::FAILURE;
95 if ( blk->tuple->addItem( "m", blk->m, 0, m_maxArray.value() ).isFailure() ) return StatusCode::FAILURE;
96 for ( int c = 0; c < nEach; ++c ) {
97 const auto s = std::to_string( c );
98 if ( blk->tuple->addItem( "d" + s, blk->d[c] ).isFailure() ) return StatusCode::FAILURE;
99 if ( blk->tuple->addItem( "f" + s, blk->f[c] ).isFailure() ) return StatusCode::FAILURE;
100 if ( blk->tuple->addItem( "i" + s, blk->i[c] ).isFailure() ) return StatusCode::FAILURE;
101 if ( blk->tuple->addItem( "b" + s, blk->b[c] ).isFailure() ) return StatusCode::FAILURE;
102 if ( blk->tuple->addIndexedItem( "var" + s, blk->n, blk->var[c] ).isFailure() ) return StatusCode::FAILURE;
103 if ( blk->tuple->addIndexedItem( "var2_" + s, blk->m, blk->var2[c] ).isFailure() ) return StatusCode::FAILURE;
104 if ( blk->tuple->addItem( "fix" + s, 3, blk->fixed[c] ).isFailure() ) return StatusCode::FAILURE;
105 if ( blk->tuple->addIndexedItem( "mat" + s, blk->n, 2, blk->mat[c] ).isFailure() ) return StatusCode::FAILURE;
106 if ( blk->tuple->addItem( "fmat" + s, 2, 3, blk->fmat[c] ).isFailure() ) return StatusCode::FAILURE;
107 }
108 m_blocks.push_back( std::move( blk ) );
109 }
110 info() << "booked " << m_blocks.size() << " tuples of about " << m_nColumns.value() << " columns" << endmsg;
111 return StatusCode::SUCCESS;
112}
113
115 const int e = m_event++;
116 if ( e >= m_entries ) return StatusCode::SUCCESS;
117
118 for ( size_t t = 0; t < m_blocks.size(); ++t ) {
119 auto& blk = *m_blocks[t];
120 // Zero on some entries on purpose
121 blk.n = ( e * 3 + static_cast<int>( t ) ) % ( m_maxArray + 1 );
122 blk.m = ( e * 5 + 2 * static_cast<int>( t ) ) % ( m_maxArray + 1 );
123 for ( size_t c = 0; c < blk.d.size(); ++c ) {
124 const double v = 1000.0 * t + 10.0 * e + c;
125 blk.d[c] = v;
126 blk.f[c] = static_cast<float>( -v );
127 blk.i[c] = static_cast<int>( v );
128 blk.b[c] = ( ( e + c ) % 2 ) == 0;
129 for ( int k = 0; k < blk.n; ++k ) {
130 blk.var[c][k] = v + 0.25 * k;
131 blk.mat[c][k][0] = v + 0.5 * k;
132 blk.mat[c][k][1] = v - 0.5 * k;
133 }
134 for ( int k = 0; k < blk.m; ++k ) blk.var2[c][k] = static_cast<int>( v ) - k;
135 for ( int k = 0; k < 3; ++k ) blk.fixed[c][k] = static_cast<float>( v + k );
136 for ( int k = 0; k < 2; ++k )
137 for ( int l = 0; l < 3; ++l ) blk.fmat[c][k][l] = static_cast<float>( v + 10 * k + l );
138 }
139 if ( blk.tuple->write().isFailure() ) {
140 error() << "cannot fill tuple " << t << endmsg;
141 return StatusCode::FAILURE;
142 }
143 }
144 return StatusCode::SUCCESS;
145}
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 & info() const
shortcut for the method msgStream(MSG::INFO)
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition Algorithm.h:175
SmartIF< INTupleSvc > & ntupleSvc() const
The standard N tuple service.
Implementation of property with value of concrete type.
Definition Property.h:35
Class acting as a smart pointer holding a N tuple _Item.
Definition NTuple.h:251
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition NTuple.h:380
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:118
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
Books a configurable grid of column-wise tuples: many trees, many columns, few entries.
std::vector< std::unique_ptr< Block > > m_blocks
Gaudi::Property< int > m_nColumns
Gaudi::Property< std::string > m_dir
Gaudi::Property< int > m_maxArray
StatusCode execute() override
StatusCode initialize() override
Gaudi::Property< int > m_entries
Gaudi::Property< int > m_nTuples
One booked tuple and everything written into it.
std::vector< NTuple::Matrix< double > > mat
std::vector< NTuple::Matrix< float > > fmat
std::vector< NTuple::Array< int > > var2
std::vector< NTuple::Item< double > > d
std::vector< NTuple::Item< int > > i
std::vector< NTuple::Array< double > > var
std::vector< NTuple::Item< bool > > b
std::vector< NTuple::Item< float > > f
std::vector< NTuple::Array< float > > fixed