Geant4 Cross Reference |
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 #include "G4AnalysisManagerState.hh" 28 #include "G4AnalysisUtilities.hh" 29 30 using std::to_string; 31 32 // 33 // private template functions 34 // 35 36 //_____________________________________________________________________________ 37 template <typename NT, typename FT> 38 G4TNtupleManager<NT, FT>::G4TNtupleManager( 39 const G4AnalysisManagerState& state) 40 : G4BaseNtupleManager(state) 41 {} 42 43 //_____________________________________________________________________________ 44 template <typename NT, typename FT> 45 G4TNtupleManager<NT, FT>::~G4TNtupleManager() 46 { 47 for ( auto ntupleDescription : fNtupleDescriptionVector ) { 48 delete ntupleDescription; 49 } 50 } 51 52 //_____________________________________________________________________________ 53 template <typename NT, typename FT> 54 G4TNtupleDescription<NT, FT>* 55 G4TNtupleManager<NT, FT>::GetNtupleDescriptionInFunction( 56 G4int id, std::string_view functionName, G4bool warn) const 57 { 58 auto index = id - fFirstId; 59 if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) { 60 if ( warn) { 61 G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.", 62 fkClass, functionName); 63 } 64 return nullptr; 65 } 66 67 return fNtupleDescriptionVector[index]; 68 } 69 70 //_____________________________________________________________________________ 71 template <typename NT, typename FT> 72 NT* G4TNtupleManager<NT, FT>::GetNtupleInFunction( 73 G4int id, std::string_view functionName, G4bool warn) const 74 { 75 auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName); 76 if ( ! ntupleDescription ) return nullptr; 77 78 if ( ! ntupleDescription->GetNtuple() ) { 79 if ( warn ) { 80 G4Analysis::Warn("Ntuple " + to_string(id) + " does not exist.", 81 fkClass, functionName); 82 } 83 return nullptr; 84 } 85 86 return ntupleDescription->GetNtuple(); 87 } 88 89 //_____________________________________________________________________________ 90 template <typename NT, typename FT> 91 template <typename T> 92 G4bool G4TNtupleManager<NT, FT>::FillNtupleTColumn( 93 G4int ntupleId, G4int columnId, const T& value) 94 { 95 if (fNewCycle) { 96 CreateNtuplesFromBooking(*fNtupleBookingVector); 97 fNewCycle = false; 98 } 99 100 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) { 101 //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl; 102 return false; 103 } 104 105 // get ntuple 106 auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn"); 107 if ( ! ntuple ) return false; 108 109 // get generic column 110 auto index = columnId - fFirstNtupleColumnId; 111 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) { 112 G4Analysis::Warn( 113 "Ntuple " + to_string(ntupleId) + " column " + to_string(columnId) + 114 " does not exist.", 115 fkClass, "FillNtupleTColumn"); 116 return false; 117 } 118 auto icolumn = ntuple->columns()[index]; 119 120 // get column and check its type 121 auto column = dynamic_cast<typename NT::template column<T>* >(icolumn); 122 if ( ! column ) { 123 G4Analysis::Warn( 124 "Column type does not match: " 125 " ntuple " + to_string(ntupleId) + " column " + to_string(columnId) + 126 " value " + G4Analysis::ToString(value), 127 fkClass, "FillNtupleTColumn"); 128 return false; 129 } 130 131 column->fill(value); 132 133 if ( IsVerbose(G4Analysis::kVL4) ) { 134 Message(G4Analysis::kVL4, "fill", "ntuple T column", 135 " ntupleId " + to_string(ntupleId) + 136 " column " + to_string(columnId) + 137 " value " + G4Analysis::ToString<T>(value)); 138 } 139 140 return true; 141 } 142 143 // 144 // protected functions 145 // 146 147 //_____________________________________________________________________________ 148 template <typename NT, typename FT> 149 G4int G4TNtupleManager<NT, FT>::CreateNtuple(G4NtupleBooking* ntupleBooking) 150 { 151 Message(G4Analysis::kVL4, "create from booking", "ntuple", 152 ntupleBooking->fNtupleBooking.name()); 153 154 // The ntuple index 155 auto index = ntupleBooking->fNtupleId - fFirstId; 156 157 // Allocate the vector element(s) if needed 158 while ( index >= G4int(fNtupleDescriptionVector.size()) ) { 159 fNtupleDescriptionVector.push_back(nullptr); 160 } 161 162 auto ntupleDescription = fNtupleDescriptionVector[index]; 163 if (ntupleDescription == nullptr) { 164 // Create ntuple description from ntuple booking 165 // if it does not yet exist 166 ntupleDescription = new G4TNtupleDescription<NT, FT>(ntupleBooking); 167 fNtupleDescriptionVector[index] = ntupleDescription; 168 } 169 170 // Do not create ntuple if it is inactivated 171 // or if it was deleted 172 if ( ( ntupleBooking->GetDeleted() ) || 173 ( fState.GetIsActivation() && 174 ( ! ntupleDescription->GetActivation() ) ) ) return G4Analysis::kInvalidId; 175 176 // Do not create ntuple if it already exists 177 if ( ntupleDescription->GetNtuple() != nullptr ) { 178 return ntupleBooking->fNtupleId; 179 } 180 181 // Create ntuple 182 CreateTNtupleFromBooking(ntupleDescription); 183 184 // Update ntuple vector if ntuple was created 185 if ( ntupleDescription->GetNtuple() != nullptr ) { 186 // Allocate the ntuple vector element(s) if needed 187 while ( index >= G4int(fNtupleVector.size()) ) { 188 fNtupleVector.push_back(nullptr); 189 } 190 fNtupleVector[index] = ntupleDescription->GetNtuple(); 191 } 192 193 // finish created ntuple 194 auto fromBooking = true; 195 FinishTNtuple(ntupleDescription, fromBooking); 196 197 Message(G4Analysis::kVL3, "create from booking", "ntuple", 198 ntupleBooking->fNtupleBooking.name()); 199 200 return ntupleBooking->fNtupleId; 201 } 202 203 //_____________________________________________________________________________ 204 template <typename NT, typename FT> 205 void G4TNtupleManager<NT, FT>::CreateNtuplesFromBooking( 206 const std::vector<G4NtupleBooking*>& ntupleBookings) 207 { 208 // Create ntuple from ntuple bookings. 209 210 // Save the ntuple booking for new cycle 211 fNtupleBookingVector = &ntupleBookings; 212 213 // Create ntuple descriptions from ntuple booking. 214 for ( auto ntupleBooking : ntupleBookings ) { 215 CreateNtuple(ntupleBooking); 216 } 217 } 218 219 //_____________________________________________________________________________ 220 template <typename NT, typename FT> 221 G4bool 222 G4TNtupleManager<NT, FT>::Reset() 223 { 224 // Reset ntuple description, this will delete ntuple if present and 225 // we have its ownership. 226 // The ntuples will be recreated with new cycle or new open file. 227 228 for ( auto ntupleDescription : fNtupleDescriptionVector ) { 229 ntupleDescription->Reset(); 230 } 231 232 fNtupleVector.clear(); 233 234 return true; 235 } 236 237 //_____________________________________________________________________________ 238 template <typename NT, typename FT> 239 void 240 G4TNtupleManager<NT, FT>::Clear() 241 { 242 // Clear all data 243 244 for ( auto ntupleDescription : fNtupleDescriptionVector ) { 245 delete ntupleDescription; 246 } 247 248 fNtupleDescriptionVector.clear(); 249 fNtupleVector.clear(); 250 251 Message(G4Analysis::kVL2, "clear", "ntuples"); 252 } 253 254 //_____________________________________________________________________________ 255 template <typename NT, typename FT> 256 G4bool 257 G4TNtupleManager<NT, FT>::Delete(G4int id) 258 { 259 if ( IsVerbose(G4Analysis::kVL4) ) { 260 Message(G4Analysis::kVL4, "delete", "ntuple ntupleId " + to_string(id)); 261 } 262 263 auto ntupleDescription = GetNtupleDescriptionInFunction(id, "Delete", true); 264 265 if (ntupleDescription == nullptr) return false; 266 267 // Delete ntuple and update ntuple description 268 delete ntupleDescription->GetNtuple(); 269 ntupleDescription->SetNtuple(nullptr); 270 // ntupleDescription->SetDeleted(true, keepSetting); 271 272 // Update ntuple vector 273 if (! fNtupleVector.empty()) { 274 auto index = id - GetFirstId(); 275 fNtupleVector[index] = nullptr; 276 } 277 278 // Register freed Id 279 // fFreeIds.insert(id); 280 281 Message(G4Analysis::kVL2, "delete", "ntuple ntupleId " + to_string(id)); 282 283 return true; 284 } 285 286 //_____________________________________________________________________________ 287 template <typename NT, typename FT> 288 G4bool G4TNtupleManager<NT, FT>::FillNtupleIColumn( 289 G4int ntupleId, G4int columnId, G4int value) 290 { 291 return FillNtupleTColumn<int>(ntupleId, columnId, value); 292 } 293 //_____________________________________________________________________________ 294 template <typename NT, typename FT> 295 G4bool G4TNtupleManager<NT, FT>::FillNtupleFColumn( 296 G4int ntupleId, G4int columnId, G4float value) 297 { 298 return FillNtupleTColumn<float>(ntupleId, columnId, value); 299 } 300 301 //_____________________________________________________________________________ 302 template <typename NT, typename FT> 303 G4bool G4TNtupleManager<NT, FT>::FillNtupleDColumn( 304 G4int ntupleId, G4int columnId, G4double value) 305 { 306 return FillNtupleTColumn<double>(ntupleId, columnId, value); 307 } 308 309 //_____________________________________________________________________________ 310 template <typename NT, typename FT> 311 G4bool G4TNtupleManager<NT, FT>::FillNtupleSColumn( 312 G4int ntupleId, G4int columnId, const G4String& value) 313 { 314 return FillNtupleTColumn<std::string>(ntupleId, columnId, value); 315 } 316 317 //_____________________________________________________________________________ 318 template <typename NT, typename FT> 319 G4bool G4TNtupleManager<NT, FT>::AddNtupleRow( 320 G4int ntupleId) 321 { 322 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) { 323 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl; 324 return false; 325 } 326 327 if ( IsVerbose(G4Analysis::kVL4) ) { 328 Message(G4Analysis::kVL4, "add", "ntuple row", 329 " ntupleId " + to_string(ntupleId)); 330 } 331 332 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow"); 333 if ( ! ntupleDescription ) return false; 334 335 auto ntuple = ntupleDescription->GetNtuple(); 336 if ( ! ntuple ) return false; 337 338 auto result = ntuple->add_row(); 339 if ( ! result ) { 340 G4Analysis::Warn( 341 "Ntuple " + to_string(ntupleId) + " adding row has failed.", 342 fkClass, "AddTNtupleRow"); 343 } 344 345 ntupleDescription->SetHasFill(true); 346 347 if ( IsVerbose(G4Analysis::kVL4) ) { 348 Message(G4Analysis::kVL4, "add", "ntuple row", 349 " ntupleId " + to_string(ntupleId)); 350 } 351 352 return true; 353 } 354 355 //_____________________________________________________________________________ 356 template <typename NT, typename FT> 357 void G4TNtupleManager<NT, FT>::SetActivation( 358 G4bool activation) 359 { 360 for ( auto ntupleDescription : fNtupleDescriptionVector ) { 361 ntupleDescription->SetActivation(activation); 362 } 363 } 364 365 //_____________________________________________________________________________ 366 template <typename NT, typename FT> 367 void G4TNtupleManager<NT, FT>::SetActivation( 368 G4int ntupleId, G4bool activation) 369 { 370 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation"); 371 if ( ! ntupleDescription ) return; 372 373 ntupleDescription->SetActivation(activation); 374 } 375 376 //_____________________________________________________________________________ 377 template <typename NT, typename FT> 378 G4bool G4TNtupleManager<NT, FT>::GetActivation( 379 G4int ntupleId) const 380 { 381 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation"); 382 if ( ! ntupleDescription ) return false; 383 384 return ntupleDescription->GetActivation(); 385 } 386 387 //_____________________________________________________________________________ 388 template <typename NT, typename FT> 389 void G4TNtupleManager<NT, FT>::SetNewCycle(G4bool value) 390 { 391 fNewCycle = value; 392 } 393 394 //_____________________________________________________________________________ 395 template <typename NT, typename FT> 396 G4bool G4TNtupleManager<NT, FT>::GetNewCycle() const 397 { 398 return fNewCycle; 399 } 400 401 //_____________________________________________________________________________ 402 template <typename NT, typename FT> 403 NT* 404 G4TNtupleManager<NT, FT>::GetNtuple() const 405 { 406 return GetNtuple(fFirstId); 407 } 408 409 //_____________________________________________________________________________ 410 template <typename NT, typename FT> 411 NT* 412 G4TNtupleManager<NT, FT>::GetNtuple(G4int ntupleId) const 413 { 414 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetNtuple"); 415 if ( ! ntupleDescription ) return nullptr; 416 417 return ntupleDescription->GetNtuple(); 418 } 419 420 //_____________________________________________________________________________ 421 template <typename NT, typename FT> 422 typename std::vector<NT*>::iterator 423 G4TNtupleManager<NT, FT>::BeginNtuple() 424 { 425 return fNtupleVector.begin(); 426 } 427 428 //_____________________________________________________________________________ 429 template <typename NT, typename FT> 430 typename std::vector<NT*>::iterator 431 G4TNtupleManager<NT, FT>::EndNtuple() 432 { 433 return fNtupleVector.end(); 434 } 435 436 //_____________________________________________________________________________ 437 template <typename NT, typename FT> 438 typename std::vector<NT*>::const_iterator 439 G4TNtupleManager<NT, FT>::BeginConstNtuple() const 440 { 441 return fNtupleVector.begin(); 442 } 443 444 //_____________________________________________________________________________ 445 template <typename NT, typename FT> 446 typename std::vector<NT*>::const_iterator 447 G4TNtupleManager<NT, FT>::EndConstNtuple() const 448 { 449 return fNtupleVector.end(); 450 } 451