Skip to content

Commit

Permalink
Add support for TVirtualMCSensitiveDetector.
Browse files Browse the repository at this point in the history
  • Loading branch information
kresan committed Feb 9, 2021
1 parent d899d32 commit bd19c48
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 136 deletions.
2 changes: 1 addition & 1 deletion base/sim/FairDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class FairDetector : public FairModule
/**
this method is called for each step during simulation (see FairMCApplication::Stepping())
*/
virtual Bool_t ProcessHits(FairVolume* v = 0) = 0;
virtual void ProcessHits()=0;
/**
this is called at the end of an event after the call to tree fill in the FairRootManager
*/
Expand Down
120 changes: 47 additions & 73 deletions base/sim/FairMCApplication.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -651,75 +651,39 @@ void FairMCApplication::FinishRunOnWorker()
//_____________________________________________________________________________
void FairMCApplication::Stepping()
{
// User actions at each step
// ---

// Work around for Fluka VMC, which does not call
// MCApplication::PreTrack()
static Int_t TrackId = 0;
if (fMcVersion == 2 || fMC->GetStack()->GetCurrentTrackNumber() != TrackId) {
PreTrack();
TrackId = fMC->GetStack()->GetCurrentTrackNumber();
}

// Check if the volume with id is in the volume multimap.
// If it is not in the map the volume is not a sensitive volume
// and we do not call nay of our ProcessHits functions.

// If the volume is in the multimap, check in second step if the current
// copy is alredy inside the multimap.
// If the volume is not in the multimap add the copy of the volume to the
// multimap.
// In any case call the ProcessHits function for this specific detector.
Int_t copyNo;
Int_t id = fMC->CurrentVolID(copyNo);
Bool_t InMap = kFALSE;
fDisVol = 0;
fDisDet = 0;
Int_t fCopyNo = 0;
fVolIter = fVolMap.find(id);

if (fVolIter != fVolMap.end()) {

// Call Process hits for FairVolume with this id, copyNo
do {
fDisVol = fVolIter->second;
fCopyNo = fDisVol->getCopyNo();
if (copyNo == fCopyNo) {
fDisDet = fDisVol->GetDetector();
if (fDisDet) {
fDisDet->ProcessHits(fDisVol);
}
InMap = kTRUE;
break;
}
++fVolIter;
} while (fVolIter != fVolMap.upper_bound(id));

// if (fDisVol && !InMap) { // fDisVolume is set previously, no check needed

// Create new FairVolume with this id, copyNo.
// Use the FairVolume with the same id found in the map to get
// the link to the detector.
// Seems that this never happens (?)
if (!InMap) {
// cout << "Volume not in map; fDisVol ? " << fDisVol << endl
FairVolume* fNewV = new FairVolume(fMC->CurrentVolName(), id);
fNewV->setMCid(id);
fNewV->setModId(fDisVol->getModId());
fNewV->SetModule(fDisVol->GetModule());
fNewV->setCopyNo(copyNo);
fVolMap.insert(pair<Int_t, FairVolume*>(id, fNewV));
fDisDet = fDisVol->GetDetector();

// LOG(info) << "FairMCApplication::Stepping: new fair volume"
// << id << " " << copyNo << " " << fDisDet;
if (fDisDet) {
fDisDet->ProcessHits(fNewV);
}
Int_t copyNo = 0;
Int_t id = 0;

// If information about the tracks should be stored the information as to be
// stored for any step.
// Information about each single step has also to be stored for the other
// special run modes of the simulation which are used to store information
// about
// 1.) Radiation length in each volume
// 2.) Energy deposition in each volume
// 3.) Fluence of particles through a defined plane which can be anywhere
// in the geometry. This plane has not to be correlated with any real
// volume
if(fTrajAccepted) {
if(fMC->TrackStep() > fTrajFilter->GetStepSizeCut()) {
fMC->TrackPosition(fTrkPos);
fTrajFilter->GetCurrentTrk()->AddPoint(fTrkPos.X(), fTrkPos.Y(), fTrkPos.Z(), fTrkPos.T());
}
}

if(fRadLenMan) {
id = fMC->CurrentVolID(copyNo);
fModVolIter = fgMasterInstance->fModVolMap.find(id);
fRadLenMan->AddPoint(fModVolIter->second);
}
if(fRadMapMan) {
id = fMC->CurrentVolID(copyNo);
fModVolIter = fgMasterInstance->fModVolMap.find(id);
fRadMapMan->AddPoint(fModVolIter->second);
}
if(fRadGridMan) {
fRadGridMan->FillMeshList();
}

// If information about the tracks should be stored the information as to be
// stored for any step.
// Information about each single step has also to be stored for the other
Expand Down Expand Up @@ -827,11 +791,6 @@ void FairMCApplication::FinishEvent()
} else {
fSaveCurrentEvent = kTRUE;
}

for (auto detectorPtr : listActiveDetectors) {
detectorPtr->EndOfEvent();
}

fStack->Reset();
if (nullptr != fTrajFilter) {
fTrajFilter->Reset();
Expand Down Expand Up @@ -1480,4 +1439,19 @@ void FairMCApplication::UndoGeometryModifications()
gGeoManager->ClearPhysicalNodes(kFALSE);
}

ClassImp(FairMCApplication);
void FairMCApplication::ConstructSensitiveDetectors()
{
for(auto const& x : fMapSensitiveDetectors)
{
LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors "
<< x.first << " " << x.second;
TVirtualMC::GetMC()->SetSensitiveDetector(x.first, x.second);
}
}

void FairMCApplication::AddSensitiveModule(std::string volName, FairModule* module)
{
fMapSensitiveDetectors[volName] = module;
}

ClassImp(FairMCApplication)
13 changes: 13 additions & 0 deletions base/sim/FairMCApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class FairRootManager;
class FairTask;
class FairTrajFilter;
class FairVolume;
class FairModule;
class FairRunSim;
class TChain;
class TIterator;
Expand Down Expand Up @@ -96,6 +97,9 @@ class FairMCApplication : public TVirtualMCApplication
virtual Bool_t MisalignGeometry();
/** Define parameters for optical processes (optional) */
virtual void ConstructOpGeometry(); // MC Application

virtual void ConstructSensitiveDetectors();

/** Define actions at the end of event */
virtual void FinishEvent(); // MC Application
/** Define actions at the end of primary track */
Expand Down Expand Up @@ -208,6 +212,11 @@ class FairMCApplication : public TVirtualMCApplication
* Get the current application state.
*/
FairMCApplicationState GetState() const { return fState; }

/**
* Add module to the list of sensitive detectors.
*/
void AddSensitiveModule(std::string volName, FairModule* module);

private:
// methods
Expand Down Expand Up @@ -301,6 +310,10 @@ class FairMCApplication : public TVirtualMCApplication

/** Current state */
FairMCApplicationState fState; //!

/** List of sensitive detectors.
* To be used with TVirtualMCSensitiveDetector. */
std::map<std::string, FairModule*> fMapSensitiveDetectors;

ClassDef(FairMCApplication, 4);

Expand Down
16 changes: 10 additions & 6 deletions base/sim/FairModule.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void FairModule::ConstructOpGeometry()
FairModule::~FairModule() {}

FairModule::FairModule(const char* Name, const char* title, Bool_t Active)
: TNamed(Name, title)
: TVirtualMCSensitiveDetector(Name, title)
, fMotherVolumeName("")
, fgeoVer("Not defined")
, fgeoName("Not defined")
Expand All @@ -93,7 +93,7 @@ FairModule::FairModule(const char* Name, const char* title, Bool_t Active)
}

FairModule::FairModule(const FairModule& rhs)
: TNamed(rhs)
: TVirtualMCSensitiveDetector(rhs)
, fMotherVolumeName(rhs.fMotherVolumeName)
, fgeoVer(rhs.fgeoVer)
, fgeoName(rhs.fgeoName)
Expand Down Expand Up @@ -133,7 +133,7 @@ FairModule::FairModule(const FairModule& rhs)
}

