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5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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"
 30 #include "G4XmlRFileManager.hh"
 31 #include "G4AnalysisManagerState.hh"
 32 #include "G4AnalysisUtilities.hh"
 33 
 34 using namespace G4Analysis;
 35 using std::to_string;
 36 
 37 //
 38 // utility function (to be provided in tools)
 39 //
 40 
 41 namespace tools {
 42 namespace aida {
 43 template <class T>
 44 bool to_vector(base_ntu& a_ntu,std::vector<T>& a_vec) {
 45   a_vec.clear();
 46   const std::vector<base_col*>& cols = a_ntu.cols();
 47   if(cols.empty()) return false;
 48   base_col* _base_col = cols.front();
 49   aida_col<T>* _col = safe_cast<base_col, aida_col<T> >(*_base_col);
 50   if(!_col) return false;
 51   a_ntu.start();
 52   uint64 _rows = a_ntu.rows();
 53   a_vec.resize(_rows);
 54   T v;
 55  {for(uint64 row=0;row<_rows;row++) {
 56     if(!a_ntu.next()) {a_vec.clear();return false;}
 57     if(!_col->get_entry(v)) {a_vec.clear();return false;}
 58     a_vec[row] = v;
 59   }}
 60   return true;
 61 }
 62 }}
 63 
 64 //_____________________________________________________________________________
 65 G4XmlRNtupleManager::G4XmlRNtupleManager(const G4AnalysisManagerState& state)
 66  : G4TRNtupleManager<tools::aida::ntuple>(state)
 67 {}
 68 
 69 //
 70 // private methods
 71 //
 72 
 73 //_____________________________________________________________________________
 74 G4int G4XmlRNtupleManager::ReadNtupleImpl(const G4String& ntupleName,
 75                                           const G4String& fileName,
 76                                           const G4String& /*dirName*/,
 77                                           G4bool isUserFileName)
 78 {
 79   Message(kVL4, "read", "ntuple", ntupleName);
 80 
 81   // Ntuples are saved per object and per thread
 82   // but apply the ntuple name and the thread suffixes
 83   // only if fileName is not provided explicitly
 84   auto fullFileName = fileName;
 85   if ( ! isUserFileName ) {
 86     fullFileName = fFileManager->GetNtupleFileName(ntupleName);
 87   }
 88 
 89   auto handler = fFileManager->GetHandler<tools::aida::ntuple>(
 90                                  fullFileName, ntupleName, "ReadNtupleImpl");
 91   if (handler == nullptr) return kInvalidId;
 92 
 93   auto rntuple = static_cast<tools::aida::ntuple*>(handler->object());
 94   auto id = SetNtuple(new G4TRNtupleDescription<tools::aida::ntuple>(rntuple));
 95 
 96   Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId);
 97 
 98   return id;
 99 }
