CMS 3D CMS Logo

CTPPSRPAlignmentCorrectionsMethods.cc
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * This is a part of CMS-TOTEM PPS offline software.
4  * Authors:
5  * Jan Kaspar
6  * Helena Malbouisson
7  * Clemencia Mora Herrera
8  *
9  ****************************************************************************/
10 
12 
17 
22 
24 
25 #include <xercesc/parsers/XercesDOMParser.hpp>
26 #include <xercesc/sax/HandlerBase.hpp>
27 #include <xercesc/util/XMLString.hpp>
28 #include <xercesc/util/PlatformUtils.hpp>
29 
30 #include <map>
31 #include <set>
32 
33 //----------------------------------------------------------------------------------------------------
34 
74 //----------------------------------------------------------------------------------------------------
76 {
77  if (str == "-inf")
79 
80  if (str == "+inf")
82 
83  size_t sep_pos = str.find(":");
84  const std::string &runStr = str.substr(0, sep_pos);
85  const std::string &lsStr = str.substr(sep_pos+1);
86 
87  return edm::IOVSyncValue(edm::EventID(atoi(runStr.c_str()), atoi(lsStr.c_str()), 1));
88 }
89 
90 //----------------------------------------------------------------------------------------------------
91 
93 {
94  if (val == edm::IOVSyncValue::beginOfTime())
95  return "-inf";
96 
97  if (val == edm::IOVSyncValue::endOfTime())
98  return "+inf";
99 
100  char buf[50];
101  sprintf(buf, "%u:%u", val.eventID().run(), val.eventID().luminosityBlock());
102  return buf;
103 }
104 
105 //----------------------------------------------------------------------------------------------------
106 
109 {
110  // prepare output
112 
113  // load DOM tree
114  try {
115  XMLPlatformUtils::Initialize();
116  } catch ( const XMLException& toCatch ) {
117  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "An XMLException caught with message: " << cms::xerces::toString( toCatch.getMessage() ) << ".";
118  }
119 
120  auto parser = std::make_unique<XercesDOMParser>();
121  parser->setValidationScheme( XercesDOMParser::Val_Always );
122  parser->setDoNamespaces( true );
123  parser->parse( fileName.c_str() );
124 
125  if ( !parser )
126  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "Cannot parse file `" << fileName << "' (parser = NULL).";
127 
128  DOMDocument* xmlDoc = parser->getDocument();
129 
130  if ( !xmlDoc )
131  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "Cannot parse file `" << fileName << "' (xmlDoc = NULL).";
132 
133  DOMElement* elementRoot = xmlDoc->getDocumentElement();
134  if ( !elementRoot )
135  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "File `" << fileName << "' is empty.";
136 
137  // extract useful information form the DOM tree
138  DOMNodeList* children = elementRoot->getChildNodes();
139  for ( unsigned int i = 0; i < children->getLength(); i++ )
140  {
141  DOMNode* node = children->item( i );
142  if ( node->getNodeType() != DOMNode::ELEMENT_NODE )
143  continue;
144 
145  const std::string node_name = cms::xerces::toString( node->getNodeName() );
146 
147  // check node type
148  unsigned char nodeType = 0;
149  if ( node_name == "iov" ) nodeType = 1;
150  else if ( node_name == "det" ) nodeType = 2;
151  else if ( node_name == "rp" ) nodeType = 3;
152 
153  if ( nodeType == 0 )
154  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "Unknown node `" << node_name << "'.";
155 
156  // for backward compatibility: support files with no iov block
157  if ( nodeType == 2 || nodeType == 3 )
158  {
160  output.insert(iov, getCorrectionsData(elementRoot));
161  break;
162  }
163 
164  // get attributes
166  bool first_set = false, last_set = false;
167  DOMNamedNodeMap* attrs = node->getAttributes();
168  for ( unsigned int j = 0; j < attrs->getLength(); j++ )
169  {
170  const DOMNode* attr = attrs->item( j );
171  const std::string attr_name = cms::xerces::toString( attr->getNodeName() );
172 
173  if ( attr_name == "first" )
174  {
175  first_set = true;
176  first = stringToIOVValue( cms::xerces::toString( attr->getNodeValue() ) );
177  }
178  else if ( attr_name == "last" ) {
179  last_set = true;
180  last = stringToIOVValue( cms::xerces::toString( attr->getNodeValue() ) );
181  }
182  else
183  edm::LogProblem("CTPPSRPAlignmentCorrectionsMethods") << ">> CTPPSRPAlignmentCorrectionsDataSequence::loadFromXML > Warning: unknown attribute `"
184  << attr_name << "'.";
185  }
186 
187  // interval of validity must be set
188  if ( !first_set || !last_set )
189  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "iov tag must have `first' and `last' attributes set.";
190 
191  // process data
193 
194  // save result
195  output.insert(edm::ValidityInterval(first, last), corrections);
196  }
197 
198  // clean up
199  parser.reset();
200  XMLPlatformUtils::Terminate();
201 
202  return output;
203 }
204 
205 
206 //----------------------------------------------------------------------------------------------------
207 
210 {
212 
213  DOMNodeList *children = root->getChildNodes();
214  for ( unsigned int i = 0; i < children->getLength(); i++ )
215  {
216  DOMNode *node = children->item( i );
217  if ( node->getNodeType() != DOMNode::ELEMENT_NODE )
218  continue;
219 
220  const std::string node_name = cms::xerces::toString( node->getNodeName() );
221 
222  // check node type
223  unsigned char nodeType = 0;
224  if ( node_name == "det" ) nodeType = 1;
225  else if ( node_name == "rp" ) nodeType = 2;
226 
227  if ( nodeType == 0 )
228  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "Unknown node `" << cms::xerces::toString( node->getNodeName() ) << "'.";
229 
230  // check children
231  if ( node->getChildNodes()->getLength() > 0 )
232  {
233  edm::LogProblem("CTPPSRPAlignmentCorrectionsMethods") << "LoadXMLFile > Warning: tag `" <<
234  cms::xerces::toString( node->getNodeName() ) << "' has " << node->getChildNodes()->getLength() << " children nodes - they will be all ignored.";
235  }
236 
237  // default values
238  double sh_x = 0., sh_y = 0., sh_z = 0., rot_x = 0., rot_y = 0., rot_z = 0.;
239  double sh_x_e = 0., sh_y_e = 0., sh_z_e = 0., rot_x_e = 0., rot_y_e = 0., rot_z_e = 0.;
240  unsigned int id = 0;
241  bool idSet = false;
242 
243  // get attributes
244  DOMNamedNodeMap* attr = node->getAttributes();
245  for ( unsigned int j = 0; j < attr->getLength(); j++ )
246  {
247  DOMNode *a = attr->item( j );
248  const std::string node_name = cms::xerces::toString( a->getNodeName() );
249 
250  if ( node_name == "id" )
251  {
252  id = cms::xerces::toUInt( a->getNodeValue() );
253  idSet = true;
254  }
255  else if ( node_name == "sh_x" ) sh_x = cms::xerces::toDouble( a->getNodeValue() );
256  else if ( node_name == "sh_x_e" ) sh_x_e = cms::xerces::toDouble( a->getNodeValue() );
257  else if ( node_name == "sh_y" ) sh_y = cms::xerces::toDouble( a->getNodeValue() );
258  else if ( node_name == "sh_y_e" ) sh_y_e = cms::xerces::toDouble( a->getNodeValue() );
259  else if ( node_name == "sh_z" ) sh_z = cms::xerces::toDouble( a->getNodeValue() );
260  else if ( node_name == "sh_z_e" ) sh_z_e = cms::xerces::toDouble( a->getNodeValue() );
261  else if ( node_name == "rot_x" ) rot_x = cms::xerces::toDouble( a->getNodeValue() );
262  else if ( node_name == "rot_x_e" ) rot_x_e = cms::xerces::toDouble( a->getNodeValue() );
263  else if ( node_name == "rot_y" ) rot_y = cms::xerces::toDouble( a->getNodeValue() );
264  else if ( node_name == "rot_y_e" ) rot_y_e = cms::xerces::toDouble( a->getNodeValue() );
265  else if ( node_name == "rot_z" ) rot_z = cms::xerces::toDouble( a->getNodeValue() );
266  else if ( node_name == "rot_z_e" ) rot_z_e = cms::xerces::toDouble( a->getNodeValue() );
267  else
268  edm::LogProblem("CTPPSRPAlignmentCorrectionsMethods") << ">> CTPPSRPAlignmentCorrectionsMethods::getCorrectionsData > Warning: unknown attribute `"
269  << cms::xerces::toString( a->getNodeName() ) << "'.";
270  }
271 
272  // id must be set
273  if ( !idSet )
274  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "Id not set for tag `" << cms::xerces::toString( node->getNodeName() ) << "'.";
275 
276  // build alignment
277  const CTPPSRPAlignmentCorrectionData align_corr(
278  sh_x*1e-3, sh_x_e*1e-3,
279  sh_y*1e-3, sh_y_e*1e-3,
280  sh_z*1e-3, sh_z_e*1e-3,
281  rot_x*1e-3, rot_x_e*1e-3,
282  rot_y*1e-3, rot_y_e*1e-3,
283  rot_z*1e-3, rot_z_e*1e-3
284  );
285 
286  // add the alignment to the right list
287  if ( nodeType == 1 ) result.addSensorCorrection( id, align_corr, true );
288  if ( nodeType == 2 ) result.addRPCorrection( id, align_corr, true );
289  }
290 
291  return result;
292 }
293 
294 
295 //----------------------------------------------------------------------------------------------------
296 //----------------------------------------------------------------------------------------------------
297 
298 void
300  bool wrSh_xy, bool wrSh_z, bool wrRot_xy, bool wrRot_z )
301 {
302  FILE* rf = fopen( fileName.c_str(), "w" );
303  if ( !rf )
304  throw cms::Exception("CTPPSRPAlignmentCorrectionsMethods") << "Cannot open file `" << fileName << "' to save alignments.";
305 
306  fprintf( rf, "<!-- Shifts in um, rotations in mrad. -->\n" );
307  fprintf( rf, "<xml DocumentType=\"AlignmentDescription\">\n" );
308 
309  // write all IOVs
310  for ( const auto &p : data )
311  {
312  fprintf( rf, "\t<iov first=\"%s\" last=\"%s\">\n",
313  iovValueToString(p.first.first()).c_str(),
314  iovValueToString(p.first.last()).c_str()
315  );
316 
317  writeXMLBlock( p.second, rf, precise, wrErrors, wrSh_xy, wrSh_z, wrRot_xy, wrRot_z );
318 
319  fprintf( rf, "\t</iov>\n" );
320  }
321 
322  fprintf( rf, "</xml>\n" );
323  fclose( rf );
324 }
325 
326 void
328  bool wrSh_xy, bool wrSh_z, bool wrRot_xy, bool wrRot_z )
329 {
330  bool firstRP = true;
331  unsigned int prevRP = 0;
332  std::set<unsigned int> writtenRPs;
333 
334  const auto &sensors = data.getSensorMap();
335  const auto &rps = data.getRPMap();
336 
337  for (auto it = sensors.begin(); it != sensors.end(); ++it)
338  {
339  CTPPSDetId sensorId(it->first);
340  unsigned int rpId = sensorId.getRPId();
341  unsigned int decRPId = sensorId.arm()*100 + sensorId.station()*10 + sensorId.rp();
342 
343  // start a RP block
344  if (firstRP || prevRP != rpId)
345  {
346  if (!firstRP)
347  fprintf(rf, "\n");
348  firstRP = false;
349 
350  fprintf(rf, "\t<!-- RP %3u -->\n", decRPId);
351 
352  auto rit = rps.find(rpId);
353  if (rit != rps.end())
354  {
355  fprintf(rf, "\t<rp id=\"%u\" ", rit->first);
356  writeXML( rit->second , rf, precise, wrErrors, wrSh_xy, wrSh_z, wrRot_xy, wrRot_z );
357  fprintf(rf, "/>\n");
358  writtenRPs.insert(rpId);
359  }
360  }
361  prevRP = rpId;
362 
363  // write plane id
364  unsigned int planeIdx = 1000;
365  if (sensorId.subdetId() == CTPPSDetId::sdTrackingStrip) planeIdx = TotemRPDetId(it->first).plane();
366  if (sensorId.subdetId() == CTPPSDetId::sdTrackingPixel) planeIdx = CTPPSPixelDetId(it->first).plane();
367  if (sensorId.subdetId() == CTPPSDetId::sdTimingDiamond) planeIdx = CTPPSDiamondDetId(it->first).plane();
368  fprintf(rf, "\t<!-- plane %u --> ", planeIdx);
369 
370  // write the correction
371  fprintf(rf, "<det id=\"%u\"", it->first);
372  writeXML(it->second, rf, precise, wrErrors, wrSh_xy, wrSh_z, wrRot_xy, wrRot_z);
373  fprintf(rf, "/>\n");
374  }
375 
376  // write remaining RPs
377  for (auto it = rps.begin(); it != rps.end(); ++it)
378  {
379  std::set<unsigned int>::iterator wit = writtenRPs.find(it->first);
380  if (wit == writtenRPs.end())
381  {
382  CTPPSDetId rpId(it->first);
383  unsigned int decRPId = rpId.arm()*100 + rpId.station()*10 + rpId.rp();
384 
385  if (!firstRP)
386  fprintf(rf, "\n");
387  firstRP = false;
388 
389  fprintf(rf, "\t<!-- RP %3u -->\n", decRPId);
390 
391  fprintf(rf, "\t<rp id=\"%u\" ", it->first);
392  writeXML(it->second, rf, precise, wrErrors, wrSh_xy, wrSh_z, wrRot_xy, wrRot_z);
393  fprintf(rf, "/>\n");
394  }
395  }
396  }
397 
398 //----------------------------------------------------------------------------------------------------
399 
400 #define WRITE(q, tag, dig, lim) \
401  if (precise) \
402  fprintf(f, " " tag "=\"%.15E\"", q*1E3);\
403  else \
404  if (fabs(q*1E3) < lim && q != 0) \
405  fprintf(f, " " tag "=\"%+8.1E\"", q*1E3);\
406  else \
407  fprintf(f, " " tag "=\"%+8." #dig "f\"", q*1E3);
408 
409 //----------------------------------------------------------------------------------------------------
410 
411 void
413  bool wrSh_xy, bool wrSh_z, bool wrRot_xy, bool wrRot_z )
414 {
415  if ( wrSh_xy )
416  {
417  WRITE( data.getShX(), "sh_x", 2, 0.1 );
418  WRITE( data.getShY(), "sh_y", 2, 0.1 );
419  if ( wrErrors )
420  {
421  WRITE( data.getShXUnc(), "sh_x_e", 2, 0.1 );
422  WRITE( data.getShYUnc(), "sh_y_e", 2, 0.1 );
423  }
424  }
425 
426  if ( wrSh_z )
427  {
428  WRITE( data.getShZ(), "sh_z", 2, 0.1 );
429  if ( wrErrors )
430  {
431  WRITE( data.getShZUnc(), "sh_z_e", 2, 0.1 );
432  }
433  }
434 
435  if ( wrRot_xy )
436  {
437  WRITE( data.getRotX(), "rot_x", 3, 0.01 );
438  WRITE( data.getRotY(), "rot_y", 3, 0.01 );
439  if ( wrErrors )
440  {
441  WRITE( data.getRotXUnc(), "rot_x_e", 3, 0.01 );
442  WRITE( data.getRotYUnc(), "rot_y_e", 3, 0.01 );
443  }
444  }
445 
446  if ( wrRot_z )
447  {
448  WRITE( data.getRotZ(), "rot_z", 3, 0.01 );
449  if ( wrErrors )
450  {
451  WRITE( data.getRotZUnc(), "rot_z_e", 3, 0.01 );
452  }
453  }
454 }
void addSensorCorrection(unsigned int, const CTPPSRPAlignmentCorrectionData &, bool sumErrors=true, bool addSh=true, bool addRot=true)
adds (merges) a RP correction on top of the current value
RunNumber_t run() const
Definition: EventID.h:39
Detector ID class for TOTEM Si strip detectors.
Definition: TotemRPDetId.h:30
uint32_t station() const
Definition: CTPPSDetId.h:58
uint32_t plane() const
static edm::IOVSyncValue stringToIOVValue(const std::string &)
const EventID & eventID() const
Definition: IOVSyncValue.h:40
Time sequence of alignment corrections. I/O methods have been factored out to: CondFormats/CTPPSReado...
static const IOVSyncValue & endOfTime()
Definition: IOVSyncValue.cc:82
uint32_t plane() const
Definition: TotemRPDetId.h:46
double toDouble(XMLCh const *toTranscode)
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
#define WRITE(q, tag, dig, lim)
static CTPPSRPAlignmentCorrectionsDataSequence loadFromXML(const std::string &fileName)
loads sequence of alignment corrections from XML file
std::string toString(XMLCh const *toTranscode)
static const IOVSyncValue & beginOfTime()
Definition: IOVSyncValue.cc:88
static std::string iovValueToString(const edm::IOVSyncValue &)
unsigned int toUInt(XMLCh const *toTranscode)
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
const mapType & getRPMap() const
returns the map of RP alignment corrections
uint32_t plane() const
double f[11][100]
CTPPSDetId getRPId() const
Definition: CTPPSDetId.h:78
uint32_t arm() const
Definition: CTPPSDetId.h:51
void addRPCorrection(unsigned int, const CTPPSRPAlignmentCorrectionData &, bool sumErrors=true, bool addSh=true, bool addRot=true)
const mapType & getSensorMap() const
returns the map of sensor alignment corrections
void insert(const edm::ValidityInterval &iov, const CTPPSRPAlignmentCorrectionsData &data)
Base class for CTPPS detector IDs.
Definition: CTPPSDetId.h:32
Container for CTPPS RP alignment corrections. The corrections are stored on two levels - RP and senso...
static void writeXMLBlock(const CTPPSRPAlignmentCorrectionsData &, FILE *, bool precise=false, bool wrErrors=true, bool wrSh_xy=true, bool wrSh_z=false, bool wrRot_xy=false, bool wrRot_z=true)
writes a block of corrections into a file
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
double a
Definition: hdecay.h:121
static void writeToXML(const CTPPSRPAlignmentCorrectionsDataSequence &seq, const std::string &fileName, bool precise=false, bool wrErrors=true, bool wrSh_xy=true, bool wrSh_z=false, bool wrRot_xy=false, bool wrRot_z=true)
writes sequence of alignment corrections into a single XML file
Detector ID class for CTPPS Timing Diamond detectors. Bits [19:31] : Assigend in CTPPSDetId Calss Bit...
#define str(s)
static void writeXML(const CTPPSRPAlignmentCorrectionData &data, FILE *f, bool precise, bool wrErrors, bool wrSh_xy, bool wrSh_z, bool wrRot_xy, bool wrRot_z)
writes data of a correction in XML format
static CTPPSRPAlignmentCorrectionsData getCorrectionsData(xercesc::DOMNode *)
load corrections data corresponding to one IOV
Alignment correction for an element of the CT-PPS detector. Within the geometry description, every sensor (more generally every element) is given its translation and rotation. These two quantities shall be understood in local-to-global coordinate transform. That is, if r_l is a point in local coordinate system and x_g in global, then it holds.
uint32_t rp() const
Definition: CTPPSDetId.h:65