Plotting / Creating a plot takes ages

Hi,
I got another but new issue: When I’m setting up a plot via plot.C it takes ages to do so. For around 350,000 particles it would probably need around 10-15 minutes. I don’t know why or what I did but a while ago it popped up in a second or so. I tried to narrow it down but nothing changed.
From my feeling, I would say something in plot.C crawls through a certain ROOT structure which is not useful but I don’t see it. First, I thought, I’m storing too much in detector.cc but I checked, I didn’t do changes there. In run.cc I also can’t figure out changes. This doesn’t mean that there can’t be something wrong but at least I checked it several times and didn’t find anything.
When I’m opening the .root file via the TBrowser it opens up immediately. All I’m aware of is that I’m having this kind of issue atm: Run crashed - WARNING - Attempt to delete the physical volume store while geometry closed! - #7 by BenjaminW but I don’t know whether this could be a cause at all?

I’m glad for any help, even if it is only something to narrow down the cause.
Thanks a lot in advance!

Here is the code:

//detector.cc
#include “detector.hh”

MySensitiveDetector::MySensitiveDetector(G4String name) : G4VSensitiveDetector(name)
{}

MySensitiveDetector::~MySensitiveDetector()
{}

G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)
{
G4Track *track = aStep->GetTrack();
track->SetTrackStatus(fStopAndKill);

G4StepPoint *preStepPoint = aStep->GetPreStepPoint();
G4ThreeVector posPhoton = preStepPoint->GetPosition();

const G4VTouchable *touchable = aStep->GetPreStepPoint()->GetTouchable();

G4VPhysicalVolume *physVol = touchable->GetVolume();
G4ThreeVector posDetector = physVol->GetTranslation();

G4int evt = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
G4double partEdep = aStep->GetTotalEnergyDeposit();
G4double energy = preStepPoint->GetTotalEnergy();
if(evt % 100 == 0)
{
G4cout << "Current particle: " << evt*100 << G4endl;
}

//Convert from MeV to keV
partEdep *= 1000;
energy *= 1000;

// Create histograms
G4AnalysisManager *man = G4AnalysisManager::Instance();
man->FillNtupleIColumn(0, evt);
man->FillNtupleDColumn(1, partEdep);
man->FillNtupleDColumn(2, energy);
man->FillNtupleDColumn(3, posDetector[0]);
man->FillNtupleDColumn(4, posDetector[1]);
man->FillNtupleDColumn(5, posDetector[2]);
man->AddNtupleRow(0);

//man->FillH1(0, energy2);

return true;
}

//run.cc
#include “run.hh”
#include

MyRunAction::MyRunAction()
{
G4AnalysisManager *man = G4AnalysisManager::Instance();

man->CreateNtuple(“Hits”, “Hits”);
man->CreateNtupleIColumn(“fEvent”);
man->CreateNtupleDColumn(“fdepEnergy”);
man->CreateNtupleDColumn(“ftotEnergy”);
man->CreateNtupleDColumn(“fX”);
man->CreateNtupleDColumn(“fY”);
man->CreateNtupleDColumn(“fZ”);
man->FinishNtuple(0);

//man->SetHistoDirectoryName(“histograms”);
//man->SetVerboseLevel(3);
//man->SetFirstHistoId(1);
//man->CreateHistogram(“Hist”);
//man->CreateH1(“0”,“energy_spectrum”,100, 0, 150*keV, “keV”);

}

MyRunAction::~MyRunAction()
{

}

void MyRunAction::BeginOfRunAction(const G4Run* run)
{
G4AnalysisManager *man = G4AnalysisManager::Instance();

G4int runID = run->GetRunID();
std::stringstream strRunID;
strRunID << runID;

man->OpenFile(“output”+strRunID.str()+“.root”);

}

void MyRunAction::EndOfRunAction(const G4Run*)
{
G4AnalysisManager *man = G4AnalysisManager::Instance();
man->Write();
// man->CloseFile(“output.root”);
man->CloseFile();
}

//plot.C

{
cout << “Loading plot…” << endl;
std::unique_ptr myFile( TFile::Open(“output0.root”));
auto tree = myFile->Get(“Hits”);

Double_t partEdep;
Double_t energy;
tree->SetBranchAddress(“ftotEnergy”, &energy);

auto htotE = new TH1D(“HtotEnergy”, “Histo_totE”, 150, 0., 150.);

  cout << "Loading hits.." << tree->GetEntriesFast() << endl;

for (int iEntry = 0; iEntry < tree->GetEntriesFast(); ++iEntry)
{
// Load the data for the given tree entry
tree->GetEntry(iEntry);

  HtotEnergy->Fill(energy);
  if(iEntry == tree->GetEntriesFast()/2){cout << "Half way through.. hold on" << endl;}

}

// Create a canvas
cout << “Creating plot…” << endl;
TCanvas* c1 = new TCanvas(“c1”, “”, 20, 20, 1000, 500);

c1->cd(1);
HtotEnergy->Draw(“HIST”);
}

Hello,

Can you try to save tree->GetEntriesFast() value in a variable before the loop, eg.

int nofEntries =  tree->GetEntriesFast();
for (int iEntry = 0; iEntry < nofEntries; ++iEntry)
{
  HtotEnergy->Fill(energy);
  if(iEntry == nofEntries/2){cout << "Half way through.. hold on" << endl;}
}

Otherwise I see no other redundant call in your macro that you could avoid.

Best regards,

1 Like