Histogram Unique Names

Hi,

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?

1 Like

Why not define the histograms in the RunAction::BeginOfRunAction()?

I have tried this, but I’m struggling to make it thread-safe as I am running in multithreaded mode. Is there a way to do that?

hope that is thread safe :face_with_raised_eyebrow:

Hi,

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?

you mean besides

    analysisManager->Write();
    analysisManager->CloseFile();

in the EndOfRunAction?

Yeah I already have that in there, but after the first loop each subsequent histogram doesn’t seem to write as expected

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?

Yeah I’m redefining them in BeginOfRunAction. The code for it is below - I added the Reset() to try and fix the issue but it hasn’t helped

`void runAction::BeginOfRunAction(const G4Run* run)
{

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(&currentTime), "%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");`

Hello,

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:

https://geant4-userdoc.web.cern.ch/UsersGuides/ForApplicationDeveloper/html/Analysis/managers.html#files-handling

Best regards,