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 Apr 26, 2019
1 parent 25bd3f5 commit 3cd6899
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 146 deletions.
2 changes: 1 addition & 1 deletion base/sim/FairDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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
*/
Expand Down
95 changes: 20 additions & 75 deletions base/sim/FairMCApplication.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -661,76 +661,9 @@ 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
Expand Down Expand Up @@ -838,11 +771,6 @@ void FairMCApplication::FinishEvent()
fSaveCurrentEvent = kTRUE;
}

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

fStack->Reset();
if(NULL != fTrajFilter) {
fTrajFilter->Reset();
Expand Down Expand Up @@ -1504,4 +1432,21 @@ void FairMCApplication::UndoGeometryModifications()

}

void FairMCApplication::ConstructSensitiveDetectors()
{
LOG(info) << "############ ?????????? Construct sensitive detectors";

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 @@ -37,6 +37,7 @@ class FairRootManager;
class FairTask;
class FairTrajFilter;
class FairVolume;
class FairModule;
class FairRunSim;
class TChain;
class TIterator;
Expand Down Expand Up @@ -93,6 +94,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 @@ -203,6 +207,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 @@ -296,6 +305,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) //Interface to MonteCarlo application

Expand Down
19 changes: 10 additions & 9 deletions base/sim/FairModule.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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 @@ -96,7 +96,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 @@ -139,8 +139,7 @@ FairModule::FairModule(const FairModule& rhs)
//__________________________________________________________________________

FairModule::FairModule()
: TNamed(),
fMotherVolumeName(""),
: fMotherVolumeName(""),
fgeoVer("Not defined"),
fgeoName("Not defined"),
fModId(-1),
Expand All @@ -160,7 +159,7 @@ FairModule& FairModule::operator= (const FairModule& rhs)
if (this == &rhs) return *this;

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

// assignment operator
fMotherVolumeName = rhs.fMotherVolumeName;
Expand Down Expand Up @@ -188,7 +187,7 @@ FairModule& FairModule::operator= (const FairModule& rhs)
//__________________________________________________________________________
void FairModule::Streamer(TBuffer& b)
{
TNamed::Streamer(b);
TVirtualMCSensitiveDetector::Streamer(b);


if (b.IsReading()) {
Expand Down Expand Up @@ -294,6 +293,9 @@ void FairModule::ProcessNodes(TList* aList)
FairGeoVolume* aVol=NULL;

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

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

volume->setModId(fModId);
volume->SetModule(this);
svList->Add(volume);
Expand All @@ -306,9 +308,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 All @@ -321,8 +324,6 @@ void FairModule::AddSensitiveVolume(TGeoVolume* v)
fNbOfSensitiveVol++;
}
}


//__________________________________________________________________________
FairVolume* FairModule::getFairVolume(FairGeoNode* fN)
{
Expand Down
11 changes: 9 additions & 2 deletions base/sim/FairModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef FAIRMODULE_H
#define FAIRMODULE_H

#include "TNamed.h" // for TNamed
#include "TVirtualMCSensitiveDetector.h"

#include "FairGeoInterface.h" // for FairGeoInterface
#include "FairGeoLoader.h" // for FairGeoLoader
Expand Down Expand Up @@ -47,7 +47,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 @@ -136,6 +136,13 @@ class FairModule: public TNamed
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 @@ -63,11 +63,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 @@ -23,7 +23,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
9 changes: 4 additions & 5 deletions examples/MQ/pixelDetector/src/Pixel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void Pixel::Initialize()
FairDetector::Initialize();
}

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

/** This method is called from the MC stepping */
Expand All @@ -108,10 +108,11 @@ Bool_t Pixel::ProcessHits(FairVolume* vol)
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; }
if (fELoss == 0. ) { return; }

// Taking stationNr and sectorNr from string is almost effortless.
// Simulation of 100k events with 5 pions without magnetic field takes:
Expand All @@ -135,8 +136,6 @@ Bool_t Pixel::ProcessHits(FairVolume* vol)
FairStack* stack = static_cast<FairStack*>(TVirtualMC::GetMC()->GetStack());
stack->AddPoint(kPixel);
}

return kTRUE;
}

void Pixel::EndOfEvent()
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 @@ -40,7 +40,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 @@ -78,7 +78,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 @@ -99,12 +99,13 @@ 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 @@ -120,8 +121,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
2 changes: 1 addition & 1 deletion examples/advanced/Tutorial3/simulation/FairTestDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FairTestDetector : 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
Loading

0 comments on commit 3cd6899

Please sign in to comment.