ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootNtupleManager.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RootNtupleManager.hh
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 // Manager class for Root ntuples.
28 // It implements functions specific to Root ntuples.
29 //
30 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
31 
32 #ifndef G4RootNtupleManager_h
33 #define G4RootNtupleManager_h 1
34 
35 #include "G4TNtupleManager.hh"
36 #include "globals.hh"
37 
38 #include "tools/wroot/ntuple"
39 
41 class G4RootFileManager;
42 
43 namespace tools {
44 namespace wroot {
45 class directory;
46 }
47 }
48 
49 enum class G4NtupleCreateMode {
50  kNoMergeBeforeOpen,
51  kNoMergeAfterOpen,
52  kMainBeforeOpen,
53  kMainAfterOpen,
55 };
56 
57 // template specializations used by this class defined below
58 
59 template <>
60 template <>
62  G4int ntupleId, G4int columnId, const std::string& value);
63 
64 
65 class G4RootNtupleManager : public G4TNtupleManager<tools::wroot::ntuple>
66 {
67  friend class G4RootAnalysisManager;
70  friend class G4RootMpiNtupleManager;
71 
72  public:
73  explicit G4RootNtupleManager(const G4AnalysisManagerState& state,
74  G4int nofMainManagers,
75  G4bool rowWise, G4bool rowMode);
76  virtual ~G4RootNtupleManager();
77 
78  private:
79  // Types alias
82 
83  // Functions specific to the output type
84 
85  void SetNtupleDirectory(tools::wroot::directory* directory);
86  void SetFileManager(std::shared_ptr<G4RootFileManager> fileManager);
87 
88  // Utility function
89  void CreateTNtuple(NtupleDescriptionType* ntupleDescription);
90 
91  // Methods from the templated base class
92 
93  virtual void CreateTNtupleFromBooking(
94  NtupleDescriptionType* ntupleDescription) final;
95 
96  virtual void FinishTNtuple(
97  NtupleDescriptionType* ntupleDescription,
98  G4bool fromBooking) final;
99 
100  virtual G4bool Reset(G4bool deleteNtuple);
101 
102  // Method for merging
103  //
104  virtual G4bool Merge();
105 
106  // Access functions
107  //
108  const std::vector<NtupleDescriptionType*>& GetNtupleDescriptionVector() const;
110  unsigned int GetBasketSize() const;
111  unsigned int GetBasketEntries() const;
112 
113  // Utility functions
114  //
115  void SetCreateMode();
116  void SetNtupleRowWise(G4bool rowWise, G4bool rowMode);
117 
118  // data members
119  //
121  std::shared_ptr<G4RootFileManager> fFileManager;
122  tools::wroot::directory* fNtupleDirectory;
123  std::vector<G4RootMainNtupleManager*> fMainNtupleManagers;
126 };
127 
128 // inline functions
129 
130 inline void
131 G4RootNtupleManager::SetNtupleDirectory(tools::wroot::directory* directory)
132 { fNtupleDirectory = directory; }
133 
134 inline void
135 G4RootNtupleManager::SetFileManager(std::shared_ptr<G4RootFileManager> fileManager)
136 { fFileManager = fileManager; }
137 
138 inline const std::vector<G4TNtupleDescription<tools::wroot::ntuple>* >&
140 { return fNtupleDescriptionVector; }
141 
142 //_____________________________________________________________________________
143 template <>
144 template <>
146  G4int ntupleId, G4int columnId, const std::string& value)
147 {
148  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
149  //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
150  return false;
151  }
152 
153  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
154  if ( ! ntuple ) return false;
155 
156  auto index = columnId - fFirstNtupleColumnId;
157  if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
158  G4ExceptionDescription description;
159  description << " " << "ntupleId " << ntupleId
160  << " columnId " << columnId << " does not exist.";
161  G4Exception("G4RootNtupleManager::FillNtupleTColumn()",
162  "Analysis_W011", JustWarning, description);
163  return false;
164  }
165 
166  auto icolumn = ntuple->columns()[index];
167  auto column = dynamic_cast<tools::wroot::ntuple::column_string* >(icolumn);
168  if ( ! column ) {
169  G4ExceptionDescription description;
170  description << " Column type does not match: "
171  << " ntupleId " << ntupleId
172  << " columnId " << columnId << " value " << value;
173  G4Exception("G4RootNtupleManager:FillNtupleColumn",
174  "Analysis_W011", JustWarning, description);
175  return false;
176  }
177 
178  column->fill(value);
179 
180 #ifdef G4VERBOSE
181  if ( fState.GetVerboseL4() ) {
182  G4ExceptionDescription description;
183  description << " ntupleId " << ntupleId
184  << " columnId " << columnId << " value " << value;
185  fState.GetVerboseL4()->Message("fill", "ntuple T column", description);
186  }
187 #endif
188  return true;
189 }
190 
191 #endif
192 
193