Skip to content

Commit

Permalink
Merge pull request #19939 from civanch/cleanup_g4_sensitive_detectors
Browse files Browse the repository at this point in the history
Cleanup Geant4 sensitive detectors
  • Loading branch information
cmsbuild authored Aug 11, 2017
2 parents 340d62a + f26db6e commit 56f2f72
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 177 deletions.
7 changes: 4 additions & 3 deletions SimG4CMS/Calo/interface/CaloSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class CaloSD : public SensitiveCaloDetector,
edm::ParameterSet const & p, const SimTrackManager*,
float timeSlice=1., bool ignoreTkID=false);
virtual ~CaloSD();
virtual bool ProcessHits(G4Step * step,G4TouchableHistory * tHistory);
virtual bool ProcessHits(G4GFlashSpot*aSpot,G4TouchableHistory*);
virtual bool ProcessHits(G4Step * step, G4TouchableHistory * tHistory);
virtual bool ProcessHits(G4GFlashSpot* aSpot, G4TouchableHistory*);
virtual double getEnergyDeposit(G4Step* step);
virtual uint32_t setDetUnitId(G4Step* step)=0;

Expand Down Expand Up @@ -116,6 +116,7 @@ class CaloSD : public SensitiveCaloDetector,

CaloHitID currentID, previousID;
G4Track* theTrack;
G4TouchableHistory* theTouchableHistory;

G4StepPoint* preStepPoint;
float edepositEM, edepositHAD;
Expand All @@ -126,7 +127,7 @@ class CaloSD : public SensitiveCaloDetector,

const SimTrackManager* m_trackManager;
CaloG4Hit* currentHit;
// TimerProxy theHitTimer;
// TimerProxy theHitTimer;
bool runInit;

bool corrTOFBeam, suppressHeavy;
Expand Down
86 changes: 40 additions & 46 deletions SimG4CMS/Calo/src/CaloSD.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@
#include "G4SystemOfUnits.hh"
#include "G4PhysicalConstants.hh"

//#define DebugLog
// #define DebugLog

