Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4NtupleBookingManager Class Reference

#include <G4NtupleBookingManager.hh>

+ Inheritance diagram for G4NtupleBookingManager:

Public Member Functions

 G4NtupleBookingManager (const G4AnalysisManagerState &state)
 
 G4NtupleBookingManager ()=delete
 
 ~G4NtupleBookingManager () override
 
const std::vector< G4NtupleBooking * > & GetNtupleBookingVector () const
 
void SetFileType (const G4String &fileType)
 
G4String GetFileType () const
 
G4bool IsEmpty () const
 
tools::ntuple_booking * GetNtuple (G4bool warn, G4bool onlyIfActive) const
 
tools::ntuple_booking * GetNtuple (G4int ntupleId, G4bool warn, G4bool onlyIfActive) const
 
- Public Member Functions inherited from G4BaseAnalysisManager
 G4BaseAnalysisManager (const G4AnalysisManagerState &state)
 
 G4BaseAnalysisManager ()=delete
 
virtual ~G4BaseAnalysisManager ()=default
 
G4bool SetFirstId (G4int firstId)
 
void SetLockFirstId (G4bool lockFirstId)
 
G4int GetFirstId () const
 
G4int GetCycle () const
 

Protected Member Functions

G4int CreateNtuple (const G4String &name, const G4String &title)
 
G4int CreateNtupleIColumn (const G4String &name, std::vector< int > *vector)
 
G4int CreateNtupleFColumn (const G4String &name, std::vector< float > *vector)
 
G4int CreateNtupleDColumn (const G4String &name, std::vector< double > *vector)
 
G4int CreateNtupleSColumn (const G4String &name, std::vector< std::string > *vector)
 
G4NtupleBookingFinishNtuple ()
 
G4int CreateNtupleIColumn (G4int ntupleId, const G4String &name, std::vector< int > *vector)
 
G4int CreateNtupleFColumn (G4int ntupleId, const G4String &name, std::vector< float > *vector)
 
G4int CreateNtupleDColumn (G4int ntupleId, const G4String &name, std::vector< double > *vector)
 
G4int CreateNtupleSColumn (G4int ntupleId, const G4String &name, std::vector< std::string > *vector)
 
G4NtupleBookingFinishNtuple (G4int ntupleId)
 
G4bool SetFirstNtupleColumnId (G4int firstId)
 
G4int GetFirstNtupleColumnId () const
 
void SetActivation (G4bool activation)
 
void SetActivation (G4int ntupleId, G4bool activation)
 
G4bool GetActivation (G4int ntupleId) const
 
void SetFileName (const G4String &fileName)
 
void SetFileName (G4int id, const G4String &fileName)
 
G4String GetFileName (G4int id) const
 
G4int GetNofNtuples (G4bool onlyIfExist=false) const
 
void ClearData ()
 
G4bool Delete (G4int id, G4bool keepSetting)
 
G4bool List (std::ostream &output, G4bool onlyIfActive=true)
 
- Protected Member Functions inherited from G4BaseAnalysisManager
G4bool IsVerbose (G4int verboseLevel) const
 
