CMS 3D CMS Logo

CTPPSPixelDAQMappingESSourceXML.cc
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  *
4  * Authors:
5  * F.Ferro ferro@ge.infn.it
6  * H. Malbouisson malbouis@cern.ch
7  *
8  ****************************************************************************/
9 
21 
23 
30 
31 #include <xercesc/parsers/XercesDOMParser.hpp>
32 #include <xercesc/dom/DOM.hpp>
33 #include <xercesc/sax/HandlerBase.hpp>
34 #include <xercesc/util/XMLString.hpp>
35 #include <xercesc/util/PlatformUtils.hpp>
36 
37 
38 
39 #include <memory>
40 #include <sstream>
41 
42 //#define DEBUG 1
43 
44 //----------------------------------------------------------------------------------------------------
45 
46 using namespace std;
47 
52 {
53 public:
54 
56  static const std::string tagRPixPlane;
57  static const std::string tagROC;
58  static const std::string tagPixel;
60  static const std::string tagArm;
61 
63  static const std::string tagRPStation;
64  static const std::string tagRPPot;
65 
66 
69 
70  std::unique_ptr<CTPPSPixelAnalysisMask> produceCTPPSPixelAnalysisMask( const CTPPSPixelAnalysisMaskRcd & );
71  std::unique_ptr<CTPPSPixelDAQMapping> produceCTPPSPixelDAQMapping( const CTPPSPixelDAQMappingRcd &);
72 
73 private:
74  unsigned int verbosity;
75 
77  string subSystemName;
78 
80  std::vector<std::string> mappingFileNames;
81 
82  struct ConfigBlock
83  {
86 
88  std::vector<std::string> mappingFileNames;
89 
91  std::vector<std::string> maskFileNames;
92  };
93 
94  vector<ConfigBlock> configuration;
95 
97  unsigned int currentBlock;
98 
101 
103  enum NodeType { nUnknown, nSkip, nTop, nArm, nRPStation, nRPPot, nRPixPlane, nROC, nPixel };
104 
106  enum ParseType { pMapping, pMask };
107 
109  void ParseXML(ParseType, const string &file, const std::unique_ptr<CTPPSPixelDAQMapping>&, const std::unique_ptr<CTPPSPixelAnalysisMask>&);
110 
111 
112 
114  void ParseTreePixel(ParseType, xercesc::DOMNode *, NodeType, unsigned int parentID,
115  const std::unique_ptr<CTPPSPixelDAQMapping>&, const std::unique_ptr<CTPPSPixelAnalysisMask>&);
116 
117 private:
119  string CompleteFileName(const string &fn);
120 
122  bool Test(xercesc::DOMNode *node, const std::string &name)
123  {
124  return !(name.compare(cms::xerces::toString(node->getNodeName())));
125  }
126 
128  NodeType GetNodeType(xercesc::DOMNode *);
129 
131  string GetNodeContent(xercesc::DOMNode *parent)
132  {
133  return cms::xerces::toString(parent->getTextContent());
134  }
135 
137  string GetNodeValue(xercesc::DOMNode *node)
138  {
139  return cms::xerces::toString(node->getNodeValue());
140  }
141 
143  CTPPSPixelFramePosition ChipFramePosition(xercesc::DOMNode *chipnode);
144 
145  void GetPixels(xercesc::DOMNode *n, std::set<std::pair<unsigned char, unsigned char> > &pixels);
146 
148  {
149  return ((type == nArm)||(type == nRPStation)||(type == nRPPot)||(type == nRPixPlane)||(type == nROC));
150  }
151 
152 
153 protected:
155  void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&, const edm::IOVSyncValue&, edm::ValidityInterval&) override;
156 };
157 
158 //----------------------------------------------------------------------------------------------------
159 
160 using namespace std;
161 using namespace edm;
162 using namespace xercesc;
163 
164 const string CTPPSPixelDAQMappingESSourceXML::tagROC="roc";
165 const string CTPPSPixelDAQMappingESSourceXML::tagPixel="pixel";
166 const string CTPPSPixelDAQMappingESSourceXML::tagAnalysisMask="analysisMask";
167 
168 // common XML position tags
169 const string CTPPSPixelDAQMappingESSourceXML::tagArm = "arm";
170 
171 // specific RP XML tags
172 const string CTPPSPixelDAQMappingESSourceXML::tagRPStation = "station";
173 const string CTPPSPixelDAQMappingESSourceXML::tagRPPot = "rp_detector_set";
174 const string CTPPSPixelDAQMappingESSourceXML::tagRPixPlane = "rpix_plane";
175 
176 
177 static const unsigned int offsetROCinDetId = 13;
178 //static const unsigned int maskROCinDetId = 0x3;
179 
181  verbosity(conf.getUntrackedParameter<unsigned int>("verbosity", 0)),
182  subSystemName(conf.getUntrackedParameter<string>("subSystem")),
183  currentBlock(0),
184  currentBlockValid(false)
185 {
186  for (const auto it : conf.getParameter<vector<ParameterSet>>("configuration"))
187  {
188  ConfigBlock b;
189  b.validityRange = it.getParameter<EventRange>("validityRange");
190  b.mappingFileNames = it.getParameter< vector<string> >("mappingFileNames");
191  b.maskFileNames = it.getParameter< vector<string> >("maskFileNames");
192  configuration.push_back(b);
193  }
194 
196  findingRecord<CTPPSPixelAnalysisMaskRcd>();
197 
199  findingRecord<CTPPSPixelDAQMappingRcd>();
200 
201 
202  LogVerbatim("CTPPSPixelDAQMappingESSourceXML") << " Inside CTPPSPixelDAQMappingESSourceXML";
203 
204 
205 }
206 
208  const edm::IOVSyncValue& iosv, edm::ValidityInterval& oValidity)
209 {
210  LogVerbatim("CTPPSPixelDAQMappingESSourceXML")
211  << ">> CTPPSPixelDAQMappingESSourceXML::setIntervalFor(" << key.name() << ")";
212 
213  LogVerbatim("CTPPSPixelDAQMappingESSourceXML")
214  << " run=" << iosv.eventID().run() << ", event=" << iosv.eventID().event();
215 
216  currentBlockValid = false;
217  for (unsigned int idx = 0; idx < configuration.size(); ++idx)
218  {
219  const auto &bl = configuration[idx];
220 
221  EventID startEventID = bl.validityRange.startEventID();
222  if (startEventID == EventID(1, 0, 1))
223  startEventID = EventID(1, 0, 0);
224 
225  if (startEventID <= iosv.eventID() && iosv.eventID() <= bl.validityRange.endEventID())
226  {
227  currentBlockValid = true;
228  currentBlock = idx;
229 
230  const IOVSyncValue begin(startEventID);
231  const IOVSyncValue end(bl.validityRange.endEventID());
232  oValidity = ValidityInterval(begin, end);
233 
234  LogVerbatim("CTPPSPixelDAQMappingESSourceXML")
235  << " block found: index=" << currentBlock
236  << ", interval=(" << startEventID << " - " << bl.validityRange.endEventID() << ")";
237 
238  return;
239  }
240  }
241 
242  if (!currentBlockValid)
243  {
244  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::setIntervalFor") <<
245  "No configuration for event " << iosv.eventID();
246  }
247 }
248 
249 
250 
252 {
253 }
254 
255 
256 
258 {
259  FileInPath fip(fn);
260  return fip.fullPath();
261 }
262 
263 
264 
266 {
267  assert(currentBlockValid);
268 
269  auto mapping = std::make_unique<CTPPSPixelDAQMapping>();
270  auto mask = std::make_unique<CTPPSPixelAnalysisMask>();
271 
272 // initialize Xerces
273 
275 
276 // load mapping files
277  for (const auto &fn : configuration[currentBlock].mappingFileNames)
279 
280 // load mask files
281  for (const auto &fn : configuration[currentBlock].maskFileNames)
282  ParseXML(pMask, CompleteFileName(fn), mapping, mask);
283 
284 // release Xerces
286 
287 // commit the product
288  return mapping;
289 }
290 
292 {
293  assert(currentBlockValid);
294 
295  auto mapping = std::make_unique<CTPPSPixelDAQMapping>();
296  auto mask = std::make_unique<CTPPSPixelAnalysisMask>();
297 
298 // initialize Xerces
299 
301 
302 // load mapping files
303  for (const auto &fn : configuration[currentBlock].mappingFileNames)
305 
306 // load mask files
307  for (const auto &fn : configuration[currentBlock].maskFileNames)
308  ParseXML(pMask, CompleteFileName(fn), mapping, mask);
309 
310 // release Xerces
312 
313 // commit the products
314 //return edm::es::products(mapping, mask);
315  return mask;
316 }
317 
318 //----------------------------------------------------------------------------------------------------
319 
321  const std::unique_ptr<CTPPSPixelDAQMapping> &mapping, const std::unique_ptr<CTPPSPixelAnalysisMask> &mask)
322 {
323  unique_ptr<XercesDOMParser> parser(new XercesDOMParser());
324  parser->parse(file.c_str());
325 
326  DOMDocument* domDoc = parser->getDocument();
327 
328  if (!domDoc)
329  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseXML") << "Cannot parse file `" << file
330  << "' (domDoc = NULL)." << endl;
331 
332  DOMElement* elementRoot = domDoc->getDocumentElement();
333 
334  if (!elementRoot)
335  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseXML") << "File `" <<
336  file << "' is empty." << endl;
337 
338  ParseTreePixel(pType, elementRoot, nTop, 0, mapping, mask);
339 
340 
341 }
342 
343 //-----------------------------------------------------------------------------------------------------------
344 
346  unsigned int parentID, const std::unique_ptr<CTPPSPixelDAQMapping>& mapping,
347  const std::unique_ptr<CTPPSPixelAnalysisMask>& mask)
348 {
349 #ifdef DEBUG
350  printf(">> CTPPSPixelDAQMappingESSourceXML::ParseTreeRP(%s, %u, %u)\n", cms::xerces::toString(parent->getNodeName()),
351  parentType, parentID);
352 #endif
353 
354  DOMNodeList *children = parent->getChildNodes();
355 
356  for (unsigned int i = 0; i < children->getLength(); i++)
357  {
358  DOMNode *n = children->item(i);
359  if (n->getNodeType() != DOMNode::ELEMENT_NODE)
360  continue;
361 
363 
364 #ifdef DEBUG
365  printf("\tname = %s, type = %u\n", cms::xerces::toString(n->getNodeName()), type);
366 #endif
367 
368  // structure control
369  if (!PixelNode(type))
370  continue;
371 
372  NodeType expectedParentType;
373  switch (type)
374  {
375  case nArm: expectedParentType = nTop; break;
376  case nRPStation: expectedParentType = nArm; break;
377  case nRPPot: expectedParentType = nRPStation; break;
378  case nRPixPlane: expectedParentType = nRPPot; break;
379  case nROC: expectedParentType = nRPixPlane; break;
380  case nPixel: expectedParentType = nROC; break;
381  default: expectedParentType = nUnknown; break;
382  }
383 
384  if (expectedParentType != parentType)
385  {
386  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML") << "Node " << cms::xerces::toString(n->getNodeName())
387  << " not allowed within " << cms::xerces::toString(parent->getNodeName()) << " block.\n";
388  }
389 
390  // parse tag attributes
391  unsigned int id = 0;
392  bool id_set = false;
393  bool fullMask = false;
394  DOMNamedNodeMap* attr = n->getAttributes();
395 
396  for (unsigned int j = 0; j < attr->getLength(); j++)
397  {
398  DOMNode *a = attr->item(j);
399 
400  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id"))
401  {
402  sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &id);
403  id_set = true;
404  }
405 
406  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "full_mask"))
407  fullMask = (strcmp(cms::xerces::toString(a->getNodeValue()).c_str(), "no") != 0);
408  }
409 
410  // content control
411  if (!id_set)
412  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseTreePixel") << "id not given for element `"
413  << cms::xerces::toString(n->getNodeName()) << "'" << endl;
414 
415  if (type == nRPixPlane && id > 5)
416  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseTreePixel") <<
417  "Plane IDs range from 0 to 5. id = " << id << " is invalid." << endl;
418 
419 #ifdef DEBUG
420  printf("\tID found: %u\n", id);
421 #endif
422 
423  // store mapping data
424  if (pType == pMapping && type == nROC)
425  {
426  const CTPPSPixelFramePosition &framepos = ChipFramePosition(n);
427  CTPPSPixelROCInfo rocInfo;
428  rocInfo.roc = id;
429 
430  const unsigned int armIdx = (parentID / 1000) % 10;
431  const unsigned int stIdx = (parentID / 100) % 10;
432  const unsigned int rpIdx = (parentID / 10) % 10;
433  const unsigned int plIdx = parentID % 10;
434 
435  rocInfo.iD = CTPPSPixelDetId(armIdx, stIdx, rpIdx, plIdx);
436 
437  mapping->insert(framepos, rocInfo);
438 
439  continue;
440  }
441 
442  // store mask data
443  if (pType == pMask && type == nROC)
444  {
445  const unsigned int armIdx = (parentID / 1000) % 10;
446  const unsigned int stIdx = (parentID / 100) % 10;
447  const unsigned int rpIdx = (parentID / 10) % 10;
448  const unsigned int plIdx = parentID % 10;
449 
450 
451  uint32_t symbId = (id << offsetROCinDetId);
452 
453  symbId |= CTPPSPixelDetId(armIdx, stIdx, rpIdx, plIdx);
454 
455 
457  am.fullMask = fullMask;
458  GetPixels(n, am.maskedPixels);
459 
460  mask->insert(symbId, am);
461 
462  continue;
463  }
464 
465  // recursion (deeper in the tree)
466  ParseTreePixel(pType, n, type, parentID * 10 + id, mapping, mask);
467  }
468 }
469 
470 
471 //----------------------------------------------------------------------------------------------------
472 
474 {
476  unsigned char attributeFlag = 0;
477 
478  DOMNamedNodeMap* attr = chipnode->getAttributes();
479  for (unsigned int j = 0; j < attr->getLength(); j++)
480  {
481  DOMNode *a = attr->item(j);
482 
483  if (fp.setXMLAttribute(cms::xerces::toString(a->getNodeName()), cms::xerces::toString(a->getNodeValue()), attributeFlag) > 1)
484  {
485  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML") <<
486  "Unrecognized tag `" << cms::xerces::toString(a->getNodeName()) <<
487  "' or incompatible value `" << cms::xerces::toString(a->getNodeValue()) <<
488  "'." << endl;
489  }
490  }
491 
492  if (!fp.checkXMLAttributeFlag(attributeFlag))
493  {
494  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML") <<
495  "Wrong/incomplete DAQ channel specification (attributeFlag = " << attributeFlag << ")." << endl;
496  }
497 
498  return fp;
499 }
500 
501 //----------------------------------------------------------------------------------------------------
502 
504 {
505 // common node types
506  if (Test(n, tagArm)) return nArm;
507  if (Test(n, tagROC)) return nROC;
508 
509 // RP node types
510  if (Test(n, tagRPStation)) return nRPStation;
511  if (Test(n, tagRPPot)) return nRPPot;
512  if (Test(n, tagRPixPlane)) return nRPixPlane;
513 
514  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::GetNodeType") << "Unknown tag `"
515  << cms::xerces::toString(n->getNodeName()) << "'.\n";
516 }
517 
518 //----------------------------------------------------------------------------------------------------
519 
520 void CTPPSPixelDAQMappingESSourceXML::GetPixels(xercesc::DOMNode *n, set<std::pair<unsigned char, unsigned char> > &pixels)
521 {
522  DOMNodeList *children = n->getChildNodes();
523  for (unsigned int i = 0; i < children->getLength(); i++)
524  {
525  DOMNode *n = children->item(i);
526  if (n->getNodeType() != DOMNode::ELEMENT_NODE || !Test(n, "pixel"))
527  continue;
528 
529  DOMNamedNodeMap* attr = n->getAttributes();
530  bool pixelSet = false;
531  bool rowSet = false;
532  bool colSet = false;
533  std::pair<unsigned int, unsigned int> currentPixel;
534  for (unsigned int j = 0; j < attr->getLength(); j++)
535  {
536  DOMNode *a = attr->item(j);
537 
538  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "row"))
539  {
540  unsigned int row = 0;
541  sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &row);
542  currentPixel.first = row;
543  rowSet = true;
544  }
545  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "col"))
546  {
547  unsigned int col = 0;
548  sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &col);
549  currentPixel.second = col;
550  colSet = true;
551  }
552 
553  pixelSet = rowSet & colSet;
554  if(pixelSet){
555  pixels.insert(currentPixel);
556  break;
557  }
558  }
559 
560 
561 
562  if (!pixelSet)
563  {
564  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::GetChannels") <<
565  "Channel tags must have a row or col attribute.";
566  }
567  }
568 }
569 
570 //----------------------------------------------------------------------------------------------------
571 
RunNumber_t run() const
Definition: EventID.h:39
string GetNodeContent(xercesc::DOMNode *parent)
returns the content of the node
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
CTPPSPixelDAQMappingESSourceXML(const edm::ParameterSet &)
Contains mappind data related to a ROC.
string subSystemName
label of the CTPPS sub-system
void ParseTreePixel(ParseType, xercesc::DOMNode *, NodeType, unsigned int parentID, const std::unique_ptr< CTPPSPixelDAQMapping > &, const std::unique_ptr< CTPPSPixelAnalysisMask > &)
recursive method to extract Pixel-related information from the DOM tree
std::vector< std::string > mappingFileNames
the mapping files
const EventID & eventID() const
Definition: IOVSyncValue.h:42
void xercesTerminate()
Definition: Xerces.cc:23
static const unsigned int offsetROCinDetId
std::unique_ptr< CTPPSPixelAnalysisMask > produceCTPPSPixelAnalysisMask(const CTPPSPixelAnalysisMaskRcd &)
CTPPSPixelFramePosition ChipFramePosition(xercesc::DOMNode *chipnode)
extracts VFAT&#39;s DAQ channel from XML attributes
ParseType
whether to parse a mapping of a mask XML
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:19
string CompleteFileName(const string &fn)
adds the path prefix, if needed
void xercesInitialize()
Definition: Xerces.cc:18
unsigned char setXMLAttribute(const std::string &attribute, const std::string &value, unsigned char &flag)
void setWhatProduced(T *iThis, const es::Label &iLabel=es::Label())
Definition: ESProducer.h:115
Contains data on masked channels of a ROC.
uint32_t iD
the symbolic id
std::string toString(XMLCh const *toTranscode)
Loads CTPPSPixelDAQMapping and CTPPSPixelAnalysisMask from two XML files.
std::unique_ptr< CTPPSPixelDAQMapping > produceCTPPSPixelDAQMapping(const CTPPSPixelDAQMappingRcd &)
bool fullMask
whether all channels of the ROC shall be masked
unsigned int currentBlock
index of the current block in &#39;configuration&#39; array
std::vector< std::string > mappingFileNames
the mapping files
void ParseXML(ParseType, const string &file, const std::unique_ptr< CTPPSPixelDAQMapping > &, const std::unique_ptr< CTPPSPixelAnalysisMask > &)
parses XML file
static const std::string tagRPStation
RP XML tags.
#define end
Definition: vmac.h:37
bool Test(xercesc::DOMNode *node, const std::string &name)
returns true iff the node is of the given name
static bool checkXMLAttributeFlag(unsigned char flag)
returns true if all attributes have been set
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:92
bool currentBlockValid
flag whether the &#39;currentBlock&#39; index is valid
double b
Definition: hdecay.h:120
std::vector< std::string > maskFileNames
the mask files
string GetNodeValue(xercesc::DOMNode *node)
returns the value of the node
void GetPixels(xercesc::DOMNode *n, std::set< std::pair< unsigned char, unsigned char > > &pixels)
static const std::string tagArm
Common position tags.
#define begin
Definition: vmac.h:30
HLT enums.
double a
Definition: hdecay.h:121
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &) override
sets infinite validity of this data
col
Definition: cuy.py:1008
std::string fullPath() const
Definition: FileInPath.cc:184
std::set< std::pair< unsigned char, unsigned char > > maskedPixels
list of channels to be masked
NodeType GetNodeType(xercesc::DOMNode *)
determines node type