CaloSD::CaloSD(G4String name, const DDCompactView & cpv,
const SensitiveDetectorCatalog & clg,
edm::ParameterSet const & p, const SimTrackManager* manager,
float timeSliceUnit, bool ignoreTkID) :
SensitiveCaloDetector(name, cpv, clg, p),
G4VGFlashSensitiveDetector(), theTrack(0), preStepPoint(0), eminHit(0),
eminHitD(0), m_trackManager(manager), currentHit(0), runInit(false),
timeSlice(timeSliceUnit), ignoreTrackID(ignoreTkID), hcID(-1), theHC(0),
meanResponse(0) {
//Add Hcal Sentitive Detector Names
G4VGFlashSensitiveDetector(), theTrack(nullptr), theTouchableHistory(nullptr),
preStepPoint(nullptr), eminHit(0.0), eminHitD(0.0), m_trackManager(manager),
currentHit(nullptr), runInit(false), timeSlice(timeSliceUnit),
ignoreTrackID(ignoreTkID), hcID(-1), theHC(nullptr), meanResponse(nullptr) {

//Add Hcal Sentitive Detector Names
collectionName.insert(name);

// initialisation
incidentEnergy = edepositEM = edepositHAD = 0.0f;
primIDSaved = -99;
emPDG = epPDG = gammaPDG = 0;

//Parameters
edm::ParameterSet m_CaloSD = p.getParameter<edm::ParameterSet>("CaloSD");
energyCut = m_CaloSD.getParameter<double>("EminTrack")*GeV;
Expand Down Expand Up @@ -111,43 +116,38 @@ CaloSD::CaloSD(G4String name, const DDCompactView & cpv,
}

CaloSD::~CaloSD() {
if (slave) delete slave;
if (theHC) delete theHC;
if (meanResponse) delete meanResponse;
delete slave;
delete theHC;
delete meanResponse;
}

bool CaloSD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {

NaNTrap( aStep ) ;
bool CaloSD::ProcessHits(G4Step * aStep, G4TouchableHistory * tHistory) {

if (aStep == NULL) {
return true;
} else {
if (getStepInfo(aStep)) {
if (hitExists() == false && edepositEM+edepositHAD>0.)
currentHit = createNewHit();
}
NaNTrap(aStep);
theTouchableHistory = tHistory;
if(getStepInfo(aStep) && !hitExists() && edepositEM+edepositHAD>0.f) {
currentHit = createNewHit();
}
return true;
}

bool CaloSD::ProcessHits(G4GFlashSpot* aSpot, G4TouchableHistory*) {

if (aSpot != NULL) {
if (aSpot != nullptr) {
theTrack = const_cast<G4Track *>(aSpot->GetOriginatorTrack()->GetPrimaryTrack());
G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();

if (particleCode == emPDG ||
particleCode == epPDG ||
particleCode == gammaPDG ) {
edepositEM = aSpot->GetEnergySpot()->GetEnergy();
edepositHAD = 0.;
edepositHAD = 0.f;
} else {
edepositEM = 0.;
edepositHAD = 0.;
edepositEM = 0.f;
edepositHAD = 0.f;
}

if (edepositEM>0.) {
if (edepositEM>0.f) {
G4Step * fFakeStep = new G4Step();
preStepPoint = fFakeStep->GetPreStepPoint();
G4StepPoint * fFakePostStepPoint = fFakeStep->GetPostStepPoint();
Expand Down Expand Up @@ -228,7 +228,6 @@ void CaloSD::EndOfEvent(G4HCofThisEvent* ) {
edm::LogInfo("CaloSim") << "CaloSD: EndofEvent entered with " << theHC->entries()
<< " entries";
#endif
// TimeMe("CaloSD:sortAndMergeHits",false);
}

void CaloSD::clear() {}
Expand All @@ -252,30 +251,27 @@ bool CaloSD::getStepInfo(G4Step* aStep) {
preStepPoint = aStep->GetPreStepPoint();
theTrack = aStep->GetTrack();

double time = (aStep->GetPostStepPoint()->GetGlobalTime())/nanosecond;
unsigned int unitID= setDetUnitId(aStep);
uint16_t depth = getDepth(aStep);
int primaryID = getTrackID(theTrack);

bool flag = (unitID > 0);
if (flag) {
double time = theTrack->GetGlobalTime()/CLHEP::nanosecond;
uint16_t depth = getDepth(aStep);
int primaryID = getTrackID(theTrack);
currentID.setID(unitID, time, primaryID, depth);

#ifdef DebugLog
G4TouchableHistory* touch =(G4TouchableHistory*)(theTrack->GetTouchable());
edm::LogInfo("CaloSim") << "CaloSD:: GetStepInfo for"
<< " PV " << touch->GetVolume(0)->GetName()
<< " PVid = " << touch->GetReplicaNumber(0)
<< " MVid = " << touch->GetReplicaNumber(1)
<< " PV " << theTouchableHistory->GetVolume(0)->GetName()
<< " PVid = " << theTouchableHistory->GetReplicaNumber(0)
<< " MVid = " << theTouchableHistory->GetReplicaNumber(1)
<< " Unit " << currentID.unitID()
<< " Edeposit = " << edepositEM << " " << edepositHAD;
<< " Edeposit = " << aStep->GetTotalEnergyDeposit();
} else {
G4TouchableHistory* touch =(G4TouchableHistory*)(theTrack->GetTouchable());
edm::LogInfo("CaloSim") << "CaloSD:: GetStepInfo for"
<< " PV " << touch->GetVolume(0)->GetName()
<< " PVid = " << touch->GetReplicaNumber(0)
<< " MVid = " << touch->GetReplicaNumber(1)
<< " Unit " << std::hex << unitID << std::dec
<< " Edeposit = " << edepositEM << " " << edepositHAD;
<< " PV " << theTouchableHistory->GetVolume(0)->GetName()
<< " PVid = " << theTouchableHistory->GetReplicaNumber(0)
<< " MVid = " << theTouchableHistory->GetReplicaNumber(1)
<< " Unit " << std::hex << unitID << std::dec;
#endif
}

Expand All @@ -284,9 +280,9 @@ bool CaloSD::getStepInfo(G4Step* aStep) {
particleCode == epPDG ||
particleCode == gammaPDG ) {
edepositEM = getEnergyDeposit(aStep);
edepositHAD = 0.;
edepositHAD = 0.f;
} else {
edepositEM = 0.;
edepositEM = 0.f;
edepositHAD = getEnergyDeposit(aStep);
}

Expand Down Expand Up @@ -433,7 +429,7 @@ CaloG4Hit* CaloSD::createNewHit() {
}
}
primIDSaved = currentID.trackID();
if (useMap) totalHits++;
if (useMap) ++totalHits;
return aHit;
}

Expand Down Expand Up @@ -490,10 +486,8 @@ void CaloSD::update(const BeginOfRun *) {
emPDG = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
epPDG = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
gammaPDG = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
#ifdef DebugLog
edm::LogInfo("CaloSim") << "CaloSD: Particle code for e- = " << emPDG
<< " for e+ = " << epPDG << " for gamma = " << gammaPDG;
#endif
initRun();
runInit = true;
}
Expand Down Expand Up @@ -615,7 +609,7 @@ double CaloSD::getResponseWt(G4Track* aTrack) {

void CaloSD::storeHit(CaloG4Hit* hit) {
if (previousID.trackID()<0) return;
if (hit == 0) {
if (hit == nullptr) {
edm::LogWarning("CaloSim") << "CaloSD: hit to be stored is NULL !!";
return;
}
Expand Down
2 changes: 1 addition & 1 deletion SimG4CMS/CherenkovAnalysis/interface/DreamSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DreamSD : public CaloSD {
int side;

/// Table of Cherenkov angle integrals vs photon momentum
std::auto_ptr<G4PhysicsOrderedFreeVector> chAngleIntegrals_;
std::unique_ptr<G4PhysicsOrderedFreeVector> chAngleIntegrals_;
G4MaterialPropertiesTable* materialPropertiesTable;
// Histogramming
TTree* ntuple_;
Expand Down
3 changes: 1 addition & 2 deletions SimG4CMS/CherenkovAnalysis/src/DreamSD.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,7 @@ bool DreamSD::setPbWO2MaterialProperties_( G4Material* aMaterial ) {

// Calculate Cherenkov angle integrals:
// This is an ad-hoc solution (we hold it in the class, not in the material)
chAngleIntegrals_ =
std::auto_ptr<G4PhysicsOrderedFreeVector>( new G4PhysicsOrderedFreeVector() );
chAngleIntegrals_ = std::make_unique<G4PhysicsOrderedFreeVector>();

int index = 0;
double currentRI = RefractiveIndex[index];
Expand Down
5 changes: 1 addition & 4 deletions SimG4Core/Geometry/src/DDDWorld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
#include "G4PVPlacement.hh"
#include "G4TransportationManager.hh"

using namespace edm;

DDDWorld::DDDWorld(const DDCompactView* cpv,
G4LogicalVolumeToDDLogicalPartMap & map,
SensitiveDetectorCatalog & catalog,
bool check) {

std::auto_ptr<DDG4Builder> theBuilder(new DDG4Builder(cpv, check));

auto theBuilder = std::make_unique<DDG4Builder>(cpv, check);
DDGeometryReturnType ret = theBuilder->BuildGeometry();
G4LogicalVolume * world = ret.logicalVolume();

Expand Down
6 changes: 3 additions & 3 deletions SimG4Core/GeometryProducer/interface/GeometryProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class GeometryProducer : public edm::one::EDProducer<edm::one::SharedResources,
SimActivityRegistry m_registry;
std::vector<std::shared_ptr<SimWatcher> > m_watchers;
std::vector<std::shared_ptr<SimProducer> > m_producers;
std::auto_ptr<sim::FieldBuilder> m_fieldBuilder;
std::auto_ptr<SimTrackManager> m_trackManager;
AttachSD * m_attach;
std::unique_ptr<sim::FieldBuilder> m_fieldBuilder;
std::unique_ptr<SimTrackManager> m_trackManager;
std::unique_ptr<AttachSD> m_attach;
std::vector<SensitiveTkDetector*> m_sensTkDets;
std::vector<SensitiveCaloDetector*> m_sensCaloDets;
edm::ParameterSet m_p;
Expand Down
8 changes: 4 additions & 4 deletions SimG4Core/GeometryProducer/src/GeometryProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ GeometryProducer::GeometryProducer(edm::ParameterSet const & p) :
m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
m_pUseSensitiveDetectors(p.getParameter<bool>("UseSensitiveDetectors")),
m_attach(nullptr), m_p(p), m_firstRun ( true )
m_p(p), m_firstRun ( true )
{
m_trackManager = std::unique_ptr<SimTrackManager>(new SimTrackManager);
m_attach = std::unique_ptr<AttachSD>(new AttachSD);

//Look for an outside SimActivityRegistry
//this is used by the visualization code
edm::Service<SimActivityRegistry> otherRegistry;
Expand All @@ -75,7 +78,6 @@ GeometryProducer::GeometryProducer(edm::ParameterSet const & p) :

GeometryProducer::~GeometryProducer()
{
delete m_attach;
delete m_kernel;
}

Expand Down Expand Up @@ -134,8 +136,6 @@ void GeometryProducer::produce(edm::Event & e, const edm::EventSetup & es)
{
edm::LogInfo("GeometryProducer") << " instantiating sensitive detectors ";
// instantiate and attach the sensitive detectors
m_trackManager = std::auto_ptr<SimTrackManager>(new SimTrackManager);
if (m_attach==0) m_attach = new AttachSD;
{
std::pair< std::vector<SensitiveTkDetector*>,
std::vector<SensitiveCaloDetector*> >
Expand Down
18 changes: 10 additions & 8 deletions SimG4Core/SensitiveDetector/interface/SensitiveDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@ class SensitiveDetector : public G4VSensitiveDetector
virtual void AssignSD(const std::string & vname);
virtual void EndOfEvent(G4HCofThisEvent * eventHC);
enum coordinates {WorldCoordinates, LocalCoordinates};
Local3DPoint InitialStepPosition(G4Step * s, coordinates);
Local3DPoint FinalStepPosition(G4Step * s, coordinates);
Local3DPoint ConvertToLocal3DPoint(const G4ThreeVector& point);
std::string nameOfSD() { return name; }
Local3DPoint InitialStepPosition(G4Step * step, coordinates);
Local3DPoint FinalStepPosition(G4Step * step, coordinates);
inline Local3DPoint ConvertToLocal3DPoint(const G4ThreeVector& point)
{
Local3DPoint res(point.x(),point.y(),point.z());
return std::move(res);
}
inline std::string& nameOfSD() { return name; }
virtual std::vector<std::string> getNames()
{
std::vector<std::string> temp;
temp.push_back(nameOfSD());
temp.push_back(name);
return temp;
}

}
void NaNTrap( G4Step* step ) ;

private:
std::string name;
G4Step * currentStep;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class SensitiveDetectorMaker : public SensitiveDetectorMakerBase
const edm::ParameterSet& p,
const SimTrackManager* m,
SimActivityRegistry& reg,
std::auto_ptr<SensitiveTkDetector>& oTK,
std::auto_ptr<SensitiveCaloDetector>& oCalo) const
std::unique_ptr<SensitiveTkDetector>& oTK,
std::unique_ptr<SensitiveCaloDetector>& oCalo) const
{
std::auto_ptr<T> returnValue(new T(iname, cpv, clg, p, m));
auto returnValue = std::make_unique<T>(iname, cpv, clg, p, m);
SimActivityRegistryEnroller::enroll(reg, returnValue.get());

this->convertTo(returnValue.get(), oTK,oCalo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class SensitiveDetectorMakerBase
const edm::ParameterSet& p,
const SimTrackManager* m,
SimActivityRegistry& reg,
std::auto_ptr<SensitiveTkDetector>& oTK,
std::auto_ptr<SensitiveCaloDetector>& oCalo) const =0;
std::unique_ptr<SensitiveTkDetector>& oTK,
std::unique_ptr<SensitiveCaloDetector>& oCalo) const =0;

// ---------- static member functions --------------------

Expand All @@ -57,14 +57,14 @@ class SensitiveDetectorMakerBase
protected:
//used to identify which type of Sensitive Detector we have
void convertTo( SensitiveTkDetector* iFrom,
std::auto_ptr<SensitiveTkDetector>& oTo,
std::auto_ptr<SensitiveCaloDetector>&) const{
oTo= std::auto_ptr<SensitiveTkDetector>(iFrom);
std::unique_ptr<SensitiveTkDetector>& oTo,
std::unique_ptr<SensitiveCaloDetector>&) const{
oTo= std::unique_ptr<SensitiveTkDetector>(iFrom);
}
void convertTo( SensitiveCaloDetector* iFrom,
std::auto_ptr<SensitiveTkDetector>&,
std::auto_ptr<SensitiveCaloDetector>& oTo) const{
oTo=std::auto_ptr<SensitiveCaloDetector>(iFrom);
std::unique_ptr<SensitiveTkDetector>&,
std::unique_ptr<SensitiveCaloDetector>& oTo) const{
oTo=std::unique_ptr<SensitiveCaloDetector>(iFrom);
}

private:
Expand Down
Loading

0 comments on commit 56f2f72

Please sign in to comment.