36 unsigned short wireID;
37 unsigned short layerID;
42 efftree->SetBranchAddress(
"wireID", &wireID);
43 efftree->SetBranchAddress(
"layerID", &layerID);
44 efftree->SetBranchAddress(
"z", &z);
45 efftree->SetBranchAddress(
"isFound", &isFound);
50 std::vector<long long> wireCounts;
53 unsigned short layerNo = wireLayer.getICLayer();
55 wireCounts.resize(wireCounts.size() + nwidbins, 0);
58 const Long64_t nEntries = efftree->GetEntries();
60 for (Long64_t i = 0; i < nEntries; i++) {
65 unsigned int totalCountsForActiveWires = 0;
66 unsigned int activeWireCount = 0;
68 for (
auto count : wireCounts) {
70 totalCountsForActiveWires += count;
75 double averageOccupancy =
76 (activeWireCount > 0) ? (1.0 * totalCountsForActiveWires / activeWireCount) : 0.0;
80 B2INFO(
"Will run the calibration for this run with enough hits per wire: " << averageOccupancy);
82 B2WARNING(
"Not enough hits per wire in this run: " << averageOccupancy);
88 unsigned short wireID;
89 unsigned short layerID;
94 efftree->SetBranchAddress(
"wireID", &wireID);
95 efftree->SetBranchAddress(
"layerID", &layerID);
96 efftree->SetBranchAddress(
"z", &z);
97 efftree->SetBranchAddress(
"isFound", &isFound);
103 unsigned short layerNo = wireLayer.getICLayer();
105 unsigned short nzbins = 30 - layerNo / 7;
108 double widbins[2] = { -0.5, (double)cdcgeo.
nWiresInLayer(layerNo) - 0.5};
109 double zbins[2] = {wireLayer.getBackwardZ(), wireLayer.getForwardZ()};
111 TEfficiency* effInLayer =
new TEfficiency(
112 Form(
"effLayer_%d", layerNo),
113 Form(
"Hit efficiency of L%d ; z (cm) ; IWire ", layerNo),
114 nzbins, zbins[0], zbins[1], nwidbins, widbins[0], widbins[1]);
119 const Long64_t nEntries = efftree->GetEntries();
121 for (Long64_t i = 0; i < nEntries; i++) {
122 efftree->GetEntry(i);
124 if (layerID < m_efficiencyList->GetEntries()) {
125 TEfficiency* efficiencyInLayer =
128 efficiencyInLayer->Fill(isFound, z, wireID);
132 TFile* outputCollection =
new TFile(
"LayerEfficiencies.root",
"RECREATE");
134 outputCollection->Close();
135 delete outputCollection;
143 unsigned short layerNo = wireLayer.getICLayer();
147 auto passed = (TH2F*)efficiencyInLayer->GetPassedHistogram();
148 auto total = (TH2F*)efficiencyInLayer->GetTotalHistogram();
151 if (!total->GetEntries())
continue;
154 double minFitRange = wireLayer.getBackwardZ() + 30;
155 double maxFitRange = wireLayer.getForwardZ() - 30;
156 TF1* constantFunction =
new TF1(
"constantFunction",
"pol0", minFitRange, maxFitRange);
159 auto passedProjectedX = passed->ProjectionX(
"projectionx1");
160 auto totalProjectedX = total->ProjectionX(
"projectionx2");
161 TEfficiency* efficiencyProjectedX =
new TEfficiency(*passedProjectedX, *totalProjectedX);
163 unsigned short minFitBin = passedProjectedX->FindBin(minFitRange) + 1;
164 unsigned short maxFitBin = passedProjectedX->FindBin(maxFitRange) - 1;
167 auto passedProjectedY = passed->ProjectionY(
"projectiony1", minFitBin, maxFitBin);
168 auto totalProjectedY = total->ProjectionY(
"projectiony2", minFitBin, maxFitBin);
169 TEfficiency* efficiencyProjectedY =
new TEfficiency(*passedProjectedY, *totalProjectedY);
172 float totalAverage = 0;
173 int nonZeroWires = 0;
174 for (
int i = 1; i <= passedProjectedY->GetNbinsX(); ++i) {
175 float efficiencyAtBin = efficiencyProjectedY->GetEfficiency(i);
176 if (efficiencyAtBin > 0.2) {
177 totalAverage += efficiencyAtBin;
182 if (nonZeroWires > 0) totalAverage /= nonZeroWires;
183 else totalAverage = 0;
185 TGraphAsymmErrors* graphEfficiencyProjected = efficiencyProjectedX->CreateGraph();
188 for (
int i = 1; i <= passed->GetNbinsY(); ++i) {
191 auto singleWirePassed = passed->ProjectionX(
"single wire projection passed", i, i);
192 auto singleWireTotal = total->ProjectionX(
"single wire projection total", i, i);
195 if (!singleWirePassed->Integral(minFitBin, maxFitBin)) {
196 double wireID = passed->GetYaxis()->GetBinCenter(i);
198 if (wireID < 0 || wireID >= maxWires) {
199 B2ERROR(
"Invalid wireID: " << wireID <<
" for LayerID: " << layerNo
200 <<
". Max wires: " << maxWires);
201 delete singleWirePassed;
202 delete singleWireTotal;
206 delete singleWirePassed;
207 delete singleWireTotal;
211 TEfficiency* singleWireEfficiency =
new TEfficiency(*singleWirePassed, *singleWireTotal);
212 TGraphAsymmErrors* graphSingleWireEfficiency = singleWireEfficiency->CreateGraph();
215 TFitResultPtr singleWireFitResults = graphSingleWireEfficiency->Fit(constantFunction,
"SQR");
216 double singleWireEfficiencyFromFit = (singleWireFitResults.Get())->Value(0);
217 double singleWireUpErrorFromFit = (singleWireFitResults.Get())->UpperError(0);
220 float p_value =
chiTest(graphSingleWireEfficiency, graphEfficiencyProjected, minFitRange, maxFitRange);
222 bool averageCondition = (0.95 * totalAverage) > (singleWireEfficiencyFromFit + 3 * singleWireUpErrorFromFit);
223 bool pvalueCondition = p_value < 0.01;
224 bool generalCondition = singleWireEfficiencyFromFit < 0.4;
225 if (generalCondition || (averageCondition && pvalueCondition)) {
226 double wireID = passed->GetYaxis()->GetBinCenter(i);
227 m_badWireList->setWire(layerNo, round(wireID), singleWireEfficiencyFromFit);
230 delete singleWireEfficiency;
231 delete graphSingleWireEfficiency;
232 delete singleWirePassed;
233 delete singleWireTotal;
236 delete constantFunction;
237 delete passedProjectedX;
238 delete totalProjectedX;
239 delete efficiencyProjectedX;
240 delete passedProjectedY;
241 delete totalProjectedY;
242 delete efficiencyProjectedY;
243 delete graphEfficiencyProjected;
251 unsigned short ndof = 0;
253 int numOfEntries1 = graph1->GetN();
254 int numOfEntries2 = graph2->GetN();
257 for (
int index1 = 0; index1 < numOfEntries1; ++index1) {
259 if (graph1->GetX()[index1] < minValue)
continue;
260 if (graph1->GetX()[index1] > maxValue)
continue;
261 for (
int index2 = 0; index2 < numOfEntries2; ++index2) {
262 if (std::abs(graph1->GetX()[index1] - graph2->GetX()[index2]) < 1e-6) {
264 double chiNumerator =
square(graph1->GetY()[index1] - graph2->GetY()[index2]);
265 double err1 = 0.5 * (graph1->GetErrorYhigh(index1) + graph1->GetErrorYlow(index1));
266 double err2 = 0.5 * (graph2->GetErrorYhigh(index2) + graph2->GetErrorYlow(index2));
267 double chiDenominator = err1 * err1 + err2 * err2;
269 chi += chiNumerator / chiDenominator;
275 return TMath::Prob(chi, ndof);