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 c6ffc2e
Show file tree
Hide file tree
Showing 23 changed files with 530 additions and 561 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
219 changes: 95 additions & 124 deletions base/sim/FairMCApplication.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -651,74 +651,38 @@ 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.
Expand Down Expand Up @@ -796,61 +760,53 @@ void FairMCApplication::StopMCRun()
//_____________________________________________________________________________
void FairMCApplication::FinishEvent()
{
// User actions after finishing of an event
// ---
LOG(debug) << "[" << fRootManager->GetInstanceId()
<< " FairMCMCApplication::FinishEvent: " << fMCEventHeader->GetEventID() << " (MC "
<< gMC->CurrentEvent() << ")";
if (gMC->IsMT()
&& fRun->GetSink()->GetSinkType() == kONLINESINK) { // fix the rare case when running G4 multithreaded on MQ
fMCEventHeader->SetEventID(gMC->CurrentEvent() + 1);
}

// --> Fill the stack output array
fStack->FillTrackArray();
// --> Update track indizes in MCTracks and MCPoints
fStack->UpdateTrackIndex(fActiveDetectors);
// --> Screen output of stack
// fStack->Print();

if (fFairTaskList) {
fFairTaskList->ExecuteTask("");
fFairTaskList->FinishEvent();
}

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

if (fRootManager && fSaveCurrentEvent) {
fRootManager->Fill();
} else {
fSaveCurrentEvent = kTRUE;
}

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

fStack->Reset();
if (nullptr != fTrajFilter) {
fTrajFilter->Reset();
// TObjArray* fListOfTracks=gGeoManager->GetListOfTracks();
// fListOfTracks->Delete();
gGeoManager->GetListOfTracks()->Delete();
}
if (nullptr != fRadLenMan) {
fRadLenMan->Reset();
}
if (nullptr != fRadMapMan) {
fRadMapMan->Reset();
}

// Store information about runtime for one event and memory consuption
// for later usage.
if ((FairRunSim::Instance()->IsRunInfoGenerated()) && !gMC->IsMT()) {
fRunInfo.StoreInfo();
}
// User actions after finishing of an event
// ---
LOG(debug) << "FairMCMCApplication::FinishEvent: "
<< fRootManager->GetInstanceId();

// --> Fill the stack output array
fStack->FillTrackArray();
// --> Update track indizes in MCTracks and MCPoints
fStack->UpdateTrackIndex(fActiveDetectors);
// --> Screen output of stack
// fStack->Print();

if (fFairTaskList) {
fFairTaskList->ExecuteTask("");
fFairTaskList->FinishEvent();
}

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

if (fRootManager && fSaveCurrentEvent) {
fRootManager->Fill();
} else {
fSaveCurrentEvent = kTRUE;
}

fStack->Reset();
if(nullptr != fTrajFilter) {
fTrajFilter->Reset();
// TObjArray* fListOfTracks=gGeoManager->GetListOfTracks();
// fListOfTracks->Delete();
gGeoManager->GetListOfTracks()->Delete();
}
if(nullptr !=fRadLenMan) {
fRadLenMan->Reset();
}
if(nullptr !=fRadMapMan) {
fRadMapMan->Reset();
}

// Store information about runtime for one event and memory consuption
// for later usage.
if ( (FairRunSim::Instance()->IsRunInfoGenerated()) && ! gMC->IsMT() ) {
fRunInfo.StoreInfo();
}
}
//_____________________________________________________________________________
Double_t FairMCApplication::TrackingRmax() const
Expand Down Expand Up @@ -1480,4 +1436,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)
17 changes: 15 additions & 2 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 @@ -95,7 +96,10 @@ class FairMCApplication : public TVirtualMCApplication
/** Align or misalign geometry before actual run */
virtual Bool_t MisalignGeometry();
/** Define parameters for optical processes (optional) */
virtual void ConstructOpGeometry(); // MC Application
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 @@ -300,7 +309,11 @@ class FairMCApplication : public TVirtualMCApplication
Bool_t fSaveCurrentEvent;

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

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

ClassDef(FairMCApplication, 4);

Expand Down
Loading

0 comments on commit c6ffc2e

Please sign in to comment.