CMS 3D CMS Logo

HGCalWaferValidation.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Validation/HGCalValidation
4 // Class: HGCalWaferValidation
5 //
20 //
21 // Original Author: Imran Yusuff
22 // Created: Thu, 27 May 2021 19:47:08 GMT
23 //
24 //
25 
26 // system include files
27 #include <memory>
28 
29 // user include files
32 
35 
40 
44 
45 #include <fstream>
46 #include <regex>
47 
48 //
49 // class declaration
50 //
51 
52 // If the analyzer does not use TFileService, please remove
53 // the template argument to the base class so the class inherits
54 // from edm::one::EDAnalyzer<>
55 // This will improve performance in multithreaded jobs.
56 
58 public:
59  explicit HGCalWaferValidation(const edm::ParameterSet&);
60  ~HGCalWaferValidation() override;
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
63 
64 private:
65  // This is MessageLogger logging category
66  const std::string logcat = "HGCalWaferValidation";
67 
68  // wafer coordinate is (layer, u, v), used as index for map
69  using WaferCoord = std::tuple<int, int, int>;
70 
71  std::string strWaferCoord(const WaferCoord& coord);
72 
73  bool DDFindHGCal(DDCompactView::GraphWalker& walker, std::string targetName);
76 
77  void beginJob() override;
78  void analyze(const edm::Event&, const edm::EventSetup&) override;
79  void endJob() override;
80 
81  // ----------member data ---------------------------
82  // module parameters
84 
85  // struct to hold wafer information from DD in map
86  struct WaferInfo {
88  double x;
89  double y;
91  int rotCode;
92  };
93 
94  // EDM token to access DD
96 
97  // map holding all wafer properties from DD
98  std::map<WaferCoord, struct WaferInfo> waferData;
99 
100  // boolean map to keep track of unaccounted DD wafers (not in flat file)
101  std::map<WaferCoord, bool> waferValidated;
102 };
103 
104 //
105 // constants, enums and typedefs
106 //
107 
108 //
109 // static data member definitions
110 //
111 
112 //
113 // constructors and destructor
114 //
116  : geometryFileName_(iConfig.getParameter<edm::FileInPath>("GeometryFileName")) {
117  viewToken_ = esConsumes<DDCompactView, IdealGeometryRecord>();
118  //now do what ever initialization is needed
119 }
120 
122  // do anything here that needs to be done at desctruction time
123  // (e.g. close files, deallocate resources etc.)
124  //
125  // please remove this method altogether if it would be left empty
126 }
127 
128 //
129 // member functions
130 //
131 
132 // convert WaferCoord tuple to string representation (i.e. for printing)
134  std::stringstream ss;
135  ss << "(" << std::get<0>(coord) << "," << std::get<1>(coord) << "," << std::get<2>(coord) << ")";
136  return ss.str();
137 }
138 
139 // ----- find HGCal entry among the DD -----
141  if (walker.current().first.name().name() == targetName) {
142  // target found
143  return true;
144  }
145  if (walker.firstChild()) {
146  do {
147  if (DDFindHGCal(walker, targetName))
148  // target inside child
149  return true;
150  } while (walker.nextSibling());
151  walker.parent();
152  }
153  return false;
154 }
155 
156 // ----- find the next wafer, then process the wafer layer -----
158  if (walker.current().first.name().fullname().rfind("hgcalwafer:", 0) == 0) {
159  // first wafer found. Now process the entire layer of wafers.
160  ProcessWaferLayer(walker);
161  return;
162  }
163  if (walker.firstChild()) {
164  do {
165  DDFindWafers(walker);
166  } while (walker.nextSibling());
167  walker.parent();
168  }
169 }
170 
171 // ----- process the layer of wafers -----
173  static int waferLayer = 0; // layer numbers in DD are assumed to be sequential from 1
174  waferLayer++;
175  edm::LogVerbatim(logcat) << "ProcessWaferLayer: Processing layer " << waferLayer;
176  do {
177  if (walker.current().first.name().fullname().rfind("hgcalwafer:", 0) == 0) {
178  auto wafer = walker.current();
179  const std::string waferName(walker.current().first.name().fullname());
180  const int copyNo = wafer.second->copyno();
181  // extract DD layer properties
182  const int waferType = HGCalTypes::getUnpackedType(copyNo);
183  const int waferU = HGCalTypes::getUnpackedU(copyNo);
184  const int waferV = HGCalTypes::getUnpackedV(copyNo);
185  const WaferCoord waferCoord(waferLayer, waferU, waferV); // map index
186  // build struct of DD wafer properties
187  struct WaferInfo waferInfo;
188  waferInfo.thickClass = waferType;
189  waferInfo.x = wafer.second->translation().x();
190  waferInfo.y = wafer.second->translation().y();
191  const std::string waferNameData =
192  std::regex_replace(waferName,
193  std::regex("(HGCal[EH]E)(Wafer[01])(Fine|Coarse[12])([a-z]*)([0-9]*)"),
194  "$1 $2-$3 $4 $5",
195  std::regex_constants::format_no_copy);
196  std::stringstream ss(waferNameData);
197  std::string EEorHE;
198  std::string typeStr;
199  std::string shapeStr;
200  std::string rotStr;
201  ss >> EEorHE >> typeStr >> shapeStr >> rotStr;
202  // assume rotational symmetry of full-sized wafers
203  if (shapeStr.empty())
204  shapeStr = "F";
205  if (rotStr.empty())
206  rotStr = "0";
207  const int rotCode(std::stoi(rotStr));
208  //edm::LogVerbatim(logcat) << "rotStr " << rotStr << " rotCode " << rotCode;
209  waferInfo.shapeCode = shapeStr;
210  waferInfo.rotCode = rotCode;
211  // populate the map
212  waferData[waferCoord] = waferInfo;
213  waferValidated[waferCoord] = false;
214  }
215  } while (walker.nextSibling());
216 }
217 
218 // ------------ method called for each event ------------
220  using namespace edm;
221 
222  // Get the CMS DD
223  auto viewH = iSetup.getHandle(viewToken_);
224 
225  if (!viewH.isValid()) {
226  edm::LogPrint(logcat) << "Error obtaining geometry handle!";
227  return;
228  }
229 
230  edm::LogVerbatim(logcat) << "Root is : " << viewH->root();
231  edm::LogVerbatim(logcat) << std::endl;
232 
233  // find HGCalEE
234  auto eeWalker = viewH->walker();
235  const bool eeFound = DDFindHGCal(eeWalker, "HGCalEE");
236  if (eeFound) {
237  edm::LogVerbatim(logcat) << "HGCalEE found!";
238  edm::LogVerbatim(logcat) << "name = " << eeWalker.current().first.name().name();
239  edm::LogVerbatim(logcat) << "fullname = " << eeWalker.current().first.name().fullname();
240  } else {
241  edm::LogPrint(logcat) << "HGCalEE not found!";
242  }
243  edm::LogVerbatim(logcat) << std::endl;
244 
245  // find HGCalHEsil
246  auto hesilWalker = viewH->walker();
247  const bool hesilFound = DDFindHGCal(hesilWalker, "HGCalHEsil");
248  if (hesilFound) {
249  edm::LogVerbatim(logcat) << "HGCalHEsil found!";
250  edm::LogVerbatim(logcat) << "name = " << hesilWalker.current().first.name().name();
251  edm::LogVerbatim(logcat) << "fullname = " << hesilWalker.current().first.name().fullname();
252  } else {
253  edm::LogPrint(logcat) << "HGCalHEsil not found!";
254  }
255  edm::LogVerbatim(logcat) << std::endl;
256 
257  // find HGCalHEmix
258  auto hemixWalker = viewH->walker();
259  const bool hemixFound = DDFindHGCal(hemixWalker, "HGCalHEmix");
260  if (hemixFound) {
261  edm::LogVerbatim(logcat) << "HGCalHEmix found!";
262  edm::LogVerbatim(logcat) << "name = " << hemixWalker.current().first.name().name();
263  edm::LogVerbatim(logcat) << "fullname = " << hemixWalker.current().first.name().fullname();
264  } else {
265  edm::LogPrint(logcat) << "HGCalHEmix not found!";
266  }
267  edm::LogVerbatim(logcat) << std::endl;
268 
269  // give up if no HGCal found at all
270  if (!(eeFound || hesilFound || hemixFound)) {
271  edm::LogPrint(logcat) << "Nothing found. Giving up.";
272  return;
273  }
274 
275  // Now walk the HGCalEE walker to find the first wafer on each layer and process them
276  edm::LogVerbatim(logcat) << "Calling DDFindWafers(eeWalker);";
277  DDFindWafers(eeWalker);
278 
279  // Walk the HGCalHEsilwalker to find the first wafer on each layer and process them
280  edm::LogVerbatim(logcat) << "Calling DDFindWafers(hesilWalker);";
281  DDFindWafers(hesilWalker);
282 
283  // Walk the HGCalHEmix walker to find the first wafer on each layer and process them
284  edm::LogVerbatim(logcat) << "Calling DDFindWafers(hemixWalker);";
285  DDFindWafers(hemixWalker);
286 
287  // Confirm all the DD wafers have been read
288  edm::LogVerbatim(logcat) << "Number of wafers read from DD: " << waferData.size();
289 
290  // Now open the geometry text file
292  edm::LogVerbatim(logcat) << "Opening geometry text file: " << fileName;
293  std::ifstream geoTxtFile(fileName);
294 
295  if (!geoTxtFile) {
296  edm::LogPrint(logcat) << "Cannot open geometry text file.";
297  return;
298  }
299 
300  // total processed counter
301  int nTotalProcessed = 0;
302 
303  // geometry error counters
304  int nMissing = 0;
305  int nThicknessError = 0;
306  int nPosXError = 0;
307  int nPosYError = 0;
308  int nShapeError = 0;
309  int nRotError = 0;
310  int nUnaccounted = 0;
311 
313 
314  // process each line on the text file
315  while (std::getline(geoTxtFile, buf)) {
316  std::stringstream ss(buf);
317  std::vector<std::string> tokens;
318  while (ss >> buf)
319  if (!buf.empty())
320  tokens.push_back(buf);
321  if (tokens.size() != 8)
322  continue;
323 
324  nTotalProcessed++;
325 
326  // extract wafer info from a textfile line
327  const int waferLayer(std::stoi(tokens[0]));
328  const std::string waferShapeCode(tokens[1]);
329  const int waferThickness(std::stoi(tokens[2]));
330  const double waferX(std::stod(tokens[3]));
331  const double waferY(std::stod(tokens[4]));
332  const int waferRotCode(std::stoi(tokens[5]));
333  const int waferU(std::stoi(tokens[6]));
334  const int waferV(std::stoi(tokens[7]));
335 
336  // map index for crosschecking with DD
337  const WaferCoord waferCoord(waferLayer, waferU, waferV);
338 
339  // now check for (and report) wafer data disagreements
340 
341  if (waferData.find(waferCoord) == waferData.end()) {
342  nMissing++;
343  edm::LogVerbatim(logcat) << "MISSING: " << strWaferCoord(waferCoord);
344  continue;
345  }
346 
347  const struct WaferInfo waferInfo = waferData[waferCoord];
348  waferValidated[waferCoord] = true;
349 
350  if ((waferInfo.thickClass == 0 && waferThickness != 120) || (waferInfo.thickClass == 1 && waferThickness != 200) ||
351  (waferInfo.thickClass == 2 && waferThickness != 300)) {
352  nThicknessError++;
353  edm::LogVerbatim(logcat) << "THICKNESS ERROR: " << strWaferCoord(waferCoord);
354  }
355 
356  // it seems that wafer x-coords relative to their layer plane are mirrored...
357  if (fabs(-waferInfo.x - waferX) > 0.015) { // assuming this much tolerance
358  nPosXError++;
359  edm::LogVerbatim(logcat) << "POSITION x ERROR: " << strWaferCoord(waferCoord);
360  }
361 
362  if (fabs(waferInfo.y - waferY) > 0.015) { // assuming this much tolerance
363  nPosYError++;
364  edm::LogVerbatim(logcat) << "POSITION y ERROR: " << strWaferCoord(waferCoord);
365  }
366 
367  if (waferInfo.shapeCode != waferShapeCode) {
368  nShapeError++;
369  edm::LogVerbatim(logcat) << "SHAPE ERROR: " << strWaferCoord(waferCoord);
370  }
371 
372  if ((waferShapeCode != 'F' && waferInfo.rotCode != waferRotCode) ||
373  (waferShapeCode == 'F' && (waferInfo.rotCode % 2 != waferRotCode % 2))) {
374  nRotError++;
375  edm::LogVerbatim(logcat) << "ROTATION ERROR: " << strWaferCoord(waferCoord) << " ( " << waferInfo.rotCode
376  << " != " << waferRotCode << " )";
377  }
378  }
379 
380  geoTxtFile.close();
381 
382  // Find unaccounted DD wafers
383  for (auto const& accounted : waferValidated) {
384  if (!accounted.second) {
385  nUnaccounted++;
386  edm::LogVerbatim(logcat) << "UNACCOUNTED: " << strWaferCoord(accounted.first);
387  }
388  }
389 
390  // Print out error counts
391  edm::LogVerbatim(logcat) << std::endl;
392  edm::LogVerbatim(logcat) << "*** ERROR COUNTS ***";
393  edm::LogVerbatim(logcat) << "Missing : " << nMissing;
394  edm::LogVerbatim(logcat) << "Thickness error : " << nThicknessError;
395  edm::LogVerbatim(logcat) << "Pos-x error : " << nPosXError;
396  edm::LogVerbatim(logcat) << "Pos-y error : " << nPosYError;
397  edm::LogVerbatim(logcat) << "Shape error : " << nShapeError;
398  edm::LogVerbatim(logcat) << "Rotation error : " << nRotError;
399  edm::LogVerbatim(logcat) << "Unaccounted : " << nUnaccounted;
400  edm::LogVerbatim(logcat) << std::endl;
401  edm::LogVerbatim(logcat) << "Total wafers processed from geotxtfile = " << nTotalProcessed;
402  edm::LogVerbatim(logcat) << std::endl;
403 
404  // Issue a LogPrint (warning) if there is at least one wafer errors
405  if (nMissing > 0 || nThicknessError > 0 || nPosXError > 0 || nPosYError > 0 || nShapeError > 0 || nRotError > 0 ||
406  nUnaccounted > 0) {
407  edm::LogPrint(logcat) << "There are at least one wafer error.";
408  }
409 }
410 
411 // ------------ method called once each job just before starting event loop ------------
413  // please remove this method if not needed
414 }
415 
416 // ------------ method called once each job just after ending the event loop ------------
418  // please remove this method if not needed
419 }
420 
421 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
423  //The following says we do not know what parameters are allowed so do no validation
424  // Please change this to state exactly what you do use, even if it is no parameters
426  desc.add<edm::FileInPath>("GeometryFileName",
427  edm::FileInPath("Validation/HGCalValidation/data/geomnew_corrected_360.txt"));
428  descriptions.add("hgcalWaferValidation", desc);
429 }
430 
431 //define this as a plug-in
HGCalWaferValidation::DDFindHGCal
bool DDFindHGCal(DDCompactView::GraphWalker &walker, std::string targetName)
Definition: HGCalWaferValidation.cc:140
HGCalWaferValidation::WaferInfo
Definition: HGCalWaferValidation.cc:86
EDAnalyzer.h
MessageLogger.h
math::GraphWalker< DDLogicalPart, DDPosData * >
HGCalWaferValidation::WaferInfo::y
double y
Definition: HGCalWaferValidation.cc:89
HGCalWaferValidation::HGCalWaferValidation
HGCalWaferValidation(const edm::ParameterSet &)
Definition: HGCalWaferValidation.cc:115
HGCalWaferValidation::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: HGCalWaferValidation.cc:219
HGCalWaferValidation::logcat
const std::string logcat
Definition: HGCalWaferValidation.cc:66
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::LogPrint
Log< level::Warning, true > LogPrint
Definition: MessageLogger.h:130
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
HGCalWaferIndex::waferU
int32_t waferU(const int32_t index)
Definition: HGCalWaferIndex.cc:27
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
HGCalWaferValidation::~HGCalWaferValidation
~HGCalWaferValidation() override
Definition: HGCalWaferValidation.cc:121
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
HGCalWaferValidation::geometryFileName_
edm::FileInPath geometryFileName_
Definition: HGCalWaferValidation.cc:83
DDCompactView.h
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
edm::FileInPath
Definition: FileInPath.h:61
MakerMacros.h
HGCalWaferValidation::waferValidated
std::map< WaferCoord, bool > waferValidated
Definition: HGCalWaferValidation.cc:101
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
math::GraphWalker::firstChild
result_type firstChild()
Definition: GraphWalker.h:110
HGCalWaferValidation::WaferInfo::rotCode
int rotCode
Definition: HGCalWaferValidation.cc:91
HGCalWaferValidation::WaferInfo::shapeCode
std::string shapeCode
Definition: HGCalWaferValidation.cc:90
HGCalWaferValidation::WaferInfo::thickClass
int thickClass
Definition: HGCalWaferValidation.cc:87
HGCalWaferIndex::waferV
int32_t waferV(const int32_t index)
Definition: HGCalWaferIndex.cc:32
HGCalTypes::getUnpackedU
static int32_t getUnpackedU(int id)
Definition: HGCalTypes.cc:16
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
FileInPath.h
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
math::GraphWalker::current
value_type current() const
Definition: GraphWalker.h:88
HGCalTypes::getUnpackedV
static int32_t getUnpackedV(int id)
Definition: HGCalTypes.cc:22
HGCalWaferValidation
Definition: HGCalWaferValidation.cc:57
HGCalWaferValidation::strWaferCoord
std::string strWaferCoord(const WaferCoord &coord)
Definition: HGCalWaferValidation.cc:133
iEvent
int iEvent
Definition: GenABIO.cc:224
HGCalWaferValidation::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: HGCalWaferValidation.cc:422
edm::EventSetup::getHandle
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:155
IdealGeometryRecord.h
edm::EventSetup
Definition: EventSetup.h:58
HGCalWaferValidation::WaferCoord
std::tuple< int, int, int > WaferCoord
Definition: HGCalWaferValidation.cc:69
HGCalWaferValidation::ProcessWaferLayer
void ProcessWaferLayer(DDCompactView::GraphWalker &walker)
Definition: HGCalWaferValidation.cc:172
visDQMUpload.buf
buf
Definition: visDQMUpload.py:160
edm::ESGetToken< DDCompactView, IdealGeometryRecord >
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
InputTag.h
math::GraphWalker::parent
result_type parent()
Definition: GraphWalker.h:132
HGCalWaferValidation::viewToken_
edm::ESGetToken< DDCompactView, IdealGeometryRecord > viewToken_
Definition: HGCalWaferValidation.cc:95
HGCalWaferIndex::waferLayer
int32_t waferLayer(const int32_t index)
Definition: HGCalWaferIndex.cc:23
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
HGCalWaferValidation::beginJob
void beginJob() override
Definition: HGCalWaferValidation.cc:412
Frameworkfwd.h
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
math::GraphWalker::nextSibling
result_type nextSibling()
Definition: GraphWalker.h:121
HGCalWaferValidation::WaferInfo::x
double x
Definition: HGCalWaferValidation.cc:88
ParameterSet.h
HGCalTypes.h
edm::Event
Definition: Event.h:73
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:161
HGCalWaferValidation::waferData
std::map< WaferCoord, struct WaferInfo > waferData
Definition: HGCalWaferValidation.cc:98
HGCalTypes::getUnpackedType
static int32_t getUnpackedType(int id)
Definition: HGCalTypes.cc:14
HGCalWaferValidation::DDFindWafers
void DDFindWafers(DDCompactView::GraphWalker &walker)
Definition: HGCalWaferValidation.cc:157
HGCalWaferValidation::endJob
void endJob() override
Definition: HGCalWaferValidation.cc:417