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 #include <memory>
38 #include <sstream>
39 
40 //#define DEBUG 1
41 
42 //----------------------------------------------------------------------------------------------------
43 
44 using namespace std;
45 
50 public:
52  static const std::string tagRPixPlane;
53  static const std::string tagROC;
54  static const std::string tagPixel;
56  static const std::string tagArm;
57 
59  static const std::string tagRPStation;
60  static const std::string tagRPPot;
61 
64 
65  std::unique_ptr<CTPPSPixelAnalysisMask> produceCTPPSPixelAnalysisMask(const CTPPSPixelAnalysisMaskRcd &);
66  std::unique_ptr<CTPPSPixelDAQMapping> produceCTPPSPixelDAQMapping(const CTPPSPixelDAQMappingRcd &);
67 
68 private:
69  unsigned int verbosity;
70 
72  string subSystemName;
73 
75  std::vector<std::string> mappingFileNames;
76 
77  struct ConfigBlock {
80 
82  std::vector<std::string> mappingFileNames;
83 
85  std::vector<std::string> maskFileNames;
86  };
87 
88  vector<ConfigBlock> configuration;
89 
91  unsigned int currentBlock;
92 
95 
97  enum NodeType { nUnknown, nSkip, nTop, nArm, nRPStation, nRPPot, nRPixPlane, nROC, nPixel };
98 
100  enum ParseType { pMapping, pMask };
101 
103  void ParseXML(ParseType,
104  const string &file,
105  const std::unique_ptr<CTPPSPixelDAQMapping> &,
106  const std::unique_ptr<CTPPSPixelAnalysisMask> &);
107 
109  void ParseTreePixel(ParseType,
110  xercesc::DOMNode *,
111  NodeType,
112  unsigned int parentID,
113  const std::unique_ptr<CTPPSPixelDAQMapping> &,
114  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  return !(name.compare(cms::xerces::toString(node->getNodeName())));
123  }
124 
126  NodeType GetNodeType(xercesc::DOMNode *);
127 
129  string GetNodeContent(xercesc::DOMNode *parent) { return cms::xerces::toString(parent->getTextContent()); }
130 
132  string GetNodeValue(xercesc::DOMNode *node) { return cms::xerces::toString(node->getNodeValue()); }
133 
135  CTPPSPixelFramePosition ChipFramePosition(xercesc::DOMNode *chipnode);
136 
137  void GetPixels(xercesc::DOMNode *n, std::set<std::pair<unsigned char, unsigned char>> &pixels);
138 
140  return ((type == nArm) || (type == nRPStation) || (type == nRPPot) || (type == nRPixPlane) || (type == nROC));
141  }
142 
143 protected:
145  void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
146  const edm::IOVSyncValue &,
147  edm::ValidityInterval &) override;
148 };
149 
150 //----------------------------------------------------------------------------------------------------
151 
152 using namespace std;
153 using namespace edm;
154 using namespace xercesc;
155 
156 const string CTPPSPixelDAQMappingESSourceXML::tagROC = "roc";
157 const string CTPPSPixelDAQMappingESSourceXML::tagPixel = "pixel";
158 const string CTPPSPixelDAQMappingESSourceXML::tagAnalysisMask = "analysisMask";
159 
160 // common XML position tags
161 const string CTPPSPixelDAQMappingESSourceXML::tagArm = "arm";
162 
163 // specific RP XML tags
164 const string CTPPSPixelDAQMappingESSourceXML::tagRPStation = "station";
165 const string CTPPSPixelDAQMappingESSourceXML::tagRPPot = "rp_detector_set";
166 const string CTPPSPixelDAQMappingESSourceXML::tagRPixPlane = "rpix_plane";
167 
168 static const unsigned int offsetROCinDetId = 13;
169 //static const unsigned int maskROCinDetId = 0x3;
170 
172  : verbosity(conf.getUntrackedParameter<unsigned int>("verbosity", 0)),
173  subSystemName(conf.getUntrackedParameter<string>("subSystem")),
174  currentBlock(0),
175  currentBlockValid(false) {
176  for (const auto &it : conf.getParameter<vector<ParameterSet>>("configuration")) {
177  ConfigBlock b;
178  b.validityRange = it.getParameter<EventRange>("validityRange");
179  b.mappingFileNames = it.getParameter<vector<string>>("mappingFileNames");
180  b.maskFileNames = it.getParameter<vector<string>>("maskFileNames");
181  configuration.push_back(b);
182  }
183 
185  findingRecord<CTPPSPixelAnalysisMaskRcd>();
186 
188  findingRecord<CTPPSPixelDAQMappingRcd>();
189 
190  LogVerbatim("CTPPSPixelDAQMappingESSourceXML") << " Inside CTPPSPixelDAQMappingESSourceXML";
191 }
192 
194  const edm::IOVSyncValue &iosv,
195  edm::ValidityInterval &oValidity) {
196  LogVerbatim("CTPPSPixelDAQMappingESSourceXML")
197  << ">> CTPPSPixelDAQMappingESSourceXML::setIntervalFor(" << key.name() << ")";
198 
199  LogVerbatim("CTPPSPixelDAQMappingESSourceXML")
200  << " run=" << iosv.eventID().run() << ", event=" << iosv.eventID().event();
201 
202  currentBlockValid = false;
203  for (unsigned int idx = 0; idx < configuration.size(); ++idx) {
204  const auto &bl = configuration[idx];
205 
206  EventID startEventID = bl.validityRange.startEventID();
207  if (startEventID == EventID(1, 0, 1))
208  startEventID = EventID(1, 0, 0);
209 
210  if (startEventID <= iosv.eventID() && iosv.eventID() <= bl.validityRange.endEventID()) {
211  currentBlockValid = true;
212  currentBlock = idx;
213 
214  const IOVSyncValue begin(startEventID);
215  const IOVSyncValue end(bl.validityRange.endEventID());
216  oValidity = ValidityInterval(begin, end);
217 
218  LogVerbatim("CTPPSPixelDAQMappingESSourceXML") << " block found: index=" << currentBlock << ", interval=("
219  << startEventID << " - " << bl.validityRange.endEventID() << ")";
220 
221  return;
222  }
223  }
224 
225  if (!currentBlockValid) {
226  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::setIntervalFor")
227  << "No configuration for event " << iosv.eventID();
228  }
229 }
230 
232 
234  FileInPath fip(fn);
235  return fip.fullPath();
236 }
237 
239  const CTPPSPixelDAQMappingRcd &) {
241 
242  auto mapping = std::make_unique<CTPPSPixelDAQMapping>();
243  auto mask = std::make_unique<CTPPSPixelAnalysisMask>();
244 
245  // initialize Xerces
246 
248 
249  // load mapping files
250  for (const auto &fn : configuration[currentBlock].mappingFileNames)
252 
253  // load mask files
254  for (const auto &fn : configuration[currentBlock].maskFileNames)
256 
257  // release Xerces
259 
260  // commit the product
261  return mapping;
262 }
263 
265  const CTPPSPixelAnalysisMaskRcd &) {
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)
282 
283  // release Xerces
285 
286  // commit the products
287  //return edm::es::products(mapping, mask);
288  return mask;
289 }
290 
291 //----------------------------------------------------------------------------------------------------
292 
294  const string &file,
295  const std::unique_ptr<CTPPSPixelDAQMapping> &mapping,
296  const std::unique_ptr<CTPPSPixelAnalysisMask> &mask) {
297  unique_ptr<XercesDOMParser> parser(new XercesDOMParser());
298  parser->parse(file.c_str());
299 
300  DOMDocument *domDoc = parser->getDocument();
301 
302  if (!domDoc)
303  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseXML")
304  << "Cannot parse file `" << file << "' (domDoc = NULL)." << endl;
305 
306  DOMElement *elementRoot = domDoc->getDocumentElement();
307 
308  if (!elementRoot)
309  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseXML") << "File `" << file << "' is empty." << endl;
310 
311  ParseTreePixel(pType, elementRoot, nTop, 0, mapping, mask);
312 }
313 
314 //-----------------------------------------------------------------------------------------------------------
315 
317  xercesc::DOMNode *parent,
318  NodeType parentType,
319  unsigned int parentID,
320  const std::unique_ptr<CTPPSPixelDAQMapping> &mapping,
321  const std::unique_ptr<CTPPSPixelAnalysisMask> &mask) {
322 #ifdef DEBUG
323  printf(">> CTPPSPixelDAQMappingESSourceXML::ParseTreeRP(%s, %u, %u)\n",
324  cms::xerces::toString(parent->getNodeName()),
325  parentType,
326  parentID);
327 #endif
328 
329  DOMNodeList *children = parent->getChildNodes();
330 
331  for (unsigned int i = 0; i < children->getLength(); i++) {
332  DOMNode *n = children->item(i);
333  if (n->getNodeType() != DOMNode::ELEMENT_NODE)
334  continue;
335 
337 
338 #ifdef DEBUG
339  printf("\tname = %s, type = %u\n", cms::xerces::toString(n->getNodeName()), type);
340 #endif
341 
342  // structure control
343  if (!PixelNode(type))
344  continue;
345 
346  NodeType expectedParentType;
347  switch (type) {
348  case nArm:
349  expectedParentType = nTop;
350  break;
351  case nRPStation:
352  expectedParentType = nArm;
353  break;
354  case nRPPot:
355  expectedParentType = nRPStation;
356  break;
357  case nRPixPlane:
358  expectedParentType = nRPPot;
359  break;
360  case nROC:
361  expectedParentType = nRPixPlane;
362  break;
363  case nPixel:
364  expectedParentType = nROC;
365  break;
366  default:
367  expectedParentType = nUnknown;
368  break;
369  }
370 
371  if (expectedParentType != parentType) {
372  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML")
373  << "Node " << cms::xerces::toString(n->getNodeName()) << " not allowed within "
374  << cms::xerces::toString(parent->getNodeName()) << " block.\n";
375  }
376 
377  // parse tag attributes
378  unsigned int id = 0;
379  bool id_set = false;
380  bool fullMask = false;
381  DOMNamedNodeMap *attr = n->getAttributes();
382 
383  for (unsigned int j = 0; j < attr->getLength(); j++) {
384  DOMNode *a = attr->item(j);
385 
386  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "id")) {
387  sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &id);
388  id_set = true;
389  }
390 
391  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "full_mask"))
392  fullMask = (strcmp(cms::xerces::toString(a->getNodeValue()).c_str(), "no") != 0);
393  }
394 
395  // content control
396  if (!id_set)
397  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseTreePixel")
398  << "id not given for element `" << cms::xerces::toString(n->getNodeName()) << "'" << endl;
399 
400  if (type == nRPixPlane && id > 5)
401  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::ParseTreePixel")
402  << "Plane IDs range from 0 to 5. id = " << id << " is invalid." << endl;
403 
404 #ifdef DEBUG
405  printf("\tID found: %u\n", id);
406 #endif
407 
408  // store mapping data
409  if (pType == pMapping && type == nROC) {
410  const CTPPSPixelFramePosition &framepos = ChipFramePosition(n);
411  CTPPSPixelROCInfo rocInfo;
412  rocInfo.roc = id;
413 
414  const unsigned int armIdx = (parentID / 1000) % 10;
415  const unsigned int stIdx = (parentID / 100) % 10;
416  const unsigned int rpIdx = (parentID / 10) % 10;
417  const unsigned int plIdx = parentID % 10;
418 
419  rocInfo.iD = CTPPSPixelDetId(armIdx, stIdx, rpIdx, plIdx);
420 
421  mapping->insert(framepos, rocInfo);
422 
423  continue;
424  }
425 
426  // store mask data
427  if (pType == pMask && type == nROC) {
428  const unsigned int armIdx = (parentID / 1000) % 10;
429  const unsigned int stIdx = (parentID / 100) % 10;
430  const unsigned int rpIdx = (parentID / 10) % 10;
431  const unsigned int plIdx = parentID % 10;
432 
433  uint32_t symbId = (id << offsetROCinDetId);
434 
435  symbId |= CTPPSPixelDetId(armIdx, stIdx, rpIdx, plIdx);
436 
438  am.fullMask = fullMask;
439  GetPixels(n, am.maskedPixels);
440 
441  mask->insert(symbId, am);
442 
443  continue;
444  }
445 
446  // recursion (deeper in the tree)
447  ParseTreePixel(pType, n, type, parentID * 10 + id, mapping, mask);
448  }
449 }
450 
451 //----------------------------------------------------------------------------------------------------
452 
455  unsigned char attributeFlag = 0;
456 
457  DOMNamedNodeMap *attr = chipnode->getAttributes();
458  for (unsigned int j = 0; j < attr->getLength(); j++) {
459  DOMNode *a = attr->item(j);
460 
461  if (fp.setXMLAttribute(
462  cms::xerces::toString(a->getNodeName()), cms::xerces::toString(a->getNodeValue()), attributeFlag) > 1) {
463  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML")
464  << "Unrecognized tag `" << cms::xerces::toString(a->getNodeName()) << "' or incompatible value `"
465  << cms::xerces::toString(a->getNodeValue()) << "'." << endl;
466  }
467  }
468 
469  if (!fp.checkXMLAttributeFlag(attributeFlag)) {
470  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML")
471  << "Wrong/incomplete DAQ channel specification (attributeFlag = " << attributeFlag << ")." << endl;
472  }
473 
474  return fp;
475 }
476 
477 //----------------------------------------------------------------------------------------------------
478 
480  // common node types
481  if (Test(n, tagArm))
482  return nArm;
483  if (Test(n, tagROC))
484  return nROC;
485 
486  // RP node types
487  if (Test(n, tagRPStation))
488  return nRPStation;
489  if (Test(n, tagRPPot))
490  return nRPPot;
491  if (Test(n, tagRPixPlane))
492  return nRPixPlane;
493 
494  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::GetNodeType")
495  << "Unknown tag `" << cms::xerces::toString(n->getNodeName()) << "'.\n";
496 }
497 
498 //----------------------------------------------------------------------------------------------------
499 
501  set<std::pair<unsigned char, unsigned char>> &pixels) {
502  DOMNodeList *children = n->getChildNodes();
503  for (unsigned int i = 0; i < children->getLength(); i++) {
504  DOMNode *n = children->item(i);
505  if (n->getNodeType() != DOMNode::ELEMENT_NODE || !Test(n, "pixel"))
506  continue;
507 
508  DOMNamedNodeMap *attr = n->getAttributes();
509  bool pixelSet = false;
510  bool rowSet = false;
511  bool colSet = false;
512  std::pair<unsigned int, unsigned int> currentPixel;
513  for (unsigned int j = 0; j < attr->getLength(); j++) {
514  DOMNode *a = attr->item(j);
515 
516  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "row")) {
517  unsigned int row = 0;
518  sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &row);
519  currentPixel.first = row;
520  rowSet = true;
521  }
522  if (!strcmp(cms::xerces::toString(a->getNodeName()).c_str(), "col")) {
523  unsigned int col = 0;
524  sscanf(cms::xerces::toString(a->getNodeValue()).c_str(), "%u", &col);
525  currentPixel.second = col;
526  colSet = true;
527  }
528 
529  pixelSet = rowSet & colSet;
530  if (pixelSet) {
531  pixels.insert(currentPixel);
532  break;
533  }
534  }
535 
536  if (!pixelSet) {
537  throw cms::Exception("CTPPSPixelDAQMappingESSourceXML::GetChannels")
538  << "Channel tags must have a row or col attribute.";
539  }
540  }
541 }
542 
543 //----------------------------------------------------------------------------------------------------
544 
string GetNodeContent(xercesc::DOMNode *parent)
returns the content of the node
Log< level::Info, true > LogVerbatim
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:166
CTPPSPixelDAQMappingESSourceXML(const edm::ParameterSet &)
Contains mappind data related to a ROC.
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
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
std::string fullPath() const
Definition: FileInPath.cc:161
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:17
string CompleteFileName(const string &fn)
adds the path prefix, if needed
void xercesInitialize()
Definition: Xerces.cc:18
assert(be >=bs)
constexpr uint32_t mask
Definition: gpuClustering.h:26
Contains data on masked channels of a ROC.
uint32_t iD
the symbolic id
std::string toString(XMLCh const *toTranscode)
void GetPixels(xercesc::DOMNode *n, std::set< std::pair< unsigned char, unsigned char >> &pixels)
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.
key
prepare the HTCondor submission files and eventually submit them
const int verbosity
bool Test(xercesc::DOMNode *node, const std::string &name)
returns true iff the node is of the given name
RunNumber_t run() const
Definition: EventID.h:38
#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
static const std::string tagArm
Common position tags.
const EventID & eventID() const
Definition: IOVSyncValue.h:40
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:1009
EventNumber_t event() const
Definition: EventID.h:40
std::set< std::pair< unsigned char, unsigned char > > maskedPixels
list of channels to be masked
NodeType GetNodeType(xercesc::DOMNode *)
determines node type