I am hoping to output unique histograms for each run (defined in a macro loop), but I am currently getting only one histogram outputted for each set of runs. I am also outputting ntuples, which are not overwriting each other as they have unique names according to the RunID. I am struggling to achieve this for the histograms as they are defined in the RunAction constructor, so trying to access the RunID causes a segmentation fault. How do I produce unique histograms for each run?
This looks like it works! As a related question, I’ve discovered that only the first geometry in the loop has a histogram being filled, and any subsequent runs result in an empty histogram - is there a command I’m missing to write the histogram for every run subsequent to the first?
Do you redefine the histograms on each new run, after opening the file, either in BeginOfRunAction or somewhere else where you know when a new run starts?
G4AnalysisManager* man = G4AnalysisManager::Instance();
man->Reset();
std::stringstream strRunID;
strRunID << runID;
auto now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
// Convert the time to a string
std::stringstream timeStream;
timeStream << std::put_time(std::localtime(¤tTime), "%d%S");
// Set the histogram name including the run ID
G4String histogramName = "Hits" + timeStream.str();
G4cout << histogramName << G4endl;
// Create the histogram
man->CreateH2(histogramName, "HitsXY",
50, -0.5 * m, 0.5 * m,
50, -0.5 * m, 0.5 * m,
"um",
"um",
"none",
"none",
"linear",
"linear");`
The problem may be that witch each CreateH2 call in BeginOfRunAction, you get a new histogram with new ID, and so you have to use this new ID when you fill your histogram.
Since the last version (11.1), it is possible to write the same objects in a file multiple times. See more details in documentation: