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