Skip to content

Commit

Permalink
Modernise some code in examples/common (#288)
Browse files Browse the repository at this point in the history
Here is a set of three commits cherry-picked from my async branch. They
modernise the ParticleGun and the HepMC3Ascii classes, which I was able
to integrate into the async work now.
  • Loading branch information
hageboeck authored Apr 24, 2024
1 parent 4e0361a commit 64b9618
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 213 deletions.
24 changes: 14 additions & 10 deletions examples/common/include/HepMC3G4AsciiReader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#ifndef HEPMC3_G4_ASCII_READER_H
#define HEPMC3_G4_ASCII_READER_H

#include <HepMC3G4Interface.hh>
#include <HepMC3/ReaderAscii.h>
#include <HepMC3/Print.h>
#include "HepMC3G4Interface.hh"
#include "HepMC3/Print.h"

#include <climits>
#include <memory>

class HepMC3G4AsciiReaderMessenger;

Expand All @@ -23,12 +23,10 @@ protected:
G4String fFilename;
int fFirstEventNumber = 0;
int fMaxNumberOfEvents = INT_MAX;
HepMC3::ReaderAscii *fAsciiInput;
std::vector<HepMC3::GenEvent> fEvents;

static std::vector<HepMC3::GenEvent *> *fEvents;

G4int fVerbose;
HepMC3G4AsciiReaderMessenger *fMessenger;
G4int fVerbose = 0;
std::unique_ptr<HepMC3G4AsciiReaderMessenger> fMessenger;

virtual HepMC3::GenEvent *GenerateHepMCEvent(int eventId);

Expand All @@ -49,8 +47,14 @@ public:
void SetFirstEventNumber(G4int i);
G4int GetFirstEventNumber() const;

// methods...
void Initialize();
void Initialize()
{
fEvents.clear();
GenerateHepMCEvent(0);
}

private:
void Read();
};

// ====================================================================
Expand Down
14 changes: 8 additions & 6 deletions examples/common/include/HepMC3G4AsciiReaderMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

//
/// \file eventgenerator/HepMC3/HepMCEx01/include/HepMC3G4AsciiReaderMessenger.hh
/// \file HepMC3G4AsciiReaderMessenger.hh
/// \brief Definition of the HepMC3G4AsciiReaderMessenger class
//
//
Expand All @@ -12,6 +12,8 @@

#include "G4UImessenger.hh"

#include <memory>

class HepMC3G4AsciiReader;
class G4UIdirectory;
class G4UIcmdWithoutParameter;
Expand All @@ -29,11 +31,11 @@ public:
private:
HepMC3G4AsciiReader *gen;

G4UIdirectory *fDir;
G4UIcmdWithAnInteger *fVerbose;
G4UIcmdWithAnInteger *fMaxevent;
G4UIcmdWithAnInteger *fFirstevent;
G4UIcmdWithAString *fOpen;
std::unique_ptr<G4UIdirectory> fDir;
std::unique_ptr<G4UIcmdWithAnInteger> fVerbose;
std::unique_ptr<G4UIcmdWithAnInteger> fMaxevent;
std::unique_ptr<G4UIcmdWithAnInteger> fFirstevent;
std::unique_ptr<G4UIcmdWithAString> fOpen;
};

#endif
25 changes: 7 additions & 18 deletions examples/common/include/HepMC3G4Interface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
#define HEPMC3_G4_INTERFACE_H

#include "G4VPrimaryGenerator.hh"
#include <HepMC3/GenEvent.h>
#include <HepMC3/GenVertex.h>
#include <HepMC3/GenParticle.h>

#include <memory>

namespace HepMC3 {
class GenEvent;
}

/// A base class for primary generation via HepMC object.
/// This class is derived from G4VPrimaryGenerator.

