Skip to content

Commit 4ada324

Browse files
noferinishahor02
authored andcommitted
optimize fits in LHCphase calculation
1 parent f50b88e commit 4ada324

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

Detectors/TOF/calibration/include/TOFCalibration/LHCClockCalibrator.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
#include "CCDB/CcdbObjectInfo.h"
2323
#include <array>
2424

25+
//#define DEBUGGING
26+
27+
#ifdef DEBUGGING
28+
#include "TH2F.h"
29+
#endif
30+
2531
namespace o2
2632
{
2733
namespace tof
@@ -37,7 +43,11 @@ struct LHCClockDataHisto {
3743

3844
LHCClockDataHisto();
3945

46+
#ifndef DEBUGGING
4047
LHCClockDataHisto(int nb, float r, o2::tof::CalibTOFapi* api) : nbins(nb), range(r), v2Bin(0), calibApi(api)
48+
#else
49+
LHCClockDataHisto(int nb, float r, o2::tof::CalibTOFapi* api, int slot, TH2F* h = nullptr) : nbins(nb), range(r), v2Bin(0), calibApi(api), mSlot(slot), mTimeHist(h)
50+
#endif
4151
{
4252
if (r <= 0. || nb < 1) {
4353
throw std::runtime_error("Wrong initialization of the histogram");
@@ -51,6 +61,11 @@ struct LHCClockDataHisto {
5161
void fill(const gsl::span<const o2::dataformats::CalibInfoTOF> data);
5262
void merge(const LHCClockDataHisto* prev);
5363

64+
#ifdef DEBUGGING
65+
TH2F* mTimeHist;
66+
int mSlot;
67+
#endif
68+
5469
ClassDefNV(LHCClockDataHisto, 1);
5570
};
5671

@@ -65,7 +80,13 @@ class LHCClockCalibrator final : public o2::calibration::TimeSlotCalibration<o2:
6580
using LHCphaseVector = std::vector<LHCphase>;
6681

6782
public:
68-
LHCClockCalibrator(int minEnt = 500, int nb = 10000, float r = 244000, const std::string path = o2::base::NameConf::getCCDBServer()) : mMinEntries(minEnt), mNBins(nb), mRange(r) { mCalibTOFapi->setURL(path); }
83+
LHCClockCalibrator(int minEnt = 500, int nb = 10000, float r = 244000, const std::string path = o2::base::NameConf::getCCDBServer()) : mMinEntries(minEnt), mNBins(nb), mRange(r)
84+
{
85+
mCalibTOFapi->setURL(path);
86+
#ifdef DEBUGGING
87+
mTimeHist = new TH2F("phaseTrend", ";slot #; t - t_{exp}^{#pi} (ps)", 200, 0, 200, mNBins, -mRange, mRange);
88+
#endif
89+
}
6990
~LHCClockCalibrator() final = default;
7091
bool hasEnoughData(const Slot& slot) const final { return slot.getContainer()->entries >= mMinEntries; }
7192
void initOutput() final;
@@ -87,6 +108,11 @@ class LHCClockCalibrator final : public o2::calibration::TimeSlotCalibration<o2:
87108
CcdbObjectInfoVector mInfoVector; // vector of CCDB Infos , each element is filled with the CCDB description of the accompanying LHCPhase
88109
LHCphaseVector mLHCphaseVector; // vector of LhcPhase, each element is filled in "process" when we finalize one slot (multiple can be finalized during the same "process", which is why we have a vector. Each element is to be considered the output of the device, and will go to the CCDB
89110

111+
#ifdef DEBUGGING
112+
int mNslot = 0;
113+
TH2F* mTimeHist; //("channelDist",";channel; t - t_{exp}^{#pi} (ps)",13104,0,157248,1000,-100000,100000);
114+
#endif
115+
90116
ClassDefOverride(LHCClockCalibrator, 1);
91117
};
92118

Detectors/TOF/calibration/src/LHCClockCalibrator.cxx

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ void LHCClockDataHisto::fill(const gsl::span<const o2::dataformats::CalibInfoTOF
5757

5858
// printf("ch=%d - tot=%f - corr=%f -> dtcorr = %f (range=%f, bin=%d)\n",ch,tot,corr,dt,range,int((dt+range)*v2Bin));
5959

60-
dt += range;
61-
if (dt > 0 && dt < 2 * range) {
62-
histo[int(dt * v2Bin)]++;
60+
float dtRange = dt + range;
61+
if (dtRange > 0 && dtRange < 2 * range) {
62+
histo[int(dtRange * v2Bin)]++;
63+
#ifdef DEBUGGING
64+
mTimeHist->Fill(mSlot, dt);
65+
#endif
6366
entries++;
6467
}
6568
}
@@ -104,7 +107,37 @@ void LHCClockCalibrator::finalizeSlot(Slot& slot)
104107
LOG(info) << "Finalize slot " << slot.getTFStart() << " <= TF <= " << slot.getTFEnd() << " with "
105108
<< c->getEntries() << " entries";
106109
std::array<double, 3> fitValues;
107-
double fitres = fitGaus(c->nbins, c->histo.data(), -(c->range), c->range, fitValues, nullptr, 2., true);
110+
std::vector<float> histoValues;
111+
int imax = c->nbins / 2;
112+
double maxval = 0;
113+
for (unsigned i = 0; i < c->nbins; ++i) { // find peak
114+
const auto& v = c->histo.at(i);
115+
if (v > maxval) {
116+
maxval = v;
117+
imax = i;
118+
}
119+
}
120+
121+
float renorm = 1.; // to avoid fit problem when stats is too large (bad chi2)
122+
if (maxval > 10) {
123+
renorm = 10. / maxval;
124+
}
125+
126+
int nbinsUsed = 0;
127+
double binwidth = 2 * c->range / c->nbins;
128+
int binrange = int(1500 / binwidth) + 1;
129+
for (unsigned i = 0; i < c->nbins; ++i) {
130+
const auto& v = c->histo.at(i);
131+
if (i >= imax - binrange && i < imax + binrange) {
132+
histoValues.push_back(v * renorm);
133+
nbinsUsed++;
134+
}
135+
}
136+
137+
float minRange = (imax - c->nbins / 2 - binrange) * binwidth;
138+
float maxRange = (imax - c->nbins / 2 + binrange) * binwidth;
139+
140+
double fitres = fitGaus(nbinsUsed, histoValues.data(), minRange, maxRange, fitValues, nullptr, 2., false);
108141
if (fitres >= 0) {
109142
LOG(info) << "Fit result " << fitres << " Mean = " << fitValues[1] << " Sigma = " << fitValues[2];
110143
} else {
@@ -130,14 +163,24 @@ void LHCClockCalibrator::finalizeSlot(Slot& slot)
130163
mLHCphaseVector.emplace_back(l);
131164

132165
slot.print();
166+
#ifdef DEBUGGING
167+
TFile fout("debug_tof_phase.root", "RECREATE");
168+
mTimeHist->Write();
169+
fout.Close();
170+
#endif
133171
}
134172

135173
//_____________________________________________
136174
Slot& LHCClockCalibrator::emplaceNewSlot(bool front, TFType tstart, TFType tend)
137175
{
138176
auto& cont = getSlots();
139177
auto& slot = front ? cont.emplace_front(tstart, tend) : cont.emplace_back(tstart, tend);
178+
#ifndef DEBUGGING
140179
slot.setContainer(std::make_unique<LHCClockDataHisto>(mNBins, mRange, mCalibTOFapi));
180+
#else
181+
slot.setContainer(std::make_unique<LHCClockDataHisto>(mNBins, mRange, mCalibTOFapi, mNslot, mTimeHist));
182+
mNslot++;
183+
#endif
141184
return slot;
142185
}
143186

Detectors/TOF/calibration/testWorkflow/LHCClockCalibratorSpec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ DataProcessorSpec getLHCClockCalibDeviceSpec(bool useCCDB)
218218
{"tf-per-slot", VariantType::UInt32, 5u, {"number of TFs per calibration time slot"}},
219219
{"max-delay", VariantType::UInt32, 3u, {"number of slots in past to consider"}},
220220
{"min-entries", VariantType::Int, 500, {"minimum number of entries to fit single time slot"}},
221-
{"nbins", VariantType::Int, 1000, {"number of bins for "}}}};
221+
{"nbins", VariantType::Int, 4000, {"number of bins for "}}}};
222222
}
223223

224224
} // namespace framework

0 commit comments

Comments
 (0)