From d8147706c927f26c4ab003ca83e8d0b380e5911a Mon Sep 17 00:00:00 2001 From: Dmytro Kresan Date: Wed, 13 Feb 2019 09:24:05 +0100 Subject: [PATCH 1/9] Add support for TVirtualMCSensitiveDetector. --- base/sim/FairDetector.h | 2 +- base/sim/FairMCApplication.cxx | 103 ++++++++---------- base/sim/FairMCApplication.h | 13 +++ base/sim/FairModule.cxx | 16 ++- base/sim/FairModule.h | 14 ++- base/sim/fastsim/FairFastSimDetector.cxx | 4 +- base/sim/fastsim/FairFastSimDetector.h | 2 +- examples/MQ/pixelDetector/src/Pixel.cxx | 14 ++- examples/MQ/pixelDetector/src/Pixel.h | 2 +- .../Tutorial3/simulation/FairTestDetector.cxx | 9 +- .../Tutorial3/simulation/FairTestDetector.h | 2 +- .../Tutorial1/src/FairTutorialDet1.cxx | 26 +++-- .../Tutorial1/src/FairTutorialDet1.h | 2 +- .../Tutorial2/src/FairTutorialDet2.cxx | 9 +- .../Tutorial2/src/FairTutorialDet2.h | 2 +- .../Tutorial4/src/mc/FairTutorialDet4.cxx | 8 +- .../Tutorial4/src/mc/FairTutorialDet4.h | 2 +- .../rutherford/src/FairRutherford.cxx | 9 +- .../rutherford/src/FairRutherford.h | 4 +- .../NewDetector/NewDetector.cxx | 9 +- .../NewDetector/NewDetector.h | 2 +- .../NewDetector/NewDetector.cxx | 9 +- .../NewDetector/NewDetector.h | 2 +- 23 files changed, 143 insertions(+), 122 deletions(-) diff --git a/base/sim/FairDetector.h b/base/sim/FairDetector.h index b89fa8ab79..164b84d7c7 100644 --- a/base/sim/FairDetector.h +++ b/base/sim/FairDetector.h @@ -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 */ diff --git a/base/sim/FairMCApplication.cxx b/base/sim/FairMCApplication.cxx index d5a0552c9c..211d93103c 100644 --- a/base/sim/FairMCApplication.cxx +++ b/base/sim/FairMCApplication.cxx @@ -526,62 +526,39 @@ void FairMCApplication::Stepping() 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); - auto voliter = fVolMap.find(id); - - if (voliter != fVolMap.end()) { - Bool_t InMap = kFALSE; - FairVolume* disvol = nullptr; - FairDetector* disdet = nullptr; - - // Call Process hits for FairVolume with this id, copyNo - do { - disvol = voliter->second; - if (copyNo == disvol->getCopyNo()) { - disdet = disvol->GetDetector(); - if (disdet) { - disdet->ProcessHits(disvol); - } - InMap = kTRUE; - break; - } - ++voliter; - } while (voliter != fVolMap.upper_bound(id)); - - // if (disvol && !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; disvol ? " << disvol << endl - FairVolume* fNewV = new FairVolume(fMC->CurrentVolName(), id); - fNewV->setMCid(id); - fNewV->setModId(disvol->getModId()); - fNewV->SetModule(disvol->GetModule()); - fNewV->setCopyNo(copyNo); - fVolMap.insert(pair(id, fNewV)); - disdet = disvol->GetDetector(); - - // LOG(info) << "FairMCApplication::Stepping: new fair volume" - // << id << " " << copyNo << " " << disdet; - if (disdet) { - disdet->ProcessHits(fNewV); - } + + // 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 @@ -689,11 +666,6 @@ void FairMCApplication::FinishEvent() } else { fSaveCurrentEvent = kTRUE; } - - for (auto detectorPtr : listActiveDetectors) { - detectorPtr->EndOfEvent(); - } - fStack->Reset(); if (nullptr != fTrajFilter) { fTrajFilter->Reset(); @@ -1338,4 +1310,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) diff --git a/base/sim/FairMCApplication.h b/base/sim/FairMCApplication.h index d90cdabf1f..e033c1d37f 100644 --- a/base/sim/FairMCApplication.h +++ b/base/sim/FairMCApplication.h @@ -39,6 +39,7 @@ class FairRootManager; class FairTask; class FairTrajFilter; class FairVolume; +class FairModule; class FairRunSim; class TChain; class TIterator; @@ -105,6 +106,9 @@ class FairMCApplication : public TVirtualMCApplication Bool_t MisalignGeometry() override; /** Define parameters for optical processes (optional) */ void ConstructOpGeometry() override; // MC Application + + virtual void ConstructSensitiveDetectors(); + /** Define actions at the end of event */ void FinishEvent() override; // MC Application /** Define actions at the end of primary track */ @@ -221,6 +225,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); /** * Return non-owning pointer to FairRadGridManager @@ -318,6 +327,10 @@ class FairMCApplication : public TVirtualMCApplication /** Current state */ FairMCApplicationState fState; //! + + /** List of sensitive detectors. + * To be used with TVirtualMCSensitiveDetector. */ + std::map fMapSensitiveDetectors; ClassDefOverride(FairMCApplication, 5); diff --git a/base/sim/FairModule.cxx b/base/sim/FairModule.cxx index 57207c0e9e..8bc66c0e76 100644 --- a/base/sim/FairModule.cxx +++ b/base/sim/FairModule.cxx @@ -73,7 +73,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") @@ -94,7 +94,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) @@ -134,7 +134,7 @@ FairModule::FairModule(const FairModule& rhs) } FairModule::FairModule() - : TNamed() + : TVirtualMCSensitiveDetector() , fMotherVolumeName("") , fgeoVer("Not defined") , fgeoName("Not defined") @@ -154,7 +154,7 @@ FairModule& FairModule::operator=(const FairModule& rhs) return *this; // base class assignment - TNamed::operator=(rhs); + TVirtualMCSensitiveDetector::operator=(rhs); // assignment operator fMotherVolumeName = rhs.fMotherVolumeName; @@ -182,7 +182,7 @@ FairModule& FairModule::operator=(const FairModule& rhs) void FairModule::Streamer(TBuffer& b) { - TNamed::Streamer(b); + TVirtualMCSensitiveDetector::Streamer(b); if (b.IsReading()) { fgeoVer.Streamer(b); @@ -282,6 +282,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); @@ -294,9 +297,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())) { diff --git a/base/sim/FairModule.h b/base/sim/FairModule.h index 409d2340d2..8efd925c22 100644 --- a/base/sim/FairModule.h +++ b/base/sim/FairModule.h @@ -15,6 +15,7 @@ #include "FairLogger.h" #include "FairRun.h" // for FairRun #include "FairRuntimeDb.h" // for FairRuntimeDb +#include "TVirtualMCSensitiveDetector.h" #include // for Bool_t, Int_t, etc #include // for TList (ptr only), TListIter @@ -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*/ @@ -148,6 +149,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); diff --git a/base/sim/fastsim/FairFastSimDetector.cxx b/base/sim/fastsim/FairFastSimDetector.cxx index f574d92e77..a48e0f4c07 100644 --- a/base/sim/fastsim/FairFastSimDetector.cxx +++ b/base/sim/fastsim/FairFastSimDetector.cxx @@ -59,11 +59,9 @@ void FairFastSimDetector::ConstructGeometry() } } -Bool_t FairFastSimDetector::ProcessHits(FairVolume*) +void FairFastSimDetector::ProcessHits() { FastSimProcessParticle(); - - return kTRUE; } ClassImp(FairFastSimDetector); diff --git a/base/sim/fastsim/FairFastSimDetector.h b/base/sim/fastsim/FairFastSimDetector.h index 187c48b9d5..5867640dbb 100644 --- a/base/sim/fastsim/FairFastSimDetector.h +++ b/base/sim/fastsim/FairFastSimDetector.h @@ -25,7 +25,7 @@ class FairFastSimDetector : public FairDetector ~FairFastSimDetector() override; - Bool_t ProcessHits(FairVolume* vol = 0) override final; + void ProcessHits() override final; void EndOfEvent() override {} diff --git a/examples/MQ/pixelDetector/src/Pixel.cxx b/examples/MQ/pixelDetector/src/Pixel.cxx index f61d6d1602..33c319afbd 100644 --- a/examples/MQ/pixelDetector/src/Pixel.cxx +++ b/examples/MQ/pixelDetector/src/Pixel.cxx @@ -73,8 +73,9 @@ Pixel::~Pixel() } } -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()) { @@ -92,10 +93,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. @@ -124,6 +126,12 @@ Bool_t Pixel::ProcessHits(FairVolume* vol) FairStack* stack = static_cast(TVirtualMC::GetMC()->GetStack()); stack->AddPoint(kPixel); } +} + +void Pixel::EndOfEvent() +{ + + fPixelPointCollection->Clear(); return kTRUE; } diff --git a/examples/MQ/pixelDetector/src/Pixel.h b/examples/MQ/pixelDetector/src/Pixel.h index 38c13e5303..512a6b6e9b 100644 --- a/examples/MQ/pixelDetector/src/Pixel.h +++ b/examples/MQ/pixelDetector/src/Pixel.h @@ -36,7 +36,7 @@ class Pixel : public FairDetector /** this method is called for each step during simulation * (see FairMCApplication::Stepping()) */ - Bool_t ProcessHits(FairVolume* v = 0) override; + virtual void ProcessHits(); /** Registers the produced collections in FAIRRootManager. */ void Register() override; diff --git a/examples/advanced/Tutorial3/simulation/FairTestDetector.cxx b/examples/advanced/Tutorial3/simulation/FairTestDetector.cxx index 98ba6c1d29..bef9b8c454 100644 --- a/examples/advanced/Tutorial3/simulation/FairTestDetector.cxx +++ b/examples/advanced/Tutorial3/simulation/FairTestDetector.cxx @@ -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 */ @@ -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, @@ -107,8 +108,6 @@ Bool_t FairTestDetector::ProcessHits(FairVolume* vol) FairStack* stack = static_cast(TVirtualMC::GetMC()->GetStack()); stack->AddPoint(kTutDet); } - - return kTRUE; } void FairTestDetector::EndOfEvent() diff --git a/examples/advanced/Tutorial3/simulation/FairTestDetector.h b/examples/advanced/Tutorial3/simulation/FairTestDetector.h index 767231c411..93ad12b2e5 100644 --- a/examples/advanced/Tutorial3/simulation/FairTestDetector.h +++ b/examples/advanced/Tutorial3/simulation/FairTestDetector.h @@ -39,7 +39,7 @@ class FairTestDetector : public FairDetector /** this method is called for each step during simulation * (see FairMCApplication::Stepping()) */ - Bool_t ProcessHits(FairVolume* v = 0) override; + virtual void ProcessHits(); /** Registers the produced collections in FAIRRootManager. */ void Register() override; diff --git a/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx b/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx index 7f0727abf1..f614ed7bc2 100644 --- a/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx +++ b/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx @@ -7,8 +7,8 @@ ********************************************************************************/ #include "FairTutorialDet1.h" -#include "FairDetectorList.h" // for DetectorId::kTutDet -#include "FairLogger.h" // for logging +#include "FairDetectorList.h" // for DetectorId::kTutDet +#include "FairLogger.h" #include "FairRootManager.h" // for FairRootManager #include "FairStack.h" // for FairStack #include "FairTutorialDet1Geo.h" // for FairTutorialDet1Geo @@ -50,7 +50,7 @@ void FairTutorialDet1::Initialize() fIsInitialised = true; } -Bool_t FairTutorialDet1::ProcessHits(FairVolume* vol) +void FairTutorialDet1::ProcessHits() { /** This method is called from the MC stepping */ @@ -71,9 +71,11 @@ Bool_t FairTutorialDet1::ProcessHits(FairVolume* vol) if (TVirtualMC::GetMC()->IsTrackExiting() || TVirtualMC::GetMC()->IsTrackStop() || TVirtualMC::GetMC()->IsTrackDisappeared()) { auto trackID = TVirtualMC::GetMC()->GetStack()->GetCurrentTrackNumber(); - auto volumeID = vol->getMCid(); + auto copyNo = 0; + auto volumeID = TVirtualMC::GetMC()->CurrentVolID(copyNo); + if (fELoss == 0.) { - return kFALSE; + return; } AddHit(trackID, volumeID, @@ -87,11 +89,15 @@ Bool_t FairTutorialDet1::ProcessHits(FairVolume* vol) auto stack = static_cast(TVirtualMC::GetMC()->GetStack()); stack->AddPoint(kTutDet); } - - return kTRUE; } -void FairTutorialDet1::EndOfEvent() { fFairTutorialDet1PointCollection->Clear(); } +void FairTutorialDet1::EndOfEvent() +{ + + LOG(info) << "FairTutorialDet1 : " << fFairTutorialDet1PointCollection->GetEntriesFast() << " points registered."; + + fFairTutorialDet1PointCollection->Clear(); +} void FairTutorialDet1::Register() { @@ -124,8 +130,8 @@ Bool_t FairTutorialDet1::IsSensitive(const std::string& name) void FairTutorialDet1::ConstructGeometry() { /** If you are using the standard ASCII input for the geometry - just copy this and use it for your detector, otherwise you can - implement here you own way of constructing the geometry. */ + just copy this and use it for your detector, otherwise you can + implement here you own way of constructing the geometry. */ ConstructASCIIGeometry("FairTutorialDet1GeoPar"); } diff --git a/examples/simulation/Tutorial1/src/FairTutorialDet1.h b/examples/simulation/Tutorial1/src/FairTutorialDet1.h index d9a640fe5e..0c1bc996e2 100644 --- a/examples/simulation/Tutorial1/src/FairTutorialDet1.h +++ b/examples/simulation/Tutorial1/src/FairTutorialDet1.h @@ -42,7 +42,7 @@ class FairTutorialDet1 : public FairDetector /** this method is called for each step during simulation * (see FairMCApplication::Stepping()) */ - Bool_t ProcessHits(FairVolume* v = nullptr) override; + void ProcessHits() override; /** Registers the produced collections in FAIRRootManager. */ void Register() override; diff --git a/examples/simulation/Tutorial2/src/FairTutorialDet2.cxx b/examples/simulation/Tutorial2/src/FairTutorialDet2.cxx index ea386449da..332efc4598 100644 --- a/examples/simulation/Tutorial2/src/FairTutorialDet2.cxx +++ b/examples/simulation/Tutorial2/src/FairTutorialDet2.cxx @@ -70,7 +70,7 @@ FairTutorialDet2::~FairTutorialDet2() } } -Bool_t FairTutorialDet2::ProcessHits(FairVolume* vol) +void FairTutorialDet2::ProcessHits() { /** This method is called from the MC stepping */ @@ -90,9 +90,10 @@ Bool_t FairTutorialDet2::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; } AddHit(fTrackID, fVolumeID, @@ -106,8 +107,6 @@ Bool_t FairTutorialDet2::ProcessHits(FairVolume* vol) FairStack* stack = static_cast(TVirtualMC::GetMC()->GetStack()); stack->AddPoint(kTutDet); } - - return kTRUE; } void FairTutorialDet2::EndOfEvent() { Reset(); } diff --git a/examples/simulation/Tutorial2/src/FairTutorialDet2.h b/examples/simulation/Tutorial2/src/FairTutorialDet2.h index 8e1a405cb9..6cced14876 100644 --- a/examples/simulation/Tutorial2/src/FairTutorialDet2.h +++ b/examples/simulation/Tutorial2/src/FairTutorialDet2.h @@ -39,7 +39,7 @@ class FairTutorialDet2 : 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(); diff --git a/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.cxx b/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.cxx index 65821aa8b0..f0fb44cb1e 100644 --- a/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.cxx +++ b/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.cxx @@ -152,7 +152,7 @@ void FairTutorialDet4::InitParContainers() fRotZ = fMisalignPar->GetRotZ(); } -Bool_t FairTutorialDet4::ProcessHits(FairVolume* /*vol*/) +void FairTutorialDet4::ProcessHits() { /** This method is called from the MC stepping */ @@ -172,10 +172,10 @@ Bool_t FairTutorialDet4::ProcessHits(FairVolume* /*vol*/) if (TVirtualMC::GetMC()->IsTrackExiting() || TVirtualMC::GetMC()->IsTrackStop() || TVirtualMC::GetMC()->IsTrackDisappeared()) { fTrackID = TVirtualMC::GetMC()->GetStack()->GetCurrentTrackNumber(); - // fVolumeID = vol->getMCid(); + // fVolumeID = vol->getMCid(); fVolumeID = fGeoHandler->GetUniqueDetectorId(); if (fELoss == 0.) { - return kFALSE; + return; } if (!fGlobalCoordinates) { @@ -206,8 +206,6 @@ Bool_t FairTutorialDet4::ProcessHits(FairVolume* /*vol*/) FairStack* stack = static_cast(TVirtualMC::GetMC()->GetStack()); stack->AddPoint(kTutDet); } - - return kTRUE; } void FairTutorialDet4::EndOfEvent() { fFairTutorialDet4PointCollection->Clear(); } diff --git a/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.h b/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.h index 4dcff9236c..a0df52f082 100644 --- a/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.h +++ b/examples/simulation/Tutorial4/src/mc/FairTutorialDet4.h @@ -50,7 +50,7 @@ class FairTutorialDet4 : 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(); diff --git a/examples/simulation/rutherford/src/FairRutherford.cxx b/examples/simulation/rutherford/src/FairRutherford.cxx index ccd2d097b4..25fbbc8164 100644 --- a/examples/simulation/rutherford/src/FairRutherford.cxx +++ b/examples/simulation/rutherford/src/FairRutherford.cxx @@ -42,7 +42,7 @@ FairRutherford::~FairRutherford() } } -Bool_t FairRutherford::ProcessHits(FairVolume* vol) +void FairRutherford::ProcessHits() { /** This method is called from the MC stepping */ @@ -62,9 +62,10 @@ Bool_t FairRutherford::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; } AddHit(fTrackID, fVolumeID, @@ -78,8 +79,6 @@ Bool_t FairRutherford::ProcessHits(FairVolume* vol) auto stack = static_cast(TVirtualMC::GetMC()->GetStack()); stack->AddPoint(kFairRutherford); } - - return kTRUE; } void FairRutherford::EndOfEvent() { fFairRutherfordPointCollection->Clear(); } diff --git a/examples/simulation/rutherford/src/FairRutherford.h b/examples/simulation/rutherford/src/FairRutherford.h index 218881d8bf..697774015e 100644 --- a/examples/simulation/rutherford/src/FairRutherford.h +++ b/examples/simulation/rutherford/src/FairRutherford.h @@ -36,9 +36,9 @@ class FairRutherford : public FairDetector ~FairRutherford() override; /** this method is called for each step during simulation - * (see FairMCApplication::Stepping()) + * (by the VMC) */ - Bool_t ProcessHits(FairVolume* v = nullptr) override; + void ProcessHits() override; /** Registers the produced collections in FAIRRootManager. */ void Register() override; diff --git a/templates/project_root_containers/NewDetector/NewDetector.cxx b/templates/project_root_containers/NewDetector/NewDetector.cxx index 1454fd5715..cc5139843b 100644 --- a/templates/project_root_containers/NewDetector/NewDetector.cxx +++ b/templates/project_root_containers/NewDetector/NewDetector.cxx @@ -92,7 +92,7 @@ void NewDetector::Initialize() (void)par; // stop warning about unused variable } -Bool_t NewDetector::ProcessHits(FairVolume* vol) +void NewDetector::ProcessHits() { /** This method is called from the MC stepping */ @@ -112,9 +112,10 @@ Bool_t NewDetector::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; } AddHit(fTrackID, fVolumeID, @@ -128,8 +129,6 @@ Bool_t NewDetector::ProcessHits(FairVolume* vol) MyProjStack* stack = (MyProjStack*)TVirtualMC::GetMC()->GetStack(); stack->AddPoint(kNewDetector); } - - return kTRUE; } void NewDetector::EndOfEvent() diff --git a/templates/project_root_containers/NewDetector/NewDetector.h b/templates/project_root_containers/NewDetector/NewDetector.h index e02458f704..a0f16208c7 100644 --- a/templates/project_root_containers/NewDetector/NewDetector.h +++ b/templates/project_root_containers/NewDetector/NewDetector.h @@ -39,7 +39,7 @@ class NewDetector : 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(); diff --git a/templates/project_stl_containers/NewDetector/NewDetector.cxx b/templates/project_stl_containers/NewDetector/NewDetector.cxx index fdb28440d7..7d99e8d3d3 100644 --- a/templates/project_stl_containers/NewDetector/NewDetector.cxx +++ b/templates/project_stl_containers/NewDetector/NewDetector.cxx @@ -91,7 +91,7 @@ void NewDetector::Initialize() (void)par; // stop warning about unused variable } -Bool_t NewDetector::ProcessHits(FairVolume* vol) +void NewDetector::ProcessHits() { /** This method is called from the MC stepping */ @@ -111,9 +111,10 @@ Bool_t NewDetector::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; } AddHit(fTrackID, fVolumeID, @@ -127,8 +128,6 @@ Bool_t NewDetector::ProcessHits(FairVolume* vol) MyProjStack* stack = (MyProjStack*)TVirtualMC::GetMC()->GetStack(); stack->AddPoint(kNewDetector); } - - return kTRUE; } void NewDetector::EndOfEvent() diff --git a/templates/project_stl_containers/NewDetector/NewDetector.h b/templates/project_stl_containers/NewDetector/NewDetector.h index 347f5ada58..78e337fd58 100644 --- a/templates/project_stl_containers/NewDetector/NewDetector.h +++ b/templates/project_stl_containers/NewDetector/NewDetector.h @@ -39,7 +39,7 @@ class NewDetector : 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(); From 0197d72d9863bf31c655751370109a4a69d9aa9e Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Thu, 27 Jan 2022 17:59:45 +0100 Subject: [PATCH 2/9] Fix Pixel detector Remove the double function definition. --- examples/MQ/pixelDetector/src/Pixel.cxx | 8 -------- 1 file changed, 8 deletions(-) diff --git a/examples/MQ/pixelDetector/src/Pixel.cxx b/examples/MQ/pixelDetector/src/Pixel.cxx index 33c319afbd..fe96881113 100644 --- a/examples/MQ/pixelDetector/src/Pixel.cxx +++ b/examples/MQ/pixelDetector/src/Pixel.cxx @@ -128,14 +128,6 @@ void Pixel::ProcessHits() } } -void Pixel::EndOfEvent() -{ - - fPixelPointCollection->Clear(); - - return kTRUE; -} - void Pixel::EndOfEvent() { fPixelPointCollection->Clear(); } void Pixel::Register() From da87fd57b9b9d6f7d0e51e4183f9154e2c856ef5 Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Thu, 27 Jan 2022 18:01:14 +0100 Subject: [PATCH 3/9] Update the feature_sensitive_volumes PR Define the void FairDetector::ProcessHits() function, which calls the depracated Bool_t ProcessHits(FairVolume*) of derived classes. Update the FairMCApplication to match the update of the VMC: introduction of EndOfEvent function called before the TVirtualMCSensitiveDetectors->EndOfEvent(). Implement the temporary FairMCApplication::GetFairVolume() function to keep the backward compability. Cleanup of FairModule, FairMCApplication double code. --- base/sim/FairDetector.cxx | 16 ++++++ base/sim/FairDetector.h | 8 ++- base/sim/FairMCApplication.cxx | 102 ++++++++++++++++++++------------- base/sim/FairMCApplication.h | 9 ++- base/sim/FairModule.h | 5 -- 5 files changed, 93 insertions(+), 47 deletions(-) diff --git a/base/sim/FairDetector.cxx b/base/sim/FairDetector.cxx index 71542f29ac..ea7809f8e3 100644 --- a/base/sim/FairDetector.cxx +++ b/base/sim/FairDetector.cxx @@ -14,6 +14,7 @@ #include "FairGeoNode.h" // for FairGeoNode #include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN +#include "FairMCApplication.h" // TEMPORARY until the depracated Bool_t ProcessHits() in use #include "FairModule.h" // for FairModule::svList, etc #include "FairRootManager.h" #include "FairVolume.h" // for FairVolume @@ -124,6 +125,21 @@ void FairDetector::Initialize() fMC = TVirtualMC::GetMC(); } +// ------------------------------------------------------------------------- + +void FairDetector::ProcessHits() +{ + static const auto printOnce = [this] { + LOG(warning) << "Calling depracated Bool_t FairDetector::ProcessHits(FairVolume* vol = 0) for \"" << GetName() << "\"."; + LOG(warning) << " Replace with void FairDetector::ProcessHits(). "; + return true; + } (); + ProcessHits(FairMCApplication::Instance()->GetFairVolume()); + return; +} + +// ------------------------------------------------------------------------- + void FairDetector::SaveGeoParams() { if (!fGeoSaved) { diff --git a/base/sim/FairDetector.h b/base/sim/FairDetector.h index 164b84d7c7..891e314805 100644 --- a/base/sim/FairDetector.h +++ b/base/sim/FairDetector.h @@ -45,9 +45,13 @@ class FairDetector : public FairModule */ virtual void Initialize(); /** - this method is called for each step during simulation (see FairMCApplication::Stepping()) + this method is called for each step during simulation by the VMC */ - virtual void ProcessHits()=0; + virtual void ProcessHits(); + /** + DEPRACTED method, currently called by void FairDetector::ProcessHits() + */ + virtual Bool_t ProcessHits(FairVolume* v = 0) { return false; } /** this is called at the end of an event after the call to tree fill in the FairRootManager */ diff --git a/base/sim/FairMCApplication.cxx b/base/sim/FairMCApplication.cxx index 211d93103c..d03d685282 100644 --- a/base/sim/FairMCApplication.cxx +++ b/base/sim/FairMCApplication.cxx @@ -529,36 +529,6 @@ void FairMCApplication::Stepping() Int_t copyNo; Int_t id = fMC->CurrentVolID(copyNo); - // 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 @@ -633,13 +603,10 @@ void FairMCApplication::StopMCRun() } //_____________________________________________________________________________ -void FairMCApplication::FinishEvent() +void FairMCApplication::EndOfEvent() { - // User actions after finishing of an event + // User actions just before 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); @@ -657,15 +624,26 @@ void FairMCApplication::FinishEvent() fFairTaskList->FinishEvent(); } - for (auto detectorPtr : listActiveDetectors) { - detectorPtr->FinishEvent(); - } - if (fRootManager && fSaveCurrentEvent) { fRootManager->Fill(); } else { fSaveCurrentEvent = kTRUE; } +} + +//_____________________________________________________________________________ +void FairMCApplication::FinishEvent() +{ + // User actions after finishing of an event + // --- + LOG(debug) << "[" << fRootManager->GetInstanceId() + << " FairMCMCApplication::FinishEvent: " << fMCEventHeader->GetEventID() << " (MC " + << gMC->CurrentEvent() << ")"; + + for (auto detectorPtr : listActiveDetectors) { + detectorPtr->FinishEvent(); + } + fStack->Reset(); if (nullptr != fTrajFilter) { fTrajFilter->Reset(); @@ -1325,4 +1303,50 @@ void FairMCApplication::AddSensitiveModule(std::string volName, FairModule* modu fMapSensitiveDetectors[volName] = module; } +FairVolume* FairMCApplication::GetFairVolume() +{ + // 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); + fDisVol = 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) { + return fDisVol; + } + ++fVolIter; + } while (fVolIter != fVolMap.upper_bound(id)); + + // 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 (?) + // 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(id, fNewV)); + + return fNewV; + } + return 0; +} + ClassImp(FairMCApplication) diff --git a/base/sim/FairMCApplication.h b/base/sim/FairMCApplication.h index e033c1d37f..e0b6b3009a 100644 --- a/base/sim/FairMCApplication.h +++ b/base/sim/FairMCApplication.h @@ -109,6 +109,8 @@ class FairMCApplication : public TVirtualMCApplication virtual void ConstructSensitiveDetectors(); + /** Define actions just before sensitive->EndOfEvent */ + virtual void EndOfEvent(); // MC Application /** Define actions at the end of event */ void FinishEvent() override; // MC Application /** Define actions at the end of primary track */ @@ -230,7 +232,7 @@ class FairMCApplication : public TVirtualMCApplication * Add module to the list of sensitive detectors. */ void AddSensitiveModule(std::string volName, FairModule* module); - + /** * Return non-owning pointer to FairRadGridManager */ @@ -241,6 +243,11 @@ class FairMCApplication : public TVirtualMCApplication */ auto GetIsMT() { return fMC ? fMC->IsMT() : false; } + /** + * Method introduced temporarily. It should go awway with DEPRACATED Bool_t FairDetector::ProcessHits() + */ + FairVolume* GetFairVolume(); + private: // methods Int_t GetIonPdg(Int_t z, Int_t a) const; diff --git a/base/sim/FairModule.h b/base/sim/FairModule.h index 8efd925c22..2437c8a598 100644 --- a/base/sim/FairModule.h +++ b/base/sim/FairModule.h @@ -149,11 +149,6 @@ class FairModule : public TVirtualMCSensitiveDetector 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() {} From 9af52a83dbc30c2b0ed55eab567e52759dbfb423 Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Tue, 1 Feb 2022 10:16:16 +0100 Subject: [PATCH 4/9] Apply format --- base/sim/FairDetector.cxx | 13 +++++++------ base/sim/FairMCApplication.cxx | 8 +++----- base/sim/FairMCApplication.h | 4 ++-- base/sim/fastsim/FairFastSimDetector.cxx | 5 +---- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/base/sim/FairDetector.cxx b/base/sim/FairDetector.cxx index ea7809f8e3..12fb8bcc0b 100644 --- a/base/sim/FairDetector.cxx +++ b/base/sim/FairDetector.cxx @@ -12,10 +12,10 @@ #include "FairDetector.h" -#include "FairGeoNode.h" // for FairGeoNode -#include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN -#include "FairMCApplication.h" // TEMPORARY until the depracated Bool_t ProcessHits() in use -#include "FairModule.h" // for FairModule::svList, etc +#include "FairGeoNode.h" // for FairGeoNode +#include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN +#include "FairMCApplication.h" // TEMPORARY until the depracated Bool_t ProcessHits() in use +#include "FairModule.h" // for FairModule::svList, etc #include "FairRootManager.h" #include "FairVolume.h" // for FairVolume @@ -130,10 +130,11 @@ void FairDetector::Initialize() void FairDetector::ProcessHits() { static const auto printOnce = [this] { - LOG(warning) << "Calling depracated Bool_t FairDetector::ProcessHits(FairVolume* vol = 0) for \"" << GetName() << "\"."; + LOG(warning) << "Calling depracated Bool_t FairDetector::ProcessHits(FairVolume* vol = 0) for \"" << GetName() + << "\"."; LOG(warning) << " Replace with void FairDetector::ProcessHits(). "; return true; - } (); + }(); ProcessHits(FairMCApplication::Instance()->GetFairVolume()); return; } diff --git a/base/sim/FairMCApplication.cxx b/base/sim/FairMCApplication.cxx index d03d685282..724ffbf641 100644 --- a/base/sim/FairMCApplication.cxx +++ b/base/sim/FairMCApplication.cxx @@ -528,7 +528,7 @@ void FairMCApplication::Stepping() Int_t copyNo; Int_t id = fMC->CurrentVolID(copyNo); - + // 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 @@ -1290,10 +1290,8 @@ void FairMCApplication::UndoGeometryModifications() void FairMCApplication::ConstructSensitiveDetectors() { - for(auto const& x : fMapSensitiveDetectors) - { - LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors " - << x.first << " " << x.second; + for (auto const& x : fMapSensitiveDetectors) { + LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors " << x.first << " " << x.second; TVirtualMC::GetMC()->SetSensitiveDetector(x.first, x.second); } } diff --git a/base/sim/FairMCApplication.h b/base/sim/FairMCApplication.h index e0b6b3009a..61d0cea025 100644 --- a/base/sim/FairMCApplication.h +++ b/base/sim/FairMCApplication.h @@ -227,7 +227,7 @@ class FairMCApplication : public TVirtualMCApplication * Get the current application state. */ FairMCApplicationState GetState() const { return fState; } - + /** * Add module to the list of sensitive detectors. */ @@ -334,7 +334,7 @@ class FairMCApplication : public TVirtualMCApplication /** Current state */ FairMCApplicationState fState; //! - + /** List of sensitive detectors. * To be used with TVirtualMCSensitiveDetector. */ std::map fMapSensitiveDetectors; diff --git a/base/sim/fastsim/FairFastSimDetector.cxx b/base/sim/fastsim/FairFastSimDetector.cxx index a48e0f4c07..2978b89ae5 100644 --- a/base/sim/fastsim/FairFastSimDetector.cxx +++ b/base/sim/fastsim/FairFastSimDetector.cxx @@ -59,9 +59,6 @@ void FairFastSimDetector::ConstructGeometry() } } -void FairFastSimDetector::ProcessHits() -{ - FastSimProcessParticle(); -} +void FairFastSimDetector::ProcessHits() { FastSimProcessParticle(); } ClassImp(FairFastSimDetector); From 6196ba08e30c5ffee5afa4719d6019c7cfa3a7ce Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Fri, 6 May 2022 13:44:35 +0200 Subject: [PATCH 5/9] Fix the construction of cloned sensitive volumes FairRoot allows for construction of clones volumes using the volume_name#{clone_number} scheme. Changing FairMCApplication::ConstructSensitiveDetectors() to support this. --- base/sim/FairMCApplication.cxx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/base/sim/FairMCApplication.cxx b/base/sim/FairMCApplication.cxx index 724ffbf641..7870458f9f 100644 --- a/base/sim/FairMCApplication.cxx +++ b/base/sim/FairMCApplication.cxx @@ -1290,9 +1290,22 @@ void FairMCApplication::UndoGeometryModifications() void FairMCApplication::ConstructSensitiveDetectors() { + std::map cloneVolumeMap; + for (auto const& x : fMapSensitiveDetectors) { - LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors " << x.first << " " << x.second; - TVirtualMC::GetMC()->SetSensitiveDetector(x.first, x.second); + std::string volName = x.first; //.substr(0, x.first.find("#", 0)); + if (volName.find('#') != std::string::npos) { + volName = volName.substr(0, volName.find("#", 0)); + std::map::iterator it; + it = cloneVolumeMap.find(volName); + LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors got clone " << x.first << " " << x.second; + if (it != cloneVolumeMap.end()) + continue; + cloneVolumeMap[volName] = x.second; + LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors really do " << volName; + } + LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors really do " << volName; + TVirtualMC::GetMC()->SetSensitiveDetector(volName, x.second); } } From 3938cbd392d96fae9b4fa6a42c6b912584e87e43 Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Fri, 6 May 2022 13:51:59 +0200 Subject: [PATCH 6/9] Override two functions in FairMCApplication --- base/sim/FairMCApplication.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/base/sim/FairMCApplication.h b/base/sim/FairMCApplication.h index 61d0cea025..fd0153b4b8 100644 --- a/base/sim/FairMCApplication.h +++ b/base/sim/FairMCApplication.h @@ -107,10 +107,11 @@ class FairMCApplication : public TVirtualMCApplication /** Define parameters for optical processes (optional) */ void ConstructOpGeometry() override; // MC Application - virtual void ConstructSensitiveDetectors(); + /** set sensitive detectors following TVirtualMCSensitiveDetector logic */ + void ConstructSensitiveDetectors() override; // MC Application /** Define actions just before sensitive->EndOfEvent */ - virtual void EndOfEvent(); // MC Application + void EndOfEvent() override; // MC Application /** Define actions at the end of event */ void FinishEvent() override; // MC Application /** Define actions at the end of primary track */ From 9b9d61128ee6b281b31098bdb83c7960b81bb8b7 Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Wed, 8 Jun 2022 10:02:20 +0200 Subject: [PATCH 7/9] Fix the double initialization of FairDetector Since VMC is initializing TVirtualMCSensitiveDetector, removed detector->Initialize() from FairMCApplication. Removed the GetFairVolume function used by FairDetector::ProcessHits(). It now calls the deprecated function with NULL argument. Changed the propagator example to use the new ProcessHits(). --- base/sim/FairDetector.cxx | 10 ++-- base/sim/FairMCApplication.cxx | 52 +------------------ base/sim/FairMCApplication.h | 2 +- base/sim/FairModule.h | 6 +-- base/sim/fastsim/FairFastSimDetector.cxx | 5 +- .../propagator/src/FairTutPropDet.cxx | 9 ++-- .../advanced/propagator/src/FairTutPropDet.h | 2 +- 7 files changed, 21 insertions(+), 65 deletions(-) diff --git a/base/sim/FairDetector.cxx b/base/sim/FairDetector.cxx index 12fb8bcc0b..096f21a64a 100644 --- a/base/sim/FairDetector.cxx +++ b/base/sim/FairDetector.cxx @@ -12,10 +12,9 @@ #include "FairDetector.h" -#include "FairGeoNode.h" // for FairGeoNode -#include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN -#include "FairMCApplication.h" // TEMPORARY until the depracated Bool_t ProcessHits() in use -#include "FairModule.h" // for FairModule::svList, etc +#include "FairGeoNode.h" // for FairGeoNode +#include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN +#include "FairModule.h" // for FairModule::svList, etc #include "FairRootManager.h" #include "FairVolume.h" // for FairVolume @@ -135,7 +134,8 @@ void FairDetector::ProcessHits() LOG(warning) << " Replace with void FairDetector::ProcessHits(). "; return true; }(); - ProcessHits(FairMCApplication::Instance()->GetFairVolume()); + (void)printOnce; + ProcessHits(NULL); return; } diff --git a/base/sim/FairMCApplication.cxx b/base/sim/FairMCApplication.cxx index 7870458f9f..401f14edae 100644 --- a/base/sim/FairMCApplication.cxx +++ b/base/sim/FairMCApplication.cxx @@ -526,9 +526,6 @@ void FairMCApplication::Stepping() TrackId = fMC->GetStack()->GetCurrentTrackNumber(); } - Int_t copyNo; - Int_t id = fMC->CurrentVolID(copyNo); - // 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 @@ -546,6 +543,8 @@ void FairMCApplication::Stepping() } } if (fRadLenMan || fRadMapMan) { + Int_t copyNo; + Int_t id = fMC->CurrentVolID(copyNo); id = fMC->CurrentVolID(copyNo); auto modvoliter = (fParent ? fParent : this)->fModVolMap.find(id); if (fRadLenMan) { @@ -1313,51 +1312,4 @@ void FairMCApplication::AddSensitiveModule(std::string volName, FairModule* modu { fMapSensitiveDetectors[volName] = module; } - -FairVolume* FairMCApplication::GetFairVolume() -{ - // 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); - fDisVol = 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) { - return fDisVol; - } - ++fVolIter; - } while (fVolIter != fVolMap.upper_bound(id)); - - // 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 (?) - // 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(id, fNewV)); - - return fNewV; - } - return 0; -} - ClassImp(FairMCApplication) diff --git a/base/sim/FairMCApplication.h b/base/sim/FairMCApplication.h index fd0153b4b8..949d84f136 100644 --- a/base/sim/FairMCApplication.h +++ b/base/sim/FairMCApplication.h @@ -233,7 +233,7 @@ class FairMCApplication : public TVirtualMCApplication * Add module to the list of sensitive detectors. */ void AddSensitiveModule(std::string volName, FairModule* module); - + /** * Return non-owning pointer to FairRadGridManager */ diff --git a/base/sim/FairModule.h b/base/sim/FairModule.h index 2437c8a598..10ebcab622 100644 --- a/base/sim/FairModule.h +++ b/base/sim/FairModule.h @@ -149,11 +149,11 @@ class FairModule : public TVirtualMCSensitiveDetector FairVolume* getFairVolume(FairGeoNode* fNode); void AddSensitiveVolume(TGeoVolume* v); - virtual void EndOfEvent() {} + void EndOfEvent() override {} - virtual void Initialize() {} + void Initialize() override {} - virtual void ProcessHits() {} + void ProcessHits() override {} private: /** Re-implimented from ROOT: TGeoMatrix::SetDefaultName() */ diff --git a/base/sim/fastsim/FairFastSimDetector.cxx b/base/sim/fastsim/FairFastSimDetector.cxx index 2978b89ae5..a48e0f4c07 100644 --- a/base/sim/fastsim/FairFastSimDetector.cxx +++ b/base/sim/fastsim/FairFastSimDetector.cxx @@ -59,6 +59,9 @@ void FairFastSimDetector::ConstructGeometry() } } -void FairFastSimDetector::ProcessHits() { FastSimProcessParticle(); } +void FairFastSimDetector::ProcessHits() +{ + FastSimProcessParticle(); +} ClassImp(FairFastSimDetector); diff --git a/examples/advanced/propagator/src/FairTutPropDet.cxx b/examples/advanced/propagator/src/FairTutPropDet.cxx index a4a0e8e985..fba4678af1 100644 --- a/examples/advanced/propagator/src/FairTutPropDet.cxx +++ b/examples/advanced/propagator/src/FairTutPropDet.cxx @@ -62,7 +62,7 @@ FairTutPropDet::~FairTutPropDet() } } -Bool_t FairTutPropDet::ProcessHits(FairVolume* vol) +void FairTutPropDet::ProcessHits() { /** This method is called from the MC stepping */ @@ -82,10 +82,11 @@ Bool_t FairTutPropDet::ProcessHits(FairVolume* vol) if (TVirtualMC::GetMC()->IsTrackExiting() || TVirtualMC::GetMC()->IsTrackStop() || TVirtualMC::GetMC()->IsTrackDisappeared()) { fTrackID = TVirtualMC::GetMC()->GetStack()->GetCurrentTrackNumber(); - fVolumeID = vol->getMCid(); + Int_t copyNo; + fVolumeID = fMC->CurrentVolID(copyNo); if (fELoss == 0.) { - return kFALSE; + return; } // Taking stationNr from string is almost effortless. @@ -110,7 +111,7 @@ Bool_t FairTutPropDet::ProcessHits(FairVolume* vol) stack->AddPoint(kTutProp); } - return kTRUE; + return; } void FairTutPropDet::EndOfEvent() { fFairTutPropPointCollection->Clear(); } diff --git a/examples/advanced/propagator/src/FairTutPropDet.h b/examples/advanced/propagator/src/FairTutPropDet.h index 0f5f3bb183..d8321dd6dc 100644 --- a/examples/advanced/propagator/src/FairTutPropDet.h +++ b/examples/advanced/propagator/src/FairTutPropDet.h @@ -35,7 +35,7 @@ class FairTutPropDet : public FairDetector /** this method is called for each step during simulation * (see FairMCApplication::Stepping()) */ - Bool_t ProcessHits(FairVolume* v = nullptr) override; + void ProcessHits() override; /** Registers the produced collections in FAIRRootManager. */ void Register() override; From 17b1c99ca814deb2c8590d81757ee7c4f5b4b05a Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Mon, 30 Jan 2023 15:22:15 +0100 Subject: [PATCH 8/9] TVirtualMCSensitiveDetector: Cleanup of functions Removed FairDetector::DefineSensitiveVolumes() function, the functionality moved to FairMCApplication private special copy constructor. Removed detector initialization from FairMCApplication::RegisterOutput(), the detectors are initialized in the VMC. Added few overrides for ProcessHits. --- base/sim/FairDetector.cxx | 29 ++++--------------- base/sim/FairDetector.h | 4 +-- base/sim/FairMCApplication.cxx | 10 ++++--- base/sim/FairMCApplication.h | 10 ++++--- base/sim/FairModule.h | 3 +- examples/MQ/pixelDetector/src/Pixel.h | 2 +- .../Tutorial3/simulation/FairTestDetector.h | 2 +- .../propagator/src/FairTutPropDet.cxx | 2 -- 8 files changed, 22 insertions(+), 40 deletions(-) diff --git a/base/sim/FairDetector.cxx b/base/sim/FairDetector.cxx index 096f21a64a..f4e61daa0b 100644 --- a/base/sim/FairDetector.cxx +++ b/base/sim/FairDetector.cxx @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -45,7 +45,10 @@ FairDetector::FairDetector(const FairDetector& rhs) , fLogger(rhs.fLogger) {} -FairDetector::~FairDetector() { delete flGeoPar; } +FairDetector::~FairDetector() +{ + delete flGeoPar; +} FairDetector& FairDetector::operator=(const FairDetector& rhs) { @@ -70,34 +73,12 @@ FairDetector::FairDetector() // ------------------------------------------------------------------------- -void FairDetector::DefineSensitiveVolumes() -{ - LOG(info) << "FairDetector::DefineSensitiveVolumes"; - TObjArray* volumes = gGeoManager->GetListOfVolumes(); - TIter next(volumes); - TGeoVolume* volume; - while ((volume = static_cast(next()))) { - if (IsSensitive(volume->GetName())) { - LOG(debug) << "Sensitive Volume " << volume->GetName(); - AddSensitiveVolume(volume); - } - } -} - -// ------------------------------------------------------------------------- - void FairDetector::Initialize() { // Registers hits collection in Root manager; // sets sensitive volumes. // --- - // Define sensitive volumes if in MT - if (gMC->IsMT()) { - std::cout << "Define sensitive volume " << std::endl; - DefineSensitiveVolumes(); - } - Int_t NoOfEntries = svList->GetEntries(); Int_t fMCid; FairGeoNode* fN; diff --git a/base/sim/FairDetector.h b/base/sim/FairDetector.h index 891e314805..6ddbc9266c 100644 --- a/base/sim/FairDetector.h +++ b/base/sim/FairDetector.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -109,8 +109,6 @@ class FairDetector : public FairModule /** Assignment operator */ FairDetector& operator=(const FairDetector&); - void DefineSensitiveVolumes(); - Int_t fDetId; // Detector Id has to be set from ctr. FairLogger* fLogger; //! /// FairLogger diff --git a/base/sim/FairMCApplication.cxx b/base/sim/FairMCApplication.cxx index 401f14edae..f704eea809 100644 --- a/base/sim/FairMCApplication.cxx +++ b/base/sim/FairMCApplication.cxx @@ -196,6 +196,11 @@ FairMCApplication::FairMCApplication(const FairMCApplication& rhs, std::unique_p auto& clone = fOwnedModules.emplace_back(module->CloneModule()); fListModules.emplace_back(clone.get()); fModules->Add(clone.get()); + for (auto sens : rhs.fMapSensitiveDetectors) { + if (sens.second == module) { + fMapSensitiveDetectors[sens.first] = clone.get(); + } + } } // Create and fill a list of active detectors @@ -545,7 +550,6 @@ void FairMCApplication::Stepping() if (fRadLenMan || fRadMapMan) { Int_t copyNo; Int_t id = fMC->CurrentVolID(copyNo); - id = fMC->CurrentVolID(copyNo); auto modvoliter = (fParent ? fParent : this)->fModVolMap.find(id); if (fRadLenMan) { fRadLenMan->AddPoint(fMC, modvoliter->second); @@ -932,7 +936,6 @@ void FairMCApplication::RegisterOutput() if (detector) { // check whether detector is active if (detector->IsActive()) { - detector->Initialize(); detector->Register(); } } @@ -1295,8 +1298,7 @@ void FairMCApplication::ConstructSensitiveDetectors() std::string volName = x.first; //.substr(0, x.first.find("#", 0)); if (volName.find('#') != std::string::npos) { volName = volName.substr(0, volName.find("#", 0)); - std::map::iterator it; - it = cloneVolumeMap.find(volName); + auto it = cloneVolumeMap.find(volName); LOG(debug) << "FairMCApplication::ConstructSensitiveDetectors got clone " << x.first << " " << x.second; if (it != cloneVolumeMap.end()) continue; diff --git a/base/sim/FairMCApplication.h b/base/sim/FairMCApplication.h index 949d84f136..df87e75db7 100644 --- a/base/sim/FairMCApplication.h +++ b/base/sim/FairMCApplication.h @@ -336,10 +336,6 @@ class FairMCApplication : public TVirtualMCApplication /** Current state */ FairMCApplicationState fState; //! - /** List of sensitive detectors. - * To be used with TVirtualMCSensitiveDetector. */ - std::map fMapSensitiveDetectors; - ClassDefOverride(FairMCApplication, 5); private: @@ -354,6 +350,12 @@ class FairMCApplication : public TVirtualMCApplication */ std::vector fListModules{}; //! + /** + * List of sensitive detectors. + * To be used with TVirtualMCSensitiveDetector. + */ + std::map fMapSensitiveDetectors; + /** * Owned Modules (inside the worker) */ diff --git a/base/sim/FairModule.h b/base/sim/FairModule.h index 10ebcab622..046a7e193f 100644 --- a/base/sim/FairModule.h +++ b/base/sim/FairModule.h @@ -15,13 +15,14 @@ #include "FairLogger.h" #include "FairRun.h" // for FairRun #include "FairRuntimeDb.h" // for FairRuntimeDb -#include "TVirtualMCSensitiveDetector.h" #include // for Bool_t, Int_t, etc #include // for TList (ptr only), TListIter #include // for TNamed #include // for TObjArray #include // for TString, operator!= +#include + #include // for string class FairVolumeList; diff --git a/examples/MQ/pixelDetector/src/Pixel.h b/examples/MQ/pixelDetector/src/Pixel.h index 512a6b6e9b..9fd65d7475 100644 --- a/examples/MQ/pixelDetector/src/Pixel.h +++ b/examples/MQ/pixelDetector/src/Pixel.h @@ -36,7 +36,7 @@ class Pixel : public FairDetector /** this method is called for each step during simulation * (see FairMCApplication::Stepping()) */ - virtual void ProcessHits(); + void ProcessHits() override; /** Registers the produced collections in FAIRRootManager. */ void Register() override; diff --git a/examples/advanced/Tutorial3/simulation/FairTestDetector.h b/examples/advanced/Tutorial3/simulation/FairTestDetector.h index 93ad12b2e5..1cb47bc4d5 100644 --- a/examples/advanced/Tutorial3/simulation/FairTestDetector.h +++ b/examples/advanced/Tutorial3/simulation/FairTestDetector.h @@ -39,7 +39,7 @@ class FairTestDetector : public FairDetector /** this method is called for each step during simulation * (see FairMCApplication::Stepping()) */ - virtual void ProcessHits(); + void ProcessHits() override; /** Registers the produced collections in FAIRRootManager. */ void Register() override; diff --git a/examples/advanced/propagator/src/FairTutPropDet.cxx b/examples/advanced/propagator/src/FairTutPropDet.cxx index fba4678af1..e3ff4a7c85 100644 --- a/examples/advanced/propagator/src/FairTutPropDet.cxx +++ b/examples/advanced/propagator/src/FairTutPropDet.cxx @@ -110,8 +110,6 @@ void FairTutPropDet::ProcessHits() FairStack* stack = static_cast(TVirtualMC::GetMC()->GetStack()); stack->AddPoint(kTutProp); } - - return; } void FairTutPropDet::EndOfEvent() { fFairTutPropPointCollection->Clear(); } From e34b57480439a1b37058dd4a812e41ca7217a2d3 Mon Sep 17 00:00:00 2001 From: Radoslaw Karabowicz Date: Wed, 1 Feb 2023 10:18:28 +0100 Subject: [PATCH 9/9] TVirtualMCSensitiveDetector: example/Tutorial1 fixup Removed the IsSensitive() function. Increase the number of events to run in the MT mode to 20. --- examples/simulation/Tutorial1/macros/CMakeLists.txt | 4 ++-- .../simulation/Tutorial1/src/FairTutorialDet1.cxx | 13 +++++++------ .../simulation/Tutorial1/src/FairTutorialDet1.h | 4 +--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/examples/simulation/Tutorial1/macros/CMakeLists.txt b/examples/simulation/Tutorial1/macros/CMakeLists.txt index 65c1eec79b..32c489dff3 100644 --- a/examples/simulation/Tutorial1/macros/CMakeLists.txt +++ b/examples/simulation/Tutorial1/macros/CMakeLists.txt @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (C) 2014-2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # +# Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # # # # This software is distributed under the terms of the # # GNU Lesser General Public Licence (LGPL) version 3, # @@ -52,7 +52,7 @@ foreach(mcEngine IN LISTS mcEngine_list) endforeach() add_test(NAME ex_tutorial1_Geant4MT - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/run_tutorial1.sh 10 \"TGeant4\" true false) + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/run_tutorial1.sh 20 \"TGeant4\" true false) set_tests_properties(ex_tutorial1_Geant4MT PROPERTIES TIMEOUT ${maxTestTime} PASS_REGULAR_EXPRESSION "Simulation successful." diff --git a/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx b/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx index f614ed7bc2..59b485f996 100644 --- a/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx +++ b/examples/simulation/Tutorial1/src/FairTutorialDet1.cxx @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2022 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -120,11 +120,9 @@ TClonesArray* FairTutorialDet1::GetCollection(Int_t iColl) const } } -void FairTutorialDet1::Reset() { fFairTutorialDet1PointCollection->Clear(); } - -Bool_t FairTutorialDet1::IsSensitive(const std::string& name) +void FairTutorialDet1::Reset() { - return name.find("tutdet") != std::string::npos; + fFairTutorialDet1PointCollection->Clear(); } void FairTutorialDet1::ConstructGeometry() @@ -149,6 +147,9 @@ FairTutorialDet1Point* FairTutorialDet1::AddHit(Int_t trackID, return new (clref[size]) FairTutorialDet1Point(trackID, detID, pos, mom, time, length, eLoss); } -FairModule* FairTutorialDet1::CloneModule() const { return new FairTutorialDet1(*this); } +FairModule* FairTutorialDet1::CloneModule() const +{ + return new FairTutorialDet1(*this); +} ClassImp(FairTutorialDet1); diff --git a/examples/simulation/Tutorial1/src/FairTutorialDet1.h b/examples/simulation/Tutorial1/src/FairTutorialDet1.h index 0c1bc996e2..957ee38455 100644 --- a/examples/simulation/Tutorial1/src/FairTutorialDet1.h +++ b/examples/simulation/Tutorial1/src/FairTutorialDet1.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2022 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -79,8 +79,6 @@ class FairTutorialDet1 : public FairDetector FairModule* CloneModule() const override; - Bool_t IsSensitive(const std::string& name) override; - private: /** Track information to be stored until the track leaves the active volume.