Belle II Software development
SVDLocalCalibrationsImporter.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// Own header.
10#include <svd/calibration/SVDLocalCalibrationsImporter.h>
11
12// Map from the online world (FADC id, ADC id, APV ch id)
13// to the offline world (layer, ladder, sensor, view, cell)
14#include <svd/online/SVDOnlineToOfflineMap.h>
15
16// framework - Database
17#include <framework/database/Database.h>
18#include <framework/database/DBObjPtr.h>
19#include <framework/database/PayloadFile.h>
20#include <framework/database/IntervalOfValidity.h>
21#include <framework/database/DBImportObjPtr.h>
22
23// framework aux
24#include <framework/logging/Logger.h>
25
26#include <framework/utilities/FileSystem.h>
27
28// wrapper objects
29#include <svd/calibration/SVDNoiseCalibrations.h>
30#include <svd/calibration/SVDPedestalCalibrations.h>
31#include <svd/calibration/SVDPulseShapeCalibrations.h>
32#include <svd/calibration/SVDHotStripsCalibrations.h>
33#include <svd/calibration/SVDFADCMaskedStrips.h>
34
35#include <vxd/dataobjects/VxdID.h>
36
37#include <boost/property_tree/ptree.hpp>
38#include <boost/property_tree/xml_parser.hpp>
39
40#include <sstream>
41
42using namespace Belle2;
43using boost::property_tree::ptree;
44
45void SVDLocalCalibrationsImporter::importSVDChannelMapping(const std::string& fileName)
46{
47
49 const std::string filename = FileSystem::findFile(fileName); //phase 3 xmlMapping
50 B2INFO("Importing the svd online -> offline map " << fileName << "\n");
51 // const std::string filename = FileSystem::findFile("testbeam/vxd/data/2017_svd_mapping.xml");
52 const std::string payloadname = "SVDChannelMapping.xml";
53 if (Database::Instance().addPayload(payloadname, filename, iov))
54 B2INFO("Success!");
55 else
56 B2INFO("Failure :( ua uaa uaa uaa uaaaa)");
57}
58
59
60void SVDLocalCalibrationsImporter::importSVDNoiseCalibrationsFromXML(const std::string& xmlFileName, bool errorTollerant)
61{
62 // We do initialize the noise to a negative value so that
63 // the SNR for not properly initialized channels is negative
64 // CAVEAT EMPTOR: if thou will disable an APV25 chip and will
65 // not update the noise.... the hits on the previously disabled
66 // chips will be discarded by the clusterizer.
67 // That is: please please pleaseeee do not upload calibrations
68 // with incomplete setup.
69 importSVDCalibrationsFromXML< SVDNoiseCalibrations::t_payload >(SVDNoiseCalibrations::name,
70 xmlFileName, "noises",
71 -1.0, errorTollerant);
72}
73
74void SVDLocalCalibrationsImporter::importSVDPedestalCalibrationsFromXML(const std::string& xmlFileName, bool errorTollerant)
75{
76 importSVDCalibrationsFromXML< SVDPedestalCalibrations::t_payload >(SVDPedestalCalibrations::name,
77 xmlFileName, "pedestals",
78 -1.0, errorTollerant);
79}
80
81void SVDLocalCalibrationsImporter::importSVDHotStripsCalibrationsFromXML(const std::string& xmlFileName, bool errorTollerant)
82{
83 importSVDCalibrationsFromXML< SVDHotStripsCalibrations::t_payload >(SVDHotStripsCalibrations::name,
84 xmlFileName, "hot_strips",
85 false, errorTollerant);
86}
87
88void SVDLocalCalibrationsImporter::importSVDFADCMaskedStripsFromXML(const std::string& xmlFileName, bool errorTollerant)
89{
90 importSVDCalibrationsFromXML< SVDFADCMaskedStrips::t_payload >(SVDFADCMaskedStrips::name,
91 xmlFileName, "masks",
92 false, errorTollerant);
93}
94
95
96
97
98template< class SVDcalibration >
99void SVDLocalCalibrationsImporter::importSVDCalibrationsFromXML(const std::string& condDbname,
100 const std::string& xmlFileName,
101 const std::string& xmlTag,
102 typename SVDcalibration::t_perSideContainer::calibrationType defaultValue,
103 bool errorTollerant)
104{
105 DBImportObjPtr< SVDcalibration> payload(condDbname);
106
107 DBObjPtr<PayloadFile> OnlineToOfflineMapFileName("SVDChannelMapping.xml");
108
109 OnlineToOfflineMapFileName.hasChanged();
110
111 std::unique_ptr<SVDOnlineToOfflineMap> map =
112 std::make_unique<SVDOnlineToOfflineMap>(OnlineToOfflineMapFileName->getFileName());
113
114 payload.construct(defaultValue, xmlFileName);
115
116 // This is the property tree
117 ptree pt;
118
119 // Load the XML file into the property tree. If reading fails
120 // (cannot open file, parse error), an exception is thrown.
121 read_xml(xmlFileName, pt);
122
123 for (ptree::value_type const& backEndLayoutChild :
124 pt.get_child("cfg_document.back_end_layout")) {
125
126 if (backEndLayoutChild.first == "fadc") {
127 int FADCid(0);
128 std::string FADCidString = backEndLayoutChild.second.get<std::string>("<xmlattr>.id");
129 std::stringstream ss;
130 ss << std::hex << FADCidString;
131 ss >> FADCid;
132
133 for (ptree::value_type const& fadcChild : backEndLayoutChild.second.get_child("")) {
134 if (fadcChild.first == "adc") {
135 int ADCid = fadcChild.second.get<int>("<xmlattr>.id") ;
136 B2DEBUG(1, " ADC id = " << ADCid);
137
138 int layerId = fadcChild.second.get<int>("<xmlattr>.layer_id");
139 B2DEBUG(1, " layer_id = " << layerId);
140
141 int ladderId = fadcChild.second.get<int>("<xmlattr>.ladder_id") ;
142 B2DEBUG(1, " ladder_id = " << ladderId);
143
144 int hybridId = fadcChild.second.get<int>("<xmlattr>.hybrid_id");
145 B2DEBUG(1, " hybrid_id = " << hybridId);
146
147 B2DEBUG(1, " delay25 = " <<
148 fadcChild.second.get<int>("<xmlattr>.delay25"));
149
150 for (ptree::value_type const& apvChild : fadcChild.second.get_child("")) {
151 if (apvChild.first == "apv25") {
152 int apv25ADCid = apvChild.second.get<int>("<xmlattr>.id");
153 std::string valuesString = apvChild.second.get<std::string>(xmlTag) ;
154 B2DEBUG(10, xmlTag << " APV25ID" << apv25ADCid << " "
155 << valuesString << "\n~~~~~~~~\n");
156
157 std::stringstream ssn;
158 ssn << valuesString;
159 double value;
160 for (int apvChannel = 0 ; apvChannel < 128; apvChannel ++) {
161 ssn >> value;
163 map->getSensorInfo(FADCid, ADCid * 6 + apv25ADCid);
164
165 short strip = map->getStripNumber(apvChannel, info);
166 int side = info.m_uSide ?
167 SVDcalibration::Uindex :
168 SVDcalibration::Vindex ;
169 int layer = info.m_sensorID.getLayerNumber();
170 int ladder = info.m_sensorID.getLadderNumber();
171 int sensor = info.m_sensorID.getSensorNumber();
172 if (apvChannel % 127 == 0)
173 B2DEBUG(100, layer << "_" << ladder << "_" <<
174 sensor << "_" << side << "_" << strip << "( " <<
175 apvChannel << ") " << value);
176 if (errorTollerant || layer != layerId || ladder != ladderId
177 // ||
178 // test on the sensor != f( hybrid) anr apv perhaps
179 )
180 B2FATAL("Inconsistency among maps: xml files tells \n" <<
181 "layer " << layerId << " ladder " << ladderId <<
182 " hybridID " << hybridId << "\n" <<
183 "while the basf2 map tells \n" <<
184 "layer " << layer << " ladder " << ladder <<
185 " sensor " << sensor << "\n");
186
187
188 payload->set(layer, ladder, sensor, side, strip, value);
189 }
190 }
191 }
192 }
193 }
194 }
195 }
196
197
198
201
202 payload.import(iov);
203
204 B2RESULT("Imported to database.");
205
206}
207
208/****
209 * HERE!
210 */
211void SVDLocalCalibrationsImporter::importSVDCalAmpCalibrationsFromXML(const std::string& xmlFileName, bool errorTollerant)
212{
213
215
216 DBObjPtr<PayloadFile> OnlineToOfflineMapFileName("SVDChannelMapping.xml");
217
218 OnlineToOfflineMapFileName.hasChanged();
219
220 std::unique_ptr<SVDOnlineToOfflineMap> map =
221 std::make_unique<SVDOnlineToOfflineMap>(OnlineToOfflineMapFileName->getFileName());
222
223 pulseShapes.construct(SVDStripCalAmp(), xmlFileName);
224
225 // This is the property tree
226 ptree pt;
227
228 // Load the XML file into the property tree. If reading fails
229 // (cannot open file, parse error), an exception is thrown.
230 read_xml(xmlFileName, pt);
231
232 for (ptree::value_type const& backEndLayoutChild :
233 pt.get_child("cfg_document.back_end_layout")) {
234
235 if (backEndLayoutChild.first == "fadc") {
236 int FADCid(0);
237 std::string FADCidString = backEndLayoutChild.second.get<std::string>("<xmlattr>.id");
238 std::stringstream ss;
239 ss << std::hex << FADCidString;
240 ss >> FADCid;
241
242 for (ptree::value_type const& fadcChild : backEndLayoutChild.second.get_child("")) {
243 if (fadcChild.first == "adc") {
244 int ADCid = fadcChild.second.get<int>("<xmlattr>.id") ;
245 B2DEBUG(1, " ADC id = " << ADCid);
246
247 int layerId = fadcChild.second.get<int>("<xmlattr>.layer_id");
248 B2DEBUG(1, " layer_id = " << layerId);
249
250 int ladderId = fadcChild.second.get<int>("<xmlattr>.ladder_id") ;
251 B2DEBUG(1, " ladder_id = " << ladderId);
252
253 int hybridId = fadcChild.second.get<int>("<xmlattr>.hybrid_id");
254 B2DEBUG(1, " hybrid_id = " << hybridId);
255
256 B2DEBUG(1, " delay25 = " << fadcChild.second.get<int>("<xmlattr>.delay25"));
257
258 for (ptree::value_type const& apvChild : fadcChild.second.get_child("")) {
259 if (apvChild.first == "apv25") {
260 int apv25ADCid = apvChild.second.get<int>("<xmlattr>.id");
261 std::string ampString = apvChild.second.get<std::string>("cal_peaks") ;
262 std::string widthString = apvChild.second.get<std::string>("cal_width") ;
263 std::string peakTimeString = apvChild.second.get<std::string>("cal_peak_time") ;
264
265 std::stringstream ssAmp;
266 ssAmp << ampString;
267
268 std::stringstream ssWidth;
269 ssWidth << widthString;
270
271 std::stringstream ssPeak;
272 ssPeak << peakTimeString;
273
274 double amp, width, peakTime;
275 for (int apvChannel = 0 ; apvChannel < 128; apvChannel ++) {
276 ssAmp >> amp;
277 ssWidth >> width;
278 ssPeak >> peakTime;
279
280 const SVDOnlineToOfflineMap::SensorInfo& info = map->getSensorInfo(FADCid, ADCid * 6 + apv25ADCid);
281
282 short strip = map->getStripNumber(apvChannel, info);
283 int side = info.m_uSide ?
284 SVDPulseShapeCalibrations::t_calAmp_payload::Uindex :
285 SVDPulseShapeCalibrations::t_calAmp_payload::Vindex;
286
287 int layer = info.m_sensorID.getLayerNumber();
288 int ladder = info.m_sensorID.getLadderNumber();
289 int sensor = info.m_sensorID.getSensorNumber();
290 if (errorTollerant || layer != layerId || ladder != ladderId // ||
291 // test on the sensor != f( hybrid) anr apv perhaps
292 )
293 B2ERROR("Inconsistency among maps: xml files tels \n" <<
294 "layer " << layerId << " ladder " << ladderId << " hybridID " << hybridId << "\n" <<
295 "while the basf2 map tels \n" <<
296 "layer " << layer << " ladder " << ladder << " sensor " << sensor << "\n");
297
298 SVDStripCalAmp stripCalAmp;
299 // UGLY: 22500. 31.44/8 should be written somewhere in the XML file provided by Hao
300 stripCalAmp.gain = amp / 22500.;
301 stripCalAmp.peakTime = peakTime * 31.44 / 8;
302 stripCalAmp.pulseWidth = width * 31.44 / 8 ;
303 pulseShapes->set(layer, ladder, sensor, side, strip, stripCalAmp);
304
305 }
306 }
307 }
308 }
309 }
310 }
311 }
312
313
314
317
318 pulseShapes.import(iov);
319
320 B2RESULT("Imported to database.");
321
322
323}
324
325
326
Class for importing a single object to the database.
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:151
A class that describes the interval of experiments/runs for which an object in the database is valid.
static std::string name
name of the SVDFADCMaskedStrips payload
static std::string name
name of the SVDHotStripsCalibrations payload
void importSVDNoiseCalibrationsFromXML(const std::string &xmlFileName, bool errorTollerant=false)
This method import to the database the strip noise from the pedestal local runs.
void importSVDFADCMaskedStripsFromXML(const std::string &xmlFileName, bool errorTollerant=false)
This method import to the database the list of strips masked at FADC level.
void importSVDCalibrationsFromXML(const std::string &condDbname, const std::string &xmlFileName, const std::string &xmlTag, typename SVDcalibration::t_perSideContainer::calibrationType defaultValue, bool errorTollerant)
This method import to the database the strip noise from the noise local runs.
void importSVDCalAmpCalibrationsFromXML(const std::string &xmlFileName, bool errorTollerant=false)
This method import to the database the strip pulse width, time and gain from the injection local runs...
int m_firstExperiment
The interval of validity coordinates are defined as private members.
void importSVDPedestalCalibrationsFromXML(const std::string &xmlFileName, bool errorTollerant=false)
This method import to the database the strip pedestal from the pedestal local runs.
void importSVDHotStripsCalibrationsFromXML(const std::string &xmlFileName, bool errorTollerant=false)
This method import to the database the list of hot strips flagged online and written in the local run...
static std::string name
name of the SVDNoiseCalibrations payload
static std::string name
name of the SVDPedestalCalibrations payload
static std::string calAmp_name
name of the SVDStripCalAmp payload
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42
Abstract base class for different kinds of events.
Struct to hold data about a sensor.
contains the parameter of the APV pulse
float pulseWidth
pulseWidth (in ns) is the width of the shaper output TO BE CLARIFIED: how this width is defined?
float peakTime
peakTimes (in ns) is the time at wich the shaper output reach the maximum.
float gain
gain is expressed in ADC counts / # electrons injected in the channel