22 std::string h_name = hist.key();
23 std::cout <<
"h_name: " << h_name << std::endl;
26 std::size_t found = (h_name).find_first_of(
"_");
27 std::string h_layer = h_name.substr(0,found);
28 if (h_layer.find(layer) == std::string::npos)
31 std::cout <<
"found layer " << layer <<
" in " << h_name << std::endl;
33 found = (h_name).find_last_of(
"_");
34 std::string sample = h_name.substr(found+1);
36 std::cout <<
"time sample is " << sample << std::endl;
38 TIter next(inFile->GetListOfKeys());
41 std::string histoname;
42 while ((key = (TKey*)next())) {
43 std::string name(key->GetName());
45 std::cout <<
"Checking if histokey " << name <<
" matches " << h_name << std::endl;
46 if (name.find(h_name) != std::string::npos){
47 TH2F *hh = (TH2F*) inFile-> Get(key->GetName());
49 std::cout <<
"Adding histo " << key->GetName() <<
" to list of histos to fit" << std::endl;
58 TF1 *fit =
new TF1(
"fit",
"gaus", xmin, xmax);
59 hist->Fit(
"fit",
"ORQN",
"");
60 double fitMean = fit->GetParameter(1);
61 double fitSig = fit->GetParameter(2);
62 double fitnorm = fit->GetParameter(0);
63 double fitchi2 = fit->GetChisquare();
64 double fitndf = fit->GetNDF();
66 double iterChi2 = fitchi2/fitndf;
68 double stepx = hist->GetBinWidth(0);
69 double iterxmax = xmax;
73 std::cout <<
"Initial xmin and xmax " << xmin <<
" | " << xmax << std::endl;
74 std::cout <<
"Initial mean and sigma " << fitMean <<
" | " << fitSig << std::endl;
75 std::cout <<
"Initial Chi2: " << iterChi2 << std::endl;
77 while((iterxmax > xmin + 10*stepx) && (newChi2 < iterChi2*0.98)){
80 iterxmax = iterxmax - stepx;
82 std::cout <<
"Iterating xmax to: " << iterxmax << std::endl;
88 fit->SetRange(xmin,iterxmax);
89 hist->Fit(
"fit",
"ORQN",
"");
90 fitMean = fit->GetParameter(1);
91 fitSig = fit->GetParameter(2);
92 fitnorm = fit->GetParameter(0);
93 fitchi2 = fit->GetChisquare();
94 fitndf = fit->GetNDF();
96 if(fitMean > iterxmax){
101 newChi2 = fitchi2/fitndf;
103 std::cout <<
"iter" << i <<
" newChi2 = " << newChi2 << std::endl;
117 double minthresh = hardminimum;
118 double threshold = hardmaximum;
121 min = hist->GetBinLowEdge(hist->FindFirstBinAbove(0.0,1));
124 max = hist->GetBinLowEdge(hist->FindLastBinAbove(0.0,1));
128 std::cout <<
"initial min: " << min <<
" | initial max: " << max << std::endl;
130 TF1 *fitA =
new TF1(
"fitA",
"gaus", min, max);
131 hist->Fit(
"fitA",
"ORQN",
"");
132 double fitAMean = fitA->GetParameter(1);
133 double fitASig = fitA->GetParameter(2);
137 if(fitAMean + fitASig*sigmaRange < threshold)
138 max = fitAMean + fitASig*sigmaRange;
139 if(fitAMean - fitASig*sigmaRange > minthresh)
140 min = fitAMean - fitASig*sigmaRange;
143 std::cout <<
"meanA " << fitAMean <<
" | sigmaA: " << fitASig << std::endl;
144 std::cout <<
"minA: " << min <<
" | maxA: " << max << std::endl;
148 TF1 *fitB =
new TF1(
"fitB",
"gaus", min, max);
149 hist->Fit(
"fitB",
"ORQN",
"");
150 double fitMean = fitB->GetParameter(1);
151 double fitSig = fitB->GetParameter(2);
153 std::cout <<
"meanB " << fitMean <<
" | sigmaB: " << fitSig << std::endl;
155 if(fitMean + fitSig*sigmaRange < threshold)
156 max = fitMean + fitSig*sigmaRange;
157 if(fitMean - fitSig*sigmaRange > minthresh)
158 min = fitMean - fitSig*sigmaRange;
161 std::cout <<
"minB: " << min <<
" | maxB: " << max << std::endl;
163 TF1 *fit =
new TF1(
"fit",
"gaus", min, max);
164 hist->Fit(
"fit",
"ORQN",
"");
166 double newFitSig = 99999;
167 double newFitMean = 99999;
169 while (std::abs(fitSig - newFitSig) > 0.0005 || std::abs(fitMean - newFitMean) > 0.0005) {
170 double itermax = max;
171 double itermin = min;
174 fitMean = newFitMean;
178 if(fitMean + fitSig*sigmaRange < threshold)
179 max = fitMean + fitSig*sigmaRange;
180 if(fitMean - fitSig*sigmaRange > minthresh)
181 min = fitMean - fitSig*sigmaRange;
182 fit->SetRange(min,max);
183 hist->Fit(
"fit",
"ORQN",
"");
185 newFitMean = fit->GetParameter(1);
186 newFitSig = fit->GetParameter(2);
200 std::vector<std::string> halfmodule_strings;
209 for(std::map<std::string, TH2F*>::iterator it =
histos2d.begin(); it !=
histos2d.end(); ++it)
212 TH2F* halfmodule_hh = it->second;
213 halfmodule_hh->RebinY(rebin_);
214 halfmodule_hh->Write();
215 std::string hh_name = it->first;
220 for(std::vector<std::string>::iterator it = halfmodule_strings.begin(); it != halfmodule_strings.end(); ++it){
221 if(hh_name.find(*it) != std::string::npos){
224 std::cout <<
"hwTag for " << hh_name <<
" is " << hwTag << std::endl;
230 std::string feb = (hwTag.substr(1,1));
231 std::string hyb = (hwTag.substr(3,1));
232 gDirectory->mkdir((
"F"+feb+
"H"+hyb).c_str())->cd();
234 for(
int cc=0; cc < 640 ; ++cc)
237 std::cout << hh_name <<
" " << cc << std::endl;
238 std::cout <<
"CHANNEL " << cc << std::endl;
241 std::cout <<
"CHANNEL " << cc << std::endl;
246 std::cout <<
"Global SVT ID: " << svt_id << std::endl;
256 std::cout <<
"THRESHOLD F" <<feb <<
"H" <<hyb <<
"channel " << cc <<
": " << threshold << std::endl;
266 TH1D* projy_h = halfmodule_hh->ProjectionY(Form(
"%s_proY_ch%i",hh_name.c_str(),cc),
269 projy_h->SetTitle(Form(
"%s_proY_ch%i",hh_name.c_str(),cc));
273 double chRMS = projy_h->GetRMS();
277 if(chRMS < deadRMS_ || projy_h->GetEntries() == 0)
282 double maxbin = projy_h->GetBinContent(projy_h->GetMaximumBin());
285 int minbin = projy_h->FindFirstBinAbove((
double)frac*maxbin,1);
286 double minx = projy_h->GetBinLowEdge(minbin);
288 double binwidth = projy_h->GetBinWidth(minbin);
289 double minxVal = projy_h->GetBinContent(minbin);
291 double maxx = threshold - binwidth*1;
296 if (minbin == -1 || projy_h->GetEntries() < minStats_ )
320 projy_h->Fit(
"fit",
"ORQ",
"");
321 double fitmean = fit->GetParameter(1);
322 double fitsigma = fit->GetParameter(2);
323 double fitnorm = fit->GetParameter(0);
324 double fitchi2 = fit->GetChisquare();
325 double fitndf = fit->GetNDF();
328 std::cout <<
"Fit Mean: " << fitmean << std::endl;
329 std::cout <<
"Fit sigma: " << fitsigma << std::endl;
330 std::cout <<
"Fit norm: " << fitnorm << std::endl;
331 std::cout <<
"Fit min: " <<
fitmin << std::endl;
332 std::cout <<
"Fit max: " <<
fitmax << std::endl;
341 bool suplowDaq =
false;
342 if(fitmean <= fitmin || fitmin >
fitmax){
345 std::cout <<
"bad fit!" << std::endl;
348 if(!badfit && fitmean >=
fitmax){
351 std::cout <<
"Super low Daq threshold" << std::endl;
355 if(!badfit && !suplowDaq){
358 double maxbinx = projy_h->GetBinLowEdge(projy_h->GetMaximumBin());
359 if ((std::abs(maxbinx - fitmean) > fitsigma)){
368 int fitmaxbin = projy_h->FindBin(
fitmax);
369 for (
int i = 1; i < 6; i++){
370 maxavg = maxavg + projy_h->GetBinContent(fitmaxbin + i);
379 std::cout <<
"Low daq threshold" << std::endl;
void fit2DHistoChannelBaselines(std::map< std::string, TH2F * > histos2d, int rebin_, int minStats_, int deadRMS_, std::string thresholdsFileIn_, FlatTupleMaker *flat_tuple_)
description
int getSvtIDFromHWChannel(int channel, std::string hwTag, std::map< std::string, std::map< int, int > > svtid_map)
Return global svt id for channel by providing local channel number and F<n>H<m> of channel.