Geant4  10.03
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 // $Id: G4THnManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 23/06/2015 (ivana@ipno.in2p3.fr)
29 
30 #include "G4AnalysisManagerState.hh"
31 #include "G4HnManager.hh"
32 #include "G4AnalysisUtilities.hh"
33 
34 #include <iostream>
35 
36 //_____________________________________________________________________________
37 template <typename T>
38 G4THnManager<T>::G4THnManager(const G4AnalysisManagerState& state,
39  const G4String& hnType)
40  : fState(state),
41  fTVector(),
42  fNameIdMap(),
43  fHnManager(nullptr)
44 {
45  fHnManager = std::make_shared<G4HnManager>(hnType, state);
46 }
47 
48 //_____________________________________________________________________________
49 template <typename T>
50 G4THnManager<T>::~G4THnManager()
51 {
52  for ( auto t : fTVector ) {
53  delete t;
54  }
55 }
56 
57 //
58 // protected methods
59 //
60 
61 //_____________________________________________________________________________
62 template <typename T>
63 T* G4THnManager<T>::GetTInFunction(G4int id,
64  G4String functionName, G4bool warn,
65  G4bool onlyIfActive) const
66 {
67  G4int index = id - fHnManager->GetFirstId();
68  if ( index < 0 || index >= G4int(fTVector.size()) ) {
69  if ( warn) {
70  G4String inFunction = "G4THnManager::";
71  inFunction += functionName;
72  G4ExceptionDescription description;
73  description << " " << "histogram " << id << " does not exist.";
74  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
75  }
76  return nullptr;
77  }
78 
79  // Do not return histogram if inactive
80  if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) {
81  return nullptr;
82  }
83 
84  return fTVector[index];
85 }
86 
87 //_____________________________________________________________________________
88 template <typename T>
89 G4int G4THnManager<T>::RegisterT(T* t, const G4String& name)
90 {
91  G4int index = fTVector.size();
92  fTVector.push_back(t);
93 
94  fHnManager->SetLockFirstId(true);
95  fNameIdMap[name] = index + fHnManager->GetFirstId();
96  return index + fHnManager->GetFirstId();
97 }
98 
99 //_____________________________________________________________________________
100 template <typename T>
101 G4int G4THnManager<T>::GetTId(const G4String& name, G4bool warn) const
102 {
103  auto it = fNameIdMap.find(name);
104  if ( it == fNameIdMap.end() ) {
105  if ( warn) {
106  G4String inFunction = "G4THnManager::GetH1Id";
107  G4ExceptionDescription description;
108  description << " " << "histogram " << name << " does not exist.";
109  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
110  }
111  return G4Analysis::kInvalidId;
112  }
113  return it->second;
114 }
115 
116 //_____________________________________________________________________________
117 template <typename T>
118 void G4THnManager<T>::AddTVector(const std::vector<T*>& tVector)
119 {
120 #ifdef G4VERBOSE
121  if ( fState.GetVerboseL4() )
122  fState.GetVerboseL4()->Message("merge", "all " + fHnManager->GetHnType(), "");
123 #endif
124  // std::vector<tools::histo::h1d*>::const_iterator itw = h1Vector.begin();
125  // std::vector<tools::histo::h1d*>::iterator it;
126  // for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
127  // (*it)->add(*(*itw++));
128  // }
129  auto itw = tVector.begin();
130  for ( auto t : fTVector ) {
131  t->add(*(*itw++));
132  }
133 #ifdef G4VERBOSE
134  if ( fState.GetVerboseL1() )
135  fState.GetVerboseL1()->Message("merge", "all " + fHnManager->GetHnType(), "");
136 #endif
137 }
138 
139 //_____________________________________________________________________________
140 template <typename T>
141 typename std::vector<T*>::iterator G4THnManager<T>::BeginT()
142 {
143  return fTVector.begin();
144 }
145 
146 //_____________________________________________________________________________
147 template <typename T>
148 typename std::vector<T*>::iterator G4THnManager<T>::EndT()
149 {
150  return fTVector.end();
151 }
152 
153 //_____________________________________________________________________________
154 template <typename T>
155 typename std::vector<T*>::const_iterator G4THnManager<T>::BeginConstT() const
156 {
157  return fTVector.begin();
158 }
159 
160 //_____________________________________________________________________________
161 template <typename T>
162 typename std::vector<T*>::const_iterator G4THnManager<T>::EndConstT() const
163 {
164  return fTVector.end();
165 }
166 
167 //
168 // public methods
169 //
170 
171 //_____________________________________________________________________________
172 template <typename T>
173 G4bool G4THnManager<T>::Reset()
174 {
175 // Reset histograms and ntuple
176 
177  G4bool finalResult = true;
178 
179  for ( auto t : fTVector ) {
180  G4bool result = t->reset();
181  if ( ! result ) finalResult = false;
182  }
183 
184  return finalResult;
185 }
186 
187 //_____________________________________________________________________________
188 template <typename T>
189 G4bool G4THnManager<T>::IsEmpty() const
190 {
191  return ! fTVector.size();
192 }