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 // $Id: G4CsvAnalysisManager.cc 93815 2015-11-02 11:32:20Z gcosmo $ 26 27 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivan 28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4CsvAnalysisManager.hh" 30 #include "G4CsvAnalysisManager.hh" 30 #include "G4CsvFileManager.hh" 31 #include "G4CsvFileManager.hh" 31 #include "G4CsvNtupleFileManager.hh" << 32 #include "G4CsvNtupleManager.hh" >> 33 #include "G4AnalysisVerbose.hh" 32 #include "G4AnalysisManagerState.hh" 34 #include "G4AnalysisManagerState.hh" 33 #include "G4AnalysisUtilities.hh" << 34 #include "G4UnitsTable.hh" 35 #include "G4UnitsTable.hh" 35 #include "G4ThreadLocalSingleton.hh" << 36 #include "G4Threading.hh" 36 #include "G4Threading.hh" >> 37 #include "G4AutoLock.hh" 37 38 38 using namespace G4Analysis; << 39 #include <iostream> >> 40 >> 41 // mutex in a file scope >> 42 >> 43 namespace { >> 44 //Mutex to lock master manager when merging H1 histograms >> 45 G4Mutex mergeH1Mutex = G4MUTEX_INITIALIZER; >> 46 //Mutex to lock master manager when merging H1 histograms >> 47 G4Mutex mergeH2Mutex = G4MUTEX_INITIALIZER; >> 48 //Mutex to lock master manager when merging H1 histograms >> 49 G4Mutex mergeH3Mutex = G4MUTEX_INITIALIZER; >> 50 //Mutex to lock master manager when merging P1 profiles >> 51 G4Mutex mergeP1Mutex = G4MUTEX_INITIALIZER; >> 52 //Mutex to lock master manager when merging P2 profiles >> 53 G4Mutex mergeP2Mutex = G4MUTEX_INITIALIZER; >> 54 } >> 55 >> 56 G4CsvAnalysisManager* G4CsvAnalysisManager::fgMasterInstance = nullptr; >> 57 G4ThreadLocal G4CsvAnalysisManager* G4CsvAnalysisManager::fgInstance = nullptr; 39 58 40 //____________________________________________ 59 //_____________________________________________________________________________ 41 G4CsvAnalysisManager* G4CsvAnalysisManager::In 60 G4CsvAnalysisManager* G4CsvAnalysisManager::Instance() 42 { 61 { 43 static G4ThreadLocalSingleton<G4CsvAnalysisM << 62 if ( fgInstance == nullptr ) { 44 fgIsInstance = true; << 63 G4bool isMaster = ! G4Threading::IsWorkerThread(); 45 return instance.Instance(); << 64 fgInstance = new G4CsvAnalysisManager(isMaster); >> 65 } >> 66 >> 67 return fgInstance; 46 } 68 } 47 69 48 //____________________________________________ 70 //_____________________________________________________________________________ 49 G4bool G4CsvAnalysisManager::IsInstance() 71 G4bool G4CsvAnalysisManager::IsInstance() 50 { 72 { 51 return fgIsInstance; << 73 return ( fgInstance != 0 ); >> 74 } >> 75 >> 76 //_____________________________________________________________________________ >> 77 G4CsvAnalysisManager::G4CsvAnalysisManager(G4bool isMaster) >> 78 : G4ToolsAnalysisManager("Csv", isMaster), >> 79 fNtupleManager(nullptr), >> 80 fFileManager(nullptr) >> 81 { >> 82 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) { >> 83 G4ExceptionDescription description; >> 84 description << " " >> 85 << "G4CsvAnalysisManager already exists." >> 86 << "Cannot create another instance."; >> 87 G4Exception("G4CsvAnalysisManager::G4CsvAnalysisManager()", >> 88 "Analysis_F001", FatalException, description); >> 89 } >> 90 >> 91 if ( isMaster ) fgMasterInstance = this; >> 92 fgInstance = this; >> 93 fNtupleManager = new G4CsvNtupleManager(fState); >> 94 fFileManager = std::make_shared<G4CsvFileManager>(fState); >> 95 fNtupleManager->SetFileManager(fFileManager); >> 96 // The managers will be deleted by the base class >> 97 >> 98 // Set managers to base class which takes then their ownreship >> 99 SetNtupleManager(fNtupleManager); >> 100 SetFileManager(fFileManager); 52 } 101 } 53 102 54 //____________________________________________ 103 //_____________________________________________________________________________ 55 G4CsvAnalysisManager::G4CsvAnalysisManager() << 104 G4CsvAnalysisManager::~G4CsvAnalysisManager() 56 : G4ToolsAnalysisManager("Csv") << 105 { >> 106 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr; >> 107 fgInstance = nullptr; >> 108 } >> 109 >> 110 // >> 111 // private methods >> 112 // >> 113 >> 114 //_____________________________________________________________________________ >> 115 G4bool G4CsvAnalysisManager::CloseNtupleFiles() 57 { 116 { 58 // File Manager << 117 auto ntupleVector 59 auto fileManager = std::make_shared<G4CsvFil << 118 = fNtupleManager->GetNtupleDescriptionVector(); 60 SetFileManager(fileManager); << 119 >> 120 // Close ntuple files >> 121 for ( auto ntupleDescription : ntupleVector) { >> 122 fFileManager->CloseNtupleFile(ntupleDescription); >> 123 } >> 124 >> 125 return true; >> 126 } >> 127 >> 128 >> 129 // >> 130 // protected methods >> 131 // 61 132 62 // Ntuple file manager << 133 // 63 fNtupleFileManager = std::make_shared<G4CsvN << 134 // private methods 64 SetNtupleFileManager(fNtupleFileManager); << 135 // 65 fNtupleFileManager->SetFileManager(std::move << 136 66 fNtupleFileManager->SetBookingManager(fNtupl << 137 >> 138 //_____________________________________________________________________________ >> 139 G4bool G4CsvAnalysisManager::WriteH1() >> 140 { >> 141 auto h1Vector = fH1Manager->GetH1Vector(); >> 142 auto hnVector = fH1Manager->GetHnVector(); >> 143 >> 144 if ( ! h1Vector.size() ) return true; >> 145 >> 146 auto result = true; >> 147 >> 148 if ( ! G4Threading::IsWorkerThread() ) { >> 149 result = WriteT(h1Vector, hnVector, "h1"); >> 150 } >> 151 else { >> 152 // The worker manager just adds its histograms to the master >> 153 // This operation needs a lock >> 154 G4AutoLock lH1(&mergeH1Mutex); >> 155 fgMasterInstance->fH1Manager->AddH1Vector(h1Vector); >> 156 lH1.unlock(); >> 157 } >> 158 >> 159 return result; 67 } 160 } 68 161 69 //____________________________________________ 162 //_____________________________________________________________________________ 70 G4CsvAnalysisManager::~G4CsvAnalysisManager() << 163 G4bool G4CsvAnalysisManager::WriteH2() >> 164 { >> 165 auto h2Vector = fH2Manager->GetH2Vector(); >> 166 auto hnVector = fH2Manager->GetHnVector(); >> 167 >> 168 if ( ! h2Vector.size() ) return true; >> 169 >> 170 auto result = true; >> 171 >> 172 if ( ! G4Threading::IsWorkerThread() ) { >> 173 result = WriteT(h2Vector, hnVector, "h2"); >> 174 } >> 175 else { >> 176 // The worker manager just adds its histograms to the master >> 177 // This operation needs a lock >> 178 G4AutoLock lH2(&mergeH2Mutex); >> 179 fgMasterInstance->fH2Manager->AddH2Vector(h2Vector); >> 180 lH2.unlock(); >> 181 } >> 182 >> 183 return result; >> 184 } >> 185 >> 186 //_____________________________________________________________________________ >> 187 G4bool G4CsvAnalysisManager::WriteH3() 71 { 188 { 72 fgIsInstance = false; << 189 auto h3Vector = fH3Manager->GetH3Vector(); >> 190 auto hnVector = fH3Manager->GetHnVector(); >> 191 >> 192 if ( ! h3Vector.size() ) return true; >> 193 >> 194 auto result = true; >> 195 >> 196 if ( ! G4Threading::IsWorkerThread() ) { >> 197 result = WriteT(h3Vector, hnVector, "h3"); >> 198 } >> 199 else { >> 200 // The worker manager just adds its histograms to the master >> 201 // This operation needs a lock >> 202 G4AutoLock lH3(&mergeH3Mutex); >> 203 fgMasterInstance->fH3Manager->AddH3Vector(h3Vector); >> 204 lH3.unlock(); >> 205 } >> 206 >> 207 return result; >> 208 } >> 209 >> 210 //_____________________________________________________________________________ >> 211 G4bool G4CsvAnalysisManager::WriteP1() >> 212 { >> 213 auto p1Vector = fP1Manager->GetP1Vector(); >> 214 auto hnVector = fP1Manager->GetHnVector(); >> 215 >> 216 if ( ! p1Vector.size() ) return true; >> 217 >> 218 auto result = true; >> 219 >> 220 if ( ! G4Threading::IsWorkerThread() ) { >> 221 result = WriteT(p1Vector, hnVector, "p1"); >> 222 } >> 223 else { >> 224 // The worker manager just adds its profiles to the master >> 225 // This operation needs a lock >> 226 G4AutoLock lP1(&mergeP1Mutex); >> 227 fgMasterInstance->fP1Manager->AddP1Vector(p1Vector); >> 228 lP1.unlock(); >> 229 } >> 230 >> 231 return result; 73 } 232 } >> 233 >> 234 //_____________________________________________________________________________ >> 235 G4bool G4CsvAnalysisManager::WriteP2() >> 236 { >> 237 auto p2Vector = fP2Manager->GetP2Vector(); >> 238 auto hnVector = fP2Manager->GetHnVector(); >> 239 >> 240 if ( ! p2Vector.size() ) return true; >> 241 >> 242 auto result = true; >> 243 >> 244 if ( ! G4Threading::IsWorkerThread() ) { >> 245 result = WriteT(p2Vector, hnVector, "p2"); >> 246 } >> 247 else { >> 248 // The worker manager just adds its profiles to the master >> 249 // This operation needs a lock >> 250 G4AutoLock lP2(&mergeP2Mutex); >> 251 fgMasterInstance->fP2Manager->AddP2Vector(p2Vector); >> 252 lP2.unlock(); >> 253 } >> 254 >> 255 return result; >> 256 } >> 257 >> 258 //_____________________________________________________________________________ >> 259 G4bool G4CsvAnalysisManager::Reset() >> 260 { >> 261 // Reset histograms and ntuple >> 262 >> 263 auto finalResult = true; >> 264 >> 265 auto result = G4ToolsAnalysisManager::Reset(); >> 266 finalResult = finalResult && result; >> 267 >> 268 result = fNtupleManager->Reset(true); >> 269 finalResult = finalResult && result; >> 270 >> 271 return finalResult; >> 272 } >> 273 >> 274 //_____________________________________________________________________________ >> 275 G4bool G4CsvAnalysisManager::OpenFileImpl(const G4String& fileName) >> 276 { >> 277 auto finalResult = true; >> 278 auto result = fFileManager->SetFileName(fileName); >> 279 finalResult = finalResult && result; >> 280 >> 281 // Only lock file name in file manager >> 282 result = fFileManager->OpenFile(fileName); >> 283 finalResult = finalResult && result; >> 284 >> 285 // Histogram and profile files are created/closed indivudually for each >> 286 // object in WriteHn{Pn] function >> 287 >> 288 // Create ntuples if they are booked >> 289 // (The files will be created with creating ntuples) >> 290 fNtupleManager->CreateNtuplesFromBooking(); >> 291 >> 292 return finalResult; >> 293 } >> 294 >> 295 //_____________________________________________________________________________ >> 296 G4bool G4CsvAnalysisManager::WriteImpl() >> 297 { >> 298 // nothing to be done for Csv file >> 299 auto finalResult = true; >> 300 >> 301 #ifdef G4VERBOSE >> 302 if ( fState.GetVerboseL4() ) >> 303 fState.GetVerboseL4()->Message("write", "files", ""); >> 304 #endif >> 305 >> 306 >> 307 if ( ! fgMasterInstance && >> 308 ( ( ! fH1Manager->IsEmpty() ) || ( ! fH2Manager->IsEmpty() ) || >> 309 ( ! fH3Manager->IsEmpty() ) || ( ! fP1Manager->IsEmpty() ) || >> 310 ( ! fP2Manager->IsEmpty() ) ) ) { >> 311 >> 312 G4ExceptionDescription description; >> 313 description >> 314 << " " << "No master G4CsvAnalysisManager instance exists." >> 315 << G4endl >> 316 << " " << "Histogram data will not be merged."; >> 317 G4Exception("G4CsvAnalysisManager::Write()", >> 318 "Analysis_W031", JustWarning, description); >> 319 } >> 320 >> 321 // H1 >> 322 auto result = WriteH1(); >> 323 finalResult = finalResult && result; >> 324 >> 325 // H2 >> 326 result = WriteH2(); >> 327 finalResult = finalResult && result; >> 328 >> 329 // H3 >> 330 result = WriteH3(); >> 331 finalResult = finalResult && result; >> 332 >> 333 // P1 >> 334 result = WriteP1(); >> 335 finalResult = finalResult && result; >> 336 >> 337 // P2 >> 338 result = WriteP2(); >> 339 finalResult = finalResult && result; >> 340 >> 341 // Ntuples >> 342 // Nothing to be done >> 343 >> 344 // Write ASCII if activated >> 345 // Not available >> 346 //if ( IsAscii() ) { >> 347 // result = WriteAscii(); >> 348 //} >> 349 >> 350 #ifdef G4VERBOSE >> 351 if ( fState.GetVerboseL1() ) >> 352 fState.GetVerboseL1() >> 353 ->Message("write", "files", "", finalResult); >> 354 #endif >> 355 >> 356 return result; >> 357 } >> 358 >> 359 //_____________________________________________________________________________ >> 360 G4bool G4CsvAnalysisManager::CloseFileImpl() >> 361 { >> 362 auto finalResult = true; >> 363 >> 364 // Unlock file name only >> 365 auto result = fFileManager->CloseFile(); >> 366 finalResult = finalResult && result; >> 367 >> 368 // Histogram and profile files are created/closed indivudually for each >> 369 // object in WriteHn{Pn] function >> 370 // In sequential mode or in MT mode only on workers >> 371 result = CloseNtupleFiles(); >> 372 finalResult = finalResult && result; >> 373 >> 374 // reset data >> 375 result = Reset(); >> 376 if ( ! result ) { >> 377 G4ExceptionDescription description; >> 378 description << " " << "Resetting data failed"; >> 379 G4Exception("G4CsvAnalysisManager::CloseFile()", >> 380 "Analysis_W021", JustWarning, description); >> 381 result = false; >> 382 } >> 383 finalResult = finalResult && result; >> 384 >> 385 return finalResult; >> 386 } 74 387