9 #include <top/calibration/TOPValidationAlgorithm.h>
10 #include <top/utilities/Chi2MinimumFinder1D.h>
32 TOPValidationAlgorithm::TOPValidationAlgorithm():
35 setDescription(
"Calibration algorithm for automatic validation of the calibration");
43 auto* file = TFile::Open(
"TOPCalValidation.root",
"recreate");
47 B2INFO(
"TOPValidationAlgorithm: get basic histograms");
49 double scaleFactor = 1;
50 auto h1 = getObjectPtr<TH1F>(
"moduleT0_pulls");
52 if (h1->GetEntries() > 1) scaleFactor = h1->GetRMS();
56 std::vector<string> slotNames;
57 for (
unsigned slot = 1; slot <= c_numModules; slot++) {
58 string slotName =
"slot";
59 if (slot < 10) slotName +=
"0";
60 slotName += to_string(slot);
61 slotNames.push_back(slotName);
64 for (
unsigned slot = 0; slot < c_numModules; slot++) {
65 string name =
"hits_" + slotNames[slot];
66 auto h = getObjectPtr<TH2F>(name);
72 B2INFO(
"TOPValidationAlgorithm: determine channel T0 residuals");
74 for (
unsigned slot = 0; slot < c_numModules; slot++) {
75 string name =
"chi2_" + slotNames[slot];
76 auto h = getObjectPtr<TH2F>(name);
79 int nx = h->GetNbinsX();
80 string nameOut =
"channelT0_" + slotNames[slot];
81 string title =
"ChannelT0, " + slotNames[slot] +
"; channel; residuals [ns]";
82 auto* h0 =
new TH1F(nameOut.c_str(), title.c_str(), nx, 0, nx);
84 for (
int ibin = 1; ibin <= nx; ibin++) {
86 const auto& minimum = finder.getMinimum();
87 if (not minimum.valid)
continue;
88 h0->SetBinContent(ibin, minimum.position);
89 h0->SetBinError(ibin, minimum.error);
95 B2INFO(
"TOPValidationAlgorithm: get input tree");
97 auto inputTree = getObjectPtr<TTree>(
"tree");
99 B2ERROR(
"TOPValidationAlgorithm: input tree not found");
120 B2INFO(
"TOPValidationAlgorithm: sort input tree entries");
122 std::multimap<int, ValidationTreeStruct> sortedEntries;
123 std::set<int> sortedRuns;
124 for (
int iev = 0; iev < inputTree->GetEntries(); iev++) {
125 inputTree->GetEntry(iev);
128 sortedRuns.insert(expRun);
133 B2INFO(
"TOPValidationAlgorithm: merge input tree entries");
135 std::vector<ValidationTreeStruct> mergedEntries;
136 for (
auto expRun : sortedRuns) {
138 const auto range = sortedEntries.equal_range(expRun);
139 for (
auto it = range.first; it != range.second; ++it) {
140 mergedEntry.
merge(it->second);
143 mergedEntries.push_back(mergedEntry);
148 int nx = mergedEntries.size();
150 B2ERROR(
"TOPValidationAlgorithm: input tree is empty");
158 std::set<int> sortedExps;
159 for (
auto& entry : mergedEntries) sortedExps.insert(entry.expNo);
160 std::string titleIndex =
"Experiment";
161 if (sortedExps.size() > 1) titleIndex +=
"s";
162 for (
auto expNo : sortedExps) titleIndex +=
" " + to_string(expNo) +
",";
163 titleIndex.pop_back();
164 auto* h_index =
new TH1F(
"runIndex", (titleIndex +
"; run index; run number").c_str(), nx, 0, nx);
165 for (
int i = 0; i < nx; i++) {
166 h_index->SetBinContent(i + 1, mergedEntries[i].runNo);
169 auto* h_numTracks =
new TH1F(
"numTracks",
"Number of tracks; run index; number of tracks", nx, 0, nx);
170 for (
int i = 0; i < nx; i++) {
171 h_numTracks->SetBinContent(i + 1, mergedEntries[i].numTracks);
174 auto* h_numMerged =
new TH1F(
"numMerged",
"Number of merged entries; run index; number of merged", nx, 0, nx);
175 for (
int i = 0; i < nx; i++) {
176 h_numMerged->SetBinContent(i + 1, mergedEntries[i].numMerged);
179 auto* h_commonT0 =
new TH1F(
"commonT0",
"commonT0; run index; residuals [ns]", nx, 0, nx);
180 for (
int i = 0; i < nx; i++) {
181 h_commonT0->SetBinContent(i + 1, mergedEntries[i].commonT0);
182 h_commonT0->SetBinError(i + 1, mergedEntries[i].commonT0Err);
185 for (
unsigned slot = 0; slot < c_numModules; slot++) {
186 string name =
"moduleT0_" + slotNames[slot];
187 string title =
"moduleT0, " + slotNames[slot] +
"; run index; residuals [ns]";
188 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, 0, nx);
189 for (
int i = 0; i < nx; i++) {
190 h->SetBinContent(i + 1, mergedEntries[i].moduleT0[slot]);
191 h->SetBinError(i + 1, mergedEntries[i].moduleT0Err[slot]);
195 for (
unsigned slot = 0; slot < c_numModules; slot++) {
196 string name =
"numTBCalibrated_" + slotNames[slot];
197 string title =
"Time base calibrated, " + slotNames[slot] +
"; run index; fraction";
198 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, 0, nx);
199 for (
int i = 0; i < nx; i++) {
200 h->SetBinContent(i + 1, mergedEntries[i].numTBCalibrated[slot] / 512.0);
204 for (
unsigned slot = 0; slot < c_numModules; slot++) {
205 string name =
"numT0Calibrated_" + slotNames[slot];
206 string title =
"channel T0 calibrated, " + slotNames[slot] +
"; run index; fraction";
207 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, 0, nx);
208 for (
int i = 0; i < nx; i++) {
209 h->SetBinContent(i + 1, mergedEntries[i].numT0Calibrated[slot] / 512.0);
213 for (
unsigned slot = 0; slot < c_numModules; slot++) {
214 string name =
"numActive_" + slotNames[slot];
215 string title =
"Active, " + slotNames[slot] +
"; run index; fraction";
216 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, 0, nx);
217 for (
int i = 0; i < nx; i++) {
218 h->SetBinContent(i + 1, mergedEntries[i].numActive[slot] / 512.0);
222 for (
unsigned slot = 0; slot < c_numModules; slot++) {
223 string name =
"numActiveCalibrated_" + slotNames[slot];
224 string title =
"Active and calibrated, " + slotNames[slot] +
"; run index; fraction";
225 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, 0, nx);
226 for (
int i = 0; i < nx; i++) {
227 h->SetBinContent(i + 1, mergedEntries[i].numActiveCalibrated[slot] / 512.0);
231 for (
unsigned slot = 0; slot < c_numModules; slot++) {
232 string name =
"thrEffi_" + slotNames[slot];
233 string title =
"Threshold efficiency, " + slotNames[slot] +
"; run index; efficiency";
234 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, 0, nx);
235 for (
int i = 0; i < nx; i++) {
236 h->SetBinContent(i + 1, mergedEntries[i].thrEffi[slot]);
240 for (
unsigned carrier = 0; carrier < 4; carrier++) {
241 string name =
"asicShifts_" + to_string(carrier);
242 string title =
"BS13d, carrier " + to_string(carrier) +
"; run index; shift [ns]";
243 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, 0, nx);
244 for (
int i = 0; i < nx; i++) {
245 auto y = mergedEntries[i].asicShifts[carrier];
247 h->SetBinError(i + 1, 0);
249 h->SetBinContent(i + 1, y);
250 h->SetBinError(i + 1, 0.001);
257 B2INFO(
"TOPValidationAlgorithm: write the results");
Base class for calibration algorithms.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
EResult
The result of calibration.
@ c_OK
Finished successfuly =0 in Python.
@ c_Failure
Failed =3 in Python.
Minimum finder using tabulated chi^2 values in one dimension.
virtual EResult calibrate() final
algorithm implementation
ValidationTreeStruct m_treeEntry
input tree entry
Abstract base class for different kinds of events.
Calibration validation tree structure.
int numTBCalibrated[c_numModules]
number of timebase calibrated channels, index = slot - 1
void rescaleErrors(double scaleFactor)
Rescale errors.
int numTracks
number of selected tracks
int numActive[c_numModules]
number of active channels, index = slot - 1
void merge(const ValidationTreeStruct &other)
Merge two structures.
float commonT0
common T0 residual
float commonT0Err
common T0 uncertainty (not scaled)
float thrEffi[c_numModules]
threshold efficiency: average over active calibrated channels, index = slot - 1
float moduleT0[c_numModules]
module T0 residuals, index = slot - 1
int numActiveCalibrated[c_numModules]
number of active calibrated channels, index = slot - 1
int expNo
experiment number
float moduleT0Err[c_numModules]
module T0 uncertainties (not scaled), index = slot - 1
float asicShifts[4]
carrier shifts of BS13d, index = carrier number
int numT0Calibrated[c_numModules]
number of channel T0 calibrated channels, index = slot - 1