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 "G4RootMainNtupleManager.hh" 29 #include "G4RootMainNtupleManager.hh" 30 #include "G4RootFileManager.hh" 30 #include "G4RootFileManager.hh" 31 #include "G4RootNtupleManager.hh" 31 #include "G4RootNtupleManager.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 33 34 #include "tools/wroot/file" 34 #include "tools/wroot/file" 35 #include "tools/wroot/ntuple" 35 #include "tools/wroot/ntuple" 36 36 37 using namespace G4Analysis; 37 using namespace G4Analysis; 38 38 39 //____________________________________________ 39 //_____________________________________________________________________________ 40 G4RootMainNtupleManager::G4RootMainNtupleManag 40 G4RootMainNtupleManager::G4RootMainNtupleManager( 41 G4RootNtupleManager* ntupleBui 41 G4RootNtupleManager* ntupleBuilder, 42 std::shared_ptr<G4NtupleBookin 42 std::shared_ptr<G4NtupleBookingManager> bookingManager, 43 G4bool rowWise, 43 G4bool rowWise, 44 G4int fileNumber, 44 G4int fileNumber, 45 const G4AnalysisManagerState& 45 const G4AnalysisManagerState& state) 46 : G4BaseAnalysisManager(state), 46 : G4BaseAnalysisManager(state), 47 fNtupleBuilder(ntupleBuilder), 47 fNtupleBuilder(ntupleBuilder), 48 fBookingManager(std::move(bookingManager)), 48 fBookingManager(std::move(bookingManager)), 49 fRowWise(rowWise), 49 fRowWise(rowWise), 50 fFileNumber(fileNumber) 50 fFileNumber(fileNumber) 51 {} 51 {} 52 52 53 // 53 // 54 // private functions 54 // private functions 55 // 55 // 56 56 57 //____________________________________________ 57 //_____________________________________________________________________________ 58 G4int G4RootMainNtupleManager::CreateNtupleFro 58 G4int G4RootMainNtupleManager::CreateNtupleFromBooking( 59 G4NtupleBooking* g4NtupleBooking, std::share 59 G4NtupleBooking* g4NtupleBooking, std::shared_ptr<G4RootFile> ntupleFile) 60 { 60 { 61 // Do not create ntuple if it was deleted 61 // Do not create ntuple if it was deleted 62 if ( g4NtupleBooking->GetDeleted()) { 62 if ( g4NtupleBooking->GetDeleted()) { 63 // G4cout << "Ntuple " << g4NtupleBooking- 63 // G4cout << "Ntuple " << g4NtupleBooking->fNtupleId << " was deleted" << G4endl; 64 return G4Analysis::kInvalidId; 64 return G4Analysis::kInvalidId; 65 } 65 } 66 66 67 // The ntuple index 67 // The ntuple index 68 auto index = g4NtupleBooking->fNtupleId - fF 68 auto index = g4NtupleBooking->fNtupleId - fFirstId; 69 69 70 // Do not create ntuple if it already exists 70 // Do not create ntuple if it already exists 71 if ( (index < G4int(fNtupleVector.size())) & 71 if ( (index < G4int(fNtupleVector.size())) && (fNtupleVector[index] != nullptr) ) { 72 // G4cout << "Ntuple " << g4NtupleBooking- 72 // G4cout << "Ntuple " << g4NtupleBooking->fNtupleId << " already exists" << G4endl; 73 return G4Analysis::kInvalidId; 73 return G4Analysis::kInvalidId; 74 } 74 } 75 75 76 // Get ntuple booking 76 // Get ntuple booking 77 const auto& ntupleBooking = g4NtupleBooking- 77 const auto& ntupleBooking = g4NtupleBooking->fNtupleBooking; 78 78 79 Message(kVL4, "create", "main ntuple", ntupl 79 Message(kVL4, "create", "main ntuple", ntupleBooking.name()); 80 80 81 // Allocate the ntuple vector element(s) if 81 // Allocate the ntuple vector element(s) if needed 82 while ( index >= G4int(fNtupleVector.size()) 82 while ( index >= G4int(fNtupleVector.size()) ) { 83 fNtupleVector.push_back(nullptr); 83 fNtupleVector.push_back(nullptr); 84 } 84 } 85 85 86 // Create ntuple 86 // Create ntuple 87 auto ntuple = new tools::wroot::ntuple(*std: 87 auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise); 88 // ntuple object is deleted automatic 88 // ntuple object is deleted automatically when closing a file 89 auto basketSize = fNtupleBuilder->GetBasketS 89 auto basketSize = fNtupleBuilder->GetBasketSize(); 90 ntuple->set_basket_size(basketSize); 90 ntuple->set_basket_size(basketSize); 91 91 92 // Save ntuple & ntuple description pair in 92 // Save ntuple & ntuple description pair in vectors 93 fNtupleVector[index] = ntuple; 93 fNtupleVector[index] = ntuple; 94 94 95 Message(kVL3, "create", "main ntuple", ntupl 95 Message(kVL3, "create", "main ntuple", ntupleBooking.name()); 96 96 97 return index; 97 return index; 98 } 98 } 99 99 100 // 100 // 101 // protected functions 101 // protected functions 102 // 102 // 103 103 104 //____________________________________________ 104 //_____________________________________________________________________________ 105 void G4RootMainNtupleManager::CreateNtuple(Roo 105 void G4RootMainNtupleManager::CreateNtuple(RootNtupleDescription* ntupleDescription, 106 G4b 106 G4bool warn) 107 { 107 { 108 // Create ntuple from booking if file was open 108 // Create ntuple from booking if file was open 109 109 110 // Get/Create main ntuple file 110 // Get/Create main ntuple file 111 auto ntupleFile = fFileManager->CreateNtuple 111 auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription, fFileNumber); 112 if ( ntupleFile == nullptr ) { 112 if ( ntupleFile == nullptr ) { 113 if ( warn ) { 113 if ( warn ) { 114 Warn("Ntuple file must be defined first. 114 Warn("Ntuple file must be defined first.\n" 115 "Cannot create main ntuple.", 115 "Cannot create main ntuple.", 116 fkClass, "CreateNtuple"); 116 fkClass, "CreateNtuple"); 117 } 117 } 118 return; 118 return; 119 } 119 } 120 120 121 // Create ntuple from g4 booking 121 // Create ntuple from g4 booking 122 auto g4NtupleBooking = ntupleDescription->Ge 122 auto g4NtupleBooking = ntupleDescription->GetG4NtupleBooking(); 123 auto index = CreateNtupleFromBooking(g4Ntupl 123 auto index = CreateNtupleFromBooking(g4NtupleBooking, ntupleFile); 124 124 125 // Return if ntuple was not created 125 // Return if ntuple was not created 126 if (index == G4Analysis::kInvalidId) return; 126 if (index == G4Analysis::kInvalidId) return; 127 127 128 // Allocate the ntuple description pair vect 128 // Allocate the ntuple description pair vector element(s) if needed 129 while ( index >= G4int(fNtupleDescriptionVec 129 while ( index >= G4int(fNtupleDescriptionVector.size()) ) { 130 fNtupleDescriptionVector.emplace_back(null << 130 fNtupleDescriptionVector.push_back(std::make_pair(nullptr, nullptr)); 131 } 131 } 132 132 133 // Save ntuple description pair in vectors 133 // Save ntuple description pair in vectors 134 fNtupleDescriptionVector[index] = std::make_ 134 fNtupleDescriptionVector[index] = std::make_pair(ntupleDescription, ntupleFile); 135 } 135 } 136 136 137 //____________________________________________ 137 //_____________________________________________________________________________ 138 G4bool G4RootMainNtupleManager::Delete(G4int i 138 G4bool G4RootMainNtupleManager::Delete(G4int id) 139 { 139 { 140 if (fNtupleVector.empty()) { 140 if (fNtupleVector.empty()) { 141 // Main ntuples are delete with each new c 141 // Main ntuples are delete with each new cycle 142 return true; 142 return true; 143 } 143 } 144 144 145 // Proceed with deleting if vector is not em 145 // Proceed with deleting if vector is not empty 146 146 147 Message(kVL4, "delete", "main ntuple ntupleI 147 Message(kVL4, "delete", "main ntuple ntupleId: " + to_string(id)); 148 148 149 // Get ntuple description 149 // Get ntuple description 150 auto index = id - fFirstId; 150 auto index = id - fFirstId; 151 if ( index < 0 || index >= G4int(fNtupleVect 151 if ( index < 0 || index >= G4int(fNtupleVector.size()) ) { 152 G4Analysis::Warn("Main ntuple " + to_strin 152 G4Analysis::Warn("Main ntuple " + to_string(id) + " does not exist.", 153 fkClass, "Delete"); 153 fkClass, "Delete"); 154 return false; 154 return false; 155 } 155 } 156 156 157 // Delete main ntuple and update ntuple vect 157 // Delete main ntuple and update ntuple vector 158 delete fNtupleVector[index]; 158 delete fNtupleVector[index]; 159 fNtupleVector[index] = nullptr; 159 fNtupleVector[index] = nullptr; 160 160 161 Message(kVL3, "delete", "main ntuple ntupleI 161 Message(kVL3, "delete", "main ntuple ntupleId: " + to_string(id)); 162 162 163 return true; 163 return true; 164 } 164 } 165 165 166 //____________________________________________ 166 //_____________________________________________________________________________ 167 G4bool G4RootMainNtupleManager::Merge() 167 G4bool G4RootMainNtupleManager::Merge() 168 { 168 { 169 std::size_t counter = 0; 169 std::size_t counter = 0; 170 170 171 for ( auto ntuple : fNtupleVector ) { 171 for ( auto ntuple : fNtupleVector ) { 172 // skip deleted ntuples 172 // skip deleted ntuples 173 if (ntuple == nullptr) continue; 173 if (ntuple == nullptr) continue; 174 174 175 ntuple->merge_number_of_entries(); 175 ntuple->merge_number_of_entries(); 176 176 177 // Notify ntuple description that file is 177 // Notify ntuple description that file is not empty 178 if (ntuple->entries() != 0u) { 178 if (ntuple->entries() != 0u) { 179 auto ntupleDescription = fNtupleDescript 179 auto ntupleDescription = fNtupleDescriptionVector.at(counter).first; 180 ntupleDescription->SetHasFill(true); 180 ntupleDescription->SetHasFill(true); 181 } 181 } 182 ++counter; 182 ++counter; 183 } 183 } 184 184 185 return true; 185 return true; 186 } 186 } 187 187 188 //____________________________________________ 188 //_____________________________________________________________________________ 189 G4bool G4RootMainNtupleManager::Reset() 189 G4bool G4RootMainNtupleManager::Reset() 190 { 190 { 191 // The ntuples will be recreated with new cy 191 // The ntuples will be recreated with new cycle or new open file. 192 // Ntuple objects are deleted automatically 192 // Ntuple objects are deleted automatically when closing a file 193 193 194 fNtupleVector.clear(); 194 fNtupleVector.clear(); 195 195 196 return true; 196 return true; 197 } 197 } 198 198 199 //____________________________________________ 199 //_____________________________________________________________________________ 200 void G4RootMainNtupleManager::ClearData() 200 void G4RootMainNtupleManager::ClearData() 201 { 201 { 202 fNtupleDescriptionVector.clear(); 202 fNtupleDescriptionVector.clear(); 203 fNtupleVector.clear(); 203 fNtupleVector.clear(); 204 204 205 Message(G4Analysis::kVL2, "clear", "main ntu 205 Message(G4Analysis::kVL2, "clear", "main ntuples"); 206 } 206 } 207 207 208 //____________________________________________ 208 //_____________________________________________________________________________ 209 std::shared_ptr<G4RootFile> 209 std::shared_ptr<G4RootFile> 210 G4RootMainNtupleManager::GetNtupleFile(RootNtu 210 G4RootMainNtupleManager::GetNtupleFile(RootNtupleDescription* ntupleDescription) const 211 { 211 { 212 auto perThread = false; 212 auto perThread = false; 213 return fFileManager->GetNtupleFile(ntupleDes 213 return fFileManager->GetNtupleFile(ntupleDescription, perThread, fFileNumber); 214 } 214 } 215 215 216 // 216 // 217 // public functions 217 // public functions 218 // 218 // 219 219 220 //____________________________________________ 220 //_____________________________________________________________________________ 221 void G4RootMainNtupleManager::CreateNtuplesFro 221 void G4RootMainNtupleManager::CreateNtuplesFromBooking() 222 { 222 { 223 // Create ntuples from booking (without creati 223 // Create ntuples from booking (without creating ntuple description) 224 // This function is triggered from workers at 224 // This function is triggered from workers at new cycle. 225 225 226 for (auto [ntupleDescription, ntupleFile] : 226 for (auto [ntupleDescription, ntupleFile] : fNtupleDescriptionVector) { 227 CreateNtupleFromBooking(ntupleDescription- << 227 CreateNtupleFromBooking(ntupleDescription->GetG4NtupleBooking(), ntupleFile); 228 } 228 } 229 229 230 SetNewCycle(false); 230 SetNewCycle(false); 231 } 231 } 232 232