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 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 29 #include "G4GenericAnalysisManager.hh" 30 #include "G4GenericFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 33 #include "G4NtupleBookingManager.hh" 34 #include "G4VNtupleFileManager.hh" 35 #include "G4ThreadLocalSingleton.hh" 36 #include "G4Exception.hh" 37 38 using namespace G4Analysis; 39 using std::to_string; 40 41 // mutex in a file scope 42 43 namespace { 44 45 //_____________________________________________________________________________ 46 void WriteHnWarning(const G4String& hnType, G4int id, 47 std::string_view inClass, 48 std::string_view inFunction) 49 { 50 Warn("Failed to get " + hnType + " id " + to_string(id), inClass, inFunction); 51 } 52 53 } 54 55 //_____________________________________________________________________________ 56 G4GenericAnalysisManager* G4GenericAnalysisManager::Instance() 57 { 58 static G4ThreadLocalSingleton<G4GenericAnalysisManager> instance; 59 fgIsInstance = true; 60 return instance.Instance(); 61 } 62 63 //_____________________________________________________________________________ 64 G4bool G4GenericAnalysisManager::IsInstance() 65 { 66 return fgIsInstance; 67 } 68 69 //_____________________________________________________________________________ 70 G4GenericAnalysisManager::G4GenericAnalysisManager() 71 : G4ToolsAnalysisManager("") 72 { 73 if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this; 74 75 // File manager 76 fFileManager = std::make_shared<G4GenericFileManager>(fState); 77 SetFileManager(fFileManager); 78 } 79 80 //_____________________________________________________________________________ 81 G4GenericAnalysisManager::~G4GenericAnalysisManager() 82 { 83 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr; 84 fgIsInstance = false; 85 } 86 87 // 88 // private methods 89 // 90 91 //_____________________________________________________________________________ 92 void G4GenericAnalysisManager::CreateNtupleFileManager(const G4String& fileName) 93 { 94 if ( fNtupleFileManager ) { 95 Warn("The ntuple file manager already exists.", 96 fkClass, "CreateNtupleFileManager"); 97 return; 98 } 99 100 auto fileType = GetExtension(fileName); 101 auto output = G4Analysis::GetOutput(fileType); 102 if ( output == G4AnalysisOutput::kNone ) { 103 Warn("The file type " + fileType + "is not supported.", 104 fkClass, "CreateNtupleFileManager"); 105 return; 106 } 107 108 // Set file type to booked ntuples 109 fNtupleBookingManager->SetFileType(fileType); 110 111 Message(kVL4, "create", "ntuple file manager", fileType); 112 113 fNtupleFileManager = fFileManager->CreateNtupleFileManager(output); 114 if (fNtupleFileManager) { 115 SetNtupleFileManager(fNtupleFileManager); 116 fNtupleFileManager->SetBookingManager(fNtupleBookingManager); 117 118 if ( fNtupleFileManager->IsNtupleMergingSupported() ) { 119 // set merginng 120 fNtupleFileManager->SetNtupleMerging(fMergeNtuples, fNofNtupleFiles); 121 fNtupleFileManager->SetNtupleRowWise(fNtupleRowWise, fNtupleRowMode); 122 fNtupleFileManager->SetBasketSize(fBasketSize); 123 fNtupleFileManager->SetBasketEntries(fBasketEntries); 124 } 125 else if ( fIsNtupleMergingSet && fMergeNtuples ) { 126 Warn("Ntuple merging is not available with " + fileType + " output.\n" + 127 "Setting is ignored.", 128 fkClass, "CreateNtupleFileManager"); 129 } 130 } 131 132 Message(kVL3, "create", "ntuple file manager", fileType); 133 } 134 135 // 136 // protected methods 137 // 138 139 //_____________________________________________________________________________ 140 G4bool G4GenericAnalysisManager::OpenFileImpl(const G4String& fileName) 141 { 142 Message(kVL4, "open", "file", fileName); 143 144 // Add file name extension, if missing 145 auto fullFileName = fileName; 146 if (GetExtension(fileName).size() == 0u) { 147 auto defaultFileType = fFileManager->GetDefaultFileType(); 148 // G4cout << "File type is not defined, using default: " << defaultFileType << G4endl; 149 if (defaultFileType.size() == 0u) { 150 G4Exception("G4GenericAnalysisManager::OpenFileImpl", "Analysis_F001", 151 FatalException, 152 G4String("Cannot open file \"" + fileName + "\".\n" 153 "Please, use a file name with an extension or define the default file type\n" 154 "via G4AnalysisManager::SetDefaultFileType()")); 155 } 156 157 fullFileName = fileName + "." + fFileManager->GetDefaultFileType(); 158 } 159 160 // Create ntuple file manager if there are booked ntuples 161 if (! fNtupleFileManager) { 162 CreateNtupleFileManager(fullFileName); 163 } 164 165 auto result = true; 166 if (fNtupleFileManager) { 167 result &= G4ToolsAnalysisManager::OpenFileImpl(fullFileName); 168 } 169 else { 170 // no ntuples (check if this mode is supported) 171 result &= fFileManager->OpenFile(fullFileName); 172 } 173 174 Message(kVL3, "open", "file", fileName, result); 175 176 return result; 177 } 178 179 //_____________________________________________________________________________ 180 void G4GenericAnalysisManager::SetDefaultFileTypeImpl(const G4String& value) 181 { 182 G4VAnalysisManager::SetDefaultFileTypeImpl(value); 183 184 fFileManager->SetDefaultFileType(value); 185 } 186 187 //_____________________________________________________________________________ 188 G4bool G4GenericAnalysisManager::WriteH1(G4int id, const G4String& fileName) 189 { 190 // Experimental extra write 191 192 // Do not write histo on worker (redundant and fails in hdf5 ) 193 // If default file is not used, users have to call Merge from their code 194 if ( G4Threading::IsWorkerThread() ) return false; 195 196 auto h1d = GetH1(id, false); 197 if (h1d == nullptr) { 198 WriteHnWarning("H1", id, fkClass, "WriteH1"); 199 return false; 200 } 201 202 auto h1Name = GetH1Name(id); 203 return fFileManager->WriteTExtra<tools::histo::h1d>(fileName, h1d, h1Name); 204 } 205 206 //_____________________________________________________________________________ 207 G4bool G4GenericAnalysisManager::WriteH2(G4int id, const G4String& fileName) 208 { 209 // Experimental extra write 210 211 // Do not write histo on worker (redundant and fails in hdf5 ) 212 // If default file is not used, users have to call Merge from their code 213 if ( G4Threading::IsWorkerThread() ) return false; 214 215 auto h2d = GetH2(id, false); 216 if (h2d == nullptr) { 217 WriteHnWarning("H2", id, fkClass, "WriteH2"); 218 return false; 219 } 220 221 auto h2Name = GetH2Name(id); 222 return fFileManager->WriteTExtra<tools::histo::h2d>(fileName, h2d, h2Name); 223 } 224 //_____________________________________________________________________________ 225 G4bool G4GenericAnalysisManager::WriteH3(G4int id, const G4String& fileName) 226 { 227 // Experimental extra write 228 229 // Do not write histo on worker (redundant and fails in hdf5 ) 230 // If default file is not used, users have to call Merge from their code 231 if ( G4Threading::IsWorkerThread() ) return false; 232 233 auto h3d = GetH3(id, false); 234 if (h3d == nullptr) { 235 WriteHnWarning("H3", id, fkClass, "WriteH3"); 236 return false; 237 } 238 239 auto h3Name = GetH3Name(id); 240 return fFileManager->WriteTExtra<tools::histo::h3d>(fileName, h3d, h3Name); 241 } 242 243 //_____________________________________________________________________________ 244 G4bool G4GenericAnalysisManager::WriteP1(G4int id, const G4String& fileName) 245 { 246 // Experimental extra write 247 248 // Do not write histo on worker (redundant and fails in hdf5 ) 249 // If default file is not used, users have to call Merge from their code 250 if ( G4Threading::IsWorkerThread() ) return false; 251 252 auto p1d = GetP1(id, false); 253 if (p1d == nullptr) { 254 WriteHnWarning("P1", id, fkClass, "WriteP1"); 255 return false; 256 } 257 258 auto p1Name = GetP1Name(id); 259 return fFileManager->WriteTExtra<tools::histo::p1d>(fileName, p1d, p1Name); 260 } 261 262 //_____________________________________________________________________________ 263 G4bool G4GenericAnalysisManager::WriteP2(G4int id, const G4String& fileName) 264 { 265 // Experimental extra write 266 267 // Do not write histo on worker (redundant and fails in hdf5 ) 268 // If default file is not used, users have to call Merge from their code 269 if ( G4Threading::IsWorkerThread() ) return false; 270 271 auto p2d = GetP2(id, false); 272 if (p2d == nullptr) { 273 WriteHnWarning("P2", id, fkClass, "WriteP2"); 274 return false; 275 } 276 277 auto p2Name = GetP2Name(id); 278 return fFileManager->WriteTExtra<tools::histo::p2d>(fileName, p2d, p2Name); 279 } 280 281 //_____________________________________________________________________________ 282 tools::ntuple_booking* G4GenericAnalysisManager::GetNtuple( 283 G4bool warn, G4bool onlyIfActive) const 284 { 285 return fNtupleBookingManager->GetNtuple(warn, onlyIfActive); 286 } 287 288 //_____________________________________________________________________________ 289 tools::ntuple_booking* G4GenericAnalysisManager::GetNtuple( 290 G4int ntupleId, G4bool warn, G4bool onlyIfActive) const 291 { 292 return fNtupleBookingManager->GetNtuple(ntupleId, warn, onlyIfActive); 293 } 294