Skip to content
Closed
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
211 changes: 125 additions & 86 deletions Detectors/FIT/FT0/simulation/src/Digitizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "DataFormatsFT0/PMLookupTable.h"
#include "FT0Base/Constants.h"
#include <array>
#include <map>
#include <unordered_map>

#include "TMath.h"
#include "TRandom.h"
Expand All @@ -39,84 +41,24 @@ namespace o2::ft0
template <typename Float>
Float signalForm_i(Float x)
{
float p0, p1, p2, p3, p4, p5, p6, p7;
p0 = 1.30853;
p1 = 0.516807;
p2 = 3.36714;
p3 = -1.01206;
p4 = 1.42832;
p5 = 1.1589;
p6 = 1.22019;
p7 = 0.426818;

Double_t val = 0;

if (x > p3) {
Double_t x1 = x - p3;
Double_t arg1 = (log(x1) - p0) / p1;
val += p2 * (1.0 / (x1 * p1 * sqrt(2 * TMath::Pi()))) * exp(-0.5 * arg1 * arg1);
}

if (x > p7) {
Double_t x2 = x - p7;
Double_t arg2 = (log(x2) - p4) / p5;
val += p6 * (1.0 / (x2 * p5 * sqrt(2 * TMath::Pi()))) * exp(-0.5 * arg2 * arg2);
}

return val;
using namespace std;
Float const a = -0.45458;
Float const b = -0.83344945;
return x > Float(0) ? -(exp(b * x) - exp(a * x)) / Float(7.8446501) : Float(0);
};