void Message (G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
 

Protected Attributes

std::vector< G4NtupleBooking * > fNtupleBookingVector
 
std::set< G4intfFreeIds
 
- Protected Attributes inherited from G4BaseAnalysisManager
const G4AnalysisManagerStatefState
 
G4int fFirstId { 0 }
 
G4bool fLockFirstId { false }
 

Friends

class G4VAnalysisManager
 

Detailed Description

Definition at line 71 of file G4NtupleBookingManager.hh.

Constructor & Destructor Documentation

◆ G4NtupleBookingManager() [1/2]

G4NtupleBookingManager::G4NtupleBookingManager ( const G4AnalysisManagerState & state)
explicit

Definition at line 41 of file G4NtupleBookingManager.cc.

44{}
G4BaseAnalysisManager()=delete

◆ G4NtupleBookingManager() [2/2]

G4NtupleBookingManager::G4NtupleBookingManager ( )
delete

◆ ~G4NtupleBookingManager()

G4NtupleBookingManager::~G4NtupleBookingManager ( )
override

Definition at line 47 of file G4NtupleBookingManager.cc.

48{
49 for ( auto ntupleBooking : fNtupleBookingVector ) {
50 delete ntupleBooking;
51 }
52}
std::vector< G4NtupleBooking * > fNtupleBookingVector

Member Function Documentation

◆ ClearData()

void G4NtupleBookingManager::ClearData ( )
protected

Definition at line 304 of file G4NtupleBookingManager.cc.

305{
306 for ( auto ntupleBooking : fNtupleBookingVector ) {
307 delete ntupleBooking;
308 }
309 fNtupleBookingVector.clear();
310 fLockFirstNtupleColumnId = false;
311
312 Message(G4Analysis::kVL2, "clear", "ntupleBookings");
313}
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
constexpr G4int kVL2

◆ CreateNtuple()

G4int G4NtupleBookingManager::CreateNtuple ( const G4String & name,
const G4String & title )
protected

Definition at line 94 of file G4NtupleBookingManager.cc.

96{
97 if ( ! CheckName(name, "Ntuple") ) return kInvalidId;
98
99 Message(kVL4, "create", "ntuple booking", name);
100
101 G4int index = 0;
102 G4NtupleBooking* ntupleBooking = nullptr;
103 if (fFreeIds.empty()) {
104 index = (G4int)fNtupleBookingVector.size();
105 ntupleBooking = new G4NtupleBooking();
106 fNtupleBookingVector.push_back(ntupleBooking);
107 ntupleBooking->fNtupleId = G4int(index + fFirstId);
108 }
109 else {
110 // Get the first freed Id
111 index = *(fFreeIds.begin()) - GetFirstId();
112 ntupleBooking = fNtupleBookingVector[index];
113 ntupleBooking->fNtupleBooking = tools::ntuple_booking();
114 ntupleBooking->Reset();
115
116 // Remove the id from the set
117 fFreeIds.erase(fFreeIds.begin());
118 }
119
120 // Save name & title in ntuple booking
121 ntupleBooking->fNtupleBooking.set_name(name);
122 ntupleBooking->fNtupleBooking.set_title(title);
123
124 // Update related data
125 fLockFirstId = true;
126 fCurrentNtupleId = ntupleBooking->fNtupleId;
127
128 Message(kVL2, "create", "ntuple booking",
129 name + " ntupleId " + to_string(ntupleBooking->fNtupleId));
130
131 return fCurrentNtupleId;
132}
int G4int
Definition G4Types.hh:85
constexpr G4int kVL4
constexpr G4int kInvalidId
tools::ntuple_booking fNtupleBooking

◆ CreateNtupleDColumn() [1/2]

G4int G4NtupleBookingManager::CreateNtupleDColumn ( const G4String & name,
std::vector< double > * vector )
protected

Definition at line 149 of file G4NtupleBookingManager.cc.

151{
152 return CreateNtupleDColumn(GetCurrentNtupleId(), name, vector);
153}
G4int CreateNtupleDColumn(const G4String &name, std::vector< double > *vector)

Referenced by CreateNtupleDColumn().

◆ CreateNtupleDColumn() [2/2]

G4int G4NtupleBookingManager::CreateNtupleDColumn ( G4int ntupleId,
const G4String & name,
std::vector< double > * vector )
protected

Definition at line 183 of file G4NtupleBookingManager.cc.

185{
186 return CreateNtupleTColumn<double>(ntupleId, name, vector);
187}

◆ CreateNtupleFColumn() [1/2]

G4int G4NtupleBookingManager::CreateNtupleFColumn ( const G4String & name,
std::vector< float > * vector )
protected

Definition at line 142 of file G4NtupleBookingManager.cc.

144{
145 return CreateNtupleFColumn(GetCurrentNtupleId(), name, vector);
146}
G4int CreateNtupleFColumn(const G4String &name, std::vector< float > *vector)

Referenced by CreateNtupleFColumn().

◆ CreateNtupleFColumn() [2/2]

G4int G4NtupleBookingManager::CreateNtupleFColumn ( G4int ntupleId,
const G4String & name,
std::vector< float > * vector )
protected

Definition at line 176 of file G4NtupleBookingManager.cc.

178{
179 return CreateNtupleTColumn<float>(ntupleId, name, vector);
180}

◆ CreateNtupleIColumn() [1/2]

G4int G4NtupleBookingManager::CreateNtupleIColumn ( const G4String & name,
std::vector< int > * vector )
protected

Definition at line 135 of file G4NtupleBookingManager.cc.

137{
138 return CreateNtupleIColumn(GetCurrentNtupleId(), name, vector);
139}
G4int CreateNtupleIColumn(const G4String &name, std::vector< int > *vector)

Referenced by CreateNtupleIColumn().

◆ CreateNtupleIColumn() [2/2]

G4int G4NtupleBookingManager::CreateNtupleIColumn ( G4int ntupleId,
const G4String & name,
std::vector< int > * vector )
protected

Definition at line 169 of file G4NtupleBookingManager.cc.

171{
172 return CreateNtupleTColumn<int>(ntupleId, name, vector);
173}

◆ CreateNtupleSColumn() [1/2]

G4int G4NtupleBookingManager::CreateNtupleSColumn ( const G4String & name,
std::vector< std::string > * vector )
protected

Definition at line 156 of file G4NtupleBookingManager.cc.

158{
159 return CreateNtupleSColumn(GetCurrentNtupleId(), name, vector);
160}
G4int CreateNtupleSColumn(const G4String &name, std::vector< std::string > *vector)

Referenced by CreateNtupleSColumn().

◆ CreateNtupleSColumn() [2/2]

G4int G4NtupleBookingManager::CreateNtupleSColumn ( G4int ntupleId,
const G4String & name,
std::vector< std::string > * vector )
protected

Definition at line 190 of file G4NtupleBookingManager.cc.

192{
193 return CreateNtupleTColumn<std::string>(ntupleId, name, vector);
194}

◆ Delete()

G4bool G4NtupleBookingManager::Delete ( G4int id,
G4bool keepSetting )
protected

Definition at line 316 of file G4NtupleBookingManager.cc.

317{
318 Message(kVL4, "delete", "ntuple booking ntupleId " + to_string(id));
319
320 auto ntupleBooking = GetNtupleBookingInFunction(id, "Delete", true);
321
322 if (ntupleBooking == nullptr) return false;
323
324 // Update ntuple booking
325 ntupleBooking->SetDeleted(true, keepSetting);
326
327 // Register freed Id
328 fFreeIds.insert(id);
329
330 Message(G4Analysis::kVL2, "delete", "ntuple booking ntupleId " + to_string(id));
331
332 return true;
333}

◆ FinishNtuple() [1/2]

G4NtupleBooking * G4NtupleBookingManager::FinishNtuple ( )
protected

Definition at line 163 of file G4NtupleBookingManager.cc.

164{
165 return FinishNtuple(GetCurrentNtupleId());
166}

Referenced by FinishNtuple().

◆ FinishNtuple() [2/2]

G4NtupleBooking * G4NtupleBookingManager::FinishNtuple ( G4int ntupleId)
protected

Definition at line 197 of file G4NtupleBookingManager.cc.

199{
200 // Nothing to be done for booking,
201 return GetNtupleBookingInFunction(ntupleId, "FinishNtuple");
202}

◆ GetActivation()

G4bool G4NtupleBookingManager::GetActivation ( G4int ntupleId) const
protected

Definition at line 238 of file G4NtupleBookingManager.cc.

240{
241 auto ntupleBooking
242 = GetNtupleBookingInFunction(ntupleId, "GetActivation");
243 if (ntupleBooking == nullptr) return false;
244
245 return ntupleBooking->fActivation;
246}

◆ GetFileName()

G4String G4NtupleBookingManager::GetFileName ( G4int id) const
protected

Definition at line 293 of file G4NtupleBookingManager.cc.

295{
296 auto ntupleBooking
297 = GetNtupleBookingInFunction(ntupleId, "GetFileName");
298 if (ntupleBooking == nullptr) return "";
299
300 return ntupleBooking->fFileName;
301}

◆ GetFileType()

G4String G4NtupleBookingManager::GetFileType ( ) const

◆ GetFirstNtupleColumnId()

G4int G4NtupleBookingManager::GetFirstNtupleColumnId ( ) const
protected

◆ GetNofNtuples()

G4int G4NtupleBookingManager::GetNofNtuples ( G4bool onlyIfExist = false) const
protected

Referenced by List().

◆ GetNtuple() [1/2]

tools::ntuple_booking * G4NtupleBookingManager::GetNtuple ( G4bool warn,
G4bool onlyIfActive ) const

Definition at line 434 of file G4NtupleBookingManager.cc.

436{
437 return GetNtuple(fFirstId, warn, onlyIfActive);
438}
tools::ntuple_booking * GetNtuple(G4bool warn, G4bool onlyIfActive) const

Referenced by GetNtuple().

◆ GetNtuple() [2/2]

tools::ntuple_booking * G4NtupleBookingManager::GetNtuple ( G4int ntupleId,
G4bool warn,
G4bool onlyIfActive ) const

Definition at line 441 of file G4NtupleBookingManager.cc.

443{
444 auto g4Booking = GetNtupleBookingInFunction(ntupleId, "GetNtuple", warn);
445
446 if (g4Booking == nullptr) return nullptr;
447
448 if ( ( g4Booking->GetDeleted() ) ||
449 ( onlyIfActive && (! g4Booking->fActivation) ) ) return nullptr;
450
451 return &(g4Booking->fNtupleBooking);
452}

◆ GetNtupleBookingVector()

const std::vector< G4NtupleBooking * > & G4NtupleBookingManager::GetNtupleBookingVector ( ) const

◆ IsEmpty()

G4bool G4NtupleBookingManager::IsEmpty ( ) const

Definition at line 88 of file G4NtupleBookingManager.cc.

89{
90 return fNtupleBookingVector.size() == 0u;
91}

◆ List()

G4bool G4NtupleBookingManager::List ( std::ostream & output,
G4bool onlyIfActive = true )
protected

Definition at line 336 of file G4NtupleBookingManager.cc.

337{
338 // Save current output stream formatting
339 std::ios_base::fmtflags outputFlags(output.flags() );
340
341 // Define optimal field widths
342 size_t maxNameLength = 0;
343 size_t maxTitleLength = 0;
344 // size_t maxEntries = 0;
345 size_t nofActive = 0;
346 for (auto g4NtupleBooking : fNtupleBookingVector) {
347 const auto& ntupleBooking = g4NtupleBooking->fNtupleBooking;
348 if (ntupleBooking.name().length() > maxNameLength) {
349 maxNameLength = ntupleBooking.name().length();
350 }
351 if (ntupleBooking.title().length() > maxTitleLength) {
352 maxTitleLength = ntupleBooking.title().length();
353 }
354 // if (ntuple->entries() > maxEntries) {
355 // maxEntries = ntuple->entries();
356 // }
357 if (g4NtupleBooking->fActivation) {
358 ++nofActive;
359 }
360 }
361 size_t maxIdWidth = std::to_string(fNtupleBookingVector.size() + GetFirstId()).length();
362 // update strings width for added double quotas
363 maxNameLength += 2;
364 maxTitleLength += 2;
365
366 // List general info
367 output << "Ntuple: " << nofActive << " active ";
368 if (! onlyIfActive) {
369 output << " of " << GetNofNtuples(true) << " defined ";
370 }
371 output << G4endl;
372
373 // List objects
374 G4int counter = 0;
375 for (auto g4NtupleBooking : fNtupleBookingVector) {
376 const auto& ntupleBooking = g4NtupleBooking->fNtupleBooking;
377
378 // skip inactivated objcets
379 if (fState.GetIsActivation() && onlyIfActive && (! g4NtupleBooking->fActivation)) continue;
380
381 // print selected info
382 output << " id: " << std::setw((G4int)maxIdWidth) << GetFirstId() + counter++
383 << " name: \"" << std::setw((G4int)maxNameLength) << std::left << ntupleBooking.name() + "\""
384 << " title: \"" << std::setw((G4int)maxTitleLength) << std::left << ntupleBooking.title() + "\"";
385 // << " entries: " << std::setw((G4int)maxEntriesWidth) << ntuple->entries();
386 if (! onlyIfActive) {
387 output << " active: " << std::boolalpha << g4NtupleBooking->fActivation;
388 }
389 output << G4endl;
390 }
391
392 // Restore the output stream formatting
393 output.flags(outputFlags);
394
395 return output.good();
396}
#define G4endl
Definition G4ios.hh:67
const G4AnalysisManagerState & fState
G4int GetNofNtuples(G4bool onlyIfExist=false) const

◆ SetActivation() [1/2]

void G4NtupleBookingManager::SetActivation ( G4bool activation)
protected

Definition at line 218 of file G4NtupleBookingManager.cc.

220{
221 for ( auto ntupleBooking : fNtupleBookingVector ) {
222 ntupleBooking->fActivation = activation;
223 }
224}

◆ SetActivation() [2/2]

void G4NtupleBookingManager::SetActivation ( G4int ntupleId,
G4bool activation )
protected

Definition at line 227 of file G4NtupleBookingManager.cc.

229{
230 auto ntupleBooking
231 = GetNtupleBookingInFunction(ntupleId, "SetActivation");
232 if (ntupleBooking == nullptr) return;
233
234 ntupleBooking->fActivation = activation;
235}

◆ SetFileName() [1/2]

void G4NtupleBookingManager::SetFileName ( const G4String & fileName)
protected

Definition at line 249 of file G4NtupleBookingManager.cc.

251{
252 for ( auto ntupleBooking : fNtupleBookingVector ) {
253 ntupleBooking->fFileName = fileName;
254 }
255}

◆ SetFileName() [2/2]

void G4NtupleBookingManager::SetFileName ( G4int id,
const G4String & fileName )
protected

Definition at line 258 of file G4NtupleBookingManager.cc.

260{
261 auto ntupleBooking
262 = GetNtupleBookingInFunction(ntupleId, "SetFileName");
263 if (ntupleBooking == nullptr) return;
264
265 // Do nothing if file name does not change
266 if ( ntupleBooking->fFileName == fileName ) return;
267
268 auto ntupleFileName = fileName;
269 auto extension = GetExtension(fileName);
270 if (extension.size() != 0u) {
271 // Check if valid extension (if present)
272 auto output = G4Analysis::GetOutput(extension);
273 if ( output == G4AnalysisOutput::kNone ) {
274 Warn("The file extension " + extension + " is not supported.",
275 fkClass, "SetFileName");
276 return;
277 }
278 }
279 else {
280 if (fFileType.size() != 0u) {
281 //add extension if missing and file type is defined
282 ntupleFileName = fileName + "." + fFileType;
283 }
284 }
285
286 // Save the fileName in booking
287 // If extension is still missing (possible with generic manager),
288 // it will be completed with the default one at OpenFile
289 ntupleBooking->fFileName = ntupleFileName;
290}
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)

