forked from sPHENIX-Collaboration/coresoftware
/
RawClusterBuilderFwd.cc
338 lines (295 loc) · 8.51 KB
/
RawClusterBuilderFwd.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "RawClusterBuilderFwd.h"
#include "PHMakeGroups.h"
#include <calobase/RawCluster.h>
#include <calobase/RawClusterContainer.h>
#include <calobase/RawClusterDefs.h>
#include <calobase/RawClusterv1.h>
#include <calobase/RawTower.h>
#include <calobase/RawTowerContainer.h>
#include <calobase/RawTowerDefs.h>
#include <calobase/RawTowerGeom.h>
#include <calobase/RawTowerGeomContainer.h>
#include <fun4all/Fun4AllReturnCodes.h>
#include <fun4all/SubsysReco.h>
#include <phool/getClass.h>
#include <phool/PHCompositeNode.h>
#include <phool/PHIODataNode.h>
#include <phool/PHNode.h>
#include <phool/PHNodeIterator.h>
#include <phool/PHObject.h>
#include <phool/phool.h>
#include <cassert>
#include <cmath>
#include <exception>
#include <iostream>
#include <map>
#include <stdexcept>
#include <utility>
#include <vector>
using namespace std;
class twrs_fwd
{
public:
twrs_fwd(RawTower *);
virtual ~twrs_fwd() {}
bool is_adjacent(twrs_fwd &);
void set_id(const int i)
{
id = i;
}
int get_id() const
{
return id;
}
int get_j_bin() const
{
return bin_j;
}
int get_k_bin() const
{
return bin_k;
}
protected:
int bin_j;
int bin_k;
RawTowerDefs::keytype id;
};
twrs_fwd::twrs_fwd(RawTower *rt)
: id(-1)
{
bin_j = rt->get_bineta();
bin_k = rt->get_binphi();
}
bool twrs_fwd::is_adjacent(twrs_fwd &tower)
{
if (bin_j - 1 <= tower.get_j_bin() && tower.get_j_bin() <= bin_j + 1)
{
if (bin_k - 1 <= tower.get_k_bin() && tower.get_k_bin() <= bin_k + 1)
{
return true;
}
}
return false;
}
bool operator<(const twrs_fwd &a, const twrs_fwd &b)
{
if (a.get_j_bin() != b.get_j_bin())
{
return a.get_j_bin() < b.get_j_bin();
}
return a.get_k_bin() < b.get_k_bin();
}
RawClusterBuilderFwd::RawClusterBuilderFwd(const std::string &name)
: SubsysReco(name)
, _clusters(nullptr)
, _min_tower_e(0.0)
, chkenergyconservation(0)
, detector("NONE")
{
}
int RawClusterBuilderFwd::InitRun(PHCompositeNode *topNode)
{
try
{
CreateNodes(topNode);
}
catch (std::exception &e)
{
std::cout << PHWHERE << ": " << e.what() << std::endl;
throw;
}
return Fun4AllReturnCodes::EVENT_OK;
}
int RawClusterBuilderFwd::process_event(PHCompositeNode *topNode)
{
string towernodename = "TOWER_CALIB_" + detector;
// Grab the towers
RawTowerContainer *towers = findNode::getClass<RawTowerContainer>(topNode, towernodename);
if (!towers)
{
std::cout << PHWHERE << ": Could not find node " << towernodename << std::endl;
return Fun4AllReturnCodes::DISCARDEVENT;
}
string towergeomnodename = "TOWERGEOM_" + detector;
RawTowerGeomContainer *towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, towergeomnodename);
if (!towergeom)
{
cout << PHWHERE << ": Could not find node " << towergeomnodename << endl;
return Fun4AllReturnCodes::ABORTEVENT;
}
// make the list of towers above threshold
std::vector<twrs_fwd> towerVector;
RawTowerContainer::ConstRange begin_end = towers->getTowers();
RawTowerContainer::ConstIterator itr = begin_end.first;
for (; itr != begin_end.second; ++itr)
{
RawTower *tower = itr->second;
RawTowerDefs::keytype towerid = itr->first;
if (tower->get_energy() > _min_tower_e)
{
twrs_fwd twr(tower);
twr.set_id(towerid);
towerVector.push_back(twr);
}
}
// cluster the towers
std::multimap<int, twrs_fwd> clusteredTowers;
PHMakeGroups(towerVector, clusteredTowers);
RawCluster *cluster = nullptr;
int last_id = -1;
std::multimap<int, twrs_fwd>::iterator ctitr = clusteredTowers.begin();
std::multimap<int, twrs_fwd>::iterator lastct = clusteredTowers.end();
for (; ctitr != lastct; ++ctitr)
{
int clusterid = ctitr->first;
if (last_id != clusterid)
{
// new cluster
cluster = new RawClusterv1();
_clusters->AddCluster(cluster);
last_id = clusterid;
}
assert(cluster);
const twrs_fwd &tmptower = ctitr->second;
RawTower *rawtower = towers->getTower(tmptower.get_id());
const double e = rawtower->get_energy();
cluster->addTower(rawtower->get_id(), e);
}
for (const auto &cluster_pair : _clusters->getClustersMap())
{
const RawClusterDefs::keytype clusterid = cluster_pair.first;
RawCluster *cluster = cluster_pair.second;
assert(cluster);
assert(cluster->get_id() == clusterid);
double sum_x(0);
double sum_y(0);
double sum_z(0);
double sum_e(0);
for (const auto tower_pair : cluster->get_towermap())
{
const RawTower *rawtower = towers->getTower(tower_pair.first);
const RawTowerGeom *rawtowergeom = towergeom->get_tower_geometry(tower_pair.first);
assert(rawtower);
assert(rawtowergeom);
const double e = rawtower->get_energy();
sum_e += e;
if (e > 0)
{
sum_x += e * rawtowergeom->get_center_x();
sum_y += e * rawtowergeom->get_center_y();
sum_z += e * rawtowergeom->get_center_z();
}
} // for (const auto tower_pair : cluster->get_towermap())
cluster->set_energy(sum_e);
if (sum_e > 0)
{
sum_x /= sum_e;
sum_y /= sum_e;
sum_z /= sum_e;
cluster->set_r(sqrt(sum_y * sum_y + sum_x * sum_x));
cluster->set_phi(atan2(sum_y, sum_x));
cluster->set_z(sum_z);
}
if (Verbosity() > 1)
{
cout << "RawClusterBuilder constucted ";
cluster->identify();
}
} // for (const auto & cluster_pair : _clusters->getClustersMap())
if (chkenergyconservation)
{
double ecluster = _clusters->getTotalEdep();
double etower = towers->getTotalEdep();
if (ecluster > 0)
{
if (fabs(etower - ecluster) / ecluster > 1e-9)
{
cout << "energy conservation violation: ETower: " << etower
<< " ECluster: " << ecluster
<< " diff: " << etower - ecluster << endl;
}
}
else
{
if (etower != 0)
{
cout << "energy conservation violation: ETower: " << etower
<< " ECluster: " << ecluster << endl;
}
}
}
return Fun4AllReturnCodes::EVENT_OK;
}
bool RawClusterBuilderFwd::CorrectPhi(RawCluster *cluster, RawTowerContainer *towers, RawTowerGeomContainer *towergeom)
{
double sum = cluster->get_energy();
double phimin = 999.;
double phimax = -999.;
RawCluster::TowerConstRange begin_end = cluster->get_towers();
RawCluster::TowerConstIterator iter;
for (iter = begin_end.first; iter != begin_end.second; ++iter)
{
RawTower *tmpt = towers->getTower(iter->first);
RawTowerGeom *tgeo =
towergeom->get_tower_geometry(tmpt->get_id());
double phi = tgeo->get_phi();
if (phi > M_PI) phi = phi - 2. * M_PI; // correct the cluster phi for slat geometry which is 0-2pi (L. Xue)
if (phi < phimin)
{
phimin = phi;
}
if (phi > phimax)
{
phimax = phi;
}
}
if ((phimax - phimin) < 3.) return false; // cluster is not at phi discontinuity
float mean = 0.;
for (iter = begin_end.first; iter != begin_end.second; ++iter)
{
RawTower *tmpt = towers->getTower(iter->first);
double e = tmpt->get_energy();
RawTowerGeom *tgeo =
towergeom->get_tower_geometry(tmpt->get_id());
double phi = tgeo->get_phi();
if (phi > M_PI) phi = phi - 2. * M_PI; // correct the cluster phi for slat geometry which is 0-2pi (L. Xue)
if (phi < 0.)
{
phi = phi + 2. * M_PI; // shift phi range for correct mean calculation
}
mean += e * phi;
}
mean = mean / sum;
if (mean > M_PI)
{
mean = mean - 2. * M_PI; // shift back
}
cluster->set_phi(mean);
return true; // mean phi was corrected
}
int RawClusterBuilderFwd::End(PHCompositeNode */*topNode*/)
{
return Fun4AllReturnCodes::EVENT_OK;
}
void RawClusterBuilderFwd::CreateNodes(PHCompositeNode *topNode)
{
PHNodeIterator iter(topNode);
// Grab the cEMC node
PHCompositeNode *dstNode = static_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
if (!dstNode)
{
std::cerr << PHWHERE << "DST Node missing, doing nothing." << std::endl;
throw std::runtime_error("Failed to find DST node in EmcRawTowerBuilder::CreateNodes");
}
PHNodeIterator dstiter(dstNode);
PHCompositeNode *DetNode = dynamic_cast<PHCompositeNode *>(dstiter.findFirst("PHCompositeNode", detector));
if (!DetNode)
{
DetNode = new PHCompositeNode(detector);
dstNode->addNode(DetNode);
}
_clusters = new RawClusterContainer();
ClusterNodeName = "CLUSTER_" + detector;
PHIODataNode<PHObject> *clusterNode = new PHIODataNode<PHObject>(_clusters, ClusterNodeName, "PHObject");
DetNode->addNode(clusterNode);
}