#include "ProjetAnalysisManager.hh" #include "G4AutoLock.hh" #include "TError.h" #include "G4Version.hh" #include "G4SystemOfUnits.hh" ProjetAnalysisManager* ProjetAnalysisManager::instance = 0; namespace { //Mutex to acquire access to singleton instance check/creation G4Mutex instanceMutex = G4MUTEX_INITIALIZER; //Mutex to acquire accss to histograms creation/access //It is also used to control all operations related to histos //File writing and check analysis G4Mutex dataManipulationMutex = G4MUTEX_INITIALIZER; } ProjetAnalysisManager::ProjetAnalysisManager() : fFile(0),fTree(0), fhAthos(0), fhAramis(0), fhAthosAndAramis(0), fhAthosOrAramis(0), fhAthosVsAramis(0) {;} //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... ProjetAnalysisManager::~ProjetAnalysisManager() { //No need to mutex, this is a real singleton. //loop over all histograms if (fTree) delete fTree; if (fFile) delete fFile; if(fhAthos) delete fhAthos; if(fhAramis) delete fhAramis; if(fhAthosAndAramis) delete fhAthosAndAramis; if(fhAthosOrAramis) delete fhAthosOrAramis; if(fhAthosVsAramis) delete fhAthosVsAramis; } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... ProjetAnalysisManager* ProjetAnalysisManager::getInstance() { G4AutoLock l(&instanceMutex); if (instance == 0) instance = new ProjetAnalysisManager(); return instance; } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... void ProjetAnalysisManager::Book(const G4Run* run) { //Booking of histograms has to be protected. //In addition there are issues with ROOT that is //heavily non thread-safe. In particular I/O related operations //are not thread safe. To avoid problems let's mutex everything //here G4AutoLock l(&dataManipulationMutex); if (!fFile) { //Use the version name for the ROOT file // Open an output file // G4String fileName = "test_Projet_run_"; G4int numero=run->GetRunID(); if(numero <= 8) fileName += "0"; std::ostringstream num; num << fileName << numero+1; G4cout << "Creation of " << fileName << G4endl; // analysisManager->OpenFile(fileName+num.str()); fFile = new TFile(fileName,"RECREATE"); } if(!fhAthos) fhAthos = new TH1D("hAthos","E dep in Athos",8192,0.,2600.*keV); if(!fhAramis) fhAramis = new TH1D("hAramis","E dep in Aramis",8192,0.,2600.*keV); if(!fhAthosOrAramis) fhAthosOrAramis = new TH1D("hAthosOrAramis","E dep in Athos or Aramis",8192,0.,2600.*keV); if(!fhAthosAndAramis) fhAthosAndAramis = new TH1D("hAthosAndAramis","E dep in Athos and Aramis",8192,0.,2600.*keV); if(!fhAthosVsAramis) fhAthosVsAramis = new TH2D("fhAthosVsAramis","E dep in Athos vs.Aramis",1024,0.,2600.*keV,1024,0,2600.*keV); if (!fTree) { fTree = new TTree("t","t"); fTree->Branch("E_Athos",&fAthosEdep,"E_Athos/D"); fTree->Branch("E_Aramis",&fAramisEdep,"E_Aramis/D"); } return; } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... void ProjetAnalysisManager::AddEvent(const G4double &AthosEner, const G4double &AramisEner) { G4AutoLock l(&dataManipulationMutex); //Here, it crashes. Comment all the following code to avoid the crash if ((AthosEner > 0.)){ fhAthos->Fill(AthosEner); fhAthosOrAramis->Fill(AthosEner); } if ((AramisEner > 0.)){ fhAramis->Fill(AramisEner); fhAthosOrAramis->Fill(AramisEner); } if ((AthosEner > 0.) && (AramisEner > 0.)) { fhAthosAndAramis->Fill(AthosEner); fhAthosAndAramis->Fill(AramisEner); fhAthosVsAramis->Fill(AthosEner,AramisEner); } fTree->Fill(); } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... void ProjetAnalysisManager::CloseFile() { G4AutoLock l(&dataManipulationMutex); if (!fFile) //file not created at all: e.g. for a vis-only execution return; if (!fFile->IsOpen()) { G4Exception("ProjetAnalysisManager::CloseFile()","tst67_02",FatalException, "Trying to close a ROOT file which is not open"); return; } fFile->cd(); if (fTree) fTree->Write(fTree->GetName()); if(fhAthos) fhAthos->Write(); if(fhAramis) fhAramis->Write(); if(fhAthosOrAramis) fhAthosOrAramis->Write(); if(fhAthosAndAramis) fhAthosAndAramis->Write(); if(fhAthosVsAramis) fhAthosVsAramis->Write(); fFile->Close(); }