◆ SetFileType()

void G4NtupleBookingManager::SetFileType ( const G4String & fileType)

Definition at line 403 of file G4NtupleBookingManager.cc.

404{
405 // do nothing if file type is defined and is same
406 if ( fFileType == fileType ) return;
407
408 // save the type
409 fFileType = fileType;
410
411 // Give warning and redefine file extension in bookings
412 // with file name of different fileTypes
413 for ( auto ntupleBooking : fNtupleBookingVector ) {
414 if ((ntupleBooking->fFileName).size() == 0u) continue;
415
416 auto extension = GetExtension(ntupleBooking->fFileName);
417 if ( fFileType == extension ) continue;
418
419 // multiple file types are not suported
420 auto baseFileName = GetBaseName(ntupleBooking->fFileName);
421 auto ntupleFileName = baseFileName + "." + fFileType;
422 if (extension.size() != 0u) {
423 Warn("Writing ntuples in files of different output types " +
424 fFileType + ", " + extension + " is not supported.",
425 fkClass, "SetFileType");
426 }
427
428 // Save the info in ntuple description
429 ntupleBooking->fFileName = ntupleFileName;
430 }
431}
G4String GetBaseName(const G4String &fileName)

◆ SetFirstNtupleColumnId()

G4bool G4NtupleBookingManager::SetFirstNtupleColumnId ( G4int firstId)
protected

Definition at line 205 of file G4NtupleBookingManager.cc.

206{
207 if ( fLockFirstNtupleColumnId ) {
208 Warn("Cannot set FirstNtupleColumnId as its value was already used.",
209 fkClass, "SetFirstNtupleColumnId");
210 return false;
211 }
212
213 fFirstNtupleColumnId = firstId;
214 return true;
215}

Friends And Related Symbol Documentation

◆ G4VAnalysisManager

friend class G4VAnalysisManager
friend

Definition at line 75 of file G4NtupleBookingManager.hh.

Member Data Documentation

◆ fFreeIds

std::set<G4int> G4NtupleBookingManager::fFreeIds
protected

Definition at line 155 of file G4NtupleBookingManager.hh.

Referenced by CreateNtuple(), and Delete().

◆ fNtupleBookingVector

std::vector<G4NtupleBooking*> G4NtupleBookingManager::fNtupleBookingVector
protected

The documentation for this class was generated from the following files: