15#ifndef ALICEO2_BASE_DETECTOR_H_
16#define ALICEO2_BASE_DETECTOR_H_
19#include <tbb/concurrent_unordered_map.h>
21#include <initializer_list>
43#include <fairmq/FwdDecls.h>
69 void Medium(Int_t numed,
const char*
name, Int_t nmat, Int_t isvol, Int_t ifield,
Float_t fieldm,
74 void SpecialCuts(Int_t numed,
const std::initializer_list<std::pair<ECut, Float_t>>& parIDValMap);
78 void SpecialProcesses(Int_t numed,
const std::initializer_list<std::pair<EProc, int>>& parIDValMap);
90 void Matrix(Int_t& nmat, Double_t theta1, Double_t phi1, Double_t theta2, Double_t phi2, Double_t theta3,
95 mDensityFactor = density;
100 return mDensityFactor;
119 virtual void defineLayer(Int_t nlay, Double_t phi0, Double_t
r, Int_t nladd, Int_t nmod,
120 Double_t lthick = 0., Double_t dthick = 0., UInt_t detType = 0, Int_t buildFlag = 0);
122 virtual void defineLayerTurbo(Int_t nlay, Double_t phi0, Double_t
r, Int_t nladd, Int_t nmod,
123 Double_t
width, Double_t tilt, Double_t lthick = 0., Double_t dthick = 0.,
124 UInt_t detType = 0, Int_t buildFlag = 0);
131 return mgr.getMaterialID(GetName(), imat);
139 return mgr.getMediumID(GetName(), imed);
147 mgr.getMediumIDMappingAsVector(GetName(), mapping);
153 std::string
s(GetName());
171 const auto iter = indexmapping.find(trackID);
172 return iter != indexmapping.end() ? iter->second : -1;
178 static int offsetTrackIndex(
int trackID,
int nprimaries,
int primaryOffset,
int secondaryOffset)
183 return trackID + (trackID < nprimaries ? primaryOffset : secondaryOffset);
188 virtual void attachHits(fair::mq::Channel&, fair::mq::Parts&) = 0;
193 std::vector<int>
const& trackoffsets,
194 std::vector<int>
const& nprimaries,
195 std::vector<int>
const& subevtsOrdered) = 0;
200 virtual void mergeHitEntries(TTree&
origin, TTree&
target, std::vector<int>
const& trackoffsets, std::vector<int>
const& nprimaries, std::vector<int>
const& subevtsOrdered) = 0;
235 [[deprecated(
"Use getHits API on concrete detectors!")]] TClonesArray*
GetCollection(
int iColl)
const final;
256 std::map<int, int> mMapMaterial;
259 std::map<int, int> mMapMedium;
263 static std::vector<int> sDetId2HitBitIndex;
272 std::unique_ptr<
char,
void (*)(
void*)>
res{abi::__cxa_demangle(
name,
nullptr,
nullptr, &status), std::free};
273 return (status == 0) ?
res.get() :
name;
291template <
typename Container>
292void attachTMessage(Container
const& hits, fair::mq::Channel& channel, fair::mq::Parts& parts)
315 if (
auto br =
tree.GetBranch(brname)) {
316 br->SetAddress(
static_cast<void*
>(&
ptr));
320 return tree.Branch(brname,
ptr);
324template <
typename Det>
326 static constexpr bool value =
false;
333template <
typename Det>
350 return std::string();
360 while (
auto hits =
static_cast<Det*
>(
this)->Det::getHits(probe++)) {
361 for (
auto& hit : *hits) {
367 void attachHits(fair::mq::Channel& channel, fair::mq::Parts& parts)
override
372 if (
static_cast<Det*
>(
this)->Det::getHits(0) ==
nullptr) {
380 while (
auto hits =
static_cast<Det*
>(
this)->Det::getHits(probe++)) {
396 template <
typename T>
398 std::vector<int>
const& trackoffsets, std::vector<int>
const& nprimaries, std::vector<int>
const& subevtsOrdered)
400 auto originbr =
origin.GetBranch(brname.c_str());
402 auto targetdata =
new T;
403 T* incomingdata =
nullptr;
404 originbr->SetAddress(&incomingdata);
406 T* filladdress =
nullptr;
407 if (
origin.GetEntries() == 1) {
408 originbr->GetEntry(0);
409 filladdress = incomingdata;
414 nprimTot += nprimaries[
entry];
419 Int_t idelta1 = nprimTot;
424 Int_t nprim = nprimaries[
index];
426 filladdress = targetdata;
427 originbr->GetEntry(
index);
430 for (
auto& hit : *incomingdata) {
431 hit.SetTrackID(
offsetTrackIndex(hit.GetTrackID(), nprim, idelta0, idelta1));
434 std::copy(incomingdata->begin(), incomingdata->end(), std::back_inserter(*targetdata));
436 incomingdata =
nullptr;
440 idelta1 += trackoffsets[
index];
445 targetbr->SetAddress(&filladdress);
447 targetbr->ResetAddress();
451 incomingdata =
nullptr;
460 template <
typename T,
typename L>
462 std::vector<int>
const& trackoffsets, std::vector<int>
const& nprimaries,
463 std::vector<int>
const& subevtsOrdered)
465 auto entries = hitbuffervector.size();
468 T* filladdress =
nullptr;
470 filladdress = hitbuffervector[0].get();
477 nprimTot += nprimaries[
entry];
478 nhits += hitbuffervector[
entry] ? hitbuffervector[
entry]->size() : 0;
480 targetdata.reserve(nhits);
484 int idelta1 = nprimTot;
485 filladdress = &targetdata;
490 int nprim = nprimaries[
index];
494 auto incomingdata = hitbuffervector[
index].get();
497 for (
auto& hit : *incomingdata) {
498 hit.SetTrackID(
offsetTrackIndex(hit.GetTrackID(), nprim, idelta0, idelta1));
501 targetdata.insert(targetdata.end(), std::make_move_iterator(incomingdata->begin()), std::make_move_iterator(incomingdata->end()));
505 idelta1 += trackoffsets[
index];
510 targetbr->SetAddress(&filladdress);
512 targetbr->ResetAddress();
513 hitbuffervector = L();
516 void mergeHitEntries(TTree&
origin, TTree&
target, std::vector<int>
const& trackoffsets, std::vector<int>
const& nprimaries, std::vector<int>
const& subevtsOrdered)
final
521 using Hit_t =
decltype(
static_cast<Det*
>(
this)->Det::getHits(probe));
523 while (
name.size() > 0) {
524 mergeAndAdjustHits<typename std::remove_pointer<Hit_t>::type>(
name,
origin,
target, trackoffsets, nprimaries, subevtsOrdered);
533 using Hit_t =
typename std::remove_pointer<decltype(static_cast<Det*>(
this)->Det::getHits(0))>
::type;
534 using Collector_t = tbb::concurrent_unordered_map<int, std::vector<std::vector<std::unique_ptr<Hit_t>>>>;
541 void mergeHitEntriesAndFlush(
int eventID, TTree&
target, std::vector<int>
const& trackoffsets, std::vector<int>
const& nprimaries, std::vector<int>
const& subevtsOrdered)
final
546 using Hit_t =
typename std::remove_pointer<decltype(static_cast<Det*>(
this)->Det::getHits(0))>
::type;
549 auto iter = collector.find(eventID);
550 if (iter == collector.end()) {
551 LOG(
error) <<
"No buffered hits available for event " << eventID;
556 while (
name.size() > 0) {
557 auto& vectorofHitBuffers = (*iter).second[probe];
559 mergeAndAdjustHits<Hit_t>(
name, vectorofHitBuffers,
target, trackoffsets, nprimaries, subevtsOrdered);
572 using Hit_t =
typename std::remove_pointer<decltype(static_cast<Det*>(
this)->Det::getHits(0))>
::type;
577 using HitPtr_t =
decltype(
static_cast<Det*
>(
this)->Det::getHits(probe));
581 auto store = [eventID, &hitcollector](std::unique_ptr<Hit_t> hits,
int probe) {
582 auto& hitvector = hitcollector[eventID];
583 if (probe >= hitvector.size()) {
584 hitvector.resize(probe + 1);
586 hitvector[probe].emplace_back(std::move(hits));
589 while (
name.size() > 0) {
592 if (
auto hitsptr = decodeTMessage<HitPtr_t>(parts,
index++)) {
593 store(std::unique_ptr<Hit_t>(hitsptr), probe);
597 auto hitsptr = decodeShmMessage<HitPtr_t>(parts,
index++, busy);
598 store(std::make_unique<Hit_t>(*hitsptr), probe);
615 using Hit_t =
decltype(
static_cast<Det*
>(
this)->Det::getHits(probe));
617 while (
name.size() > 0) {
621 auto hitsptr = decodeTMessage<Hit_t>(parts,
index++);
625 br->SetAddress(
static_cast<void*
>(&hitsptr));
632 auto hitsptr = decodeShmMessage<Hit_t>(parts,
index++, busy);
635 br->SetAddress(
static_cast<void*
>(&hitsptr));
653 return new Det(
static_cast<const Det&
>(*
this));
658 using Hit_t =
decltype(
static_cast<Det*
>(
this)->Det::getHits(0));
671 template <
typename Hit_t>
675 static_cast<Det*
>(
this)->Det::mHits =
ptr;
684 using VectorHit_t =
decltype(
static_cast<Det*
>(
this)->Det::getHits(0));
685 using Hit_t =
typename std::remove_pointer<VectorHit_t>::type::value_type;
690 auto ptr = o2::utils::createSimVector<Hit_t>();
691 more =
static_cast<Det*
>(
this)->Det::setHits(probe,
ptr);
702 static_cast<Det*
>(
this)->Det::createHitBuffers();
719 LOG(info) <<
" BUSY WAITING SIZE ";
723 using Hit_t =
decltype(
static_cast<Det*
>(
this)->Det::getHits(0));
728 auto hits =
static_cast<Hit_t
>(bareptr);
730 static_cast<Det*
>(
this)->Det::setHits(probe, hits);
742 if (instance.hasSegment()) {
header::DataOrigin origin
std::vector< long > entries
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error s
char error[CURL_ERROR_SIZE]
virtual void Initialize()
void updateHitTrackIndices(std::map< int, int > const &indexmapping) override
std::shared_ptr< void > mHitCollector
void mergeHitEntriesAndFlush(int eventID, TTree &target, std::vector< int > const &trackoffsets, std::vector< int > const &nprimaries, std::vector< int > const &subevtsOrdered) final
std::vector< void * > mCachedPtr[NHITBUFFERS]
pointer to flag in shared mem indicating of IO busy
std::string getHitBranchNames(int probe) const override
DetImpl(const DetImpl &rhs)
copy for a worker thread of multithreaded Geant4; the copy creates its own hit buffers in initializeL...
void collectHits(int eventID, fair::mq::Parts &parts, int &index, bool shm) override
static constexpr int NHITBUFFERS
void fillHitBranch(TTree &tr, fair::mq::Parts &parts, int &index) override
void attachHits(fair::mq::Channel &channel, fair::mq::Parts &parts) override
void mergeAndAdjustHits(std::string const &brname, TTree &origin, TTree &target, std::vector< int > const &trackoffsets, std::vector< int > const &nprimaries, std::vector< int > const &subevtsOrdered)
void mergeAndAdjustHits(std::string const &brname, L &hitbuffervector, TTree &target, std::vector< int > const &trackoffsets, std::vector< int > const &nprimaries, std::vector< int > const &subevtsOrdered)
bool setHits(int i, std::vector< Hit_t > *ptr)
ShmBusyFlag * mShmBusy[NHITBUFFERS]
FairModule * CloneModule() const final
void initializeLate() final
void mergeHitEntries(TTree &origin, TTree &target, std::vector< int > const &trackoffsets, std::vector< int > const &nprimaries, std::vector< int > const &subevtsOrdered) final
ClassDefOverride(DetImpl, 0)
type-erased hit buffers of this instance in the hit merger (see hitCollector())
DetImpl & operator=(const DetImpl &)=delete
virtual std::string getHitBranchNames(int probe) const =0
void SpecialProcess(Int_t numed, EProc parID, int val)
Set process by name and value.
int registerSensitiveVolumeAndGetVolID(std::string const &name)
~Detector() override
Default Destructor.
static std::vector< int > const & getDetId2HitBitIndex()
virtual void initializeLate()=0
Detector & operator=(const Detector &)
virtual void defineLayerTurbo(Int_t nlay, Double_t phi0, Double_t r, Int_t nladd, Int_t nmod, Double_t width, Double_t tilt, Double_t lthick=0., Double_t dthick=0., UInt_t detType=0, Int_t buildFlag=0)
TClonesArray * GetCollection(int iColl) const final
virtual void attachHits(fair::mq::Channel &, fair::mq::Parts &)=0
virtual void fillParallelWorld() const
fill parallel geometry with sensitive volumes of detector
int getMaterialID(int imat) const
void getMediumIDMappingAsVector(std::vector< int > &mapping)
static void setDensityFactor(Float_t density)
static int updatedTrackIndex(std::map< int, int > const &indexmapping, int trackID)
virtual bool isMTSafe() const
whether this detector may run in multithreaded Geant4, with one clone per worker thread
void SpecialCuts(Int_t numed, const std::initializer_list< std::pair< ECut, Float_t > > &parIDValMap)
Custom processes and transport cuts.
void SpecialCut(Int_t numed, ECut parID, Float_t val)
Set cut by name and value.
void SpecialProcesses(Int_t numed, const std::initializer_list< std::pair< EProc, int > > &parIDValMap)
void Matrix(Int_t &nmat, Double_t theta1, Double_t phi1, Double_t theta2, Double_t phi2, Double_t theta3, Double_t phi3) const
Detector()
Default Constructor.
virtual void setNumberOfWrapperVolumes(Int_t n)
Books arrays for wrapper volumes.
virtual void updateHitTrackIndices(std::map< int, int > const &)=0
virtual void mergeHitEntries(TTree &origin, TTree &target, std::vector< int > const &trackoffsets, std::vector< int > const &nprimaries, std::vector< int > const &subevtsOrdered)=0
virtual void collectHits(int eventID, fair::mq::Parts &parts, int &index, bool shm)=0
void Mixture(Int_t imat, const char *name, Float_t *a, Float_t *z, Float_t dens, Int_t nlmat, Float_t *wmat)
virtual void addAlignableVolumes() const
declare alignable volumes of detector
void Medium(Int_t numed, const char *name, Int_t nmat, Int_t isvol, Int_t ifield, Float_t fieldm, Float_t tmaxfd, Float_t stemax, Float_t deemax, Float_t epsil, Float_t stmin, Float_t *ubuf=nullptr, Int_t nbuf=0)
void SetSpecialPhysicsCuts() override
static Float_t getDensityFactor()
int getMediumID(int imed) const
virtual void fillHitBranch(TTree &tr, fair::mq::Parts &parts, int &index)=0
static void setDetId2HitBitIndex(std::vector< int > const &v)
set the DetID to HitBitIndex mapping. Succeeds if not already set.
static void initFieldTrackingParams(int &mode, float &maxfield)
virtual void defineWrapperVolume(Int_t id, Double_t rmin, Double_t rmax, Double_t zspan)
Sets per wrapper volume parameters.
virtual void defineLayer(Int_t nlay, Double_t phi0, Double_t r, Int_t nladd, Int_t nmod, Double_t lthick=0., Double_t dthick=0., UInt_t detType=0, Int_t buildFlag=0)
virtual void mergeHitEntriesAndFlush(int eventID, TTree &target, std::vector< int > const &trackoffsets, std::vector< int > const &nprimaries, std::vector< int > const &subevtsOrdered)=0
static int offsetTrackIndex(int trackID, int nprimaries, int primaryOffset, int secondaryOffset)
void Material(Int_t imat, const char *name, Float_t a, Float_t z, Float_t dens, Float_t radl, Float_t absl, Float_t *buf=nullptr, Int_t nwbuf=0)
std::string addNameTo(const char *ext) const
virtual void InitializeO2Detector()=0
static MaterialManager & Instance()
static ShmManager & Instance()
bool isOperational() const
GLuint const GLchar * name
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * value
GLint GLint GLsizei GLint GLenum GLenum type
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLboolean GLboolean GLboolean GLboolean a
GLenum GLuint GLenum GLsizei const GLchar * buf
GLdouble GLdouble GLdouble z
void * decodeShmCore(fair::mq::Parts &dataparts, int index, ShmBusyFlag *&busy)
void attachShmMessage(void *hitsptr, fair::mq::Channel &channel, fair::mq::Parts &parts, ShmBusyFlag *busy_ptr)
void * decodeTMessageCore(fair::mq::Parts &dataparts, int index)
std::atomic< bool > ShmBusyFlag
TBranch * getOrMakeBranch(TTree &tree, const char *brname, T *ptr)
void attachMessageBufferToParts(fair::mq::Parts &parts, fair::mq::Channel &channel, void *data, TClass *cl)
void attachTMessage(Container const &hits, fair::mq::Channel &channel, fair::mq::Parts &parts)
void attachHitsHeaderMessage(HitsHeader const &header, fair::mq::Channel &channel, fair::mq::Parts &parts)
std::string demangle(const char *name)
utility function to demangle cxx type names
T decodeShmMessage(fair::mq::Parts &dataparts, int index, ShmBusyFlag *&busy)
T decodeTMessage(fair::mq::Parts &dataparts, int index)
void freeSimVector(std::vector< T > *ptr)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))