// integrated signal shape function
inline float signalForm_integral(float x)
{
float p0, p1, p2, p3, p4, p5, p6, p7;
p0 = 1.30853;
p1 = 0.516807;
p2 = 3.36714;
p3 = -1.01206;
p4 = 1.42832;
p5 = 1.1589;
p6 = 1.22019;
p7 = 0.426818;
Double_t val = 0;

if (x > p3) {
Double_t x1 = x - p3;
Double_t z1 = (log(x1) - p0) / (sqrt(2) * p1);
val += p2 * 0.5 * (1 + TMath::Erf(z1)); // norm1 * CDF1
using namespace std;
double const a = -0.45458;
double const b = -0.83344945;
if (x < 0) {
x = 0;
}

if (x > p7) {
Double_t x2 = x - p7;
Double_t z2 = (log(x2) - p4) / (sqrt(2) * p5);
val += p6 * 0.5 * (1 + TMath::Erf(z2)); // norm2 * CDF2
}

return val;
};
/*
// signal shape function
template <typename Float>
Float signalForm_i(Float x)
{
using namespace std;
Float const a = -0.45458;
Float const b = -0.83344945;
return x > Float(0) ? -(exp(b * x) - exp(a * x)) / Float(7.8446501) : Float(0);
return -(exp(b * x) / b - exp(a * x) / a) / 7.8446501;
};

// integrated signal shape function
inline float signalForm_integral(float x)
{
using namespace std;
double const a = -0.45458;
double const b = -0.83344945;
if (x < 0) {
x = 0;
}
return -(exp(b * x) / b - exp(a * x) / a) / 7.8446501;
};
*/
// SIMD version of the integrated signal shape function
inline Vc::float_v signalForm_integralVc(Vc::float_v x)
{
Expand Down Expand Up @@ -328,19 +270,49 @@ void Digitizer::storeBC(BCCache& bc,

int vertex_time;
const auto& params = FT0DigParam::Instance();

static const auto pmtChannelGroups = [] {
const auto& lookup = PMLookupTable::Instance();
std::vector<std::array<int, 4>> groups;
std::unordered_map<uint8_t, std::vector<int>> tmpGroups;
// Group only simulated sensor channels. The PM address space includes unused slots.
for (int ch = 0; ch < Geometry::Nchannels; ++ch) {
tmpGroups[lookup.getPMHash(static_cast<PMLookupTable::ChannelID>(ch))].push_back(ch);
}

for (const auto& [pmHash, chVec] : tmpGroups) {
if (chVec.size() % 4 != 0) {
LOG(fatal) << "PM hash " << int(pmHash) << " has " << chVec.size()
<< " channels in LUT, expected multiplicity of 4";
}
for (size_t i = 0; i < chVec.size(); i += 4) {
groups.push_back({chVec[i], chVec[i + 1], chVec[i + 2], chVec[i + 3]});
}
}
return groups;
}();

int first = digitsCh.size(), nStored = 0;
auto& particles = bc.hits;
std::sort(std::begin(particles), std::end(particles));
auto channel_end = particles.begin();
std::vector<float> channel_times;
// std::set<int> disabledChannels = {40, 41, 42, 43, 88, 89, 90, 91, 56, 57, 58, 59, 60, 61, 62, 63, 72, 73, 74, 75, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 164, 165, 166, 167, 184, 185, 186, 187, 160, 161, 162, 163, 188, 189, 190, 191, 156, 157, 158, 159, 192, 193, 194, 195, 152, 153, 154, 155, 196, 197, 198, 199, 148, 149, 150, 151, 144, 145, 146, 147, 204, 205, 206, 207, 200, 201, 202, 203}; // przykładowe kanały
std::vector<float> baseAmp(params.mMCPs, 0.f);
std::vector<float> finalAmp(params.mMCPs, 0.f);
std::vector<int> chTime(params.mMCPs, -5000);
std::vector<int> chChain(params.mMCPs, 0);
std::vector<bool> chValid(params.mMCPs, false);

static const std::array<std::array<int, 3>, 4> localNeighbours = {{{{1, 2, 3}},
{{0, 3, 2}},
{{0, 3, 1}},
{{1, 2, 0}}}};

for (Int_t ipmt = 0; ipmt < params.mMCPs; ++ipmt) {
auto channel_begin = channel_end;
channel_end = std::find_if(channel_begin, particles.end(),
[ipmt](BCCache::particle const& p) { return p.hit_ch != ipmt; });

// The hits between 'channel_begin' and 'channel_end' now contains all hits for channel 'ipmt'

if (channel_end - channel_begin < params.mAmp_trsh) {
continue;
}
Expand All @@ -361,31 +333,98 @@ void Digitizer::storeBC(BCCache& bc,
if (mCalibOffset) {
miscalib = mCalibOffset->mTimeOffsets[ipmt];
}
int smeared_time = 1000. * (*cfd.particle - params.mCfdShift) * params.mChannelWidthInverse + miscalib; // + int(1000. * mIntRecord.getTimeOffsetWrtBC() * params.mChannelWidthInverse);
int smeared_time = 1000. * (*cfd.particle - params.mCfdShift) * params.mChannelWidthInverse + miscalib;
bool is_time_in_signal_gate = (smeared_time > -params.mTime_trg_gate && smeared_time < params.mTime_trg_gate);
float charge = measure_amplitude(channel_times) * params.mCharge2amp;
float amp = is_time_in_signal_gate ? params.mMV_2_Nchannels * charge : 0;
if (amp > 4095) {
amp = 4095;
float amp = is_time_in_signal_gate ? params.mMV_2_Nchannels * charge : 0.f;
if (amp > 4095.f) {
amp = 4095.f;
}
// if (!disabledChannels.count(ipmt)) {
// continue;
// }

LOG(debug) << mEventID << " bc " << firstBCinDeque.bc << " orbit " << firstBCinDeque.orbit << ", ipmt " << ipmt << ", smeared_time " << smeared_time << " nStored " << nStored << " offset " << miscalib;
LOG(debug) << mEventID << " bc " << firstBCinDeque.bc << " orbit " << firstBCinDeque.orbit
<< ", ipmt " << ipmt << ", smeared_time " << smeared_time
<< " nStored " << nStored << " offset " << miscalib
<< " base amp " << amp;
if (is_time_in_signal_gate) {
chain |= (1 << o2::ft0::ChannelData::EEventDataBit::kIsCFDinADCgate);
chain |= (1 << o2::ft0::ChannelData::EEventDataBit::kIsEventInTVDC);
// Sum channel charge per PM (similar logic as in digits2trgFT0)
if (ipmt < o2::ft0::Constants::sNCHANNELS_PM) {
mapPMhash2sumAmpl[pmLookupTable.getPMHash(static_cast<PMLookupTable::ChannelID>(ipmt))] += static_cast<int>(amp);
}

baseAmp[ipmt] = amp;
finalAmp[ipmt] = amp;
chTime[ipmt] = smeared_time;
chChain[ipmt] = chain;
chValid[ipmt] = true;
}

for (const auto& channels : pmtChannelGroups) {
if (channels.back() >= baseAmp.size()) {
continue;
}
for (int localIdx = 0; localIdx < 4; ++localIdx) {
const int src = channels[localIdx];
if (!chValid[src] || baseAmp[src] <= 0.f) {
continue;
}

const int nb1 = channels[localNeighbours[localIdx][0]];
const int nb2 = channels[localNeighbours[localIdx][1]];
const int diag = channels[localNeighbours[localIdx][2]];

const float directXtalk = baseAmp[src] * params.Cross_Talk_Frac;
const float diagXtalk = baseAmp[src] * (params.Cross_Talk_Frac / 3.f);

finalAmp[nb1] += directXtalk;
finalAmp[nb2] += directXtalk;
finalAmp[diag] += diagXtalk;

if (!chValid[nb1] && directXtalk >= params.mAmpThresholdForCrossTalkDigit) {
chValid[nb1] = true;
chTime[nb1] = chTime[src];
chChain[nb1] = chChain[src];
}

if (!chValid[nb2] && directXtalk >= params.mAmpThresholdForCrossTalkDigit) {
chValid[nb2] = true;
chTime[nb2] = chTime[src];
chChain[nb2] = chChain[src];
}

if (!chValid[diag] && diagXtalk >= params.mAmpThresholdForCrossTalkDigit) {
chValid[diag] = true;
chTime[diag] = chTime[src];
chChain[diag] = chChain[src];
}
}
}

for (Int_t ipmt = 0; ipmt < params.mMCPs; ++ipmt) {
if (!chValid[ipmt]) {
continue;
}

float amp = finalAmp[ipmt];
if (amp > 4095.f) {
amp = 4095.f;
}
const bool hasPrimarySignal = (baseAmp[ipmt] > 0.f);
const bool isCrossTalkOnly = (!hasPrimarySignal && amp > 0.f);

if (isCrossTalkOnly && amp < params.mAmpThresholdForCrossTalkDigit) {
continue;
}

const int smeared_time = chTime[ipmt];
const int chain = chChain[ipmt];
const bool is_time_in_signal_gate = (smeared_time > -params.mTime_trg_gate && smeared_time < params.mTime_trg_gate);

if (is_time_in_signal_gate && ipmt < o2::ft0::Constants::sNCHANNELS_PM) {
mapPMhash2sumAmpl[pmLookupTable.getPMHash(static_cast<PMLookupTable::ChannelID>(ipmt))] += static_cast<int>(amp);
}

digitsCh.emplace_back(ipmt, smeared_time, int(amp), chain);
nStored++;

// fill triggers

Bool_t is_A_side = (ipmt < 4 * mGeometry.NCellsA);
if (!is_time_in_signal_gate) {
continue;
Expand Down