Geant4-11
G4THnManager.icc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Ivana Hrivnacova, 23/06/2015 (ivana@ipno.in2p3.fr)
28
29#include "G4AnalysisManagerState.hh"
30#include "G4HnManager.hh"
31#include "G4AnalysisUtilities.hh"
32#include "G4AutoLock.hh"
33
34using std::to_string;
35
36//_____________________________________________________________________________
37template <typename HT>
38G4THnManager<HT>::G4THnManager(const G4AnalysisManagerState& state,
39 const G4String& hnType)
40 : fState(state)
41{
42 fHnManager = std::make_shared<G4HnManager>(hnType, state);
43}
44
45//_____________________________________________________________________________
46template <typename HT>
47G4THnManager<HT>::~G4THnManager()
48{
49 for ( auto t : fTVector ) {
50 delete t;
51 }
52}
53
54//
55// protected methods
56//
57
58//_____________________________________________________________________________
59template <typename HT>
60HT* G4THnManager<HT>::GetTInFunction(G4int id,
61 std::string_view functionName, G4bool warn,
62 G4bool onlyIfActive) const
63{
64 G4int index = id - fHnManager->GetFirstId();
65 if ( index < 0 || index >= G4int(fTVector.size()) ) {
66 if ( warn) {
67 G4Analysis::Warn("Histogram " + to_string(id) + " does not exist.",
68 fkClass, functionName);
69 }
70 return nullptr;
71 }
72
73 // Do not return histogram if inactive
74 if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) {
75 return nullptr;
76 }
77
78 return fTVector[index];
79}
80
81//_____________________________________________________________________________
82template <typename HT>
83G4int G4THnManager<HT>::GetTId(const G4String& name, G4bool warn) const
84{
85 auto it = fNameIdMap.find(name);
86 if ( it == fNameIdMap.end() ) {
87 if ( warn) {
88 G4Analysis::Warn("histogram " + name + " does not exist.",
89 fkClass, "GetTId");
90 }
91 return G4Analysis::kInvalidId;
92 }
93 return it->second;
94}
95
96//_____________________________________________________________________________
97template <typename HT>
98G4bool G4THnManager<HT>::IsVerbose(G4int verboseLevel) const
99{
100 return fState.IsVerbose(verboseLevel);
101}
102
103//_____________________________________________________________________________
104template <typename HT>
105void G4THnManager<HT>::Message(
106 G4int level, const G4String& action, const G4String& objectType,
107 const G4String& objectName, G4bool success) const
108{
109 fState.Message(level, action, objectType, objectName, success);
110}
111
112//_____________________________________________________________________________
113template <typename HT>
114void G4THnManager<HT>::AddTVector(const std::vector<HT*>& tVector)
115{
116 Message(G4Analysis::kVL4, "merge", "all " + fHnManager->GetHnType());
117
118 // std::vector<tools::histo::h1d*>::const_iterator itw = h1Vector.begin();
119 // std::vector<tools::histo::h1d*>::iterator it;
120 // for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
121 // (*it)->add(*(*itw++));
122 // }
123 auto itw = tVector.begin();
124 for ( auto t : fTVector ) {
125 t->add(*(*itw++));
126 }
127
128 Message(G4Analysis::kVL1, "merge", "all " + fHnManager->GetHnType());
129}
130
131//_____________________________________________________________________________
132template <typename HT>
133void G4THnManager<HT>::Merge(
134 G4Mutex& mergeMutex, G4THnManager<HT>* masterInstance)
135{
136 G4AutoLock lH1(&mergeMutex);
137 masterInstance->AddTVector(fTVector);
138 lH1.unlock();
139}
140
141//_____________________________________________________________________________
142template <typename HT>
143typename std::vector<HT*>::iterator G4THnManager<HT>::BeginT()
144{
145 return fTVector.begin();
146}
147
148//_____________________________________________________________________________
149template <typename HT>
150typename std::vector<HT*>::iterator G4THnManager<HT>::EndT()
151{
152 return fTVector.end();
153}
154
155//_____________________________________________________________________________
156template <typename HT>
157typename std::vector<HT*>::const_iterator G4THnManager<HT>::BeginConstT() const
158{
159 return fTVector.begin();
160}
161
162//_____________________________________________________________________________
163template <typename HT>
164typename std::vector<HT*>::const_iterator G4THnManager<HT>::EndConstT() const
165{
166 return fTVector.end();
167}
168
169//
170// public methods
171//
172
173//_____________________________________________________________________________
174template <typename HT>
175G4int G4THnManager<HT>::RegisterT(HT* t, const G4String& name)
176{
177 G4int index = fTVector.size();
178 fTVector.push_back(t);
179
180 fHnManager->SetLockFirstId(true);
181 fNameIdMap[name] = index + fHnManager->GetFirstId();
182 return index + fHnManager->GetFirstId();
183}
184
185//_____________________________________________________________________________
186template <typename HT>
187G4bool G4THnManager<HT>::Reset()
188{
189// Reset histograms
190
191 auto result = true;
192
193 for ( auto t : fTVector ) {
194 result &= t->reset();
195 }
196
197 return result;
198}
199
200//_____________________________________________________________________________
201template <typename HT>
202void
203G4THnManager<HT>::ClearData()
204{
205 for ( auto t : fTVector ) {
206 delete t;
207 }
208
209 fTVector.clear();
210 fNameIdMap.clear();
211
212 if ( fHnManager != nullptr ) {
213 fHnManager->ClearData();
214 }
215
216 Message(G4Analysis::kVL2, "clear", G4Analysis::GetHnType<HT>());
217}
218
219//_____________________________________________________________________________
220template <typename HT>
221G4bool G4THnManager<HT>::IsEmpty() const
222{
223 return ! fTVector.size();
224}
225
226//_____________________________________________________________________________
227template <typename HT>
228HT* G4THnManager<HT>::GetT(G4int id) const
229{
230 return GetTInFunction(id, "GetT");
231}