Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions Common/MathUtils/include/MathUtils/RandomRing.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,22 @@ class RandomRing

/// constructor
/// @param [in] randomType type of the random generator
RandomRing(const RandomType randomType = RandomType::Gaus);
/// @param [in] rng random generator to draw from, gRandom if null
RandomRing(const RandomType randomType = RandomType::Gaus, TRandom* rng = nullptr);

/// constructor accepting TF1
/// @param [in] function TF1 function
RandomRing(TF1& function);

/// initialisation of the random ring
/// @param [in] randomType type of the random generator
void initialize(const RandomType randomType = RandomType::Gaus);
/// @param [in] rng random generator to draw from, gRandom if null
void initialize(const RandomType randomType = RandomType::Gaus, TRandom* rng = nullptr);

/// initialisation of the random ring
/// @param [in] randomType type of the random generator
void initialize(TF1& function);
/// @param [in] function TF1 function
/// @param [in] rng random generator to draw from, gRandom if null
void initialize(TF1& function, TRandom* rng = nullptr);

/// initialisation of the random ring
/// @param [in] randomType type of the random generator
Expand Down Expand Up @@ -98,6 +101,10 @@ class RandomRing
// within this header file (to reduce memory problems during compilation).
// The hope is that the calling user calls this with a
// correct Vc type (Vc::float_v) in a source file.
// a vector must not run past the end of the ring, also after setRandomPosition()
if (mRingPosition + VcType::size() > mRandomNumbers.size()) {
mRingPosition = 0;
}
const VcType value = VcType(&mRandomNumbers[mRingPosition]);
mRingPosition += VcType::size();
if (mRingPosition >= mRandomNumbers.size()) {
Expand All @@ -110,6 +117,10 @@ class RandomRing
/// @return position in the ring buffer
unsigned int getRingPosition() const { return mRingPosition; }

/// move to a random position in the ring buffer
/// @param [in] rng random generator to draw the position from
void setRandomPosition(TRandom& rng) { mRingPosition = rng.Integer(N); }

private:
// =========================================================================
// ===| members |===========================================================
Expand All @@ -123,12 +134,12 @@ class RandomRing

//______________________________________________________________________________
template <size_t N>
inline RandomRing<N>::RandomRing(const RandomType randomType)
inline RandomRing<N>::RandomRing(const RandomType randomType, TRandom* rng)
: mRandomType(randomType),
mRandomNumbers()

{
initialize(randomType);
initialize(randomType, rng);
}

//______________________________________________________________________________
Expand All @@ -142,18 +153,18 @@ inline RandomRing<N>::RandomRing(TF1& function)

//______________________________________________________________________________
template <size_t N>
inline void RandomRing<N>::initialize(const RandomType randomType)
inline void RandomRing<N>::initialize(const RandomType randomType, TRandom* rng)
{

TRandom* random = rng ? rng : gRandom;
for (auto& v : mRandomNumbers) {
// TODO: configurable mean and sigma
switch (randomType) {
case RandomType::Gaus: {
v = gRandom->Gaus(0, 1);
v = random->Gaus(0, 1);
break;
}
case RandomType::Flat: {
v = gRandom->Rndm();
v = random->Rndm();
break;
}
default: {
Expand All @@ -166,11 +177,11 @@ inline void RandomRing<N>::initialize(const RandomType randomType)

//______________________________________________________________________________
template <size_t N>
inline void RandomRing<N>::initialize(TF1& function)
inline void RandomRing<N>::initialize(TF1& function, TRandom* rng)
{
mRandomType = RandomType::CustomTF1;
for (auto& v : mRandomNumbers) {
v = function.GetRandom();
v = function.GetRandom(rng);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "TPCBase/CRU.h"
#include "DataFormatsTPC/Defs.h"
#include "TPCSimulation/DigitTime.h"
#include "TPCSimulation/SAMPAProcessing.h"
#include "TPCBase/CalDet.h"
#include "CommonUtils/DebugStreamer.h"
#include "TPCBase/ParameterDetector.h"
Expand Down Expand Up @@ -87,6 +88,9 @@ class DigitContainer
/// Get the size of the container for one event
size_t size() const { return mTimeBins.size(); }

/// Use this SAMPAProcessing instead of the singleton when filling the output
void setSAMPAProcessing(SAMPAProcessing* sampaProcessing) { mSAMPAProcessing = sampaProcessing; }

/// Fetch the calibration objects used by fillOutputContainer from the CDBInterface
/// Must be called after the CDBInterface is configured; fillOutputContainer calls it lazily otherwise
void updateCalibrations();
Expand All @@ -100,9 +104,12 @@ class DigitContainer
std::unique_ptr<DigitTime::PrevDigitInfoArray> mPrevDigArr; ///< Keep track of ToT and ion tail cumul from last time bin
o2::utils::DebugStreamer mStreamer; ///< Debug streamer
const CalPad* mPadParams[3] = {nullptr, nullptr, nullptr}; //!< Ion tail fraction, ion tail slope and common mode k-values per pad
const CalDet<bool>* mDeadMap = nullptr; //!< Dead channel map
const CalDet<bool>* mDeadMap = nullptr; //!< Dead channel map, points to mDeadMapCopy if set
std::unique_ptr<CalDet<bool>> mDeadMapCopy; //!< Own copy of the dead channel map, which the CDBInterface rebuilds on each access
bool mCMCEnabled = false; //!< Common mode correction enabled in the FEE config (DigiMode Auto)
bool mCalibrationsUpdated = false; //!< Whether updateCalibrations was called
SAMPAProcessing* mSAMPAProcessing = nullptr; //!< SAMPAProcessing used for the output, the singleton if not set
std::vector<std::pair<MCCompLabel, int>> mLabelCollector; //!< Workspace to sort the MC labels of a digit

void reportSettings();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class DigitGlobalPad
const CRU& cru, TimeBin timeBin,
GlobalPadNumber globalPad,
o2::dataformats::LabelContainer<std::pair<MCCompLabel, int>, false>& labelContainer,
SAMPAProcessing& sampaProcessing, std::vector<std::pair<MCCompLabel, int>>& labelCollector,
float commonMode = 0.f, const PrevDigitInfo& prevDigit = PrevDigitInfo(), Streamer* debugStream = nullptr, const CalDet<bool>* deadMap = nullptr);

private:
Expand Down Expand Up @@ -210,12 +211,11 @@ inline void DigitGlobalPad::fillOutputContainer(std::vector<Digit>& output,
const CRU& cru, TimeBin timeBin,
GlobalPadNumber globalPad,
o2::dataformats::LabelContainer<std::pair<MCCompLabel, int>, false>& labels,
SAMPAProcessing& sampaProcessing, std::vector<std::pair<MCCompLabel, int>>& labelCollector,
float commonMode, const PrevDigitInfo& prevDigit, Streamer* debugStream, const CalDet<bool>* deadMap)
{
const Mapper& mapper = Mapper::instance();
SAMPAProcessing& sampaProcessing = SAMPAProcessing::instance();
const PadPos pad = mapper.padPos(globalPad);
static std::vector<std::pair<MCCompLabel, int>> labelCollector; // static workspace container for sorting

/// The charge accumulated on that pad is converted into ADC counts, saturation of the SAMPA is applied and a Digit
/// is created in written out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class DigitTime
template <DigitzationMode MODE>
void fillOutputContainer(std::vector<Digit>& output, dataformats::MCTruthContainer<MCCompLabel>& mcTruth,
std::vector<CommonMode>& commonModeOutput, const Sector& sector, TimeBin timeBin,
SAMPAProcessing& sampaProcessing, std::vector<std::pair<MCCompLabel, int>>& labelCollector,
PrevDigitInfoArray* prevTime = nullptr, Streamer* debugStream = nullptr,
const CalPad* padParams[3] = nullptr, const CalDet<bool>* deadMap = nullptr);

Expand Down Expand Up @@ -129,6 +130,7 @@ inline float DigitTime::getCommonMode(const GEMstack& gemstack) const
template <DigitzationMode MODE>
inline void DigitTime::fillOutputContainer(std::vector<Digit>& output, dataformats::MCTruthContainer<MCCompLabel>& mcTruth,
std::vector<CommonMode>& commonModeOutput, const Sector& sector, TimeBin timeBin,
SAMPAProcessing& sampaProcessing, std::vector<std::pair<MCCompLabel, int>>& labelCollector,
PrevDigitInfoArray* prevTime, Streamer* debugStream, const CalPad* padParams[3],
const CalDet<bool>* deadMap)
{
Expand Down Expand Up @@ -167,7 +169,7 @@ inline void DigitTime::fillOutputContainer(std::vector<Digit>& output, dataforma
prevDigit = (*prevTime)[iPad];
}
const CRU cru = mapper.getCRU(sector, iPad);
digit.fillOutputContainer<MODE>(output, mcTruth, cru, timeBin, iPad, mLabels, getCommonMode(cru), prevDigit, debugStream, deadMap);
digit.fillOutputContainer<MODE>(output, mcTruth, cru, timeBin, iPad, mLabels, sampaProcessing, labelCollector, getCommonMode(cru), prevDigit, debugStream, deadMap);
}
}
}
Expand Down
69 changes: 53 additions & 16 deletions Detectors/TPC/simulation/include/TPCSimulation/Digitizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace tpc
{

class DigitContainer;
class ElectronTransport;
class GEMAmplification;

template <class T>
class SpaceCharge;
Expand Down Expand Up @@ -118,10 +120,10 @@ class Digitizer
/// \param nRBins number of grid points in r, must be (2**N)+1
void setUseSCDistortions(const SCDistortionType& distortionType, const TH3* hisInitialSCDensity);
/// Enable the use of space-charge distortions and provide SpaceCharge object as input
/// \param spaceCharge unique pointer to spaceCharge object
/// \param spaceCharge spaceCharge object, not owned: the caller (e.g. the DPL CCDB cache) keeps it alive
void setUseSCDistortions(SC* spaceCharge);

/// \param spaceCharge unique pointer to spaceCharge object
/// \param spaceCharge spaceCharge object of the derivative map, not owned: the caller keeps it alive
void setSCDistortionsDerivative(SC* spaceCharge);

/// Enable the use of space-charge distortions by providing global distortions and global corrections stored in a ROOT file
Expand All @@ -141,21 +143,56 @@ class Digitizer
/// in case of scaled distortions, the distortions can be recalculated to ensure consistent distortions and corrections
void recalculateDistortions();

/// Use the distortion maps and scaling of another digitizer, which keeps ownership of the maps
void setSCDistortionsFrom(const Digitizer& other);

/// Use digitizer-owned random rings filled from seed and positioned per sector and timeframe in init(),
/// so that the digits of a sector do not depend on which sectors were processed before.
/// All sectors share the ring contents and differ in the start positions, as with the singleton rings.
/// \param seed base seed, typically DigiParams.seed
void setUseSectorSeeding(ULong64_t seed);

/// Set the timeframe identifier that enters the sector seeding
void setSeedingTimeframe(ULong64_t timeframe) { mSeedingTimeframe = timeframe; }

/// Use copies of the sector-seeding random rings of another digitizer, which fills them if needed
void setSectorSeedingFrom(Digitizer& other);

/// Whether flush() also flushes the process-wide debug streamer
void setFlushDebugStreamer(bool flush) { mFlushDebugStreamer = flush; }

private:
DigitContainer mDigitContainer; ///< Container for the Digits
std::unique_ptr<SC> mSpaceCharge; ///< Handler of full distortions (static + IR dependant)
std::unique_ptr<SC> mSpaceChargeDer; ///< Handler of reference static distortions
Sector mSector = -1; ///< ID of the currently processed sector
double mEventTime = 0.f; ///< Time of the currently processed event
double mOutputDigitTimeOffset = 0; ///< Time of the first IR sampled in the digitizer
float mVDrift = 0; ///< VDrift for current timestamp
float mTDriftOffset = 0; ///< drift time additive offset in \mus
bool mIsContinuous; ///< Switch for continuous readout
bool mUseSCDistortions = false; ///< Flag to switch on the use of space-charge distortions
int mDistortionScaleType = 0; ///< type=0: no scaling of distortions, type=1 distortions without any scaling, type=2 distortions scaling with lumi
float mLumiScaleFactor = 0; ///< value used to scale the derivative map
bool mUseScaledDistortions = false; ///< whether the distortions are already scaled
ClassDefNV(Digitizer, 3);
/// Fill the random rings of the sector seeding, if not done yet
void initSectorSeeding();

ElectronTransport& electronTransport();
GEMAmplification& gemAmplification();
SAMPAProcessing& sampaProcessing();

std::unique_ptr<ElectronTransport> mElectronTransport; //!< Digitizer-owned instance with sector seeding, else the singleton is used
std::unique_ptr<GEMAmplification> mGEMAmplification; //!< Digitizer-owned instance with sector seeding, else the singleton is used
std::unique_ptr<SAMPAProcessing> mSAMPAProcessing; //!< Digitizer-owned instance with sector seeding, else the singleton is used
ULong64_t mSeed = 0; //!< Base seed for sector seeding
ULong64_t mSeedingTimeframe = 0; //!< Timeframe identifier for sector seeding
bool mUseSectorSeeding = false; //!< Whether sector seeding is used
bool mFlushDebugStreamer = true; //!< Whether flush() flushes the debug streamer

DigitContainer mDigitContainer; ///< Container for the Digits
std::vector<float> mSignalArray; //!< Workspace for the shaped signal of one electron
std::unique_ptr<SC> mSpaceChargeOwned; //!< Distortions created by the digitizer itself
SC* mSpaceCharge = nullptr; //!< Handler of full distortions (static + IR dependant), owned by mSpaceChargeOwned or by the caller
SC* mSpaceChargeDer = nullptr; //!< Handler of reference static distortions, owned by the caller
Sector mSector = -1; ///< ID of the currently processed sector
double mEventTime = 0.f; ///< Time of the currently processed event
double mOutputDigitTimeOffset = 0; ///< Time of the first IR sampled in the digitizer
float mVDrift = 0; ///< VDrift for current timestamp
float mTDriftOffset = 0; ///< drift time additive offset in \mus
bool mIsContinuous; ///< Switch for continuous readout
bool mUseSCDistortions = false; ///< Flag to switch on the use of space-charge distortions
int mDistortionScaleType = 0; ///< type=0: no scaling of distortions, type=1 distortions without any scaling, type=2 distortions scaling with lumi
float mLumiScaleFactor = 0; ///< value used to scale the derivative map
bool mUseScaledDistortions = false; ///< whether the distortions are already scaled
ClassDefNV(Digitizer, 4);
};
} // namespace tpc
} // namespace o2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ class ElectronTransport
return electronTransport;
}

/// Constructor
/// \param rng random generator to fill the random rings from, gRandom if null
explicit ElectronTransport(TRandom* rng = nullptr);

/// Destructor
~ElectronTransport() = default;

/// Move the random rings to new positions drawn from rng
void setRandomRingPositions(TRandom& rng);

/// Update the OCDB parameters cached in the class. To be called once per event
void updateParameters(float vdrift = 0);

Expand Down Expand Up @@ -72,8 +79,6 @@ class ElectronTransport
float getDriftTime(float zPos, float signChange = 1.f) const;

private:
ElectronTransport();

/// Circular random buffer containing random values of the Gauss distribution to take into account diffusion of the
/// electrons
math_utils::RandomRing<> mRandomGaus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ class GEMAmplification
return gemAmplification;
}

/// Constructor
/// \param rng random generator to fill the random rings from, gRandom if null
explicit GEMAmplification(TRandom* rng = nullptr);

/// Destructor
~GEMAmplification() = default;

/// Move the random rings to new positions drawn from rng
void setRandomRingPositions(TRandom& rng);

/// Update the OCDB parameters cached in the class. To be called once per event
void updateParameters();

Expand Down Expand Up @@ -90,8 +97,6 @@ class GEMAmplification
int getGEMMultiplication(int nElectrons, int GEM);

private:
GEMAmplification();

/// Circular random buffer containing random Gaus values for gain fluctuation if the number of electrons is larger
/// (central limit theorem)
math_utils::RandomRing<> mRandomGaus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ class SAMPAProcessing
static SAMPAProcessing sampaProcessing;
return sampaProcessing;
}
/// Constructor
/// \param rng random generator to fill the random ring from, gRandom if null
explicit SAMPAProcessing(TRandom* rng = nullptr);

/// Destructor
~SAMPAProcessing() = default;

/// Move the random ring to a new position drawn from rng
void setRandomRingPositions(TRandom& rng) { mRandomNoiseRing.setRandomPosition(rng); }

/// Update the OCDB parameters cached in the class. To be called once per event
void updateParameters(float vdrift = 0);

Expand Down Expand Up @@ -134,7 +141,6 @@ class SAMPAProcessing
float getPedestalCRU(const int sector, const int globalPadInSector) const;

private:
SAMPAProcessing();
const ParameterGas* mGasParam; ///< Caching of the parameter class to avoid multiple CDB calls
const ParameterDetector* mDetParam; ///< Caching of the parameter class to avoid multiple CDB calls
const ParameterElectronics* mEleParam; ///< Caching of the parameter class to avoid multiple CDB calls
Expand Down
Loading
Loading