class HepMC3G4Interface : public G4VPrimaryGenerator {
protected:
// Note that the life of HepMC event object must be handled by users.
// In the default implementation, a current HepMC event will be
// deleted at GeneratePrimaryVertex() in the next event.
HepMC3::GenEvent *fHepmcEvent; // (care for single event case only)

// We have to take care for the position of primaries because
// primary vertices outside the world voulme give rise to G4Execption.
// If the default implementation is not adequate, an alternative
Expand All @@ -38,21 +36,12 @@ protected:
virtual HepMC3::GenEvent *GenerateHepMCEvent(int) = 0;

public:
HepMC3G4Interface();
HepMC3G4Interface() = default;
virtual ~HepMC3G4Interface();

// set/get methods
HepMC3::GenEvent *GetHepMCGenEvent() const;

// The default behavior is that a single HepMC event generated by
// GenerateHepMCEvent() will be converted to G4Event through HepMC2G4().
virtual void GeneratePrimaryVertex(G4Event *anEvent);
};

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
inline HepMC3::GenEvent *HepMC3G4Interface::GetHepMCGenEvent() const
{
return fHepmcEvent;
}

#endif
31 changes: 17 additions & 14 deletions examples/common/include/ParticleGun.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

#include "G4ParticleGun.hh"
#include "G4VPrimaryGenerator.hh"
#include "G4Event.hh"

#include <memory>
#include <vector>

class ParticleGunMessenger;
class G4Event;

class ParticleGun : public G4ParticleGun {
public:
Expand All @@ -35,23 +38,23 @@ public:
void ReWeight();

private:
G4double fPrintGun;
G4double fPrintGun = 0.;
// Gun randomization
bool fRandomizeGun;
std::vector<G4ParticleDefinition *> *fParticleList;
std::vector<float> *fParticleWeights;
std::vector<float> *fParticleEnergies;
bool fInitializationDone;
G4double fMinPhi;
G4double fMaxPhi;
G4double fMinTheta;
G4double fMaxTheta;
bool fRandomizeGun = false;
std::vector<G4ParticleDefinition *> fParticleList;
std::vector<float> fParticleWeights;
std::vector<float> fParticleEnergies;
bool fInitializationDone = false;
G4double fMinPhi = 0.;
G4double fMaxPhi = 0.;
G4double fMinTheta = 0.;
G4double fMaxTheta = 0.;

// HepMC3 reader
G4VPrimaryGenerator *fHepmcAscii;
G4bool fUseHepMC;
std::unique_ptr<G4VPrimaryGenerator> fHepmcAscii;
G4bool fUseHepMC = false;

ParticleGunMessenger *fMessenger;
std::unique_ptr<ParticleGunMessenger> fMessenger;
};

#endif /* PARTICLEGUN_HH */
26 changes: 14 additions & 12 deletions examples/common/include/ParticleGunMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "G4UImessenger.hh"
#include "globals.hh"

#include <memory>

class ParticleGun;
class G4UIdirectory;
class G4UIcmdWithoutParameter;
Expand All @@ -32,18 +34,18 @@ public:
virtual void SetNewValue(G4UIcommand *, G4String);

private:
ParticleGun *fGun;

G4UIdirectory *fDir;
G4UIcmdWithoutParameter *fHepmcCmd;
G4UIcmdWithoutParameter *fDefaultCmd;
G4UIcmdWithABool *fPrintCmd;
G4UIcmdWithABool *fRandomizeGunCmd;
G4UIcmdWithAString *fAddParticleCmd;
G4UIcmdWithADoubleAndUnit *fMinPhiCmd;
G4UIcmdWithADoubleAndUnit *fMaxPhiCmd;
G4UIcmdWithADoubleAndUnit *fMinThetaCmd;
G4UIcmdWithADoubleAndUnit *fMaxThetaCmd;
ParticleGun *fGun = nullptr;

std::unique_ptr<G4UIdirectory> fDir;
std::unique_ptr<G4UIcmdWithoutParameter> fHepmcCmd;
std::unique_ptr<G4UIcmdWithoutParameter> fDefaultCmd;
std::unique_ptr<G4UIcmdWithABool> fPrintCmd;
std::unique_ptr<G4UIcmdWithABool> fRandomizeGunCmd;
std::unique_ptr<G4UIcmdWithAString> fAddParticleCmd;
std::unique_ptr<G4UIcmdWithADoubleAndUnit> fMinPhiCmd;
std::unique_ptr<G4UIcmdWithADoubleAndUnit> fMaxPhiCmd;
std::unique_ptr<G4UIcmdWithADoubleAndUnit> fMinThetaCmd;
std::unique_ptr<G4UIcmdWithADoubleAndUnit> fMaxThetaCmd;
};

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Expand Down
80 changes: 34 additions & 46 deletions examples/common/src/HepMC3G4AsciiReader.cc
Original file line number Diff line number Diff line change
@@ -1,70 +1,58 @@
// SPDX-FileCopyrightText: 2022 CERN
// SPDX-FileCopyrightText: 2024 CERN
// SPDX-License-Identifier: Apache-2.0