FairModule::FairModule()
: TNamed()
: TVirtualMCSensitiveDetector()
, fMotherVolumeName("")
, fgeoVer("Not defined")
, fgeoName("Not defined")
Expand All @@ -153,7 +153,7 @@ FairModule& FairModule::operator=(const FairModule& rhs)
return *this;

// base class assignment
TNamed::operator=(rhs);
TVirtualMCSensitiveDetector::operator=(rhs);

// assignment operator
fMotherVolumeName = rhs.fMotherVolumeName;
Expand Down Expand Up @@ -181,7 +181,7 @@ FairModule& FairModule::operator=(const FairModule& rhs)

void FairModule::Streamer(TBuffer& b)
{
TNamed::Streamer(b);
TVirtualMCSensitiveDetector::Streamer(b);

if (b.IsReading()) {
fgeoVer.Streamer(b);
Expand Down Expand Up @@ -281,6 +281,9 @@ void FairModule::ProcessNodes(TList* aList)
FairGeoVolume* aVol = nullptr;

if (node->isSensitive() && fActive) {

FairMCApplication::Instance()->AddSensitiveModule(volume->GetName(), this);

volume->setModId(fModId);
volume->SetModule(this);
svList->Add(volume);
Expand All @@ -293,9 +296,10 @@ void FairModule::ProcessNodes(TList* aList)

void FairModule::AddSensitiveVolume(TGeoVolume* v)
{

LOG(debug2) << "AddSensitiveVolume " << v->GetName();

FairMCApplication::Instance()->AddSensitiveModule(v->GetName(), this);

// Only register volumes which are not already registered
// Otherwise the stepping will be slowed down
if (!vList->findObject(v->GetName())) {
Expand Down
14 changes: 13 additions & 1 deletion base/sim/FairModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "FairLogger.h"
#include "FairRun.h" // for FairRun
#include "FairRuntimeDb.h" // for FairRuntimeDb
#include "TVirtualMCSensitiveDetector.h"

#include <Rtypes.h> // for Bool_t, Int_t, etc
#include <TList.h> // for TList (ptr only), TListIter
Expand Down Expand Up @@ -43,7 +44,7 @@ class TVirtualMC;
* Changelog: 29.02.2012 [O.Merle] Fixed missing material assignment for top volume.
* ... and please - add some documentation to your code.
*/
class FairModule : public TNamed
class FairModule : public TVirtualMCSensitiveDetector
{
public:
/**default ctor*/
Expand Down Expand Up @@ -141,6 +142,17 @@ class FairModule : public TNamed
FairVolume* getFairVolume(FairGeoNode* fNode);
void AddSensitiveVolume(TGeoVolume* v);

static thread_local TArrayI* volNumber; //!
TString fMotherVolumeName; //!
FairVolume* getFairVolume(FairGeoNode* fNode);
void AddSensitiveVolume(TGeoVolume* v);

virtual void EndOfEvent() {}

virtual void Initialize() {}

virtual void ProcessHits() {}

private:
/** Re-implimented from ROOT: TGeoMatrix::SetDefaultName() */
void SetDefaultMatrixName(TGeoMatrix* matrix);
Expand Down
4 changes: 1 addition & 3 deletions base/sim/fastsim/FairFastSimDetector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ void FairFastSimDetector::ConstructGeometry()
}
}

Bool_t FairFastSimDetector::ProcessHits(FairVolume*)
void FairFastSimDetector::ProcessHits()
{
FastSimProcessParticle();

return kTRUE;
}

ClassImp(FairFastSimDetector);
2 changes: 1 addition & 1 deletion base/sim/fastsim/FairFastSimDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FairFastSimDetector : public FairDetector

virtual void Initialize() = 0;

virtual Bool_t ProcessHits(FairVolume* vol = 0) final;
virtual void ProcessHits() final;

virtual void EndOfEvent() {}

Expand Down
14 changes: 11 additions & 3 deletions examples/MQ/pixelDetector/src/Pixel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ Pixel::~Pixel()

void Pixel::Initialize() { FairDetector::Initialize(); }

Bool_t Pixel::ProcessHits(FairVolume* vol)
void Pixel::ProcessHits()
{

/** This method is called from the MC stepping */
// Set parameters at entrance of volume. Reset ELoss.
if (TVirtualMC::GetMC()->IsTrackEntering()) {
Expand All @@ -94,10 +95,11 @@ Bool_t Pixel::ProcessHits(FairVolume* vol)
if (TVirtualMC::GetMC()->IsTrackExiting() || TVirtualMC::GetMC()->IsTrackStop()
|| TVirtualMC::GetMC()->IsTrackDisappeared()) {
fTrackID = TVirtualMC::GetMC()->GetStack()->GetCurrentTrackNumber();
fVolumeID = vol->getMCid();
Int_t copyNo = 0;
fVolumeID = TVirtualMC::GetMC()->CurrentVolID(copyNo);

if (fELoss == 0.) {
return kFALSE;
return;
}

// Taking stationNr and sectorNr from string is almost effortless.
Expand Down Expand Up @@ -126,6 +128,12 @@ Bool_t Pixel::ProcessHits(FairVolume* vol)
FairStack* stack = static_cast<FairStack*>(TVirtualMC::GetMC()->GetStack());
stack->AddPoint(kPixel);
}
}

void Pixel::EndOfEvent()
{

fPixelPointCollection->Clear();

return kTRUE;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/MQ/pixelDetector/src/Pixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Pixel : public FairDetector
/** this method is called for each step during simulation
* (see FairMCApplication::Stepping())
*/
virtual Bool_t ProcessHits(FairVolume* v = 0);
virtual void ProcessHits();

/** Registers the produced collections in FAIRRootManager. */
virtual void Register();
Expand Down
9 changes: 4 additions & 5 deletions examples/advanced/Tutorial3/simulation/FairTestDetector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void FairTestDetector::Initialize()
rtdb->getContainer("FairTestDetectorGeoPar");
}

Bool_t FairTestDetector::ProcessHits(FairVolume* vol)
void FairTestDetector::ProcessHits()
{
/** This method is called from the MC stepping */

Expand All @@ -87,11 +87,12 @@ Bool_t FairTestDetector::ProcessHits(FairVolume* vol)
if (TVirtualMC::GetMC()->IsTrackExiting() || TVirtualMC::GetMC()->IsTrackStop()
|| TVirtualMC::GetMC()->IsTrackDisappeared()) {
fTrackID = TVirtualMC::GetMC()->GetStack()->GetCurrentTrackNumber();
fVolumeID = vol->getMCid();
Int_t copyNo = 0;
fVolumeID = TVirtualMC::GetMC()->CurrentVolID(copyNo);
TVirtualMC::GetMC()->TrackPosition(fPosOut);
TVirtualMC::GetMC()->TrackMomentum(fMomOut);
if (fELoss == 0.) {
return kFALSE;
return;
}
AddHit(fTrackID,
fVolumeID,
Expand All @@ -107,8 +108,6 @@ Bool_t FairTestDetector::ProcessHits(FairVolume* vol)
FairStack* stack = static_cast<FairStack*>(TVirtualMC::GetMC()->GetStack());
stack->AddPoint(kTutDet);
}

return kTRUE;
}

void FairTestDetector::EndOfEvent()
Expand Down
Loading

0 comments on commit bd19c48

Please sign in to comment.