forked from sPHENIX-Collaboration/coresoftware
/
RawTowerCalibration.cc
290 lines (253 loc) · 9.05 KB
/
RawTowerCalibration.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
#include "RawTowerCalibration.h"
#include <calobase/RawTower.h>
#include <calobase/RawTowerContainer.h>
#include <calobase/RawTowerDefs.h>
#include <calobase/RawTowerGeom.h>
#include <calobase/RawTowerGeomContainer.h>
#include <calobase/RawTowerv1.h>
#include <calobase/RawTowerv2.h>
#include <phparameter/PHParameters.h>
#include <fun4all/Fun4AllReturnCodes.h>
#include <fun4all/SubsysReco.h>
#include <phool/PHCompositeNode.h>
#include <phool/PHIODataNode.h>
#include <phool/PHNode.h>
#include <phool/PHNodeIterator.h>
#include <phool/PHObject.h>
#include <phool/getClass.h>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <utility>
using namespace std;
RawTowerCalibration::RawTowerCalibration(const std::string &name)
: SubsysReco(name)
, _calib_algorithm(kNo_calibration)
, //
_calib_towers(nullptr)
, _raw_towers(nullptr)
, //
rawtowergeom(nullptr)
, //
detector("NONE")
, //
_calib_tower_node_prefix("CALIB")
, _raw_tower_node_prefix("RAW")
, //
//! pedstal in unit of ADC
_pedstal_ADC(NAN)
,
//! default to fixed pedestal
_pedestal_file(false)
,
//! calibration constant in unit of GeV per ADC
_calib_const_GeV_ADC(NAN)
, //
//! default to fixed GeV per ADC
_GeV_ADC_file(false)
, _tower_type(-1)
, _tower_calib_params(name)
{
}
int RawTowerCalibration::InitRun(PHCompositeNode *topNode)
{
PHNodeIterator iter(topNode);
// Looking for the DST node
PHCompositeNode *dstNode;
dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode",
"DST"));
if (!dstNode)
{
std::cout << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< "DST Node missing, doing nothing." << std::endl;
exit(1);
}
try
{
CreateNodes(topNode);
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
return Fun4AllReturnCodes::ABORTRUN;
}
return Fun4AllReturnCodes::EVENT_OK;
}
int RawTowerCalibration::process_event(PHCompositeNode */*topNode*/)
{
if (Verbosity())
{
std::cout << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< "Process event entered" << std::endl;
}
RawTowerContainer::ConstRange begin_end = _raw_towers->getTowers();
RawTowerContainer::ConstIterator rtiter;
for (rtiter = begin_end.first; rtiter != begin_end.second; ++rtiter)
{
const RawTowerDefs::keytype key = rtiter->first;
const RawTower *raw_tower = rtiter->second;
assert(raw_tower);
RawTowerGeom *raw_tower_geom = rawtowergeom->get_tower_geometry(
raw_tower->get_id());
assert(raw_tower_geom);
if (_tower_type >= 0)
{
// Skip towers that don't match the type we are supposed to calibrate
if (_tower_type != raw_tower_geom->get_tower_type())
{
continue;
}
}
if (_calib_algorithm == kNo_calibration)
{
_calib_towers->AddTower(key, new RawTowerv2(*raw_tower));
}
else if (_calib_algorithm == kSimple_linear_calibration)
{
const double raw_energy = raw_tower->get_energy();
const double calib_energy = (raw_energy - _pedstal_ADC) * _calib_const_GeV_ADC;
RawTower *calib_tower = new RawTowerv2(*raw_tower);
calib_tower->set_energy(calib_energy);
_calib_towers->AddTower(key, calib_tower);
}
else if (_calib_algorithm == kTower_by_tower_calibration)
{
RawTowerDefs::CalorimeterId caloid = RawTowerDefs::decode_caloid(key);
const int eta = raw_tower->get_bineta();
const int phi = raw_tower->get_binphi();
double tower_by_tower_calib = 1.;
if (caloid == RawTowerDefs::LFHCAL)
{
const int l = raw_tower->get_binl();
const string calib_const_name("calib_const_eta" + to_string(eta) + "_phi" + to_string(phi) + "_l" + to_string(l));
tower_by_tower_calib = _tower_calib_params.get_double_param(calib_const_name);
if (_pedestal_file == true)
{
const string pedstal_name("PedCentral_ADC_eta" + to_string(eta) + "_phi" + to_string(phi)+ "_l" + to_string(l));
_pedstal_ADC =
_tower_calib_params.get_double_param(pedstal_name);
}
if (_GeV_ADC_file == true)
{
const string GeVperADCname("GeVperADC_eta" + to_string(eta) + "_phi" + to_string(phi)+ "_l" + to_string(l));
_calib_const_GeV_ADC =
_tower_calib_params.get_double_param(GeVperADCname);
}
}
else
{
const string calib_const_name("calib_const_eta" + to_string(eta) + "_phi" + to_string(phi));
tower_by_tower_calib = _tower_calib_params.get_double_param(calib_const_name);
if (_pedestal_file == true)
{
const string pedstal_name("PedCentral_ADC_eta" + to_string(eta) + "_phi" + to_string(phi));
_pedstal_ADC =
_tower_calib_params.get_double_param(pedstal_name);
}
if (_GeV_ADC_file == true)
{
const string GeVperADCname("GeVperADC_eta" + to_string(eta) + "_phi" + to_string(phi));
_calib_const_GeV_ADC =
_tower_calib_params.get_double_param(GeVperADCname);
}
}
const double raw_energy = raw_tower->get_energy();
const double calib_energy = (raw_energy - _pedstal_ADC) * _calib_const_GeV_ADC * tower_by_tower_calib;
RawTower *calib_tower = new RawTowerv2(*raw_tower);
calib_tower->set_energy(calib_energy);
_calib_towers->AddTower(key, calib_tower);
}
else
{
std::cout << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< " invalid calibration algorithm #" << _calib_algorithm
<< std::endl;
return Fun4AllReturnCodes::ABORTRUN;
}
} // for (rtiter = begin_end.first; rtiter != begin_end.second; ++rtiter)
if (Verbosity())
{
std::cout << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< "input sum energy = " << _raw_towers->getTotalEdep()
<< ", output sum digitalized value = "
<< _calib_towers->getTotalEdep() << std::endl;
}
return Fun4AllReturnCodes::EVENT_OK;
}
int RawTowerCalibration::End(PHCompositeNode */*topNode*/)
{
return Fun4AllReturnCodes::EVENT_OK;
}
void RawTowerCalibration::CreateNodes(PHCompositeNode *topNode)
{
PHNodeIterator iter(topNode);
PHCompositeNode *runNode = static_cast<PHCompositeNode *>(iter.findFirst(
"PHCompositeNode", "RUN"));
if (!runNode)
{
std::cerr << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< "Run Node missing, doing nothing." << std::endl;
throw std::runtime_error(
"Failed to find Run node in RawTowerCalibration::CreateNodes");
}
TowerGeomNodeName = "TOWERGEOM_" + detector;
rawtowergeom = findNode::getClass<RawTowerGeomContainer>(topNode,
TowerGeomNodeName);
if (!rawtowergeom)
{
std::cerr << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< " " << TowerGeomNodeName << " Node missing, doing bail out!"
<< std::endl;
throw std::runtime_error(
"Failed to find " + TowerGeomNodeName + " node in RawTowerCalibration::CreateNodes");
}
if (Verbosity() >= 1)
{
rawtowergeom->identify();
}
PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst(
"PHCompositeNode", "DST"));
if (!dstNode)
{
std::cerr << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< "DST Node missing, doing nothing." << std::endl;
throw std::runtime_error(
"Failed to find DST node in RawTowerCalibration::CreateNodes");
}
RawTowerNodeName = "TOWER_" + _raw_tower_node_prefix + "_" + detector;
_raw_towers = findNode::getClass<RawTowerContainer>(dstNode,
RawTowerNodeName);
if (!_raw_towers)
{
std::cerr << Name() << "::" << detector << "::" << __PRETTY_FUNCTION__
<< " " << RawTowerNodeName << " Node missing, doing bail out!"
<< std::endl;
throw std::runtime_error(
"Failed to find " + RawTowerNodeName + " node in RawTowerCalibration::CreateNodes");
}
// Create the tower nodes on the tree
PHNodeIterator dstiter(dstNode);
PHCompositeNode *DetNode = dynamic_cast<PHCompositeNode *>(dstiter.findFirst(
"PHCompositeNode", detector));
if (!DetNode)
{
DetNode = new PHCompositeNode(detector);
dstNode->addNode(DetNode);
}
// Be careful as a previous calibrator may have been registered for this detector
CaliTowerNodeName = "TOWER_" + _calib_tower_node_prefix + "_" + detector;
_calib_towers = findNode::getClass<RawTowerContainer>(DetNode,
CaliTowerNodeName);
if (!_calib_towers)
{
_calib_towers = new RawTowerContainer(_raw_towers->getCalorimeterID());
PHIODataNode<PHObject> *towerNode = new PHIODataNode<PHObject>(_calib_towers, CaliTowerNodeName, "PHObject");
DetNode->addNode(towerNode);
}
return;
}