ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4XmlRNtupleManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4XmlRNtupleManager.cc
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, 25/07/2014 (ivana@ipno.in2p3.fr)
28 
29 #include "G4XmlRNtupleManager.hh"
31 
32 //
33 // utility function (to be provided in tools)
34 //
35 
36 namespace tools {
37 namespace aida {
38 template <class T>
39 bool to_vector(base_ntu& a_ntu,std::vector<T>& a_vec) {
40  a_vec.clear();
41  const std::vector<base_col*>& cols = a_ntu.cols();
42  if(cols.empty()) return false;
43  base_col* _base_col = cols.front();
44  aida_col<T>* _col = safe_cast<base_col, aida_col<T> >(*_base_col);
45  if(!_col) return false;
46  a_ntu.start();
47  uint64 _rows = a_ntu.rows();
48  a_vec.resize(_rows);
49  T v;
50  {for(uint64 row=0;row<_rows;row++) {
51  if(!a_ntu.next()) {a_vec.clear();return false;}
52  if(!_col->get_entry(v)) {a_vec.clear();return false;}
53  a_vec[row] = v;
54  }}
55  return true;
56 }
57 }}
58 
59 //_____________________________________________________________________________
61  : G4TRNtupleManager<tools::aida::ntuple>(state)
62 {}
63 
64 //_____________________________________________________________________________
66 {}
67 
68 //
69 // private methods
70 //
71 
72 //_____________________________________________________________________________
74  const G4String& columnName,
75  std::vector<G4int>& vector)
76 {
77 // Override base class default implementation
78 
79 #ifdef G4VERBOSE
80  if ( fState.GetVerboseL4() ) {
81  G4ExceptionDescription description;
82  description << " ntupleId " << ntupleId << " " << columnName;
83  fState.GetVerboseL4()->Message("set", "ntuple I column", description);
84  }
85 #endif
86 
87  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleIColumn");
88  if ( ! ntupleDescription ) return false;
89 
90  // not supported
91  //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
92  //ntupleBinding->add_column(columnName, vector);
93 
94  auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
95  ntupleDescription->fIVectorBindingMap[subNtuple] = &vector;
96  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
97  ntupleBinding->add_column_cid(columnName, *subNtuple);
98 
99 #ifdef G4VERBOSE
100  if ( fState.GetVerboseL2() ) {
101  G4ExceptionDescription description;
102  description << " ntupleId " << ntupleId << " " << columnName;
103  fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true);
104  }
105 #endif
106 
107  return true;
108 }
109 
110 //_____________________________________________________________________________
112  const G4String& columnName,
113  std::vector<G4float>& vector)
114 {
115 // Override base class default implementation
116 
117 #ifdef G4VERBOSE
118  if ( fState.GetVerboseL4() ) {
119  G4ExceptionDescription description;
120  description << " ntupleId " << ntupleId << " " << columnName;
121  fState.GetVerboseL4()->Message("set", "ntuple F column of vector", description);
122  }
123 #endif
124 
125  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleFColumn");
126  if ( ! ntupleDescription ) return false;
127 
128  // not supported
129  //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
130  //ntupleBinding->add_column(columnName, vector);
131 
132  auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
133  ntupleDescription->fFVectorBindingMap[subNtuple] = &vector;
134  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
135  ntupleBinding->add_column_cid(columnName, *subNtuple);
136 
137 #ifdef G4VERBOSE
138  if ( fState.GetVerboseL2() ) {
139  G4ExceptionDescription description;
140  description << " ntupleId " << ntupleId << " " << columnName;
141  fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true);
142  }
143 #endif
144 
145  return true;
146 }
147 
148 //_____________________________________________________________________________
150  const G4String& columnName,
151  std::vector<G4double>& vector)
152 {
153 // Override base class default implementation
154 
155 #ifdef G4VERBOSE
156  if ( fState.GetVerboseL4() ) {
157  G4ExceptionDescription description;
158  description << " ntupleId " << ntupleId << " " << columnName;
159  fState.GetVerboseL4()->Message("set", "ntuple D column of vector", description);
160  }
161 #endif
162 
163  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleDColumn");
164  if ( ! ntupleDescription ) return false;
165 
166  // not supported
167  //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
168  //ntupleBinding->add_column(columnName, vector);
169 
170  auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
171  ntupleDescription->fDVectorBindingMap[subNtuple] = &vector;
172  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
173  ntupleBinding->add_column_cid(columnName, *subNtuple);
174 
175 #ifdef G4VERBOSE
176  if ( fState.GetVerboseL2() ) {
177  G4ExceptionDescription description;
178  description << " ntupleId " << ntupleId << " " << columnName;
179  fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true);
180  }
181 #endif
182 
183  return true;
184 }
185 
186 //_____________________________________________________________________________
189 {
190  auto ntuple = ntupleDescription->fNtuple;
191 
192  G4bool isInitialized = ntupleDescription->fIsInitialized;
193  if ( ! isInitialized ) {
194  tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
195  if ( ! ntuple->set_binding(std::cout, *ntupleBinding) ) {
196  G4ExceptionDescription description;
197  description
198  << " "
199  << "Ntuple initialization failed !!";
200  G4Exception("G4XmlRNtuple::GetNtupleRow()",
201  "Analysis_WR021", JustWarning, description);
202  return false;
203  }
204  ntupleDescription->fIsInitialized = true;
205  ntuple->start();
206  }
207 
208  G4bool next = ntuple->next();
209  if ( next ) {
210  if ( ! ntuple->get_row() ) {
211  G4ExceptionDescription description;
212  description
213  << " "
214  << "Ntuple get_row() failed !!";
215  G4Exception("G4XmlRNtuple::GetNtupleRow()",
216  "Analysis_WR021", JustWarning, description);
217  return false;
218  }
219 
220  // fill vector from sub ntuples
221 
222  {std::map<tools::aida::ntuple*, std::vector<int>* >::iterator it;
223  for ( it = ntupleDescription->fIVectorBindingMap.begin();
224  it != ntupleDescription->fIVectorBindingMap.end(); it++) {
225  tools::aida::to_vector<int>(*(it->first), *(it->second));
226  }}
227  {std::map<tools::aida::ntuple*, std::vector<float>* >::iterator it;
228  for ( it = ntupleDescription->fFVectorBindingMap.begin();
229  it != ntupleDescription->fFVectorBindingMap.end(); it++) {
230  tools::aida::to_vector<float>(*(it->first), *(it->second));
231  }}
232  {std::map<tools::aida::ntuple*, std::vector<double>* >::iterator it;
233  for ( it = ntupleDescription->fDVectorBindingMap.begin();
234  it != ntupleDescription->fDVectorBindingMap.end(); it++) {
235  tools::aida::to_vector<double>(*(it->first), *(it->second));
236  }}
237  }
238 
239  return next;
240 }