///////////////////////////////////////////////////////////////////////// // G4Tutorial: // // AnalysisManager.cc // // Creating and maintaining analysis objects to store information // retrieved from the simulation (here: energy deposition) // ///////////////////////////////////////////////////////////////////////// #include "AnalysisManager.hh" AnalysisManager* AnalysisManager::instance = 0; AnalysisManager* AnalysisManager::Instance(G4String fileNameBase, G4int bins, G4double lowerBound, G4double upperBound) { // A new instance of AnalysisManager is created if it does not exist: if (instance == 0) { instance = new AnalysisManager(fileNameBase, bins, lowerBound, upperBound); } // The instance of AnalysisManager is returned: return instance; } void AnalysisManager::Destroy() { // The AnalysisManager instance is deleted if it exists: if (instance != 0) { delete instance; instance = 0; } } AnalysisManager::AnalysisManager(G4String fileNameBase, G4int bins, G4double lowerBound, G4double upperBound) : analysisFactory(0), tree(0), histogramFactory(0), energyDeposit(0), numbBins(bins), lowerBoundary(lowerBound), upperBoundary(upperBound) { // The file extension is added to the file name: // // G4String fileName = fileNameBase + ".xml"; G4String fileName = fileNameBase + ".hbook"; // The analysis factory is created analysisFactory = AIDA_createAnalysisFactory(); // The analysis factory is used to create a tree factory: AIDA::ITreeFactory* treeFactory = analysisFactory -> createTreeFactory(); // Using the tree factory, a tree is created to which a store is assigned: tree = treeFactory -> create(fileName, // file name //"XML", // file format: XML "hbook", // file format: HBOOK false, // file is not read-only true, // create new file "uncompressed"); // no compression is applied // The tree factory can be deleted after the tree was created: delete treeFactory; // The analysis factory is used to create a histogram factory (where the // the tree must be passed as argument): histogramFactory = analysisFactory -> createHistogramFactory(*tree); } AnalysisManager::~AnalysisManager() { // The data is written to the file and the tree is closed: tree -> commit(); tree -> close(); // The instances of histogram factory, tree and analysis factory are deleted: delete histogramFactory; delete tree; delete analysisFactory; // NOTE: The instance of the histogram is deleted by the tree and MUST NOT // be deleted by the user. } void AnalysisManager::SetBinning(G4int bins, G4double lowerBound, G4double upperBound) { // In our example, the binning of the histogram can only be set by the user // as long as the ScoreEnergyDeposit was not yet called (since the histogram // is created during the first call of ScoreEnergyDeposit): if (energyDeposit != 0) { G4cout << "WARNING: Histogram binning cannot be changed: " << "Histogram already created." << G4endl; return; } // The new number of bins is only accepted if it is >0 if (bins > 0) { numbBins = bins; } else { G4cerr << "ERROR: Histogram binning: Number of bins is < 0." << G4endl; } // The new boundaries are only accepted if the lower boundary is < than // the upper boundary if (lowerBound < upperBound) { lowerBoundary = lowerBound; upperBoundary = upperBound; } else { G4cerr << "ERROR: Histogram binning: lower boundary >= upper boundary." << G4endl; } } void AnalysisManager::ScoreEnergyDeposit(G4double zPosition, G4double energy) { // If the histogram was not yet instantiated (since this function is // called the first time), it is created now: if (energyDeposit == 0) { // The histogram factory is used to create the histogram (for storing the // energy deposition as a function of depth): // Booking for XML histograms //energyDeposit = histogramFactory -> createHistogram1D("energydeposit", // numbBins, // lowerBoundary, // upperBoundary); // Booking for HBOOK histograms energyDeposit = histogramFactory -> createHistogram1D("1", "energy deposit", numbBins, lowerBoundary, upperBoundary); } // The energy deposition is filled into the histogram energyDeposit -> fill(zPosition, energy); }