34 static double largestCorrection = 0.0;
37 for(
int i = 0; i < 4; ++i) {
40 std::cout <<
DRED <<
"Couldn't find a channel for "
47 static int largestDet = -1;
48 static int largestCrystal1 = -1;
49 static int largestCrystal2 = -1;
51 std::string namebase = Form(
"det_%d", det);
54 double lowCut = energy - 15;
55 double highCut = energy + 15;
58 std::array<double, 5> xpts = {lowCut, 0, 0, highCut, lowCut};
59 std::array<double, 5> ypts = {0, lowCut, highCut, 0, 0};
60 TCutG cut(
"cut", 5, xpts.data(), ypts.data());
62 auto* d =
new double[16];
63 auto* eD =
new double[16];
65 for(
int crystal1 = 0; crystal1 < 4; crystal1++) {
66 for(
int crystal2 = crystal1 + 1; crystal2 < 4; crystal2++) {
68 std::string name = Form(
"%s_%d_%d", namebase.c_str(), crystal1, crystal2);
69 TH2*
mat =
dynamic_cast<TH2*
>(inputFile->Get(name.c_str()));
71 std::cout <<
"can not find: " << name << std::endl;
74 std::cout <<
mat->GetName() << std::endl;
75 int xbins =
mat->GetNbinsX();
76 int ybins =
mat->GetNbinsY();
78 TH2* cmat =
dynamic_cast<TH2*
>(
mat->Clone(Form(
"%s_clone",
mat->GetName())));
82 auto* fitGraph =
new TGraphErrors;
83 fitGraph->SetNameTitle(Form(
"%s_graph",
mat->GetName()),
"Graph");
86 for(
int i = 1; i <= xbins; i++) {
87 bool insideYet =
false;
88 for(
int j = 1; j <= ybins; j++) {
89 double xc =
mat->GetXaxis()->GetBinCenter(i);
90 double yc =
mat->GetYaxis()->GetBinCenter(j);
91 if(cut.IsInside(xc, yc) != 0) {
95 cmat->Fill(xc, yc,
mat->GetBinContent(i, j));
98 cmat->GetXaxis()->SetRange(i, i);
103 if(cmat->Integral() > 6) {
104 fitGraph->SetPoint(fitGraph->GetN(), cmat->GetYaxis()->GetBinCenter(i), cmat->GetMean(2));
105 fitGraph->SetPointError(fitGraph->GetN() - 1, cmat->GetXaxis()->GetBinWidth(i), cmat->GetMeanError(2));
109 cmat->GetXaxis()->SetRange(1, cmat->GetXaxis()->GetNbins());
113 auto* fpx =
new TF1(Form(
"pxfit_%i_%i_%i", det, crystal1, crystal2),
CrossTalkFit, 6, 1167, 3);
114 fpx->SetParameter(0, 0.0001);
115 fpx->SetParameter(1, 0.0001);
116 fpx->SetParameter(2, energy);
117 fpx->FixParameter(2, energy);
123 TProfile* px = cmat->ProfileX();
126 TH1* residualPlot =
new TH1D(Form(
"%s_resid", fpx->GetName()),
"residuals", 2000, 0, 2000);
127 for(
int i = 0; i < residualPlot->GetNbinsX(); ++i) {
128 if(px->GetBinContent(i) != 0) {
129 residualPlot->SetBinContent(i, px->GetBinContent(i) - fpx->Eval(residualPlot->GetBinCenter(i)));
131 residualPlot->SetBinError(i, px->GetBinError(i));
133 residualPlot->Write();
140 std::cout <<
"=====================" << std::endl;
141 std::cout <<
mat->GetName() << std::endl;
142 std::cout <<
"d" << crystal1 << crystal2 <<
" at zero " << (fpx->Eval(energy)) / energy << std::endl;
143 std::cout <<
"=====================" << std::endl;
146 d[crystal2 * 4 + crystal1] = fpx->GetParameter(0);
147 d[crystal1 * 4 + crystal2] = fpx->GetParameter(1);
148 eD[crystal2 * 4 + crystal1] = fpx->GetParError(0);
149 eD[crystal1 * 4 + crystal2] = fpx->GetParError(1);
153 if(fpx->GetParameter(0) > largestCorrection) {
154 largestCorrection = fpx->GetParameter(0);
156 largestCrystal2 = crystal2;
157 largestCrystal1 = crystal1;
159 if(fpx->GetParameter(1) > largestCorrection) {
160 largestCorrection = fpx->GetParameter(1);
162 largestCrystal2 = crystal2;
163 largestCrystal1 = crystal1;
168 std::cout <<
" -------------------- " << std::endl;
169 std::cout <<
" -------------------- " << std::endl;
170 std::cout << std::endl;
174 std::streamsize prec = std::cout.precision();
175 std::cout.precision(10);
176 std::cout.setf(std::ios::fixed, std::ios::floatfield);
177 for(
int i = 0; i < 4; i++) {
178 for(
int j = 0; j < 4; j++) {
180 d[i * 4 + j] = 0.0000;
181 eD[i * 4 + j] = 0.0000;
184 std::cout << d[j * 4 + i] <<
"\t";
188 if(chan ==
nullptr) {
189 std::cout <<
DRED <<
"Couldn't find a channel for "
196 std::cout << std::endl;
199 std::cout.precision(prec);
200 std::cout.unsetf(std::ios::floatfield);
202 std::cout << std::endl;
203 std::cout <<
"Largest correction = " << largestCorrection <<
" Shift = " << largestCorrection * energy << std::endl;
204 std::cout <<
"Largest combo, det = " << largestDet <<
" Crystals = " << largestCrystal1 <<
", " << largestCrystal2 << std::endl;
205 std::cout <<
" -------------------- " << std::endl;
206 std::cout <<
" -------------------- " << std::endl;
223 if(argc != 3 && argc != 4) {
224 std::cout <<
"Usage: " << argv[0] <<
" <matrix file> <cal file> <user settings (optional)>" << std::endl;
230 std::cout <<
"Aborting" << std::endl;
234 auto* inputFile =
new TFile(argv[1]);
235 if(inputFile ==
nullptr || !inputFile->IsOpen()) {
236 std::cout <<
"Failed to open file '" << argv[1] <<
"'!" << std::endl;
242 userSettings->ReadSettings(argv[3]);
246 std::string outputFileName = argv[1];
247 auto lastSlash = outputFileName.find_last_of(
'/');
248 if(lastSlash != std::string::npos) {
249 outputFileName = std::string(
"ct_") + outputFileName.substr(lastSlash + 1);
251 outputFileName = std::string(
"ct_") + std::string(argv[1]);
254 auto* outputFile =
new TFile(outputFileName.c_str(),
"recreate");
256 FixAll(inputFile, userSettings->GetDouble(
"CrossTalkEnergy", 1332.));