forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanualCalibFit.C
More file actions
193 lines (173 loc) · 7.41 KB
/
manualCalibFit.C
File metadata and controls
193 lines (173 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file manualCalibFit.C
/// \author Felix Schlepper
#if !defined(__CLING__) || defined(__ROOTCLING__)
#include <array>
#include <memory>
#include <cmath>
#include <utility>
#include <vector>
// ROOT header
#include <TBranch.h>
#include <TCanvas.h>
#include <TChain.h>
#include <TProfile.h>
#include <TFile.h>
#include <Fit/Fitter.h>
// O2 header
#include <TRDCalibration/CalibratorVdExB.h>
#include "DetectorsBase/Propagator.h"
#endif
// This root macro reads in 'trdangreshistos.root' and
// performs the calibration fits manually as in CalibratorVdExB.cxx
// This can be used for checking if the calibration fits make sense.
void manualCalibFit(int runNumber = 563335, bool usePreCorrFromCCDB = false)
{
//----------------------------------------------------
// TTree and File
//----------------------------------------------------
std::unique_ptr<TFile> inFilePtr(TFile::Open("trdcaliboutput.root"));
if (inFilePtr == nullptr) {
printf("Input File could not be read!\n'");
return;
}
auto tree = inFilePtr->Get<TTree>("calibdata");
if (tree == nullptr) {
printf("Tree 'calibdata' not in file!\n");
return;
}
Float_t mHistogramEntries[13500];
Int_t mNEntriesPerBin[13500];
std::array<Float_t, 13500> mHistogramEntriesSum;
std::array<Int_t, 13500> mNEntriesPerBinSum;
mHistogramEntriesSum.fill(0.f);
mNEntriesPerBinSum.fill(0);
tree->SetBranchAddress("mHistogramEntries[13500]", &mHistogramEntries);
tree->SetBranchAddress("mNEntriesPerBin[13500]", &mNEntriesPerBin);
// use precorr values from ccdb
// necessary when the angular residuals were calculated already using ccdb calibration (e.g. in a local run)
o2::trd::CalVdriftExB* calObject;
if (usePreCorrFromCCDB) {
auto& ccdbmgr = o2::ccdb::BasicCCDBManager::instance();
o2::ccdb::CcdbApi ccdb;
ccdb.init("http://alice-ccdb.cern.ch");
auto runDuration = ccdbmgr.getRunDuration(runNumber);
std::map<std::string, std::string> metadata;
std::map<std::string, std::string> headers;
calObject = ccdb.retrieveFromTFileAny<o2::trd::CalVdriftExB>("TRD/Calib/CalVdriftExB", metadata, runDuration.first + 60000, &headers, "", "", "1689478811721");
}
//----------------------------------------------------
// Configure Fitter
//----------------------------------------------------
o2::trd::FitFunctor mFitFunctor;
std::array<std::unique_ptr<TProfile>, 540> profiles; ///< profile histograms for each TRD chamber
int counter = 0;
for (int iDet = 0; iDet < 540; ++iDet) {
mFitFunctor.profiles[iDet] = std::make_unique<TProfile>(Form("profAngleDiff_%i", iDet), Form("profAngleDiff_%i", iDet), 25, -25.f, 25.f);
if (usePreCorrFromCCDB) {
if (calObject->isGoodExB(iDet))
counter++;
mFitFunctor.vdPreCorr[iDet] = calObject->getVdrift(iDet, true);
mFitFunctor.laPreCorr[iDet] = calObject->getExB(iDet, true);
}
}
std::cout << counter << " good entries in the CCDB " << std::endl;
mFitFunctor.mAnodePlane = 3.35; // don't really care as long as it's not zero, this parameter could be removed
mFitFunctor.lowerBoundAngleFit = 80 * TMath::DegToRad();
mFitFunctor.upperBoundAngleFit = 100 * TMath::DegToRad();
if (!usePreCorrFromCCDB) {
mFitFunctor.vdPreCorr.fill(1.546);
mFitFunctor.laPreCorr.fill(0.0);
}
//----------------------------------------------------
// Loop
//----------------------------------------------------
for (Int_t iEntry = 0; tree->LoadTree(iEntry) >= 0; ++iEntry) {
// Load data
tree->GetEntry(iEntry);
for (int iBin = 0; iBin < 13500; ++iBin) { // Sum the histograms from different tfs
mHistogramEntriesSum[iBin] += mHistogramEntries[iBin];
mNEntriesPerBinSum[iBin] += mNEntriesPerBin[iBin];
}
}
//----------------------------------------------------
// Fill profiles
//----------------------------------------------------
int nEntriesDetTotal[540] = {};
for (int iDet = 0; iDet < 540; ++iDet) {
for (int iBin = 0; iBin < 25; ++iBin) {
auto angleDiffSum = mHistogramEntriesSum[iDet * 25 + iBin];
auto nEntries = mNEntriesPerBinSum[iDet * 25 + iBin];
nEntriesDetTotal[iDet] += nEntries;
if (nEntries > 0) { // skip entries which have no entries; ?
// add to the respective profile for fitting later on
mFitFunctor.profiles[iDet]->Fill(2 * iBin - 25.f, angleDiffSum / nEntries, nEntries);
}
}
printf("Det %d: nEntries=%d \n", iDet, nEntriesDetTotal[iDet]);
}
//----------------------------------------------------
// Fitting
//----------------------------------------------------
printf("-------- Started fits\n");
std::array<float, 540> laFitResults{};
std::array<float, 540> vdFitResults{};
TH1F* hVd = new TH1F("hVd", "v drift", 150, 0.5, 2.);
TH1F* hLa = new TH1F("hLa", "lorentz angle", 200, -25., 25.);
o2::trd::CalVdriftExB* calObjectOut = new o2::trd::CalVdriftExB();
for (int iDet = 0; iDet < 540; ++iDet) {
if (nEntriesDetTotal[iDet] < 75)
continue;
mFitFunctor.currDet = iDet;
ROOT::Fit::Fitter fitter;
double paramsStart[2];
paramsStart[0] = 0.;
paramsStart[1] = 1.;
fitter.SetFCN<o2::trd::FitFunctor>(2, mFitFunctor, paramsStart);
fitter.Config().ParSettings(0).SetLimits(-0.7, 0.7);
fitter.Config().ParSettings(0).SetStepSize(.01);
fitter.Config().ParSettings(1).SetLimits(0.01, 3.);
fitter.Config().ParSettings(1).SetStepSize(.01);
ROOT::Math::MinimizerOptions opt;
opt.SetMinimizerType("Minuit2");
opt.SetMinimizerAlgorithm("Migrad");
opt.SetPrintLevel(0);
opt.SetMaxFunctionCalls(1'000);
opt.SetTolerance(.001);
fitter.Config().SetMinimizerOptions(opt);
fitter.FitFCN();
auto fitResult = fitter.Result();
laFitResults[iDet] = fitResult.Parameter(0);
vdFitResults[iDet] = fitResult.Parameter(1);
if (fitResult.MinFcnValue() > 0.03)
continue;
printf("Det %d: la=%.3f \tvd=%.3f \t100*minValue=%f \tentries=%d\n", iDet, laFitResults[iDet] * TMath::RadToDeg(), vdFitResults[iDet], 100 * fitResult.MinFcnValue(), nEntriesDetTotal[iDet]);
hVd->Fill(vdFitResults[iDet]);
hLa->Fill(laFitResults[iDet] * TMath::RadToDeg());
calObjectOut->setVdrift(iDet, vdFitResults[iDet]);
calObjectOut->setExB(iDet, laFitResults[iDet]);
}
printf("-------- Finished fits\n");
std::cout << "number of chambers with enough entries: " << hVd->GetEntries() << std::endl;
;
std::cout << "vdrift mean: " << hVd->GetMean() << " sigma: " << hVd->GetStdDev() << std::endl;
std::cout << "lorentz angle mean: " << hLa->GetMean() << " sigma: " << hLa->GetStdDev() << std::endl;
//----------------------------------------------------
// Write
//----------------------------------------------------
std::unique_ptr<TFile> outFilePtr(TFile::Open("manualCalibFit.root", "RECREATE"));
hVd->Write();
hLa->Write();
outFilePtr->WriteObjectAny(calObjectOut, "o2::trd::CalVdriftExB", "calObject");
for (auto& p : mFitFunctor.profiles)
p->Write();
}