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, 18/06/2013 (ivan 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4GenericAnalysisManager.hh" 29 #include "G4GenericAnalysisManager.hh" 30 #include "G4GenericFileManager.hh" 30 #include "G4GenericFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 #include "G4NtupleBookingManager.hh" 33 #include "G4NtupleBookingManager.hh" 34 #include "G4VNtupleFileManager.hh" 34 #include "G4VNtupleFileManager.hh" 35 #include "G4ThreadLocalSingleton.hh" 35 #include "G4ThreadLocalSingleton.hh" 36 #include "G4Exception.hh" 36 #include "G4Exception.hh" 37 37 38 using namespace G4Analysis; 38 using namespace G4Analysis; 39 using std::to_string; 39 using std::to_string; 40 40 41 // mutex in a file scope 41 // mutex in a file scope 42 42 43 namespace { 43 namespace { 44 44 45 //____________________________________________ 45 //_____________________________________________________________________________ 46 void WriteHnWarning(const G4String& hnType, G4 46 void WriteHnWarning(const G4String& hnType, G4int id, 47 std::string_view inClass, 47 std::string_view inClass, 48 std::string_view inFunctio 48 std::string_view inFunction) 49 { 49 { 50 Warn("Failed to get " + hnType + " id " + to 50 Warn("Failed to get " + hnType + " id " + to_string(id), inClass, inFunction); 51 } 51 } 52 52 53 } 53 } 54 54 55 //____________________________________________ 55 //_____________________________________________________________________________ 56 G4GenericAnalysisManager* G4GenericAnalysisMan 56 G4GenericAnalysisManager* G4GenericAnalysisManager::Instance() 57 { 57 { 58 static G4ThreadLocalSingleton<G4GenericAnaly 58 static G4ThreadLocalSingleton<G4GenericAnalysisManager> instance; 59 fgIsInstance = true; 59 fgIsInstance = true; 60 return instance.Instance(); 60 return instance.Instance(); 61 } 61 } 62 62 63 //____________________________________________ 63 //_____________________________________________________________________________ 64 G4bool G4GenericAnalysisManager::IsInstance() 64 G4bool G4GenericAnalysisManager::IsInstance() 65 { 65 { 66 return fgIsInstance; 66 return fgIsInstance; 67 } 67 } 68 68 69 //____________________________________________ 69 //_____________________________________________________________________________ 70 G4GenericAnalysisManager::G4GenericAnalysisMan 70 G4GenericAnalysisManager::G4GenericAnalysisManager() 71 : G4ToolsAnalysisManager("") 71 : G4ToolsAnalysisManager("") 72 { 72 { 73 if ( ! G4Threading::IsWorkerThread() ) fgMas 73 if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this; 74 74 75 // File manager 75 // File manager 76 fFileManager = std::make_shared<G4GenericFil 76 fFileManager = std::make_shared<G4GenericFileManager>(fState); 77 SetFileManager(fFileManager); 77 SetFileManager(fFileManager); 78 } 78 } 79 79 80 //____________________________________________ 80 //_____________________________________________________________________________ 81 G4GenericAnalysisManager::~G4GenericAnalysisMa 81 G4GenericAnalysisManager::~G4GenericAnalysisManager() 82 { 82 { 83 if ( fState.GetIsMaster() ) fgMasterInstance 83 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr; 84 fgIsInstance = false; 84 fgIsInstance = false; 85 } 85 } 86 86 87 // 87 // 88 // private methods 88 // private methods 89 // 89 // 90 90 91 //____________________________________________ 91 //_____________________________________________________________________________ 92 void G4GenericAnalysisManager::CreateNtupleFil 92 void G4GenericAnalysisManager::CreateNtupleFileManager(const G4String& fileName) 93 { 93 { 94 if ( fNtupleFileManager ) { 94 if ( fNtupleFileManager ) { 95 Warn("The ntuple file manager already exis 95 Warn("The ntuple file manager already exists.", 96 fkClass, "CreateNtupleFileManager"); 96 fkClass, "CreateNtupleFileManager"); 97 return; 97 return; 98 } 98 } 99 99 100 auto fileType = GetExtension(fileName); 100 auto fileType = GetExtension(fileName); 101 auto output = G4Analysis::GetOutput(fileType 101 auto output = G4Analysis::GetOutput(fileType); 102 if ( output == G4AnalysisOutput::kNone ) { 102 if ( output == G4AnalysisOutput::kNone ) { 103 Warn("The file type " + fileType + "is not 103 Warn("The file type " + fileType + "is not supported.", 104 fkClass, "CreateNtupleFileManager"); 104 fkClass, "CreateNtupleFileManager"); 105 return; 105 return; 106 } 106 } 107 107 108 // Set file type to booked ntuples 108 // Set file type to booked ntuples 109 fNtupleBookingManager->SetFileType(fileType) 109 fNtupleBookingManager->SetFileType(fileType); 110 110 111 Message(kVL4, "create", "ntuple file manager 111 Message(kVL4, "create", "ntuple file manager", fileType); 112 112 113 fNtupleFileManager = fFileManager->CreateNtu 113 fNtupleFileManager = fFileManager->CreateNtupleFileManager(output); 114 if (fNtupleFileManager) { 114 if (fNtupleFileManager) { 115 SetNtupleFileManager(fNtupleFileManager); << 116 fNtupleFileManager->SetBookingManager(fNtu 115 fNtupleFileManager->SetBookingManager(fNtupleBookingManager); 117 116 118 if ( fNtupleFileManager->IsNtupleMergingSu 117 if ( fNtupleFileManager->IsNtupleMergingSupported() ) { 119 // set merginng 118 // set merginng 120 fNtupleFileManager->SetNtupleMerging(fMe 119 fNtupleFileManager->SetNtupleMerging(fMergeNtuples, fNofNtupleFiles); 121 fNtupleFileManager->SetNtupleRowWise(fNt 120 fNtupleFileManager->SetNtupleRowWise(fNtupleRowWise, fNtupleRowMode); 122 fNtupleFileManager->SetBasketSize(fBaske 121 fNtupleFileManager->SetBasketSize(fBasketSize); 123 fNtupleFileManager->SetBasketEntries(fBa 122 fNtupleFileManager->SetBasketEntries(fBasketEntries); 124 } 123 } 125 else if ( fIsNtupleMergingSet && fMergeNtu 124 else if ( fIsNtupleMergingSet && fMergeNtuples ) { 126 Warn("Ntuple merging is not available wi 125 Warn("Ntuple merging is not available with " + fileType + " output.\n" + 127 "Setting is ignored.", 126 "Setting is ignored.", 128 fkClass, "CreateNtupleFileManager") 127 fkClass, "CreateNtupleFileManager"); 129 } 128 } 130 } 129 } 131 130 132 Message(kVL3, "create", "ntuple file manager 131 Message(kVL3, "create", "ntuple file manager", fileType); 133 } 132 } 134 133 135 // 134 // 136 // protected methods 135 // protected methods 137 // 136 // 138 137 139 //____________________________________________ 138 //_____________________________________________________________________________ 140 G4bool G4GenericAnalysisManager::OpenFileImpl( 139 G4bool G4GenericAnalysisManager::OpenFileImpl(const G4String& fileName) 141 { 140 { 142 Message(kVL4, "open", "file", fileName); 141 Message(kVL4, "open", "file", fileName); 143 142 144 // Add file name extension, if missing 143 // Add file name extension, if missing 145 auto fullFileName = fileName; 144 auto fullFileName = fileName; 146 if (GetExtension(fileName).size() == 0u) { << 145 if ( ! GetExtension(fileName).size() ) { 147 auto defaultFileType = fFileManager->GetDe 146 auto defaultFileType = fFileManager->GetDefaultFileType(); 148 // G4cout << "File type is not defined, us 147 // G4cout << "File type is not defined, using default: " << defaultFileType << G4endl; 149 if (defaultFileType.size() == 0u) { << 148 if ( ! defaultFileType.size() ) { 150 G4Exception("G4GenericAnalysisManager::O 149 G4Exception("G4GenericAnalysisManager::OpenFileImpl", "Analysis_F001", 151 FatalException, 150 FatalException, 152 G4String("Cannot open file \"" + fileN 151 G4String("Cannot open file \"" + fileName + "\".\n" 153 "Please, use a file name with an ext 152 "Please, use a file name with an extension or define the default file type\n" 154 "via G4AnalysisManager::SetDefaultFi 153 "via G4AnalysisManager::SetDefaultFileType()")); 155 } 154 } 156 155 157 fullFileName = fileName + "." + fFileManag 156 fullFileName = fileName + "." + fFileManager->GetDefaultFileType(); 158 } 157 } 159 158 160 // Create ntuple file manager if there are b 159 // Create ntuple file manager if there are booked ntuples 161 if (! fNtupleFileManager) { 160 if (! fNtupleFileManager) { 162 CreateNtupleFileManager(fullFileName); 161 CreateNtupleFileManager(fullFileName); 163 } 162 } 164 163 >> 164 // Create ntuple manager >> 165 // and set it to base class which takes then its ownership >> 166 if (fNtupleFileManager) { >> 167 SetNtupleManager(fNtupleFileManager->CreateNtupleManager()); >> 168 } >> 169 165 auto result = true; 170 auto result = true; >> 171 >> 172 // Open file for histograms/profiles >> 173 result &= fFileManager->OpenFile(fullFileName); >> 174 >> 175 // Open ntuple file(s) and create ntuples from bookings 166 if (fNtupleFileManager) { 176 if (fNtupleFileManager) { 167 result &= G4ToolsAnalysisManager::OpenFile << 177 result &= fNtupleFileManager->ActionAtOpenFile(fullFileName); >> 178 } >> 179 >> 180 Message(kVL3, "open", "file", fileName); >> 181 >> 182 return result; >> 183 } >> 184 >> 185 //_____________________________________________________________________________ >> 186 G4bool G4GenericAnalysisManager::WriteImpl() >> 187 { >> 188 Message(kVL4, "write", "files"); >> 189 >> 190 auto result = true; >> 191 if ( G4Threading::IsWorkerThread() ) { >> 192 result &= G4ToolsAnalysisManager::Merge(); 168 } 193 } 169 else { 194 else { 170 // no ntuples (check if this mode is suppo << 195 // Open all files registered with objects 171 result &= fFileManager->OpenFile(fullFileN << 196 fFileManager->OpenFiles(); >> 197 >> 198 // Write all histograms/profile on master >> 199 result &= G4ToolsAnalysisManager::WriteImpl(); >> 200 } >> 201 >> 202 // Ntuples >> 203 if (fNtupleFileManager) { >> 204 result &= fNtupleFileManager->ActionAtWrite(); >> 205 } >> 206 >> 207 // File >> 208 result &= fFileManager->WriteFiles(); >> 209 >> 210 // Write ASCII if activated >> 211 if ( IsAscii() ) { >> 212 result &= WriteAscii(fFileManager->GetFileName()); 172 } 213 } 173 214 174 Message(kVL3, "open", "file", fileName, resu << 215 Message(kVL3, "write", "files", "", result); 175 216 176 return result; 217 return result; 177 } 218 } 178 219 179 //____________________________________________ 220 //_____________________________________________________________________________ 180 void G4GenericAnalysisManager::SetDefaultFileT << 221 G4bool G4GenericAnalysisManager::CloseFileImpl(G4bool reset) 181 { 222 { 182 G4VAnalysisManager::SetDefaultFileTypeImpl(v << 223 Message(kVL4, "close", "files"); >> 224 >> 225 auto result = true; >> 226 if (fNtupleFileManager) { >> 227 result &= fNtupleFileManager->ActionAtCloseFile(reset); >> 228 } >> 229 >> 230 // close file >> 231 if ( ! fFileManager->CloseFiles() ) { >> 232 Warn("Closing files failed", fkClass, "CloseFileImpl"); >> 233 result = false; >> 234 } 183 235 184 fFileManager->SetDefaultFileType(value); << 236 // delete empty files >> 237 if ( ! fFileManager->DeleteEmptyFiles() ) { >> 238 Warn("Deleting empty files failed", fkClass, "CloseFileImpl"); >> 239 result = false; >> 240 } >> 241 >> 242 if ( reset ) { >> 243 if ( ! Reset() ) { >> 244 Warn("Resetting data failed", fkClass, "CloseFileImpl"); >> 245 result = false; >> 246 } >> 247 } >> 248 >> 249 Message(kVL3, "close", "files", "", result); >> 250 >> 251 return result; >> 252 } >> 253 >> 254 //_____________________________________________________________________________ >> 255 G4bool G4GenericAnalysisManager::ResetImpl() >> 256 { >> 257 // Reset histograms and ntuple >> 258 >> 259 Message(kVL4, "reset", ""); >> 260 >> 261 auto result = true; >> 262 result &= G4ToolsAnalysisManager::ResetImpl(); >> 263 if ( fNtupleFileManager != nullptr ) { >> 264 result &= fNtupleFileManager->Reset(); >> 265 } >> 266 >> 267 Message(kVL3, "reset", "", "", result); >> 268 >> 269 return result; 185 } 270 } 186 271 187 //____________________________________________ 272 //_____________________________________________________________________________ 188 G4bool G4GenericAnalysisManager::WriteH1(G4int 273 G4bool G4GenericAnalysisManager::WriteH1(G4int id, const G4String& fileName) 189 { 274 { 190 // Experimental extra write 275 // Experimental extra write 191 276 192 // Do not write histo on worker (redundant a 277 // Do not write histo on worker (redundant and fails in hdf5 ) 193 // If default file is not used, users have t 278 // If default file is not used, users have to call Merge from their code 194 if ( G4Threading::IsWorkerThread() ) return 279 if ( G4Threading::IsWorkerThread() ) return false; 195 280 196 auto h1d = GetH1(id, false); 281 auto h1d = GetH1(id, false); 197 if (h1d == nullptr) { << 282 if ( ! h1d ) { 198 WriteHnWarning("H1", id, fkClass, "WriteH1 283 WriteHnWarning("H1", id, fkClass, "WriteH1"); 199 return false; 284 return false; 200 } 285 } 201 286 202 auto h1Name = GetH1Name(id); 287 auto h1Name = GetH1Name(id); 203 return fFileManager->WriteTExtra<tools::hist 288 return fFileManager->WriteTExtra<tools::histo::h1d>(fileName, h1d, h1Name); 204 } 289 } 205 290 206 //____________________________________________ 291 //_____________________________________________________________________________ 207 G4bool G4GenericAnalysisManager::WriteH2(G4int 292 G4bool G4GenericAnalysisManager::WriteH2(G4int id, const G4String& fileName) 208 { 293 { 209 // Experimental extra write 294 // Experimental extra write 210 295 211 // Do not write histo on worker (redundant a 296 // Do not write histo on worker (redundant and fails in hdf5 ) 212 // If default file is not used, users have t 297 // If default file is not used, users have to call Merge from their code 213 if ( G4Threading::IsWorkerThread() ) return 298 if ( G4Threading::IsWorkerThread() ) return false; 214 299 215 auto h2d = GetH2(id, false); 300 auto h2d = GetH2(id, false); 216 if (h2d == nullptr) { << 301 if ( ! h2d ) { 217 WriteHnWarning("H2", id, fkClass, "WriteH2 302 WriteHnWarning("H2", id, fkClass, "WriteH2"); 218 return false; 303 return false; 219 } 304 } 220 305 221 auto h2Name = GetH2Name(id); 306 auto h2Name = GetH2Name(id); 222 return fFileManager->WriteTExtra<tools::hist 307 return fFileManager->WriteTExtra<tools::histo::h2d>(fileName, h2d, h2Name); 223 } 308 } 224 //____________________________________________ 309 //_____________________________________________________________________________ 225 G4bool G4GenericAnalysisManager::WriteH3(G4int 310 G4bool G4GenericAnalysisManager::WriteH3(G4int id, const G4String& fileName) 226 { 311 { 227 // Experimental extra write 312 // Experimental extra write 228 313 229 // Do not write histo on worker (redundant a 314 // Do not write histo on worker (redundant and fails in hdf5 ) 230 // If default file is not used, users have t 315 // If default file is not used, users have to call Merge from their code 231 if ( G4Threading::IsWorkerThread() ) return 316 if ( G4Threading::IsWorkerThread() ) return false; 232 317 233 auto h3d = GetH3(id, false); 318 auto h3d = GetH3(id, false); 234 if (h3d == nullptr) { << 319 if ( ! h3d ) { 235 WriteHnWarning("H3", id, fkClass, "WriteH3 320 WriteHnWarning("H3", id, fkClass, "WriteH3"); 236 return false; 321 return false; 237 } 322 } 238 323 239 auto h3Name = GetH3Name(id); 324 auto h3Name = GetH3Name(id); 240 return fFileManager->WriteTExtra<tools::hist 325 return fFileManager->WriteTExtra<tools::histo::h3d>(fileName, h3d, h3Name); 241 } 326 } 242 327 243 //____________________________________________ 328 //_____________________________________________________________________________ 244 G4bool G4GenericAnalysisManager::WriteP1(G4int 329 G4bool G4GenericAnalysisManager::WriteP1(G4int id, const G4String& fileName) 245 { 330 { 246 // Experimental extra write 331 // Experimental extra write 247 332 248 // Do not write histo on worker (redundant a 333 // Do not write histo on worker (redundant and fails in hdf5 ) 249 // If default file is not used, users have t 334 // If default file is not used, users have to call Merge from their code 250 if ( G4Threading::IsWorkerThread() ) return 335 if ( G4Threading::IsWorkerThread() ) return false; 251 336 252 auto p1d = GetP1(id, false); 337 auto p1d = GetP1(id, false); 253 if (p1d == nullptr) { << 338 if ( ! p1d ) { 254 WriteHnWarning("P1", id, fkClass, "WriteP1 339 WriteHnWarning("P1", id, fkClass, "WriteP1"); 255 return false; 340 return false; 256 } 341 } 257 342 258 auto p1Name = GetP1Name(id); 343 auto p1Name = GetP1Name(id); 259 return fFileManager->WriteTExtra<tools::hist 344 return fFileManager->WriteTExtra<tools::histo::p1d>(fileName, p1d, p1Name); 260 } 345 } 261 << 262 //____________________________________________ 346 //_____________________________________________________________________________ 263 G4bool G4GenericAnalysisManager::WriteP2(G4int 347 G4bool G4GenericAnalysisManager::WriteP2(G4int id, const G4String& fileName) 264 { 348 { 265 // Experimental extra write 349 // Experimental extra write 266 350 267 // Do not write histo on worker (redundant a 351 // Do not write histo on worker (redundant and fails in hdf5 ) 268 // If default file is not used, users have t 352 // If default file is not used, users have to call Merge from their code 269 if ( G4Threading::IsWorkerThread() ) return 353 if ( G4Threading::IsWorkerThread() ) return false; 270 354 271 auto p2d = GetP2(id, false); 355 auto p2d = GetP2(id, false); 272 if (p2d == nullptr) { << 356 if ( ! p2d ) { 273 WriteHnWarning("P2", id, fkClass, "WriteP2 357 WriteHnWarning("P2", id, fkClass, "WriteP2"); 274 return false; 358 return false; 275 } 359 } 276 360 277 auto p2Name = GetP2Name(id); 361 auto p2Name = GetP2Name(id); 278 return fFileManager->WriteTExtra<tools::hist 362 return fFileManager->WriteTExtra<tools::histo::p2d>(fileName, p2d, p2Name); 279 } << 280 << 281 //____________________________________________ << 282 tools::ntuple_booking* G4GenericAnalysisManage << 283 G4bool warn, G4bool onlyIfActive) const << 284 { << 285 return fNtupleBookingManager->GetNtuple(warn << 286 } << 287 << 288 //____________________________________________ << 289 tools::ntuple_booking* G4GenericAnalysisManage << 290 G4int ntupleId, G4bool warn, G4bool onlyIfAc << 291 { << 292 return fNtupleBookingManager->GetNtuple(ntup << 293 } 363 } 294 364