Histogram not filling

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.

Are you writing your histogram to your file at the end of your run?

@Jailbone
Hi
Easiest way for you to understand;

In the RunAction constructor; define:

G4int nbins = 100;
G4double hitmin = -300;
G4double hitmax = 300;
G4AnalysisManager* analysis = G4AnalysisManager::Instance();
analysis->CreateH1("Detected photons", "Jailbone", nbins, hitmin, hitmax);

In the BeginOfRunAction; define

G4AnalysisManager* analysis = G4AnalysisManager::Instance();
analysis->OpenFile("Jailbone.root");

In the EndOfRunAction; define

  G4AnalysisManager* analysis = G4AnalysisManager::Instance();
  analysis->Write();
  analysis->CloseFile();

Do not forget to add

#include "G4RootAnalysisManager.hh"
using G4AnalysisManager = G4RootAnalysisManager;

Cheers
VRS

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.

In the constructor:

{
	G4int nbins = 100;
	G4double hitmin = 0;
	G4double hitmax = 100000;
	G4AnalysisManager* analysis = G4AnalysisManager::Instance();
	analysis->CreateH1("Detected photons", "Jailbone", nbins, hitmin, hitmax);
}

In BeginOfRunAction:

{
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.

Hello,

The message

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.

Best regards,

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?

You can switch off resetting histograms in CloseFile and call Reset function in BeginOfRun:

{
auto man = G4AnalysisManager::Instance();
man->Reset();
man->OpenFile("test.root");
}

EndOfRun:

  auto man = G4AnalysisManager::Instance();
  man->Write();
  man->CloseFile(false);

This makes also histograms available for visualisation, that is demonstrated in basic example B5.

Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.