CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CastorDbXml.cc
Go to the documentation of this file.
1 // F.Ratnikov (UMd), Oct 28, 2005
2 // Modified by S. Won 6 May 2008
3 //
4 #include <vector>
5 #include <string>
6 
8 
11 
12 namespace {
13  void dumpProlog(std::ostream& fOutput) {
14  fOutput << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
15  fOutput << "<!DOCTYPE root []>" << std::endl;
16  fOutput << "<ROOT>" << std::endl;
17  }
18 
19  void dumpRun(std::ostream& fOutput, unsigned fRun) {
20  fOutput << "<RUN>" << std::endl;
21  fOutput << " <RUN_TYPE>"
22  << "CastorDbXml"
23  << "</RUN_TYPE>" << std::endl;
24  fOutput << " <RUN_NUMBER>" << fRun << "</RUN_NUMBER>" << std::endl;
25  fOutput << "</RUN>" << std::endl;
26  }
27 
28  void dumpHeader(std::ostream& fOutput, unsigned fRun, const std::string& fTableName, const std::string& fTypeName) {
29  fOutput << " <HEADER>" << std::endl;
30  fOutput << " <TYPE>" << std::endl;
31  fOutput << " <EXTENSION_TABLE_NAME>" << fTableName << "</EXTENSION_TABLE_NAME>" << std::endl;
32  fOutput << " <NAME>" << fTypeName << "</NAME>" << std::endl;
33  fOutput << " </TYPE>" << std::endl;
34  dumpRun(fOutput, fRun);
35  fOutput << " </HEADER>" << std::endl;
36  }
37 
38  void dumpFooter(std::ostream& fOutput) { fOutput << "</ROOT>" << std::endl; }
39 
40  void dumpChannelId(std::ostream& fOutput, DetId fChannel) {
41  CastorText2DetIdConverter converter(fChannel);
42  fOutput << "<CHANNEL> " << std::endl;
43  fOutput << " <EXTENSION_TABLE_NAME>HCAL_CHANNELS</EXTENSION_TABLE_NAME> " << std::endl;
44  fOutput << " <ETA>" << abs(converter.getField(1)) << "</ETA>" << std::endl;
45  fOutput << " <PHI>" << converter.getField(2) << "</PHI> " << std::endl;
46  fOutput << " <DEPTH>" << converter.getField(3) << "</DEPTH> " << std::endl;
47  fOutput << " <Z>" << (converter.getField(1) > 0 ? "1" : "-1") << "</Z> " << std::endl;
48  fOutput << " <DETECTOR_NAME>" << converter.getFlavor() << "</DETECTOR_NAME> " << std::endl;
49  fOutput << " <HCAL_CHANNEL_ID>" << converter.getId().rawId() << "</HCAL_CHANNEL_ID> " << std::endl;
50  fOutput << "</CHANNEL>" << std::endl;
51  fOutput << std::endl;
52  }
53 
54  void dumpData(std::ostream& fOutput, const float* fValues, const CastorPedestalWidth& fErrors) {
55  fOutput << "<DATA> " << std::endl;
56  fOutput << " <CAPACITOR_0_VALUE>" << fValues[0] << "</CAPACITOR_0_VALUE> " << std::endl;
57  fOutput << " <CAPACITOR_1_VALUE>" << fValues[1] << "</CAPACITOR_1_VALUE> " << std::endl;
58  fOutput << " <CAPACITOR_2_VALUE>" << fValues[2] << "</CAPACITOR_2_VALUE> " << std::endl;
59  fOutput << " <CAPACITOR_3_VALUE>" << fValues[3] << "</CAPACITOR_3_VALUE> " << std::endl;
60  fOutput << " <SIGMA_0_0>" << fErrors.getSigma(0, 0) << "</SIGMA_0_0> " << std::endl;
61  fOutput << " <SIGMA_1_1>" << fErrors.getSigma(1, 1) << "</SIGMA_1_1> " << std::endl;
62  fOutput << " <SIGMA_2_2>" << fErrors.getSigma(2, 2) << "</SIGMA_2_2> " << std::endl;
63  fOutput << " <SIGMA_3_3>" << fErrors.getSigma(3, 3) << "</SIGMA_3_3> " << std::endl;
64  fOutput << " <SIGMA_0_1>" << fErrors.getSigma(1, 0) << "</SIGMA_0_1> " << std::endl;
65  fOutput << " <SIGMA_0_2>" << fErrors.getSigma(2, 0) << "</SIGMA_0_2> " << std::endl;
66  fOutput << " <SIGMA_0_3>" << fErrors.getSigma(3, 0) << "</SIGMA_0_3> " << std::endl;
67  fOutput << " <SIGMA_1_2>" << fErrors.getSigma(2, 1) << "</SIGMA_1_2> " << std::endl;
68  fOutput << " <SIGMA_1_3>" << fErrors.getSigma(3, 1) << "</SIGMA_1_3> " << std::endl;
69  fOutput << " <SIGMA_2_3>" << fErrors.getSigma(3, 2) << "</SIGMA_2_3> " << std::endl;
70  fOutput << "</DATA> " << std::endl;
71  }
72 
73  void dumpData(std::ostream& fOutput, const float* fValues, const float* fErrors) {
74  fOutput << "<DATA> " << std::endl;
75  fOutput << " <CAPACITOR_0_VALUE>" << fValues[0] << "</CAPACITOR_0_VALUE> " << std::endl;
76  fOutput << " <CAPACITOR_1_VALUE>" << fValues[1] << "</CAPACITOR_1_VALUE> " << std::endl;
77  fOutput << " <CAPACITOR_2_VALUE>" << fValues[2] << "</CAPACITOR_2_VALUE> " << std::endl;
78  fOutput << " <CAPACITOR_3_VALUE>" << fValues[3] << "</CAPACITOR_3_VALUE> " << std::endl;
79  fOutput << " <CAPACITOR_0_ERROR>" << fErrors[0] << "</CAPACITOR_0_ERROR> " << std::endl;
80  fOutput << " <CAPACITOR_1_ERROR>" << fErrors[1] << "</CAPACITOR_1_ERROR> " << std::endl;
81  fOutput << " <CAPACITOR_2_ERROR>" << fErrors[2] << "</CAPACITOR_2_ERROR> " << std::endl;
82  fOutput << " <CAPACITOR_3_ERROR>" << fErrors[3] << "</CAPACITOR_3_ERROR> " << std::endl;
83  fOutput << "</DATA> " << std::endl;
84  }
85 
86  void dumpDataset(std::ostream& fOutput,
87  unsigned fVersion = 0,
88  const std::string& fFileName = "",
89  const std::string& fDescription = "") {
90  fOutput << "<DATA_SET>" << std::endl;
91  fOutput << " <VERSION>" << fVersion << "</VERSION>" << std::endl;
92  if (!fFileName.empty())
93  fOutput << " <DATA_FILE_NAME>" << fFileName << "</DATA_FILE_NAME>" << std::endl;
94  if (!fDescription.empty())
95  fOutput << " <COMMENT_DESCRIPTION>" << fDescription << "</COMMENT_DESCRIPTION>" << std::endl;
96  }
97 
98  void endDataset(std::ostream& fOutput) { fOutput << "</DATA_SET>" << std::endl; }
99 
100  void dumpMapping(std::ostream& fOutput,
101  unsigned fRun,
102  const std::string& fKind,
103  unsigned long fGMTIOVBegin,
104  unsigned long fGMTIOVEnd,
105  const std::string& fTag,
106  unsigned fVersion,
107  const std::vector<DetId>& fChannels) {
108  const std::string IOV_ID = "IOV_ID";
109  const std::string TAG_ID = "TAG_ID";
110  fOutput << "<ELEMENTS>" << std::endl;
111  // set channels affected
112  int i = fChannels.size();
113  while (--i >= 0) {
114  fOutput << "<DATA_SET id=\"" << i << "\">" << std::endl;
115  dumpRun(fOutput, fRun);
116  fOutput << "<KIND_OF_CONDITION><NAME>" << fKind << "</NAME></KIND_OF_CONDITION>" << std::endl;
117  dumpChannelId(fOutput, fChannels[i]);
118  fOutput << "<VERSION>" << fVersion << "</VERSION>" << std::endl;
119  fOutput << "</DATA_SET>" << std::endl;
120  }
121  // set IOV
122  fOutput << "<IOV id=\"" << IOV_ID << "\">";
123  fOutput << " <INTERVAL_OF_VALIDITY_BEGIN>" << fGMTIOVBegin << "</INTERVAL_OF_VALIDITY_BEGIN>" << std::endl;
124  fOutput << " <INTERVAL_OF_VALIDITY_END>" << fGMTIOVEnd << "</INTERVAL_OF_VALIDITY_END>" << std::endl;
125  fOutput << "</IOV>" << std::endl;
126  // set TAG
127  fOutput << "<TAG id=\"" << TAG_ID << "\" mode=\"create\">" << std::endl;
128  fOutput << " <TAG_NAME>" << fTag << "</TAG_NAME>" << std::endl;
129  fOutput << " <DETECTOR_NAME>HCAL</DETECTOR_NAME>" << std::endl;
130  fOutput << " <COMMENT_DESCRIPTION>Automatically created by CastorDbXml</COMMENT_DESCRIPTION>" << std::endl;
131  fOutput << "</TAG>" << std::endl;
132 
133  fOutput << "</ELEMENTS>" << std::endl;
134 
135  // mapping itself
136  fOutput << "<MAPS>" << std::endl;
137  fOutput << "<TAG idref=\"" << TAG_ID << "\">" << std::endl;
138  fOutput << "<IOV idref=\"" << IOV_ID << "\">" << std::endl;
139  i = fChannels.size();
140  while (--i >= 0) {
141  fOutput << "<DATA_SET idref=\"" << i << "\"/>" << std::endl;
142  }
143  fOutput << "</IOV>" << std::endl;
144  fOutput << "</TAG>" << std::endl;
145  fOutput << "</MAPS>" << std::endl;
146  }
147 } // namespace
148 
149 bool CastorDbXml::dumpObject(std::ostream& fOutput,
150  unsigned fRun,
151  unsigned long fGMTIOVBegin,
152  unsigned long fGMTIOVEnd,
153  const std::string& fTag,
154  unsigned fVersion,
155  const CastorPedestals& fObject) {
156  float dummyError = 0.0001;
157  std::cout << "CastorDbXml::dumpObject-> set default errors: 0.0001, 0.0001, 0.0001, 0.0001" << std::endl;
158  CastorPedestalWidths widths(fObject.isADC());
159  std::vector<DetId> channels = fObject.getAllChannels();
160  for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
161  CastorPedestalWidth item(*channel);
162  for (int iCapId = 1; iCapId <= 4; iCapId++) {
163  item.setSigma(iCapId, iCapId, dummyError * dummyError);
164  }
165  widths.addValues(item);
166  }
167  return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
168 }
169 
170 bool CastorDbXml::dumpObject(std::ostream& fOutput,
171  unsigned fRun,
172  unsigned long fGMTIOVBegin,
173  unsigned long fGMTIOVEnd,
174  const std::string& fTag,
175  unsigned fVersion,
176  const CastorPedestals& fObject,
177  const CastorPedestalWidths& fError) {
178  const std::string KIND = "HCAL_PEDESTALS_V2";
179 
180  dumpProlog(fOutput);
181  dumpHeader(fOutput, fRun, KIND, KIND);
182 
183  std::vector<DetId> channels = fObject.getAllChannels();
184  for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
185  DetId chId = *channel;
186  const float* values = fObject.getValues(chId)->getValues();
187  const CastorPedestalWidth* errors = fError.getValues(chId);
188  if (!values) {
189  std::cerr << "CastorDbXml::dumpObject-> Can not get data for channel "
190  << CastorText2DetIdConverter(chId).toString() << std::endl;
191  continue;
192  }
193  if (!errors) {
194  std::cerr << "CastorDbXml::dumpObject-> Can not get errors for channel "
195  << CastorText2DetIdConverter(chId).toString() << ". Use defaults" << std::endl;
196  continue;
197  }
198  dumpDataset(fOutput, fVersion, "", "");
199  dumpChannelId(fOutput, chId);
200  dumpData(fOutput, values, *errors);
201  endDataset(fOutput);
202  }
203  dumpMapping(fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
204 
205  dumpFooter(fOutput);
206  return true;
207 }
208 
209 bool CastorDbXml::dumpObject(std::ostream& fOutput,
210  unsigned fRun,
211  unsigned long fGMTIOVBegin,
212  unsigned long fGMTIOVEnd,
213  const std::string& fTag,
214  unsigned fVersion,
215  const CastorGains& fObject) {
216  float dummyErrors[4] = {0., 0., 0., 0.};
217  std::cout << "CastorDbXml::dumpObject-> set default errors: 4 x 0.0" << std::endl;
218 
219  CastorGainWidths widths;
220  std::vector<DetId> channels = fObject.getAllChannels();
221  for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
222  CastorGainWidth item(*channel, dummyErrors[0], dummyErrors[1], dummyErrors[2], dummyErrors[3]);
223  widths.addValues(item);
224  }
225 
226  return dumpObject(fOutput, fRun, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, fObject, widths);
227 }
228 
229 bool CastorDbXml::dumpObject(std::ostream& fOutput,
230  unsigned fRun,
231  unsigned long fGMTIOVBegin,
232  unsigned long fGMTIOVEnd,
233  const std::string& fTag,
234  unsigned fVersion,
235  const CastorGains& fObject,
236  const CastorGainWidths& fError) {
237  const std::string KIND = "HCAL Gains";
238  const std::string TABLE = "HCAL_GAIN_PEDSTL_CALIBRATIONS";
239 
240  dumpProlog(fOutput);
241  dumpHeader(fOutput, fRun, TABLE, KIND);
242 
243  std::vector<DetId> channels = fObject.getAllChannels();
244  for (std::vector<DetId>::iterator channel = channels.begin(); channel != channels.end(); ++channel) {
245  DetId chId = *channel;
246  const float* values = fObject.getValues(chId)->getValues();
247  const float* errors = fError.getValues(chId)->getValues();
248  if (!values) {
249  std::cerr << "CastorDbXml::dumpObject-> Can not get data for channel "
250  << CastorText2DetIdConverter(chId).toString() << std::endl;
251  continue;
252  }
253  if (!errors) {
254  std::cerr << "CastorDbXml::dumpObject-> Can not get errors for channel "
255  << CastorText2DetIdConverter(chId).toString() << ". Use defaults" << std::endl;
256  continue;
257  }
258  dumpDataset(fOutput, fVersion, "", "");
259  dumpChannelId(fOutput, chId);
260  dumpData(fOutput, values, errors);
261  endDataset(fOutput);
262  }
263  dumpMapping(fOutput, fRun, KIND, fGMTIOVBegin, fGMTIOVEnd, fTag, fVersion, channels);
264 
265  dumpFooter(fOutput);
266  return true;
267 }
void setSigma(int fCapId1, int fCapId2, float fSigma)
std::vector< DetId > getAllChannels() const
bool dumpObject(std::ostream &fOutput, unsigned fRun, unsigned long fGMTIOVBegin, unsigned long fGMTIOVEnd, const std::string &fTag, unsigned fVersion, const CastorPedestals &fObject, const CastorPedestalWidths &fError)
Definition: CastorDbXml.cc:170
const Item * getValues(DetId fId, bool throwOnFail=true) const
float getSigma(int fCapId1, int fCapId2) const
get correlation element for capId1/2 = 0..3
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const float * getValues() const
get value for all capId = 0..3
Definition: DetId.h:17
bool dumpObject(std::ostream &fOutput, const CastorPedestals &fObject)
bool isADC() const
bool addValues(const Item &myItem)
const float * getValues() const
get value for all capId = 0..3
tuple cout
Definition: gather_cfg.py:144
const float * getValues() const
get value for all capId = 0..3
Definition: CastorGain.h:16