Belle II Software  release-06-02-00
SVDDetectorConfigurationImporter.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 include
10 #include <svd/calibration/SVDDetectorConfigurationImporter.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/IntervalOfValidity.h>
19 #include <framework/database/DBImportObjPtr.h>
20 
21 // framework aux
22 #include <framework/logging/Logger.h>
23 
24 #include <framework/utilities/FileSystem.h>
25 
26 // DB objects
27 #include <svd/dbobjects/SVDLocalConfigParameters.h>
28 #include <svd/dbobjects/SVDGlobalConfigParameters.h>
29 
30 #include <boost/property_tree/ptree.hpp>
31 #include <boost/property_tree/xml_parser.hpp>
32 
33 #include <iostream>
34 #include <fstream>
35 #include <sstream>
36 
37 using namespace std;
38 using namespace Belle2;
39 using boost::property_tree::ptree;
40 
41 
42 void SVDDetectorConfigurationImporter::importSVDGlobalXMLFile(const std::string& fileName)
43 {
44 
45  IntervalOfValidity iov(m_firstExperiment, m_firstRun, m_lastExperiment, m_lastRun);
46  const std::string filename = FileSystem::findFile(fileName);
47  B2INFO("Importing the global run configuration xml file " << fileName << "\n");
48 
49  const std::string payloadname = "SVDGlobalXMLFile.xml";
50  if (Database::Instance().addPayload(payloadname, filename, iov))
51  B2INFO("Success!");
52  else
53  B2INFO("Failure :( ua uaa uaa uaa uaaaa)");
54 }
55 
56 void SVDDetectorConfigurationImporter::importSVDGlobalConfigParametersFromXML(const std::string& xmlFileName)
57 {
58  // This is the property tree
59  ptree pt;
60 
61  // Load the XML file into the property tree. If reading fails
62  // (cannot open file, parse error), an exception is thrown.
63  read_xml(xmlFileName, pt);
64 
65  //auxilairy variables to store the XML file values
66  int maskFilter = 0;
67  float zeroSuppression = 0;
68  float latency = 0;
69  std::string systemClock = "";
70  float hv = 0;
71  int relativeTimeShift = 0;
72  int nrFrames = 0;
73 
74  for (ptree::value_type const& cfgDocumentChild :
75  pt.get_child("cfg_document")) {
76 
77  if (cfgDocumentChild.first == "noise_run") {
78  maskFilter = cfgDocumentChild.second.get<int>("<xmlattr>.mask") ;
79  B2INFO(" masking bitmap = " << maskFilter);
80 
81  }
82  if (cfgDocumentChild.first == "hardware_run") {
83  zeroSuppression = cfgDocumentChild.second.get<float>("<xmlattr>.zs_cut") ;
84  B2INFO(" zero suppression cut = " << zeroSuppression);
85 
86  }
87 
88  if (cfgDocumentChild.first == "i2c") {
89  latency = cfgDocumentChild.second.get<float>("<xmlattr>.lat") ;
90  B2INFO(" latency = " << latency);
91 
92  }
93 
94  if (cfgDocumentChild.first == "fadc_ctrl") {
95  systemClock = cfgDocumentChild.second.get<std::string>("<xmlattr>.system_clock") ;
96  B2INFO(" APV clock units = " << systemClock);
97  nrFrames = cfgDocumentChild.second.get<int>("<xmlattr>.nr_frames") ;
98  B2INFO(" Number of Frames = " << nrFrames);
99 
100  }
101 
102  if (cfgDocumentChild.first == "controller") {
103  relativeTimeShift = cfgDocumentChild.second.get<int>("<xmlattr>.mix_trg_delay") ;
104  B2INFO(" delay of 3-sample VS 6-sample in units of APV clock /4 = " << relativeTimeShift);
105  if ((relativeTimeShift < 0) || (relativeTimeShift > 15))
106  B2FATAL("OOPS!! the relative time shift = " << relativeTimeShift <<
107  " is not allowed! It must be an int between 0 and 15 included. Please check the global xml. For the moment we set it to 0");
108  }
109  }
110 
111  for (ptree::value_type const& cfgDocumentChild :
112  pt.get_child("cfg_document.ps_setup.hv_config")) {
113  if (cfgDocumentChild.first == "config") {
114  hv = cfgDocumentChild.second.get<float>("<xmlattr>.v_conf") ;
115  B2INFO(" HV = " << hv);
116  }
117 
118  }
119 
120  DBImportObjPtr<SVDGlobalConfigParameters> svdGlobalConfig("SVDGlobalConfigParameters");
121 
122  svdGlobalConfig.construct(xmlFileName);
123 
124  svdGlobalConfig->setZeroSuppression(zeroSuppression);
125  svdGlobalConfig->setLatency(latency);
126  svdGlobalConfig->setMaskFilter(maskFilter);
127  svdGlobalConfig->setAPVClockInRFCUnits(systemClock);
128  svdGlobalConfig->setHV(hv);
129  svdGlobalConfig->setRelativeTimeShift(relativeTimeShift);
130  svdGlobalConfig->setNrFrames(nrFrames);
131 
132  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
133  m_lastExperiment, m_lastRun);
134 
135  svdGlobalConfig.import(iov);
136  B2RESULT("SVDGlobalConfigParameters imported to database.");
137 
138 }
139 
140 void SVDDetectorConfigurationImporter::importSVDLocalConfigParametersFromXML(const std::string& xmlFileName)
141 {
142 
143  // This is the property tree
144  ptree pt;
145 
146  // Load the XML file into the property tree. If reading fails
147  // (cannot open file, parse error), an exception is thrown.
148  read_xml(xmlFileName, pt);
149 
150  //auxilairy variables to store the XML file values
151  // TODO: calInjectedCharge is not used! Check if it can be removed.
152  std::string calInjectedCharge;
153  std::string calibTimeUnits = "";
154  std::string calibDate = "";
155 
156  for (ptree::value_type const& apvChild :
157  pt.get_child("cfg_document.back_end_layout.fadc.adc.apv25")) {
158 
159 
160  if (apvChild.first == "cal_peaks") {
161  calInjectedCharge = apvChild.second.get<std::string>("<xmlattr>.units");
162  B2INFO(" injected charge from XML = " << calInjectedCharge << ", but actually set to 22500, hardcoded");
163  }
164 
165  if (apvChild.first == "cal_peak_time") {
166  calibTimeUnits = apvChild.second.get<std::string>("<xmlattr>.units");
167  B2INFO(" calibration time units = " << calibTimeUnits);
168 
169  }
170  }
171  for (ptree::value_type const& latestRunChild :
172  pt.get_child("cfg_document.latest_runs")) {
173 
174  if (latestRunChild.first == "Noise") {
175  calibDate = latestRunChild.second.get<std::string>("<xmlattr>.end_of_run");
176  B2INFO(" calibration date = " << calibDate);
177 
178  }
179  }
180 
181  DBImportObjPtr<SVDLocalConfigParameters> svdLocalConfig("SVDLocalConfigParameters");
182 
183  svdLocalConfig.construct(xmlFileName);
184 
185  svdLocalConfig->setCalibrationTimeInRFCUnits(calibTimeUnits);
186  svdLocalConfig->setCalibDate(calibDate);
187  /* Injected charge set is HARDCODED here, by default 22500 electrons
188  */
189  svdLocalConfig->setInjectedCharge(22500);
190 
191 
192  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
193  m_lastExperiment, m_lastRun);
194 
195  svdLocalConfig.import(iov);
196  B2RESULT("SVDLocalConfigParameters imported to database.");
197 
198 }
199 
200 
201 
202 
203 
204 
205 
206 
Class for importing a single object to the database.
A class that describes the interval of experiments/runs for which an object in the database is valid.
Abstract base class for different kinds of events.