//
/// \file eventgenerator/HepMC3/HepMCEx01/src/HepMC3G4AsciiReader.cc
/// \brief Implementation of the HepMC3G4AsciiReader class
//
//

#include "HepMC3G4AsciiReader.hh"
#include "HepMC3G4AsciiReaderMessenger.hh"
#include "G4EventManager.hh"
#include "G4Event.hh"

#include <iostream>
#include <fstream>
#include "HepMC3/ReaderAscii.h"

std::vector<HepMC3::GenEvent *> *HepMC3G4AsciiReader::fEvents = nullptr;
#include <iostream>

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
HepMC3G4AsciiReader::HepMC3G4AsciiReader() : fFilename("xxx.dat"), fVerbose(0)
{
fMessenger = new HepMC3G4AsciiReaderMessenger(this);
}
HepMC3G4AsciiReader::HepMC3G4AsciiReader() : fMessenger{new HepMC3G4AsciiReaderMessenger(this)} {}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
HepMC3G4AsciiReader::~HepMC3G4AsciiReader()
{
for (auto evt : *fEvents)
delete evt;

delete fEvents;
delete fAsciiInput;
delete fMessenger;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void HepMC3G4AsciiReader::Initialize()
{
fAsciiInput = new HepMC3::ReaderAscii(fFilename.c_str());

fEvents = new std::vector<HepMC3::GenEvent *>();
HepMC3::GenEvent *evt = nullptr;

int counter = 0;

if (fFirstEventNumber > 0) fAsciiInput->skip(fFirstEventNumber);

// read file
while (!fAsciiInput->failed() && counter < fMaxNumberOfEvents) {
evt = new HepMC3::GenEvent();
fAsciiInput->read_event(*evt);

if (fAsciiInput->failed()) break;

fEvents->push_back(evt);
counter++;
}
G4cout << "Read " << fFilename << " file with " << fEvents->size() << " events." << G4endl;
}
HepMC3G4AsciiReader::~HepMC3G4AsciiReader() {}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
HepMC3::GenEvent *HepMC3G4AsciiReader::GenerateHepMCEvent(int eventId)
{
HepMC3::GenEvent *evt = (*fEvents)[(eventId % fEvents->size())];
if (eventId >= fMaxNumberOfEvents) return nullptr;
if (fEvents.empty()) Read();
if (eventId >= static_cast<int>(fEvents.size())) return nullptr;

HepMC3::GenEvent *evt = &fEvents[eventId];
if (fVerbose > 0) HepMC3::Print::content(*evt);

return evt;
}

void HepMC3G4AsciiReader::Read()
{
fEvents.clear();
HepMC3::ReaderAscii asciiInput{fFilename.c_str()};
if (asciiInput.failed()) throw std::runtime_error{"Failed to read from " + fFilename};

if (fMaxNumberOfEvents != INT_MAX) fEvents.reserve(fMaxNumberOfEvents);
// The function seems to use a weird convention. skip(0) skips one event.
if (fFirstEventNumber > 0) asciiInput.skip(fFirstEventNumber - 1);

for (int i = 0; i < fMaxNumberOfEvents; ++i) {
fEvents.emplace_back();
auto &event = fEvents.back();
asciiInput.read_event(event);

if (asciiInput.failed()) {
if (fMaxNumberOfEvents != INT_MAX) {
std::cerr << __FILE__ << ':' << __LINE__ << " Read " << i << " events whereas " << fMaxNumberOfEvents
<< " were requested.\n";
}
break;
}
}
}
Loading

0 comments on commit 64b9618

Please sign in to comment.