100 
101 //_____________________________________________________________________________
102 G4bool G4XmlRNtupleManager::SetNtupleIColumn(G4int ntupleId,
103                                              const G4String& columnName,
104                                              std::vector<G4int>& vector)
105 {
106 // Override base class default implementation
107 
108   Message(kVL4, "set", "ntuple I column",
109     " ntupleId " + to_string(ntupleId) + " " + columnName);
110 
111   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleIColumn");
112   if (ntupleDescription == nullptr) return false;
113 
114   // not supported
115   //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
116   //ntupleBinding->add_column(columnName, vector);
117 
118   auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
119   ntupleDescription->fIVectorBindingMap[subNtuple] = &vector;
120   tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
121   ntupleBinding->add_column_cid(columnName, *subNtuple);
122 
123   Message(kVL2, "set", "ntuple I column",
124     " ntupleId " + to_string(ntupleId) + " " + columnName);
125 
126   return true;
127 }
128 
129 //_____________________________________________________________________________
130 G4bool G4XmlRNtupleManager::SetNtupleFColumn(G4int ntupleId,
131                                              const G4String& columnName,
132                                              std::vector<G4float>& vector)
133 {
134 // Override base class default implementation
135 
136   Message(kVL4, "set", "ntuple F column",
137     " ntupleId " + to_string(ntupleId) + " " + columnName);
138 
139 
140   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleFColumn");
141   if (ntupleDescription == nullptr) return false;
142 
143   // not supported
144   //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
145   //ntupleBinding->add_column(columnName, vector);
146 
147   auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
148   ntupleDescription->fFVectorBindingMap[subNtuple] = &vector;
149   tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
150   ntupleBinding->add_column_cid(columnName, *subNtuple);
151 
152   Message(kVL4, "set", "ntuple F column",
153     " ntupleId " + to_string(ntupleId) + " " + columnName);
154 
155   return true;
156 }
157 
158 //_____________________________________________________________________________
159 G4bool G4XmlRNtupleManager::SetNtupleDColumn(G4int ntupleId,
160                                              const G4String& columnName,
161                                              std::vector<G4double>& vector)
162 {
163 // Override base class default implementation
164 
165   Message(kVL4, "set", "ntuple D column",
166     " ntupleId " + to_string(ntupleId) + " " + columnName);
167 
168   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleDColumn");
169   if (ntupleDescription == nullptr) return false;
170 
171   // not supported
172   //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
173   //ntupleBinding->add_column(columnName, vector);
174 
175   auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
176   ntupleDescription->fDVectorBindingMap[subNtuple] = &vector;
177   tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
178   ntupleBinding->add_column_cid(columnName, *subNtuple);
179 
180   Message(kVL2, "set", "ntuple D column",
181     " ntupleId " + to_string(ntupleId) + " " + columnName);
182 
183   return true;
184 }
185 
186 //_____________________________________________________________________________
187 G4bool G4XmlRNtupleManager::SetNtupleSColumn(G4int ntupleId,
188                                              const G4String& columnName,
189                                              std::vector<std::string>& vector)
190 {
191 // Override base class default implementation
192 
193   Message(kVL4, "set", "ntuple S column",
194     " ntupleId " + to_string(ntupleId) + " " + columnName);
195 
196   auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleSColumn");
197   if (ntupleDescription == nullptr) return false;
198 
199   // not supported
200   //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
201   //ntupleBinding->add_column(columnName, vector);
202 
203   auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
204   ntupleDescription->fSVectorBindingMap[subNtuple] = &vector;
205   tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
206   ntupleBinding->add_column_cid(columnName, *subNtuple);
207 
208   Message(kVL2, "set", "ntuple S column",
209     " ntupleId " + to_string(ntupleId) + " " + columnName);
210 
211   return true;
212 }
213 
214 //_____________________________________________________________________________
215 G4bool G4XmlRNtupleManager::GetTNtupleRow(
216   G4TRNtupleDescription<tools::aida::ntuple>* ntupleDescription)
217 {
218   auto ntuple = ntupleDescription->fNtuple;
219 
220   G4bool isInitialized = ntupleDescription->fIsInitialized;
221   if ( ! isInitialized ) {
222     tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
223     if ( ! ntuple->set_binding(std::cout, *ntupleBinding) ) {
224       Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow");
225       return false;
226     }
227     ntupleDescription->fIsInitialized = true;
228     ntuple->start();
229   }
230 
231   G4bool next = ntuple->next();
232   if ( next ) {
233     if ( ! ntuple->get_row() ) {
234       Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow");
235       return false;
236     }
237 
238     // fill vector from sub ntuples
239     for ( auto [key, value] : ntupleDescription->fIVectorBindingMap) {
240       tools::aida::to_vector<int>(*key, *value);
241     }
242     for ( auto [key, value] : ntupleDescription->fFVectorBindingMap) {
243       tools::aida::to_vector<float>(*key, *value);
244     }
245     for ( auto [key, value] : ntupleDescription->fDVectorBindingMap) {
246       tools::aida::to_vector<double>(*key, *value);
247     }
248     for ( auto [key, value] : ntupleDescription->fSVectorBindingMap) {
249       tools::aida::to_vector<std::string>(*key, *value);
250     }
251   }
252 
253   return next;
254 }
255