The Gaudi Framework  master (ba5b4fb7)
Loading...
Searching...
No Matches
THistWrite.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\***********************************************************************************/
11// Include files
12#include "THistWrite.h"
13
18#include <math.h>
19
20#include <TDirectory.h>
21#include <TError.h>
22#include <TH1F.h>
23#include <TH2F.h>
24#include <TH3F.h>
25#include <TKey.h>
26#include <TProfile.h>
27#include <TTree.h>
28
30
31//------------------------------------------------------------------------------
32THistWrite::THistWrite( const std::string& name, ISvcLocator* pSvcLocator )
33 : Algorithm( name, pSvcLocator )
34//------------------------------------------------------------------------------
35{}
36
37//------------------------------------------------------------------------------
39//------------------------------------------------------------------------------
40{
41
42 if ( m_ths.retrieve().isFailure() ) {
43 error() << "Couldn't get THistSvc" << endmsg;
45 }
46
47 // Temporary Trees
48 std::unique_ptr<TH1F> h1 = std::make_unique<TH1F>( "TempHist1", "Temporary Tree 1", 100, 0., 100. );
49 if ( m_ths->regHist( "TempHist1", std::move( h1 ) ).isFailure() ) {
50 error() << "Couldn't register TempHist1" << endmsg;
51 }
52
53 std::unique_ptr<TH1F> h1a = std::make_unique<TH1F>( "TempHist1a", "Temporary Tree 1a", 100, 0., 100. );
54 if ( m_ths->regHist( "other/TempHist1a", std::move( h1a ) ).isFailure() ) {
55 error() << "Couldn't register TempHist1a" << endmsg;
56 }
57
58 // Write to stream "new"
59 std::unique_ptr<TH1F> h2 = std::make_unique<TH1F>( "Tree2", "Tree 2", 100, 0., 100. );
60 if ( m_ths->regHist( "/new/Tree2", std::move( h2 ) ).isFailure() ) { error() << "Couldn't register Tree2" << endmsg; }
61
62 // Update to stream "upd", dir "/xxx"
63 std::unique_ptr<TH1F> h3 = std::make_unique<TH1F>( "1Dgauss", "1D Gaussian", 100, -50., 50. );
64 if ( m_ths->regHist( "/upd/xxx/gauss1d", std::move( h3 ) ).isFailure() ) {
65 error() << "Couldn't register gauss1d" << endmsg;
66 }
67
68 // Recreate 2D tree in "/"
69 std::unique_ptr<TH2F> h3a = std::make_unique<TH2F>( "2Dgauss", "2D Gaussian", 100, -50., 50., 100, -50, 50 );
70 if ( m_ths->regHist( "/rec/gauss2d", std::move( h3a ) ).isFailure() ) {
71 error() << "Couldn't register gauss2d" << endmsg;
72 }
73
74 // 3D tree in "/"
75 std::unique_ptr<TH3F> h4 =
76 std::make_unique<TH3F>( "3Dgauss", "3D Gaussian", 100, -50., 50., 100, -50, 50, 100, -50, 50 );
77 if ( m_ths->regHist( "/rec/gauss3d", std::move( h4 ) ).isFailure() ) {
78 error() << "Couldn't register gauss3d" << endmsg;
79 }
80
81 TH1F* h5 = new TH1F( "TempHist5", "Temporary Tree 5", 100, 0., 100. );
82 if ( m_ths->regHist( "TempHist5", std::unique_ptr<TH1F>( h5 ) ).isFailure() ) {
83 error() << "Couldn't register TempHist5" << endmsg;
84 }
85 if ( strcmp( h5->GetName(), "TempHist5" ) ) {
86 error() << "Couldn't use TempHist5 afterwards. getName = " << h5->GetName() << endmsg;
87 }
88
89 TH1D* h7 = new TH1D( "TempHist7", "Temporary Tree 7", 100, 0., 100. );
90 if ( m_ths->regHist( "TempHist7", h7 ).isFailure() ) { error() << "Couldn't register TempHist7" << endmsg; }
91 if ( strcmp( h7->GetName(), "TempHist7" ) ) {
92 error() << "Couldn't use TempHist7 afterwards. getName = " << h7->GetName() << endmsg;
93 }
94
95 // Profile in "/"
96 std::unique_ptr<TH1> tp = std::make_unique<TProfile>( "profile", "profile", 100, -50., -50. );
97 if ( m_ths->regHist( "/rec/prof", std::move( tp ) ).isFailure() ) { error() << "Couldn't register prof" << endmsg; }
98
99 // Tree with branches in "/trees/stuff"
100 std::unique_ptr<TTree> tr = std::make_unique<TTree>( "treename", "tree title" );
101 if ( m_ths->regTree( "/rec/trees/stuff/tree1", std::move( tr ) ).isFailure() ) {
102 error() << "Couldn't register tr" << endmsg;
103 }
104
105 // Update to stream "upd", dir "/xxx"
106 std::unique_ptr<TH1F> h3s = std::make_unique<TH1F>( "1Dgauss_shared", "1D Gaussian", 100, -50., 50. );
107 LockedHandle<TH1> lh1( nullptr, nullptr );
108 if ( m_ths->regShared( "/upd/xxx/gauss1d_shared", std::move( h3s ), lh1 ).isFailure() ) {
109 error() << "Couldn't register gauss1d_shared" << endmsg;
110 }
111
112 // Recreate 2D tree in "/"
113 std::unique_ptr<TH2F> h3sa = std::make_unique<TH2F>( "2Dgauss_shared", "2D Gaussian", 100, -50., 50., 100, -50, 50 );
114 LockedHandle<TH2> lh2( nullptr, nullptr );
115 if ( m_ths->regShared( "/rec/gauss2d_shared", std::move( h3sa ), lh2 ).isFailure() ) {
116 error() << "Couldn't register gauss2d_shared" << endmsg;
117 }
118
119 // 3D tree in "/"
120 std::unique_ptr<TH3F> h4s =
121 std::make_unique<TH3F>( "3Dgauss_shared", "3D Gaussian", 100, -50., 50., 100, -50, 50, 100, -50, 50 );
122 LockedHandle<TH3> lh3( nullptr, nullptr );
123 if ( m_ths->regShared( "/rec/gauss3d_shared", std::move( h4s ), lh3 ).isFailure() ) {
124 error() << "Couldn't register gauss3d_shared" << endmsg;
125 }
126
127 return StatusCode::SUCCESS;
128}
129
130//------------------------------------------------------------------------------
132//------------------------------------------------------------------------------
133{
134 Rndm::Numbers gauss( randSvc(), Rndm::Gauss( 0., 15. ) );
135
136 static int n = 0;
137
138 double x = sin( double( n ) ) * 52. + 50.;
139
140 TH1* h( nullptr );
141 if ( m_ths->getHist( "TempHist1", h ).isSuccess() ) {
142 h->Fill( x );
143 } else {
144 error() << "Couldn't retrieve TempHist 1" << endmsg;
145 }
146
147 if ( m_ths->getHist( "other/TempHist1a", h ).isSuccess() ) {
148 h->Fill( x );
149 } else {
150 error() << "Couldn't retrieve TempHist 1a" << endmsg;
151 }
152
153 if ( m_ths->getHist( "/new/Tree2", h ).isSuccess() ) {
154 h->Fill( x );
155 } else {
156 error() << "Couldn't retrieve Tree2" << endmsg;
157 }
158
159 if ( m_ths->getHist( "/upd/xxx/gauss1d", h ).isSuccess() ) {
160 for ( int i = 0; i < 1000; ++i ) { h->Fill( gauss(), 1. ); }
161 } else {
162 error() << "Couldn't retrieve 1Dgauss" << endmsg;
163 }
164
165 TH2* h2( nullptr );
166 if ( m_ths->getHist( "/rec/gauss2d", h2 ).isSuccess() ) {
167 for ( int i = 0; i < 1000; ++i ) { h2->Fill( gauss(), gauss(), 1. ); }
168 } else {
169 error() << "Couldn't retrieve 2Dgauss" << endmsg;
170 }
171
172 TH3* h3( nullptr );
173 if ( m_ths->getHist( "/rec/gauss3d", h3 ).isSuccess() ) {
174 for ( int i = 0; i < 1000; ++i ) { h3->Fill( gauss(), gauss(), gauss(), 1. ); }
175 } else {
176 error() << "Couldn't retrieve 3Dgauss" << endmsg;
177 }
178
179 if ( m_ths->getHist( "TempHist5", h ).isSuccess() ) {
180 h->Fill( x );
181 } else {
182 error() << "Couldn't retrieve TempHist 5" << endmsg;
183 }
184
185 LockedHandle<TH1> lh1( nullptr, nullptr );
186 if ( m_ths->getShared( "/upd/xxx/gauss1d_shared", lh1 ).isSuccess() ) {
187 for ( int i = 0; i < 1000; ++i ) { lh1->Fill( gauss(), 1. ); }
188 } else {
189 error() << "Couldn't retrieve 1Dgauss_shared" << endmsg;
190 }
191
192 LockedHandle<TH2> lh2( nullptr, nullptr );
193 if ( m_ths->getShared( "/rec/gauss2d_shared", lh2 ).isSuccess() ) {
194 for ( int i = 0; i < 1000; ++i ) { lh2->Fill( gauss(), gauss(), 1. ); }
195 } else {
196 error() << "Couldn't retrieve 2Dgauss_shared" << endmsg;
197 }
198
199 LockedHandle<TH3> lh3( nullptr, nullptr );
200 if ( m_ths->getShared( "/rec/gauss3d_shared", lh3 ).isSuccess() ) {
201 for ( int i = 0; i < 1000; ++i ) { lh3->Fill( gauss(), gauss(), gauss(), 1. ); }
202 } else {
203 error() << "Couldn't retrieve 3Dgauss_shared" << endmsg;
204 }
205
206 TTree* tr( nullptr );
207 if ( m_ths->getTree( "/rec/trees/stuff/tree1", tr ).isFailure() ) {
208 error() << "Couldn't retrieve tree tree1" << endmsg;
209 } else {
210 if ( n == 0 ) {
211 int p1, p2, p3;
212 tr->Branch( "branch1", &p1, "point1/I" );
213 tr->Branch( "branch2", &p2, "point2/I" );
214 tr->Branch( "branch3", &p3, "point3/I" );
215
216 for ( int i = 0; i < 1000; i++ ) {
217 p1 = i;
218 p2 = i % 10;
219 p3 = i % 7;
220
221 tr->Fill();
222 }
223 }
224 }
225
226 n++;
227 return StatusCode::SUCCESS;
228}
229
230//------------------------------------------------------------------------------
231void THistWrite::listKeys( TDirectory* td ) {
232 //------------------------------------------------------------------------------
233
234 info() << "printing keys for: " << td->GetPath() << " (" << td->GetList()->GetSize() << ")" << endmsg;
235 TIter nextkey( td->GetList() );
236 while ( TKey* key = (TKey*)nextkey() ) {
237 if ( key != 0 ) {
238 info() << key->GetName() << " (" << key->IsA()->GetName()
239 << ") "
240 // << key->GetObjectStat()
241 // << " " << key->IsOnHeap()
242 << key->GetCycle() << endmsg;
243 } else {
244 info() << "key == 0" << endmsg;
245 }
246 }
247
248 return;
249}
250
251//------------------------------------------------------------------------------
253//------------------------------------------------------------------------------
254{
255 info() << "Finalizing..." << endmsg;
256
257 TH1* h7 = nullptr;
258 if ( m_ths->getHist( "TempHist7", h7 ).isFailure() ) { error() << "Couldn't retrieve TempHist7" << endmsg; }
259 if ( m_ths->deReg( h7 ).isFailure() ) { error() << "Failed to deregister histogram TempHist7" << endmsg; }
260 delete h7;
261
262 return StatusCode::SUCCESS;
263}
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
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
const std::string & name() const override
The identifying name of the algorithm object.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
Provides automatic lock/unlock access to a class upon deref of ptr.
Parameters for the Gauss random number generation.
Random number accessor This small class encapsulates the use of the random number generator.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
ServiceHandle< ITHistSvc > m_ths
Definition THistWrite.h:33
StatusCode finalize() override
THistWrite(const std::string &name, ISvcLocator *pSvcLocator)
void listKeys(TDirectory *td)
StatusCode execute() override
StatusCode initialize() override
STL namespace.