I am building a simulation and using example OpNovice2 as guidance.
I want to store optical photon hits in a sensitive detector in a histogram on a per event basis.
Therefore through a HistogramManager class (like in the example) I am creating an appropriate Histogram.
But when I want to fill it in the EndOfEvent method of my sensitive detector it won’t work.
Here is the relevant code:
void MySensitiveDetector::EndOfEvent(G4HCofThisEvent* hce){
G4int nofHits = hitsCollection->entries();
G4cout << G4endl
<< "-------->Hits Collection: in this event there are " << nofHits
<< " photon hits in the PMT: " << G4endl;
G4AnalysisManager* man = G4AnalysisManager::Instance();
G4int id = man->GetH1Id("Detected photons");
man->FillH1(id, noHits);
G4cout << id << G4endl;
}
It prints the correct Histogram id (and also the name if I want to), so it ought to be there and should be initialized correctly. Because also, in the end I get my desired root file with the Histogram (as defined in my HistoManager class) but it has no entries. I can’t see why this is the case.
Ok I think it almost works now.
I followed @drvijayraj 's advice leaving out my HistoManager class for now and doing the histogram creation directly in the RunAction class.
{
G4AnalysisManager* man = G4AnalysisManager::Instance();
man->OpenFile("test.root");
}
In EndOfRunAction
{
G4AnalysisManager* man = G4AnalysisManager::Instance();
man->Write();
man->CloseFile();
}
So, everything as suggested above. The filling still happening in the sensitive detector class as described.
It is ouputting a root file with the histogram correctly filled. Strangely, in the terminal output I get the message
There is 1 h1 histogram
0 with 0 entries: Jailbone
which bothers me.
If I make the minor change in the EndOfRunAction:
G4AnalysisManager* man = G4AnalysisManager::Instance();
if(man->IsActive())
{
man->Write();
man->CloseFile();
}
I get the correct terminal output:
There is 1 h1 histogram
0 with 1 entries: Jailbone
But now the histogram is not present in the root file and I get an error message when trying to open it:
root [1] Error in <TFile::ReadBuffer>: error reading all requested bytes from file test.root, got 160 of 300
Error in <TFile::Init>: test.root failed to read the file type data.
There is 1 h1 histogram
0 with 0 entries: Jailbone
is issued by visualisation system at the end of run, after your EndOfRunAction was executed, and so the file was written and closed. By default, when a file is closed, all histograms are reset. That’s why there is 0 entries printed.
(This message is going to be improved in the next Geant4 version.)
The test
if(man->IsActive()) {
...
makes a good sense only if you activate the Activation feature, by SetActivation() call, what is not your use case. The analysis manager returns false in this case, and the calls to Write() and CloseFile() functions are not executed, what causes a problem when browsing with ROOT.
Could you rerun your application after removing the IsActive test and then open your histogram with ROOT browser, to see whether it has entries? As you see the printing after your FillH1 call, your histogram should not be empty.
Hi @ivana, yes without the if statement the histogram is filled, so that is ok. Is there a workaround to also get the right message in the terminal, i.e. the right number of entries and not 0?