Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 // Author: Ivana Hrivnacova, 04/10/2016 (ivan 27 // Author: Ivana Hrivnacova, 04/10/2016 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4NtupleBookingManager.hh" << 30 #include "G4RootPNtupleManager.hh" 29 #include "G4RootPNtupleManager.hh" >> 30 #include "G4RootMainNtupleManager.hh" 31 #include "G4AnalysisUtilities.hh" 31 #include "G4AnalysisUtilities.hh" 32 32 33 #include "tools/wroot/file" 33 #include "tools/wroot/file" 34 #include "tools/wroot/ntuple" 34 #include "tools/wroot/ntuple" 35 35 36 using namespace G4Analysis; << 37 using std::to_string; << 38 << 39 // mutex in a file scope 36 // mutex in a file scope 40 namespace { 37 namespace { 41 << 38 //Mutex to lock master manager when adding ntuple row and ending fill 42 //Mutex to lock master manager when adding ntu << 39 G4Mutex pntupleMutex = G4MUTEX_INITIALIZER; 43 G4Mutex pntupleMutex = G4MUTEX_INITIALIZER; << 44 //Mutex to lock master manager when createing << 45 G4Mutex createMainMutex = G4MUTEX_INITIALIZER; << 46 << 47 //____________________________________________ << 48 void NotExistWarning(const G4String& what, G4i << 49 std::string_view classNam << 50 std::string_view function << 51 { << 52 Warn(what + " id= " + to_string(id) + " does << 53 className, functionName); << 54 << 55 } << 56 << 57 } 40 } 58 41 59 //____________________________________________ 42 //_____________________________________________________________________________ 60 G4RootPNtupleManager::G4RootPNtupleManager(con << 43 G4RootPNtupleManager::G4RootPNtupleManager(G4RootMainNtupleManager* main, 61 std::shared_ptr<G4Ntup << 44 const G4AnalysisManagerState& state, 62 std::shared_ptr<G4Root << 45 G4bool rowWise, G4bool rowMode) 63 G4bool rowWise, G4bool << 64 : G4BaseNtupleManager(state), 46 : G4BaseNtupleManager(state), 65 fBookingManager(std::move(bookingManger)), << 47 fCreateMode(G4PNtupleCreateMode::kUndefined), 66 fMainNtupleManager(std::move(main)), << 48 fMainNtupleManager(main), >> 49 fNtupleVector(), 67 fRowWise(rowWise), 50 fRowWise(rowWise), 68 fRowMode(rowMode) 51 fRowMode(rowMode) 69 {} 52 {} 70 53 71 //____________________________________________ 54 //_____________________________________________________________________________ 72 G4RootPNtupleManager::~G4RootPNtupleManager() 55 G4RootPNtupleManager::~G4RootPNtupleManager() 73 { 56 { 74 for ( auto ntupleDescription : fNtupleDescri 57 for ( auto ntupleDescription : fNtupleDescriptionVector ) { 75 delete ntupleDescription; 58 delete ntupleDescription; 76 } << 59 } 77 } 60 } 78 61 79 // 62 // 80 // private functions 63 // private functions 81 // 64 // 82 65 83 //____________________________________________ 66 //_____________________________________________________________________________ 84 G4RootPNtupleDescription* << 67 G4RootPNtupleDescription* 85 G4RootPNtupleManager::GetNtupleDescriptionInFu 68 G4RootPNtupleManager::GetNtupleDescriptionInFunction( 86 G4int id, std::string_view functionName, G4b << 69 G4int id, G4String functionName, G4bool warn) const 87 { << 70 { 88 auto index = id - fFirstId; 71 auto index = id - fFirstId; 89 if ( index < 0 || index >= G4int(fNtupleDesc 72 if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) { 90 if ( warn) { 73 if ( warn) { 91 NotExistWarning("ntuple description", id << 74 G4String inFunction = "G4RootPNtupleManager::"; >> 75 inFunction += functionName; >> 76 G4ExceptionDescription description; >> 77 description << " " << "ntuple " << id << " does not exist."; >> 78 G4Exception(inFunction, "Analysis_W011", JustWarning, description); 92 } 79 } 93 return nullptr; << 80 return nullptr; 94 } 81 } 95 << 82 96 return fNtupleDescriptionVector[index]; 83 return fNtupleDescriptionVector[index]; 97 } 84 } 98 85 99 //____________________________________________ 86 //_____________________________________________________________________________ 100 tools::wroot::base_pntuple* << 87 tools::wroot::base_pntuple* 101 G4RootPNtupleManager::GetNtupleInFunction( 88 G4RootPNtupleManager::GetNtupleInFunction( 102 G4int id, std::string_view functionName, G4b << 89 G4int id, G4String functionName, G4bool warn) const 103 { << 90 { 104 auto ntupleDescription = GetNtupleDescriptio 91 auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName); 105 if (ntupleDescription == nullptr) return nul << 92 if ( ! ntupleDescription ) return nullptr; 106 93 107 if (ntupleDescription->GetBasePNtuple() == n << 94 if ( ! ntupleDescription->fBasePNtuple ) { 108 if ( warn ) { 95 if ( warn ) { 109 NotExistWarning("ntuple", id, fkClass, f << 96 G4String inFunction = "G4RootPNtupleManager::"; >> 97 inFunction += functionName; >> 98 G4ExceptionDescription description; >> 99 description << " " << "ntupleId " << id << " does not exist."; >> 100 G4Exception(inFunction, "Analysis_W011", JustWarning, description); 110 } 101 } 111 return nullptr; 102 return nullptr; 112 } << 103 } 113 return ntupleDescription->GetBasePNtuple(); << 104 return ntupleDescription->fBasePNtuple; 114 } 105 } 115 106 116 //____________________________________________ 107 //_____________________________________________________________________________ 117 tools::wroot::ntuple* 108 tools::wroot::ntuple* 118 G4RootPNtupleManager::GetMainNtupleInFunction( 109 G4RootPNtupleManager::GetMainNtupleInFunction( 119 G4int id, std::string_view functionName, G4b << 110 G4int id, G4String functionName, G4bool warn) const 120 { << 111 { 121 auto& mainNtupleVector = fMainNtupleManager- << 112 auto& mainNtupleVector 122 << 113 = fMainNtupleManager->GetNtupleVector(); >> 114 123 auto index = id - fFirstId; 115 auto index = id - fFirstId; 124 if ( index < 0 || index >= G4int(mainNtupleV 116 if ( index < 0 || index >= G4int(mainNtupleVector.size()) ) { 125 if ( warn) { 117 if ( warn) { 126 NotExistWarning("main ntuple", id, fkCla << 118 G4String inFunction = "G4RootPNtupleManager::"; >> 119 inFunction += functionName; >> 120 G4ExceptionDescription description; >> 121 description << " " << "main ntuple " << id << " does not exist."; >> 122 G4Exception(inFunction, "Analysis_W011", JustWarning, description); 127 } 123 } 128 return nullptr; << 124 return nullptr; 129 } 125 } 130 << 126 131 return mainNtupleVector[index]; 127 return mainNtupleVector[index]; 132 } 128 } 133 129 >> 130 134 // 131 // 135 // protected functions 132 // protected functions 136 // 133 // 137 134 138 //____________________________________________ 135 //_____________________________________________________________________________ 139 void G4RootPNtupleManager::CreateNtupleFromMai << 136 void G4RootPNtupleManager::CreateNtuple(G4RootPNtupleDescription* ntupleDescription, 140 G4RootPNtupleDesc << 137 tools::wroot::ntuple* mainNtuple) 141 tools::wroot::ntu << 138 { 142 { << 139 #ifdef G4VERBOSE 143 // Do not create pntuple if ntuple was delet << 140 if ( fState.GetVerboseL4() ) 144 if (mainNtuple == nullptr) { << 141 fState.GetVerboseL4() 145 ntupleDescription->SetNtuple(nullptr); << 142 ->Message("create from main", "pntuple", mainNtuple->name()); 146 ntupleDescription->SetBasePNtuple(nullptr) << 143 #endif 147 return; << 148 } << 149 << 150 Message(kVL4, "create from main", "pntuple", << 151 << 152 auto file = fMainNtupleManager->GetNtupleFil << 153 if ( ! file ) { << 154 Warn("Cannot create pntuple. Main ntuple f << 155 fkClass, "CreateNtupleFromMain"); << 156 return; << 157 } << 158 144 159 ntupleDescription->GetDescription().SetFile( << 145 auto rfile = fMainNtupleManager->GetNtupleFile(); >> 146 // auto directory = fMainNtupleManager->GetNtupleDirectory(); 160 147 161 // Get parameters from ntupleDescription 148 // Get parameters from ntupleDescription 162 mainNtuple->get_branches(ntupleDescription-> << 149 ntupleDescription->fFile = rfile.get(); >> 150 mainNtuple->get_branches(ntupleDescription->fMainBranches); 163 151 164 auto rfile = std::get<0>(*file); << 165 G4bool verbose = true; 152 G4bool verbose = true; 166 if ( fRowWise ) { 153 if ( fRowWise ) { 167 auto mainBranch = mainNtuple->get_row_wise 154 auto mainBranch = mainNtuple->get_row_wise_branch(); 168 auto mtNtuple << 155 tools::wroot::mt_ntuple_row_wise* mtNtuple 169 = new tools::wroot::mt_ntuple_row_wise( 156 = new tools::wroot::mt_ntuple_row_wise( 170 G4cout, rfile->byte_swap(), rfil 157 G4cout, rfile->byte_swap(), rfile->compression(), 171 mainNtuple->dir().seek_directory 158 mainNtuple->dir().seek_directory(), 172 *mainBranch, mainBranch->basket_ << 159 *mainBranch, mainBranch->basket_size(), 173 ntupleDescription->GetDescriptio << 160 ntupleDescription->fNtupleBooking, verbose); 174 161 175 ntupleDescription->SetNtuple( << 162 ntupleDescription->fNtuple 176 static_cast<tools::wroot::imt_ntuple*>(m << 163 = static_cast<tools::wroot::imt_ntuple*>(mtNtuple); 177 ntupleDescription->SetBasePNtuple( << 164 ntupleDescription->fBasePNtuple 178 static_cast<tools::wroot::base_pntuple*> << 165 = static_cast<tools::wroot::base_pntuple*>(mtNtuple); 179 } << 166 } 180 else { 167 else { 181 std::vector<tools::uint32> basketSizes; 168 std::vector<tools::uint32> basketSizes; 182 tools_vforcit(tools::wroot::branch*, ntupl << 169 tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) { 183 basketSizes.push_back((*it)->basket_size 170 basketSizes.push_back((*it)->basket_size()); 184 } 171 } 185 auto basketEntries = fMainNtupleManager->G 172 auto basketEntries = fMainNtupleManager->GetBasketEntries(); 186 << 173 187 auto mtNtuple = << 174 tools::wroot::mt_ntuple_column_wise* mtNtuple = 188 new tools::wroot::mt_ntuple_column_wise( 175 new tools::wroot::mt_ntuple_column_wise( 189 G4cout, rfile->byte_swap(), rfile- 176 G4cout, rfile->byte_swap(), rfile->compression(), 190 mainNtuple->dir().seek_directory() 177 mainNtuple->dir().seek_directory(), 191 ntupleDescription->GetMainBranches << 178 ntupleDescription->fMainBranches, basketSizes, 192 ntupleDescription->GetDescription( << 179 ntupleDescription->fNtupleBooking, 193 fRowMode, basketEntries, verbose); 180 fRowMode, basketEntries, verbose); 194 << 181 195 ntupleDescription->SetNtuple( << 182 ntupleDescription->fNtuple 196 static_cast<tools::wroot::imt_ntuple*>(m << 183 = static_cast<tools::wroot::imt_ntuple*>(mtNtuple); 197 ntupleDescription->SetBasePNtuple( << 184 ntupleDescription->fBasePNtuple 198 static_cast<tools::wroot::base_pntuple*> << 185 = static_cast<tools::wroot::base_pntuple*>(mtNtuple); 199 } 186 } 200 187 201 ntupleDescription->GetDescription().SetIsNtu << 188 ntupleDescription->fIsNtupleOwner = true; 202 // // pntuple object is not deleted a 189 // // pntuple object is not deleted automatically 203 fNtupleVector.push_back(ntupleDescription->G << 190 fNtupleVector.push_back(ntupleDescription->fNtuple); 204 191 205 Message(kVL3, "create from main", "pntuple", << 192 >> 193 #ifdef G4VERBOSE >> 194 if ( fState.GetVerboseL3() ) >> 195 fState.GetVerboseL3() >> 196 ->Message("create from main", "pntuple", mainNtuple->name()); >> 197 #endif 206 } 198 } 207 199 208 //____________________________________________ 200 //_____________________________________________________________________________ 209 void G4RootPNtupleManager::CreateNtupleDescrip << 201 void G4RootPNtupleManager::CreateNtuplesFromMain() 210 { 202 { 211 // Create ntuple descriptions from booking. << 203 // Create ntuple from booking (if not yet done) and main ntuple 212 // This function is called from the first Fill << 204 // This function is called from G4AnalysisManager::OpenFile. 213 205 214 // Create pntuple descriptions from ntuple b << 206 if ( fCreateMode == G4PNtupleCreateMode::kUndefined ) { 215 auto g4NtupleBookings = fBookingManager->Get << 207 if ( fNtupleDescriptionVector.size() ) { 216 << 208 fCreateMode = G4PNtupleCreateMode::kSlaveBeforeOpen; 217 for ( auto g4NtupleBooking : g4NtupleBooking << 209 // G4cout << "Create mode: kSlaveBeforeOpen" << G4endl; 218 auto ntupleDescription = new G4RootPNtuple << 210 } else { 219 // Save g4booking, activation in pntuple b << 211 fCreateMode = G4PNtupleCreateMode::kSlaveAfterOpen; 220 fNtupleDescriptionVector.push_back(ntupleD << 212 // G4cout << "Create mode: kSlaveAfterOpen" << G4endl; >> 213 } 221 } 214 } 222 } << 223 215 224 //____________________________________________ << 216 if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) { 225 void G4RootPNtupleManager::CreateNtuplesFromMa << 217 // ntuples are not yet booked 226 { << 218 // G4cout << "Ntuples are not yet booked ?" << G4endl; 227 // Create slave ntuples from main ntuple. << 219 return; 228 // This function is called from the first Fill << 220 } 229 221 230 auto& mainNtupleVector = fMainNtupleManager- << 222 auto& mainNtupleVector >> 223 = fMainNtupleManager->GetNtupleVector(); 231 224 232 G4int lcounter = 0; 225 G4int lcounter = 0; 233 for ( auto mainNtuple : mainNtupleVector ) { 226 for ( auto mainNtuple : mainNtupleVector ) { 234 auto& ntupleDescription = fNtupleDescripti << 227 235 CreateNtupleFromMain(ntupleDescription, ma << 228 auto& ntupleDescription >> 229 = fNtupleDescriptionVector[lcounter++]; >> 230 CreateNtuple(ntupleDescription, mainNtuple); 236 } 231 } 237 } 232 } 238 233 239 //____________________________________________ 234 //_____________________________________________________________________________ 240 void G4RootPNtupleManager::CreateNtuplesIfNeed << 235 G4int G4RootPNtupleManager::CreateNtuple( >> 236 const G4String& name, const G4String& title) 241 { 237 { 242 // The ntuples on workers are created at first << 238 // Create pntuple description with ntuple_booking 243 // (if only columns of vector type) call. << 244 // When writing multiple times in teh same fil << 245 // to be recreated as well. << 246 << 247 // G4cout << "G4RootPNtupleManager::CreateNt << 248 // << " fCreateNtuple: " << fCreateNtuples << 249 // << " fNewCycle: " << fNewCycle << 250 // << " fMainNtupleManager->GetNewCycle(): << 251 // << " fNtupleDescriptionVector.size(): " << 252 // << " fNtupleVector.size(): " << fNtuple << 253 // << G4endl; << 254 << 255 if (fCreateNtuples) { << 256 // create ntuple descriptions << 257 CreateNtupleDescriptionsFromBooking(); << 258 << 259 // create main ntuples if needed << 260 G4AutoLock lock(&createMainMutex); << 261 if (fMainNtupleManager->GetNewCycle()) { << 262 fMainNtupleManager->CreateNtuplesFromBoo << 263 } << 264 lock.unlock(); << 265 239 266 // create slave ntuples << 240 #ifdef G4VERBOSE 267 CreateNtuplesFromMain(); << 241 if ( fState.GetVerboseL4() ) 268 fCreateNtuples = false; << 242 fState.GetVerboseL4()->Message("create", "pntuple booking", name); 269 } << 243 #endif 270 244 271 if (fNewCycle) { << 245 // Set create mode if not yet defined 272 // create main ntuples if needed << 246 if ( fCreateMode == G4PNtupleCreateMode::kUndefined ) { 273 G4AutoLock lock(&createMainMutex); << 247 if ( fMainNtupleManager->GetNtupleFile() ) { 274 if (fMainNtupleManager->GetNewCycle()) { << 248 fCreateMode = G4PNtupleCreateMode::kSlaveAfterOpen; 275 fMainNtupleManager->CreateNtuplesFromBoo << 249 } else { >> 250 fCreateMode = G4PNtupleCreateMode::kSlaveBeforeOpen; 276 } 251 } 277 lock.unlock(); << 278 << 279 // create slave ntuples << 280 CreateNtuplesFromMain(); << 281 fNewCycle = false; << 282 } 252 } 283 } << 253 >> 254 // Create ntuple description >> 255 auto index = fNtupleDescriptionVector.size(); >> 256 auto ntupleDescription = new G4RootPNtupleDescription(); >> 257 fNtupleDescriptionVector.push_back(ntupleDescription); >> 258 >> 259 // Save name & title in ntuple booking >> 260 ntupleDescription->fNtupleBooking.set_name(name); >> 261 ntupleDescription->fNtupleBooking.set_title(title); >> 262 >> 263 fLockFirstId = true; >> 264 >> 265 #ifdef G4VERBOSE >> 266 if ( fState.GetVerboseL2() ) { >> 267 G4ExceptionDescription description; >> 268 description << name << " ntupleId " << index + fFirstId; >> 269 fState.GetVerboseL2()->Message("create", "pntuple booking", description); >> 270 } >> 271 #endif >> 272 >> 273 return index + fFirstId; >> 274 } >> 275 >> 276 //_____________________________________________________________________________ >> 277 G4int G4RootPNtupleManager::CreateNtupleIColumn( >> 278 G4int ntupleId, const G4String& name, std::vector<int>* vector) >> 279 { >> 280 return CreateNtupleTColumn<int>(ntupleId, name, vector); >> 281 } >> 282 >> 283 //_____________________________________________________________________________ >> 284 G4int G4RootPNtupleManager::CreateNtupleFColumn( >> 285 G4int ntupleId, const G4String& name, std::vector<float>* vector) >> 286 { >> 287 return CreateNtupleTColumn<float>(ntupleId, name, vector); >> 288 } >> 289 >> 290 >> 291 //_____________________________________________________________________________ >> 292 G4int G4RootPNtupleManager::CreateNtupleDColumn( >> 293 G4int ntupleId, const G4String& name, std::vector<double>* vector) >> 294 { >> 295 return CreateNtupleTColumn<double>(ntupleId, name, vector); >> 296 } 284 297 285 //____________________________________________ 298 //_____________________________________________________________________________ 286 G4int G4RootPNtupleManager::CreateNtuple(G4Ntu << 299 G4int G4RootPNtupleManager::CreateNtupleSColumn( >> 300 G4int ntupleId, const G4String& name) 287 { 301 { 288 // Create pntuple from g4 ntuple booking. << 302 return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr); 289 // Nothing to be done here. << 303 } >> 304 >> 305 //_____________________________________________________________________________ >> 306 void G4RootPNtupleManager::FinishNtuple(G4int ntupleId) >> 307 { >> 308 // create ntuple if file was open 290 309 291 return G4Analysis::kInvalidId; << 310 // if ( fMainNtupleManager->GetNtupleFile() ) { >> 311 if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) { >> 312 auto ntupleDescription >> 313 = GetNtupleDescriptionInFunction(ntupleId, "FinishNtuple"); >> 314 if ( ! ntupleDescription ) return; >> 315 >> 316 auto mainNtuple = GetMainNtupleInFunction(ntupleId, "FinishNtuple"); >> 317 if ( ! mainNtuple ) return; >> 318 >> 319 CreateNtuple(ntupleDescription, mainNtuple); >> 320 } 292 } 321 } 293 322 294 //____________________________________________ 323 //_____________________________________________________________________________ 295 G4bool G4RootPNtupleManager::FillNtupleIColumn 324 G4bool G4RootPNtupleManager::FillNtupleIColumn( 296 G4int ntupleId, G4int columnId, G4int value) 325 G4int ntupleId, G4int columnId, G4int value) 297 { 326 { 298 return FillNtupleTColumn<int>(ntupleId, colu 327 return FillNtupleTColumn<int>(ntupleId, columnId, value); 299 } << 328 } 300 329 301 //____________________________________________ 330 //_____________________________________________________________________________ 302 G4bool G4RootPNtupleManager::FillNtupleFColumn 331 G4bool G4RootPNtupleManager::FillNtupleFColumn( 303 G4int ntupleId, G4int columnId, G4float valu 332 G4int ntupleId, G4int columnId, G4float value) 304 { 333 { 305 return FillNtupleTColumn<float>(ntupleId, co 334 return FillNtupleTColumn<float>(ntupleId, columnId, value); 306 } << 335 } 307 336 308 //____________________________________________ 337 //_____________________________________________________________________________ 309 G4bool G4RootPNtupleManager::FillNtupleDColumn 338 G4bool G4RootPNtupleManager::FillNtupleDColumn( 310 G4int ntupleId, G4int columnId, G4double val 339 G4int ntupleId, G4int columnId, G4double value) 311 { 340 { 312 return FillNtupleTColumn<double>(ntupleId, c 341 return FillNtupleTColumn<double>(ntupleId, columnId, value); 313 } << 342 } 314 343 315 //____________________________________________ 344 //_____________________________________________________________________________ 316 G4bool G4RootPNtupleManager::FillNtupleSColumn 345 G4bool G4RootPNtupleManager::FillNtupleSColumn( 317 G4int ntupleId, G4int columnId, const G4Stri 346 G4int ntupleId, G4int columnId, const G4String& value) 318 { 347 { 319 return FillNtupleTColumn<std::string>(ntuple 348 return FillNtupleTColumn<std::string>(ntupleId, columnId, value); 320 } << 349 } 321 350 322 //____________________________________________ 351 //_____________________________________________________________________________ 323 G4bool G4RootPNtupleManager::AddNtupleRow(G4in 352 G4bool G4RootPNtupleManager::AddNtupleRow(G4int ntupleId) 324 { << 353 { 325 if ( fState.GetIsActivation() && ( ! GetActi 354 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) { 326 //G4cout << "Skipping AddNtupleRow for " < << 355 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl; 327 return false; << 356 return false; 328 } << 357 } 329 << 358 330 if ( IsVerbose(kVL4) ) { << 359 #ifdef G4VERBOSE 331 Message(kVL4, "add", "pntuple row", " ntup << 360 if ( fState.GetVerboseL4() ) { 332 } << 361 G4ExceptionDescription description; 333 << 362 description << " ntupleId " << ntupleId; 334 // Creating ntuples on workers is triggered << 363 fState.GetVerboseL4()->Message("add", "pntuple row", description); 335 // or AddRow (in only columns of vector type << 364 } 336 CreateNtuplesIfNeeded(); << 365 #endif 337 366 338 auto ntupleDescription = GetNtupleDescriptio 367 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow"); 339 if (ntupleDescription == nullptr) return fal << 368 if ( ! ntupleDescription ) return false; 340 << 369 341 auto rfile = std::get<0>(*ntupleDescription- << 342 << 343 G4AutoLock lock(&pntupleMutex); 370 G4AutoLock lock(&pntupleMutex); 344 lock.unlock(); 371 lock.unlock(); 345 mutex toolsLock(lock); 372 mutex toolsLock(lock); 346 auto result << 373 auto result 347 = ntupleDescription->GetNtuple()->add_row( << 374 = ntupleDescription->fNtuple >> 375 ->add_row(toolsLock, *ntupleDescription->fFile); 348 376 349 if ( ! result ) { 377 if ( ! result ) { 350 Warn("NtupleId " + to_string(ntupleId) + " << 378 G4ExceptionDescription description; 351 fkClass, "AddNtupleRow"); << 379 description << " " << " ntupleId " << ntupleId 352 } << 380 << "adding row has failed."; 353 << 381 G4Exception("G4RootPNtupleManager::AddNtupleRow()", 354 ntupleDescription->GetDescription().SetHasFi << 382 "Analysis_W002", JustWarning, description); 355 << 383 } 356 if ( IsVerbose(kVL3) ) { << 384 357 Message(kVL3, "add", "pntuple row", " ntup << 385 #ifdef G4VERBOSE 358 } << 386 if ( fState.GetVerboseL3() ) { 359 << 387 G4ExceptionDescription description; >> 388 description << " ntupleId " << ntupleId; >> 389 fState.GetVerboseL3()->Message("add", "pntuple row", description); >> 390 } >> 391 #endif 360 392 361 return true; 393 return true; 362 } 394 } 363 395 364 //____________________________________________ 396 //_____________________________________________________________________________ 365 G4bool G4RootPNtupleManager::Merge() 397 G4bool G4RootPNtupleManager::Merge() 366 { 398 { 367 for ( auto ntupleDescription : fNtupleDescri 399 for ( auto ntupleDescription : fNtupleDescriptionVector) { 368 400 369 // skip inactivated ntuples 401 // skip inactivated ntuples 370 if (! ntupleDescription->GetDescription(). << 402 if ( ! ntupleDescription->fActivation ) continue; 371 (ntupleDescription->GetNtuple() == nul << 403 372 // G4cout << "skipping inactive ntuple " << 404 #ifdef G4VERBOSE 373 continue; << 405 if ( fState.GetVerboseL4() ) { 374 } << 406 fState.GetVerboseL4() 375 << 407 ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name()); 376 if ( IsVerbose(kVL4) ) { << 408 } 377 Message(kVL4, "merge", "pntuple", << 409 #endif 378 ntupleDescription->GetDescription().Ge << 410 379 } << 380 << 381 auto rfile = std::get<0>(*ntupleDescriptio << 382 << 383 G4AutoLock lock(&pntupleMutex); 411 G4AutoLock lock(&pntupleMutex); 384 lock.unlock(); 412 lock.unlock(); 385 mutex toolsLock(lock); 413 mutex toolsLock(lock); 386 auto result << 414 auto result 387 = ntupleDescription->GetNtuple()->end_fi << 415 = ntupleDescription->fNtuple >> 416 ->end_fill(toolsLock, *ntupleDescription->fFile); 388 417 389 if ( ! result ) { 418 if ( ! result ) { 390 Warn("Ntuple " + ntupleDescription->GetD << 419 G4ExceptionDescription description; 391 "end fill has failed.", fkClass, "M << 420 description << " " << " ntuple " << ntupleDescription->fNtupleBooking.name() 392 } << 421 << "end fill has failed."; 393 << 422 G4Exception("G4RootPNtupleManager::Merge()", 394 if ( IsVerbose(kVL3) ) { << 423 "Analysis_W002", JustWarning, description); 395 Message(kVL3, "merge", "pntuple", << 396 ntupleDescription->GetDescription().Ge << 397 } 424 } 398 425 >> 426 delete ntupleDescription->fNtuple; >> 427 ntupleDescription->fNtuple = nullptr; >> 428 >> 429 #ifdef G4VERBOSE >> 430 if ( fState.GetVerboseL3() ) { >> 431 fState.GetVerboseL3() >> 432 ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name()); >> 433 } >> 434 #endif 399 } 435 } 400 << 401 // Set new cycle << 402 fNewCycle = true; << 403 << 404 return true; 436 return true; 405 } << 406 << 407 //____________________________________________ << 408 G4bool G4RootPNtupleManager::Reset() << 409 { << 410 // Reset ntuple description, this will delet << 411 // we have its ownership. << 412 // The ntuples will be recreated with new cy << 413 437 414 for ( auto ntupleDescription : fNtupleDescri << 415 ntupleDescription->Reset(); << 416 } << 417 << 418 fNtupleVector.clear(); << 419 << 420 return true; << 421 } 438 } 422 439 423 //____________________________________________ 440 //_____________________________________________________________________________ 424 void G4RootPNtupleManager::Clear() << 441 G4bool G4RootPNtupleManager::Reset(G4bool deleteNtuple) 425 { 442 { 426 for ( auto ntupleDescription : fNtupleDescri 443 for ( auto ntupleDescription : fNtupleDescriptionVector ) { 427 delete ntupleDescription->GetNtuple(); << 444 if ( deleteNtuple ) { >> 445 delete ntupleDescription->fNtuple; >> 446 } >> 447 ntupleDescription->fNtuple = nullptr; 428 } 448 } 429 449 430 fNtupleDescriptionVector.clear(); << 450 fNtupleVector.clear(); 431 fNtupleVector.clear(); << 451 432 << 433 Message(kVL2, "clear", "pntuples"); << 434 } << 435 << 436 //____________________________________________ << 437 G4bool G4RootPNtupleManager::Delete(G4int id) << 438 { << 439 if ( IsVerbose(G4Analysis::kVL4) ) { << 440 Message(G4Analysis::kVL4, "delete", "pntup << 441 } << 442 << 443 auto ntupleDescription = GetNtupleDescriptio << 444 << 445 if (ntupleDescription == nullptr) return fal << 446 << 447 // Delete/clear ntuple data << 448 delete ntupleDescription->GetNtuple(); << 449 ntupleDescription->SetNtuple(nullptr); << 450 ntupleDescription->SetBasePNtuple(nullptr); << 451 ntupleDescription->GetMainBranches().clear() << 452 << 453 // Update ntuple vector << 454 auto index = id - GetFirstId(); << 455 fNtupleVector[index] = nullptr; << 456 << 457 Message(G4Analysis::kVL2, "delete", "pntuple << 458 << 459 return true; 452 return true; 460 } << 453 } 461 454 462 //____________________________________________ 455 //_____________________________________________________________________________ 463 456 464 void G4RootPNtupleManager::SetActivation( 457 void G4RootPNtupleManager::SetActivation( 465 G4bool activation) 458 G4bool activation) 466 { 459 { 467 for ( auto ntupleDescription : fNtupleDescri 460 for ( auto ntupleDescription : fNtupleDescriptionVector ) { 468 ntupleDescription->GetDescription().SetAct << 461 ntupleDescription->fActivation = activation; 469 } << 462 } 470 } 463 } 471 464 472 //____________________________________________ 465 //_____________________________________________________________________________ 473 466 474 void G4RootPNtupleManager::SetActivation( 467 void G4RootPNtupleManager::SetActivation( 475 G4int ntupleId, G4bool activation) 468 G4int ntupleId, G4bool activation) 476 { 469 { 477 auto ntupleDescription = GetNtupleDescriptio 470 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation"); 478 if (ntupleDescription == nullptr) return; << 471 if ( ! ntupleDescription ) return; 479 472 480 ntupleDescription->GetDescription().SetActiv << 473 ntupleDescription->fActivation = activation; 481 } 474 } 482 475 483 //____________________________________________ 476 //_____________________________________________________________________________ 484 G4bool G4RootPNtupleManager::GetActivation( 477 G4bool G4RootPNtupleManager::GetActivation( 485 G4int ntupleId) const 478 G4int ntupleId) const 486 { 479 { 487 auto ntupleDescription = GetNtupleDescriptio 480 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation"); 488 if (ntupleDescription == nullptr) return fal << 481 if ( ! ntupleDescription ) return false; 489 482 490 return ntupleDescription->GetDescription().G << 483 return ntupleDescription->fActivation; 491 } 484 } 492 485 493 //____________________________________________ 486 //_____________________________________________________________________________ 494 void G4RootPNtupleManager::SetNewCycle(G4bool << 487 G4int G4RootPNtupleManager::GetNofNtuples() const 495 { 488 { 496 fNewCycle = value; << 489 return fNtupleVector.size(); 497 } 490 } 498 491 499 //____________________________________________ 492 //_____________________________________________________________________________ 500 G4bool G4RootPNtupleManager::GetNewCycle() con << 493 G4int G4RootPNtupleManager::GetNofNtupleBookings() const 501 { 494 { 502 return fNewCycle; << 495 return fNtupleDescriptionVector.size(); 503 } 496 } 504 497 505 //____________________________________________ 498 //_____________________________________________________________________________ 506 void G4RootPNtupleManager::SetNtupleRowWise(G4 << 499 G4bool G4RootPNtupleManager::IsEmpty() const 507 { 500 { >> 501 return ! fNtupleDescriptionVector.size(); >> 502 } >> 503 >> 504 //_____________________________________________________________________________ >> 505 void G4RootPNtupleManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode) >> 506 { 508 fRowWise = rowWise; 507 fRowWise = rowWise; 509 fRowMode = rowMode; 508 fRowMode = rowMode; 510 } 509 } 511 510