Belle II Software development
ARICHChannelHist.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <arich/utility/ARICHChannelHist.h>
10#include <framework/logging/Logger.h>
11#include <iostream>
12#include <algorithm>
13#include <TGraph.h>
14#include <Math/Vector2D.h>
15#include <TList.h>
16
17using namespace std;
18using namespace Belle2;
19
20ARICHChannelHist::ARICHChannelHist(const char* name, const char* title, int type,
21 const std::vector<unsigned>& moduleIDs) : TH2Poly()
22{
23
24 m_type = type;
25 SetName(name);
26 SetTitle(title);
27 m_hapd2binMap.assign(420, 0);
28
29 // positions of HAPDs and channel mapping (avoid using DB classes...)
30 const double rs[7] = {57.35, 65.81, 74.37, 82.868, 91.305, 99.794, 108.185};
31 const unsigned nhapds[7] = {42, 48, 54, 60, 66, 72, 78};
32 const unsigned chmap[144] = {88, 86, 96, 87, 75, 72, 97, 108, 73, 74, 98, 109, 84, 85, 120, 110, 76, 77, 132, 121, 136, 124, 99, 133, 125, 113, 122, 111, 101, 137, 134, 123, 89, 112, 100, 135, 52, 28, 3, 40, 41, 5, 15, 2, 17, 29, 27, 14, 4, 16, 1, 26, 53, 65, 0, 13, 48, 49, 39, 12, 61, 62, 25, 38, 63, 60, 24, 37, 64, 50, 51, 36, 91, 115, 140, 103, 102, 138, 128, 141, 126, 114, 116, 129, 139, 127, 142, 117, 90, 78, 143, 130, 95, 94, 104, 131, 82, 81, 118, 105, 80, 83, 119, 106, 79, 93, 92, 107, 55, 57, 47, 56, 68, 71, 46, 35, 70, 69, 45, 34, 59, 58, 23, 33, 67, 66, 11, 22, 7, 19, 44, 10, 18, 30, 21, 32, 42, 6, 9, 20, 54, 31, 43, 8};
33 const double chns[12] = { -2.88, -2.37, -1.86, -1.35, -0.84, -0.33, 0.33, 0.84, 1.35, 1.86, 2.37, 2.88};
34
35 float size = 0.5 / 2. - 0.01;
36 if (m_type == 1) size = 7.0 / 2. - 0.5;
37 if (m_type == 2) size = 3.3 / 2.;
38
39 float X[5], Y[5], globX[5], globY[5];
40 X[0] = -size; Y[0] = -size;
41 X[1] = size; Y[1] = -size;
42 X[2] = size; Y[2] = size;
43 X[3] = -size; Y[3] = size;
44 X[4] = -size; Y[4] = -size;
45
46 int nhapd = 1;
47 unsigned iring = 0;
48 unsigned ihapd = 0;
49
50 std::vector<unsigned> ids;
51 if (moduleIDs.size() > 0) ids = moduleIDs;
52 else {
53 for (int hapdID = 1; hapdID < 421; hapdID++) {
54 ids.push_back(hapdID);
55 }
56 }
57
58 // HAPD bins
59 if (m_type == 1) {
60 for (int hapdID = 1; hapdID < 421; hapdID++) {
61 //for (unsigned hapdID : ids) {
62 //m_hapd2binMap[hapdID - 1] = nhapd;
63 //nhapd++;
64 float r = rs[iring];
65 float dphi = 2.*M_PI / nhapds[iring];
66 float fi = dphi / 2. + ihapd * dphi;
67 ROOT::Math::XYVector centerPos(r * cos(fi), r * sin(fi));
68 for (int i = 0; i < 5; i++) {
69 float rotX = X[i] * cos(fi) - Y[i] * sin(fi);
70 float rotY = X[i] * sin(fi) + Y[i] * cos(fi);
71 globX[i] = rotX + centerPos.X();
72 globY[i] = rotY + centerPos.Y();
73 }
74 if (std::find(ids.begin(), ids.end(), hapdID) != ids.end()) {
75 m_hapd2binMap[hapdID - 1] = nhapd;
76 nhapd++;
77 TGraph* mybox = new TGraph(5, globX, globY);
78 mybox->SetName((to_string(hapdID)).c_str());
79 AddBin(mybox);
80 }
81 ihapd++;
82 if (ihapd == nhapds[iring]) { iring++; ihapd = 0;}
83 }
84
85 } else if (m_type == 0) {
86 for (int hapdID = 1; hapdID < 421; hapdID++) {
87 //for (unsigned hapdID : ids) {
88 // m_hapd2binMap[hapdID - 1] = nhapd;
89 // nhapd++;
90 float dphi = 2.*M_PI / nhapds[iring];
91 float fi = dphi / 2. + ihapd * dphi;
92 float r = rs[iring];
93 for (int chID = 0; chID < 144; chID++) {
94
95 unsigned chX = chmap[chID] % 12;
96 unsigned chY = chmap[chID] / 12;
97 ROOT::Math::XYVector hapdPos(r * cos(fi), r * sin(fi));
98 ROOT::Math::XYVector locPos(chns[chX], chns[chY]);
99 ROOT::Math::XYVector rotatedLocPos(locPos.X() * std::cos(fi) - locPos.Y() * std::sin(fi),
100 locPos.X() * std::sin(fi) + locPos.Y() * std::cos(fi));
101 ROOT::Math::XYVector centerPos = hapdPos + rotatedLocPos;
102
103 for (int i = 0; i < 5; i++) {
104 float rotX = X[i] * cos(fi) - Y[i] * sin(fi);
105 float rotY = X[i] * sin(fi) + Y[i] * cos(fi);
106 globX[i] = rotX + centerPos.X();
107 globY[i] = rotY + centerPos.Y();
108 }
109 if (std::find(ids.begin(), ids.end(), hapdID) != ids.end()) {
110 m_hapd2binMap[hapdID - 1] = nhapd;
111 if (chID == 143) nhapd++;
112 TGraph* mybox = new TGraph(5, globX, globY);
113 mybox->SetName((to_string(hapdID)).c_str());
114 AddBin(mybox);
115 }
116 }
117 ihapd++;
118 if (ihapd == nhapds[iring]) { iring++; ihapd = 0;}
119 }
120 } else if (m_type == 2) {
121 size += 0.2;
122 for (int hapdID = 1; hapdID < 421; hapdID++) {
123 float dphi = 2.*M_PI / nhapds[iring];
124 float fi = dphi / 2. + ihapd * dphi;
125 float r = rs[iring];
126 ROOT::Math::XYVector hapdPos(r * cos(fi), r * sin(fi));
127 for (int chipID = 0; chipID < 4; chipID++) {
128 ROOT::Math::XYVector locPos(-size + (chipID / 2)*size * 2, size - (chipID % 2)*size * 2);
129 ROOT::Math::XYVector rotatedLocPos(locPos.X() * std::cos(fi) - locPos.Y() * std::sin(fi),
130 locPos.X() * std::sin(fi) + locPos.Y() * std::cos(fi));
131 ROOT::Math::XYVector centerPos = hapdPos + rotatedLocPos;
132
133 for (int i = 0; i < 5; i++) {
134 float rotX = X[i] * cos(fi) - Y[i] * sin(fi);
135 float rotY = X[i] * sin(fi) + Y[i] * cos(fi);
136 globX[i] = rotX + centerPos.X();
137 globY[i] = rotY + centerPos.Y();
138 }
139
140 if (std::find(ids.begin(), ids.end(), hapdID) != ids.end()) {
141 m_hapd2binMap[hapdID - 1] = nhapd;
142 if (chipID == 3) nhapd++;
143 TGraph* mybox = new TGraph(5, globX, globY);
144 mybox->SetName((to_string(hapdID)).c_str());
145 AddBin(mybox);
146 }
147 }
148 ihapd++;
149 if (ihapd == nhapds[iring]) { iring++; ihapd = 0;}
150 }
151 } else std::cout << "Invalid histogram type! use 0 for channel bins or 1 for HAPD bins" << std::endl;
152 SetOption("colz");
153 SetStats(0);
154 GetXaxis()->SetLimits(-115., 115.);
155 GetYaxis()->SetLimits(-115., 115.);
156}
157
158void ARICHChannelHist::Draw(Option_t* option)
159{
160 TH2Poly::Draw(option);
161 double rlin = 40;
162 double rlout = 113;
163 for (int isec = 0; isec < 6; isec++) {
164 double x1 = rlin * cos(M_PI / 3.*isec);
165 double x2 = rlout * cos(M_PI / 3.*isec);
166 double y1 = rlin * sin(M_PI / 3.*isec);
167 double y2 = rlout * sin(M_PI / 3.*isec);
168 lines[isec] = TLine(x1, y1, x2, y2);
169 lines[isec].Draw();
170 x1 = rlin * cos(M_PI / 3.*isec + M_PI / 6.);
171 y1 = rlin * sin(M_PI / 3.*isec + M_PI / 6.);
172 labels[isec] = TText(x1, y1, TString::Format("S-%d", isec + 1));
173 labels[isec].SetTextAlign(22);
174 labels[isec].SetTextSize(0.03);
175 labels[isec].Draw();
176 }
177}
178
179
180void ARICHChannelHist::fillBin(unsigned hapdID, unsigned chID, double weight)
181{
182 unsigned chIndex = 0;
183 if (m_type == 0) chIndex = (m_hapd2binMap[hapdID - 1] - 1) * 144 + chID + 1;
184 if (m_type == 2) chIndex = (m_hapd2binMap[hapdID - 1] - 1) * 4 + chID + 1;
185 SetBinContent(chIndex, GetBinContent(chIndex) + weight);
186}
187
188void ARICHChannelHist::setBinContent(unsigned hapdID, unsigned chID, double value)
189{
190 unsigned chIndex = 0;
191 if (m_type == 0) chIndex = (m_hapd2binMap[hapdID - 1] - 1) * 144 + chID + 1;
192 if (m_type == 2) chIndex = (m_hapd2binMap[hapdID - 1] - 1) * 4 + chID + 1;
193 SetBinContent(chIndex, value);
194}
195
196void ARICHChannelHist::setBinContent(unsigned hapdID, double value)
197{
198 SetBinContent(m_hapd2binMap[hapdID - 1], value);
199}
200
201void ARICHChannelHist::fillBin(unsigned hapdID, double weight)
202{
203 SetBinContent(m_hapd2binMap[hapdID - 1], GetBinContent(hapdID) + weight);
204}
205
207{
208
209 int nbins = hist->GetNbinsX();
210 if (m_type == 1) {
211 if (nbins < 420) { B2ERROR("Number of bins in histogram small than number of ChannelHist bins!"); return;}
212 if (nbins == 420) for (int i = 0; i < nbins; i++) setBinContent(i + 1, hist->GetBinContent(i + 1));
213 if (nbins == 420 * 4) {
214 for (int i = 0; i < 420; i++) {
215 for (int j = 0; j < 4; j++) fillBin(i + 1, hist->GetBinContent(i * 4 + j + 1));
216 }
217 }
218 if (nbins == 420 * 144) {
219 for (int i = 0; i < 420; i++) {
220 for (int j = 0; j < 144; j++) fillBin(i + 1, hist->GetBinContent(i * 144 + j + 1));
221 }
222 }
223 } else if (m_type == 0) {
224 if (nbins < 420 * 144) { B2ERROR("Number of bins in histogram small than number of ChannelHist bins!"); return;}
225 for (int i = 0; i < 420; i++) {
226 for (int j = 0; j < 144; j++) setBinContent(i + 1, j, hist->GetBinContent(i * 144 + j + 1));
227 }
228 } else if (m_type == 2) {
229 if (nbins < 420 * 4) { B2ERROR("Number of bins in histogram small than number of ChannelHist bins!"); return;}
230 if (nbins == 420 * 4) for (int i = 0; i < nbins; i++) setBinContent(i / 4 + 1, i % 4, hist->GetBinContent(i + 1));
231 if (nbins == 420 * 144) {
232 for (int i = 0; i < 420; i++) {
233 for (int j = 0; j < 144; j++) fillBin(i + 1, j / 36, hist->GetBinContent(i * 144 + j + 1));
234 }
235 }
236 } else return;
237}
238
239void ARICHChannelHist::setPoly(TH2Poly* poly)
240{
241
242 if (poly->GetNumberOfBins() == 0) {
243 for (const auto&& bin : *fBins) {
244 poly->AddBin((TGraph*)((TH2PolyBin*)bin)->GetPolygon());
245 }
246 }
247 if (poly->GetNumberOfBins() != GetNumberOfBins()) {std::cout << "Mismatch between number of bins in TH2Poly and ARICHChannelHist" << std::endl; return;}
248
249 double max = poly->GetMaximum();
250 for (int i = 1; i < GetNumberOfBins() + 1; i++) {
251 poly->SetBinContent(i, GetBinContent(i) > max ? max : GetBinContent(i));
252 }
253 return;
254}
255
void fillFromTH1(TH1 *hist)
Fill the channelHist from the histogram Type 0 channelHist has to be filled with 420*144bin TH1 (each...
void Draw(Option_t *option="") override
Draw the histogram.
void setBinContent(unsigned hapdID, unsigned chID, double value)
Set content of bin corresponding to hapd hapdID and channel chID.
std::vector< unsigned > m_hapd2binMap
map of bins
TLine lines[6]
array of lines
ARICHChannelHist()
Default constructor.
void fillBin(unsigned hapdID, unsigned chID, double weight=1.)
Add entry to bin corresponding to hapd hapdID and channel chID.
int m_type
histogram type
TText labels[6]
array of labels
void setPoly(TH2Poly *poly)
Fill pure TH2Poly from ARICHChannelHist, makes bins and fills content.
Abstract base class for different kinds of events.
STL namespace.