2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
27#include "G4AnalysisManagerState.hh"
28#include "G4AnalysisUtilities.hh"
33// private template functions
36//_____________________________________________________________________________
37template <typename NT, typename FT>
38G4TNtupleManager<NT, FT>::G4TNtupleManager(
39 const G4AnalysisManagerState& state)
40 : G4BaseNtupleManager(state)
43//_____________________________________________________________________________
44template <typename NT, typename FT>
45G4TNtupleManager<NT, FT>::~G4TNtupleManager()
47 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
48 delete ntupleDescription;
52//_____________________________________________________________________________
53template <typename NT, typename FT>
54G4TNtupleDescription<NT, FT>*
55G4TNtupleManager<NT, FT>::GetNtupleDescriptionInFunction(
56 G4int id, std::string_view functionName, G4bool warn) const
58 auto index = id - fFirstId;
59 if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
61 G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
62 fkClass, functionName);
67 return fNtupleDescriptionVector[index];
70//_____________________________________________________________________________
71template <typename NT, typename FT>
72NT* G4TNtupleManager<NT, FT>::GetNtupleInFunction(
73 G4int id, std::string_view functionName, G4bool warn) const
75 auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
76 if ( ! ntupleDescription ) return nullptr;
78 if ( ! ntupleDescription->fNtuple ) {
80 G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.",
81 fkClass, functionName);
86 return ntupleDescription->fNtuple;
89//_____________________________________________________________________________
90template <typename NT, typename FT>
92G4bool G4TNtupleManager<NT, FT>::FillNtupleTColumn(
93 G4int ntupleId, G4int columnId, const T& value)
95 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
96 //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
101 auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
102 if ( ! ntuple ) return false;
104 // get generic column
105 auto index = columnId - fFirstNtupleColumnId;
106 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
108 "Ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
110 fkClass, "FillNtupleTColumn");
113 auto icolumn = ntuple->columns()[index];
115 // get column and check its type
116 auto column = dynamic_cast<typename NT::template column<T>* >(icolumn);
119 "Column type does not match: "
120 " ntuple " + to_string(ntupleId) + " column " + to_string(columnId) +
121 " value " + G4Analysis::ToString(value),
122 fkClass, "FillNtupleTColumn");
128 if ( IsVerbose(G4Analysis::kVL4) ) {
129 Message(G4Analysis::kVL4, "fill", "ntuple T column",
130 " ntupleId " + to_string(ntupleId) +
131 " column " + to_string(columnId) +
132 " value " + G4Analysis::ToString<T>(value));
139// protected functions
142//_____________________________________________________________________________
143template <typename NT, typename FT>
144G4int G4TNtupleManager<NT, FT>::CreateNtuple(G4NtupleBooking* ntupleBooking)
146 Message(G4Analysis::kVL4, "create from booking", "ntuple",
147 ntupleBooking->fNtupleBooking.name());
150 auto index = ntupleBooking->fNtupleId - fFirstId;
152 // Check if the ntuple description of this id already exists
153 // (what should normally never happen)
154 // delete it and print a warning
155 if ( index < G4int(fNtupleDescriptionVector.size()) &&
156 fNtupleDescriptionVector[index] ) {
157 delete fNtupleDescriptionVector[index];
159 "Ntuple description " + to_string(ntupleBooking->fNtupleId) +
160 " already exists.", fkClass, "CreateNtuple");
163 // Allocate the vector element(s)
164 while ( index >= G4int(fNtupleDescriptionVector.size()) ) {
165 fNtupleDescriptionVector.push_back(nullptr);
168 // Create ntuple description from ntuple booking.
169 auto ntupleDescription = new G4TNtupleDescription<NT, FT>(ntupleBooking);
170 fNtupleDescriptionVector[index] = ntupleDescription;
172 // Do not create ntuple if it is inactivated
173 if ( fState.GetIsActivation() &&
174 ( ! ntupleDescription->fActivation ) ) return G4Analysis::kInvalidId;
176 // Do not create ntuple if it already exists
177 if ( ntupleDescription->fNtuple ) {
179 "Ntuple " + to_string(ntupleBooking->fNtupleId) +
180 " already exists.", fkClass, "CreateNtuple");
181 return ntupleBooking->fNtupleId;
185 CreateTNtupleFromBooking(ntupleDescription);
187 // finish created ntuple
188 auto fromBooking = true;
189 FinishTNtuple(ntupleDescription, fromBooking);
191 Message(G4Analysis::kVL3, "create from booking", "ntuple",
192 ntupleBooking->fNtupleBooking.name());
194 return ntupleBooking->fNtupleId;
197//_____________________________________________________________________________
198template <typename NT, typename FT>
199void G4TNtupleManager<NT, FT>::CreateNtuplesFromBooking(
200 const std::vector<G4NtupleBooking*>& ntupleBookings)
202// Create ntuple from ntuple bookings.
204 // Create ntuple descriptions from ntuple booking.
205 for ( auto ntupleBooking : ntupleBookings ) {
206 CreateNtuple(ntupleBooking);
210//_____________________________________________________________________________
211template <typename NT, typename FT>
213G4TNtupleManager<NT, FT>::Reset()
215 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
216 delete ntupleDescription;
219 fNtupleDescriptionVector.clear();
220 fNtupleVector.clear();
225//_____________________________________________________________________________
226template <typename NT, typename FT>
228G4TNtupleManager<NT, FT>::Clear()
230 // Reset function clears all data
233 Message(G4Analysis::kVL2, "clear", "ntuples");
236//_____________________________________________________________________________
237template <typename NT, typename FT>
238G4bool G4TNtupleManager<NT, FT>::FillNtupleIColumn(
239 G4int ntupleId, G4int columnId, G4int value)
241 return FillNtupleTColumn<int>(ntupleId, columnId, value);
243//_____________________________________________________________________________
244template <typename NT, typename FT>
245G4bool G4TNtupleManager<NT, FT>::FillNtupleFColumn(
246 G4int ntupleId, G4int columnId, G4float value)
248 return FillNtupleTColumn<float>(ntupleId, columnId, value);
251//_____________________________________________________________________________
252template <typename NT, typename FT>
253G4bool G4TNtupleManager<NT, FT>::FillNtupleDColumn(
254 G4int ntupleId, G4int columnId, G4double value)
256 return FillNtupleTColumn<double>(ntupleId, columnId, value);
259//_____________________________________________________________________________
260template <typename NT, typename FT>
261G4bool G4TNtupleManager<NT, FT>::FillNtupleSColumn(
262 G4int ntupleId, G4int columnId, const G4String& value)
264 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
267//_____________________________________________________________________________
268template <typename NT, typename FT>
269G4bool G4TNtupleManager<NT, FT>::AddNtupleRow(
272 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
273 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
277 if ( IsVerbose(G4Analysis::kVL4) ) {
278 Message(G4Analysis::kVL4, "add", "ntuple row",
279 " ntupleId " + to_string(ntupleId));
282 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
283 if ( ! ntupleDescription ) return false;
285 auto ntuple = ntupleDescription->fNtuple;
286 if ( ! ntuple ) return false;
288 auto result = ntuple->add_row();
291 "Ntuple " + to_string(ntupleId) + " adding row has failed.",
292 fkClass, "AddTNtupleRow");
295 ntupleDescription->fHasFill = true;
297 if ( IsVerbose(G4Analysis::kVL4) ) {
298 Message(G4Analysis::kVL4, "add", "ntuple row",
299 " ntupleId " + to_string(ntupleId));
305//_____________________________________________________________________________
306template <typename NT, typename FT>
307void G4TNtupleManager<NT, FT>::SetActivation(
310 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
311 ntupleDescription->fActivation = activation;
315//_____________________________________________________________________________
316template <typename NT, typename FT>
317void G4TNtupleManager<NT, FT>::SetActivation(
318 G4int ntupleId, G4bool activation)
320 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
321 if ( ! ntupleDescription ) return;
323 ntupleDescription->fActivation = activation;
326//_____________________________________________________________________________
327template <typename NT, typename FT>
328G4bool G4TNtupleManager<NT, FT>::GetActivation(
329 G4int ntupleId) const
331 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
332 if ( ! ntupleDescription ) return false;
334 return ntupleDescription->fActivation;
337//_____________________________________________________________________________
338template <typename NT, typename FT>
340G4TNtupleManager<NT, FT>::GetNtuple() const
342 return GetNtuple(fFirstId);
345//_____________________________________________________________________________
346template <typename NT, typename FT>
348G4TNtupleManager<NT, FT>::GetNtuple(G4int ntupleId) const
350 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple");
351 if ( ! ntupleDescription ) return nullptr;
353 return ntupleDescription->fNtuple;
356//_____________________________________________________________________________
357template <typename NT, typename FT>
358typename std::vector<NT*>::iterator
359G4TNtupleManager<NT, FT>::BeginNtuple()
361 return fNtupleVector.begin();
364//_____________________________________________________________________________
365template <typename NT, typename FT>
366typename std::vector<NT*>::iterator
367G4TNtupleManager<NT, FT>::EndNtuple()
369 return fNtupleVector.end();
372//_____________________________________________________________________________
373template <typename NT, typename FT>
374typename std::vector<NT*>::const_iterator
375G4TNtupleManager<NT, FT>::BeginConstNtuple() const
377 return fNtupleVector.begin();
380//_____________________________________________________________________________
381template <typename NT, typename FT>
382typename std::vector<NT*>::const_iterator
383G4TNtupleManager<NT, FT>::EndConstNtuple() const
385 return fNtupleVector.end();
388//_____________________________________________________________________________
389template <typename NT, typename FT>
390G4int G4TNtupleManager<NT, FT>::GetNofNtuples() const
392 return G4int(fNtupleVector.size());