Belle II Software  release-08-01-10
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 header.
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)
106  B2FATAL("OOPS!! The relative time shift is negative. This is not allowed. Please check the global xml. For the moment we set it to 0.");
107  else if (relativeTimeShift > 15)
108  B2FATAL("OOPS!! The relative time shift = " << relativeTimeShift <<
109  " 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.");
110  }
111  }
112 
113  for (ptree::value_type const& cfgDocumentChild :
114  pt.get_child("cfg_document.ps_setup.hv_config")) {
115  if (cfgDocumentChild.first == "config") {
116  hv = cfgDocumentChild.second.get<float>("<xmlattr>.v_conf") ;
117  B2INFO(" HV = " << hv);
118  }
119 
120  }
121 
122  DBImportObjPtr<SVDGlobalConfigParameters> svdGlobalConfig("SVDGlobalConfigParameters");
123 
124  svdGlobalConfig.construct(xmlFileName);
125 
126  svdGlobalConfig->setZeroSuppression(zeroSuppression);
127  svdGlobalConfig->setLatency(latency);
128  svdGlobalConfig->setMaskFilter(maskFilter);
129  svdGlobalConfig->setAPVClockInRFCUnits(systemClock);
130  svdGlobalConfig->setHV(hv);
131  svdGlobalConfig->setRelativeTimeShift(relativeTimeShift);
132  svdGlobalConfig->setNrFrames(nrFrames);
133 
134  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
135  m_lastExperiment, m_lastRun);
136 
137  svdGlobalConfig.import(iov);
138  B2RESULT("SVDGlobalConfigParameters imported to database.");
139 
140 }
141 
142 void SVDDetectorConfigurationImporter::importSVDLocalConfigParametersFromXML(const std::string& xmlFileName)
143 {
144 
145  // This is the property tree
146  ptree pt;
147 
148  // Load the XML file into the property tree. If reading fails
149  // (cannot open file, parse error), an exception is thrown.
150  read_xml(xmlFileName, pt);
151 
152  //auxilairy variables to store the XML file values
153  // TODO: calInjectedCharge is not used! Check if it can be removed.
154  std::string calInjectedCharge;
155  std::string calibTimeUnits = "";
156  std::string calibDate = "";
157 
158  for (ptree::value_type const& apvChild :
159  pt.get_child("cfg_document.back_end_layout.fadc.adc.apv25")) {
160 
161 
162  if (apvChild.first == "cal_peaks") {
163  calInjectedCharge = apvChild.second.get<std::string>("<xmlattr>.units");
164  B2INFO(" injected charge from XML = " << calInjectedCharge << ", but actually set to 22500, hardcoded");
165  }
166 
167  if (apvChild.first == "cal_peak_time") {
168  calibTimeUnits = apvChild.second.get<std::string>("<xmlattr>.units");
169  B2INFO(" calibration time units = " << calibTimeUnits);
170 
171  }
172  }
173  for (ptree::value_type const& latestRunChild :
174  pt.get_child("cfg_document.latest_runs")) {
175 
176  if (latestRunChild.first == "Noise") {
177  calibDate = latestRunChild.second.get<std::string>("<xmlattr>.end_of_run");
178  B2INFO(" calibration date = " << calibDate);
179 
180  }
181  }
182 
183  DBImportObjPtr<SVDLocalConfigParameters> svdLocalConfig("SVDLocalConfigParameters");
184 
185  svdLocalConfig.construct(xmlFileName);
186 
187  svdLocalConfig->setCalibrationTimeInRFCUnits(calibTimeUnits);
188  svdLocalConfig->setCalibDate(calibDate);
189  /* Injected charge set is HARDCODED here, by default 22500 electrons
190  */
191  svdLocalConfig->setInjectedCharge(22500);
192 
193 
194  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
195  m_lastExperiment, m_lastRun);
196 
197  svdLocalConfig.import(iov);
198  B2RESULT("SVDLocalConfigParameters imported to database.");
199 
200 }
201 
202 
203 
204 
205 
206 
207 
208 
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.