17#include <ecl/digitization/OfflineFitFunction.h>
24double GetMaxRes(TGraph* gin, TF1* fit)
26 double MaxResidual = 0;
28 for (
int j = 0; j < 1000; j++) {
29 if (gin->GetX()[j] < 10) {
30 double tempVal = gin->GetY()[j];
31 double tempResidual = tempVal - fit->Eval(gin->GetX()[j]);
32 if (gin->GetY()[j] > MaxData) MaxData = gin->GetY()[j];
33 if (abs(tempResidual) > abs(MaxResidual)) MaxResidual = tempResidual;
36 return abs(MaxResidual) / MaxData;
39TF1* FitPulse(TGraph* gin,
int ShapeFlag,
double* pulseInputPara)
44 double highRange = 10;
45 TF1* ShpFloat =
new TF1(Form(
"Shp_%d", ShapeFlag), Belle2::ECL::WaveFuncTwoComponent, 0, highRange, 26);
46 ShpFloat->FixParameter(0, 0);
47 ShpFloat->FixParameter(1, 0);
48 ShpFloat->FixParameter(2, 1);
49 ShpFloat->FixParameter(3, 0);
50 for (
int k = 0; k < 10; k++) {
51 ShpFloat->SetParameter(4 + k, pulseInputPara[k + 1]);
52 ShpFloat->FixParameter(10 + 4 + k, pulseInputPara[k + 1]);
54 ShpFloat->SetParameter(24, pulseInputPara[0]);
55 ShpFloat->FixParameter(25, 1);
56 ShpFloat->SetNpx(1000);
57 gin->Fit(Form(
"Shp_%d", ShapeFlag),
"Q N 0 R",
"", 0, 30);
60 double Check = GetMaxRes(gin, ShpFloat);
61 double CheckMin = Check;
63 ParMin11[0] = ShpFloat->GetParameter(24);
64 for (
int k = 0; k < 10; k++)ParMin11[k + 1] = ShpFloat->GetParameter(k + 4);
68 std::cout << nFits <<
" " << Check << std::endl;
70 while ((Check > 0.01) && nFits < 20) {
72 ShpFloat->SetParameter(24, ParMin11[0] * (1.0 + (0.01 * nFits)));
73 for (
int k = 0; k < 10; k++) ShpFloat->SetParameter(k + 4, ParMin11[k + 1] * (1.0 + (0.01 * nFits)));
75 gin->Fit(Form(
"Shp_%d", ShapeFlag),
"Q N 0 R W",
"", 0.01, highRange);
77 Check = GetMaxRes(gin, ShpFloat);
79 if (Check < CheckMin) {
82 ParMin11[0] = ShpFloat->GetParameter(24);
83 for (
int k = 0; k < 10; k++)ParMin11[k + 1] = ShpFloat->GetParameter(k + 4);
85 std::cout <<
"nFit=" << nFits <<
" " << Check << std::endl;;
87 if (nFits == 20 && Attempt == 0 && (Check > 0.01)) {
90 const double ParMin11t[11] = {36.1232, -0.284876, 0.350343, 0.432839, 0.445749, 0.27693, 0.00899611, 6.11111, 0.788569, 0.570159, -0.411252};
91 for (
int k = 0; k < 11; k++)ParMin11[k] = ParMin11t[k];
93 const double ParMin11t[11] = {10, 0.031, 4.2e-5, 0.74, 0.43, 0.61, 0.03, 3.8, 0.81, 0.77, 0.59};
94 for (
int k = 0; k < 11; k++)ParMin11[k] = ParMin11t[k];
101 ShpFloat->SetParameter(24, ParMin11[0]);
102 for (
int k = 0; k < 10; k++) ShpFloat->SetParameter(k + 4, ParMin11[k + 1]);
107int main(
int argc,
char* argv[])
110 TString OutputDirectory =
"./";
111 if (OutputDirectory ==
"") {
112 std::cout <<
"Error set output directory" << std::endl;
117 int LowCellID = atoi(argv[1]);
118 int HighCellID = atoi(argv[2]);
120 double TimeAll_A[1000];
121 double PhotonValues_A[1000];
122 double HadronValues_A[1000];
123 double DiodeValues_A[1000];
124 TFile* HadronShapeFile =
new TFile(OutputDirectory + Form(
"HadronShapes_Low%d_High%d.root", LowCellID, HighCellID));
125 TTree* chain = (TTree*) HadronShapeFile->Get(
"HadronTree");
126 chain->SetBranchAddress(
"TimeAll_A", &TimeAll_A);
127 chain->SetBranchAddress(
"ValuePhoton_A", &PhotonValues_A);
128 chain->SetBranchAddress(
"ValueHadron_A", &HadronValues_A);
129 chain->SetBranchAddress(
"ValueDiode_A", &DiodeValues_A);
131 double TempHadronPar11_A[11];
132 double TempDiodePar11_A[11];
133 double MaxResDiode_A = 0;
134 double MaxValDiode_A = 0;
135 double MaxResHadron_A = 0;
136 double MaxValHadron_A = 0;
138 TFile* f =
new TFile(OutputDirectory + Form(
"HadronPars_Low%d_High%d.root", LowCellID, HighCellID),
"RECREATE");
139 TTree* WaveformParametersTree =
new TTree(
"HadronWaveformInfo",
"");
140 WaveformParametersTree->Branch(
"TempHadronPar11_A", &TempHadronPar11_A,
"TempHadronPar11_A[11]/D");
141 WaveformParametersTree->Branch(
"TempDiodePar11_A", &TempDiodePar11_A,
"TempDiodePar11_A[11]/D");
142 WaveformParametersTree->Branch(
"MaxResDiode_A", &MaxResDiode_A,
"MaxResDiode_A/D");
143 WaveformParametersTree->Branch(
"MaxResHadron_A", &MaxResHadron_A,
"MaxResHadron_A/D");
144 WaveformParametersTree->Branch(
"MaxValHadron_A", &MaxValHadron_A,
"MaxValHadron_A/D");
145 WaveformParametersTree->Branch(
"MaxValDiode_A", &MaxValDiode_A,
"MaxValDiode_A/D");
146 std::vector<TCanvas*> AllFits;
148 Long64_t nentries = chain->GetEntriesFast();
149 for (Long64_t jentry = 0; jentry < nentries; jentry++) {
150 chain->GetEntry(jentry);
152 for (
int l = 0; l < 11; l++) {
153 TempDiodePar11_A[l] = 0;
154 TempHadronPar11_A[l] = 0;
156 int CellID = ((int)jentry) + LowCellID;
158 if (TimeAll_A[0] > -100) {
160 TGraph* G1_A =
new TGraph(1000, TimeAll_A, PhotonValues_A);
161 TGraph* G2_A =
new TGraph(1000, TimeAll_A, HadronValues_A);
162 TGraph* G3_A =
new TGraph(1000, TimeAll_A, DiodeValues_A);
164 double InputPara_A_H[11] = {1.501407e+01, 0.000000e+00, 4.693325e-03, 7.538656e-01, 4.371189e-01, 1.182754e+00, 3.242071e-03, 6.669907e+00, 8.454105e-01, 7.240517e-01, 4.097126e-01};
165 double InputPara_A_D[11] = {1.501407e+01, 0.000000e+00, 4.693325e-03, 7.538656e-01, 4.371189e-01, 1.182754e+00, 3.242071e-03, 6.669907e+00, 8.454105e-01, 7.240517e-01, 4.097126e-01};
167 TF1* F2_A = FitPulse(G2_A, 0, InputPara_A_H);
168 TF1* F3_A = FitPulse(G3_A, 1, InputPara_A_D);
170 TempHadronPar11_A[0] = F2_A->GetParameter(24);
171 TempDiodePar11_A[0] = F3_A->GetParameter(24);
172 for (
int k = 0; k < 10; k++) {
173 TempHadronPar11_A[k + 1] = F2_A->GetParameter(k + 4);
174 TempDiodePar11_A[k + 1] = F3_A->GetParameter(k + 4);
177 MaxResHadron_A = GetMaxRes(G2_A, F2_A);
178 MaxResDiode_A = GetMaxRes(G3_A, F3_A);
180 MaxValHadron_A = F2_A->GetMaximum(1, 10);
181 MaxValDiode_A = F3_A->GetMaximum(1, 10);
183 std::cout << CellID <<
" " << MaxResHadron_A <<
" " << MaxResDiode_A << std::endl;
184 WaveformParametersTree->Fill();
189 G2_A->SetMarkerColor(kBlue);
190 G3_A->SetMarkerColor(kRed);
191 G1_A->GetYaxis()->SetRangeUser(-0.5, 2);
193 AllFits.push_back(
new TCanvas(Form(
"A_all_%lld", jentry),
""));
195 G2_A->Draw(
"same P");
197 AllFits.push_back(
new TCanvas(Form(
"A_fitHad_%lld", jentry),
""));
198 AllFits[AllFits.size() - 1]->cd();
200 F2_A->Draw(
"same l");
201 AllFits.push_back(
new TCanvas(Form(
"A_fitDio_%lld", jentry),
""));
202 AllFits[AllFits.size() - 1]->cd();
204 F3_A->Draw(
"same l");
205 AllFits.push_back(
new TCanvas(Form(
"MC_all_%lld", jentry),
""));
230 std::cout <<
"eclComputePulseTemplates_Step3: Writing File " << f->GetName() << std::endl;
232 WaveformParametersTree->Write();
233 for (
unsigned int k = 0; k < AllFits.size(); k++) AllFits[k]->Write();