CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SiPixelTemplateDBObjectUploader.cc
Go to the documentation of this file.
1 #include <cstdio>
2 #include <fstream>
3 #include <iostream>
4 #include <memory>
5 
8 
11 
17 
22 
25 
29 
33 
35 public:
38 
39  typedef std::vector<std::string> vstring;
40 
41 private:
42  void beginJob() override;
43  void analyze(const edm::Event&, const edm::EventSetup&) override;
44  void endJob() override;
45 
48  float theVersion;
49  float theMagField;
50  std::vector<uint32_t> theDetIds;
53  std::vector<uint32_t> theBarrelTemplateIds;
54  std::vector<uint32_t> theEndcapTemplateIds;
58 };
59 
61  : theTemplateCalibrations(iConfig.getParameter<vstring>("siPixelTemplateCalibrations")),
62  theTemplateBaseString(iConfig.getParameter<std::string>("theTemplateBaseString")),
63  theVersion(iConfig.getParameter<double>("Version")),
64  theMagField(iConfig.getParameter<double>("MagField")),
65  theBarrelLocations(iConfig.getParameter<std::vector<std::string> >("barrelLocations")),
66  theEndcapLocations(iConfig.getParameter<std::vector<std::string> >("endcapLocations")),
67  theBarrelTemplateIds(iConfig.getParameter<std::vector<uint32_t> >("barrelTemplateIds")),
68  theEndcapTemplateIds(iConfig.getParameter<std::vector<uint32_t> >("endcapTemplateIds")),
69  useVectorIndices(iConfig.getUntrackedParameter<bool>("useVectorIndices", false)),
72 
74 
76 
78  //--- Make the POOL-ORA object to store the database object
80 
81  // Set the number of templates to be passed to the dbobject
83 
84  // Set the version of the template dbobject - this is an external parameter
86 
87  // Open the template file(s)
88  for (int m = 0; m < obj.numOfTempl(); ++m) {
90  std::ifstream in_file(file.fullPath().c_str(), std::ios::in);
91  if (in_file.is_open()) {
92  edm::LogInfo("SiPixelTemplateDBObjectUploader") << "Opened Template File: " << file.fullPath().c_str();
93 
94  // Local variables
95  char title_char[80], c;
97  float tempstore;
98  int iter, j, k;
99 
100  // Templates contain a header char - we must be clever about storing this
101  for (iter = 0; (c = in_file.get()) != '\n'; ++iter) {
102  if (iter < 79) {
103  title_char[iter] = c;
104  }
105  }
106  if (iter > 78) {
107  iter = 78;
108  }
109  title_char[iter + 1] = '\n';
110  for (j = 0; j < 80; j += 4) {
111  temp.c[0] = title_char[j];
112  temp.c[1] = title_char[j + 1];
113  temp.c[2] = title_char[j + 2];
114  temp.c[3] = title_char[j + 3];
115  obj.push_back(temp.f);
116  obj.setMaxIndex(obj.maxIndex() + 1);
117  }
118 
119  // Check if the magnetic field is the same as in the header of the input files
120  for (k = 0; k < 80; k++) {
121  if ((title_char[k] == '@') && (title_char[k - 1] == 'T')) {
122  double localMagField = (((int)title_char[k - 4]) - 48) * 10 + ((int)title_char[k - 2]) - 48;
123  if (theMagField != localMagField) {
124  edm::LogPrint("SiPixelTemplateDBObjectUploader")
125  << "\n -------- WARNING -------- \n Magnetic field in the cfg is " << theMagField << "T while it is "
126  << title_char[k - 4] << title_char[k - 2] << title_char[k - 1]
127  << " in the header \n ------------------------- \n " << std::endl;
128  }
129  }
130  }
131 
132  // Fill the dbobject
133  in_file >> tempstore;
134  while (!in_file.eof()) {
135  obj.setMaxIndex(obj.maxIndex() + 1);
136  obj.push_back(tempstore);
137  in_file >> tempstore;
138  }
139 
140  in_file.close();
141  } else {
142  // If file didn't open, report this
143  edm::LogError("SiPixelTemplateDBObjectUploader") << "Error opening File: " << file.fullPath().c_str();
144  }
145  }
146 
147  //get TrackerGeometry from the event setup
149  const TrackerGeometry* tGeo = &iSetup.getData(trackerGeometryToken_);
150 
151  // Use the TrackerTopology class for layer/disk etc. number
152  const TrackerTopology* tTopo = &iSetup.getData(trackerTopologyToken_);
153 
154  // Check if we are using Phase-1 or Phase-2 geometry
155  int phase = 0;
156  if (pDD->isThere(GeomDetEnumerators::P1PXB) && pDD->isThere(GeomDetEnumerators::P1PXEC) == true) {
157  phase = 1;
158  } else if (pDD->isThere(GeomDetEnumerators::P2PXB) && pDD->isThere(GeomDetEnumerators::P2PXEC) == true) {
159  phase = 2;
160  }
161  edm::LogPrint("SiPixelTemplateDBObjectUploader") << "Phase-" << phase << " geometry is used \n" << std::endl;
162 
163  //Loop over the detector elements and put template IDs in place
164  for (const auto& it : pDD->detUnits()) {
165  if (it != nullptr) {
166  // Here is the actual looping step over all DetIds:
167  DetId detid = it->geographicalId();
168  unsigned int layer = 0, ladder = 0, disk = 0, side = 0, blade = 0, panel = 0, module = 0;
169  short thisID = 10000;
170  unsigned int iter;
171 
172  // Now we sort them into the Barrel and Endcap:
173  //Barrel Pixels first
174  if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) ||
175  (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXB)) {
176  //Get the layer, ladder, and module corresponding to this detID
177  layer = tTopo->pxbLayer(detid.rawId());
178  ladder = tTopo->pxbLadder(detid.rawId());
179  module = tTopo->pxbModule(detid.rawId());
180 
181  if (useVectorIndices) {
182  --layer;
183  --ladder;
184  --module;
185  }
186 
187  //Assign template IDs
188  //Loop over all the barrel locations
189  for (iter = 0; iter < theBarrelLocations.size(); ++iter) {
190  //get the string of this barrel location
191  std::string loc_string = theBarrelLocations[iter];
192  //find where the delimiters are
193  unsigned int first_delim_pos = loc_string.find('_');
194  unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
195  //get the layer, ladder, and module as unsigned ints
196  unsigned int checklayer = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
197  unsigned int checkladder =
198  (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
199  unsigned int checkmodule = (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, 5));
200  //check them against the desired layer, ladder, and module
201  if (ladder == checkladder && layer == checklayer && module == checkmodule)
202  //if they match, set the template ID
203  thisID = (short)theBarrelTemplateIds[iter];
204  }
205 
206  if (thisID == 10000 || (!obj.putTemplateID(detid.rawId(), thisID)))
207  edm::LogPrint("SiPixelTemplateDBObjectUploader")
208  << " Could not fill barrel layer " << layer << ", module " << module << "\n";
209  edm::LogPrint("SiPixelTemplateDBObjectUploader")
210  << "This is a barrel element with: layer " << layer << ", ladder " << ladder << " and module " << module;
211  }
212  //Now endcaps
213  else if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap)) ||
214  (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXEC)) {
215  //Get the DetId's disk, blade, side, panel, and module
216  disk = tTopo->pxfDisk(detid.rawId()); //1,2,3
217  blade = tTopo->pxfBlade(detid.rawId()); //1-56 (Ring 1 is 1-22, Ring 2 is 23-56)
218  side = tTopo->pxfSide(detid.rawId()); //side=1 for -z, 2 for +z
219  panel = tTopo->pxfPanel(detid.rawId()); //panel=1,2
220 
221  if (useVectorIndices) {
222  --disk;
223  --blade;
224  --side;
225  --panel;
226  }
227 
228  //Assign IDs
229  //Loop over all the endcap locations
230  for (iter = 0; iter < theEndcapLocations.size(); ++iter) {
231  //get the string of this barrel location
232  std::string loc_string = theEndcapLocations[iter];
233  //find where the delimiters are
234  unsigned int first_delim_pos = loc_string.find('_');
235  unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
236  unsigned int third_delim_pos = loc_string.find('_', second_delim_pos + 1);
237  //get the disk, blade, side, panel, and module as unsigned ints
238  unsigned int checkdisk = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
239  unsigned int checkblade =
240  (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
241  unsigned int checkside =
242  (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, third_delim_pos - second_delim_pos - 1));
243  unsigned int checkpanel = (unsigned int)stoi(loc_string.substr(third_delim_pos + 1, 5));
244  //check them against the desired disk, blade, side, panel, and module
245  if (disk == checkdisk && blade == checkblade && side == checkside && panel == checkpanel)
246  //if they match, set the template ID
247  thisID = (short)theEndcapTemplateIds[iter];
248  }
249 
250  if (thisID == 10000 || (!obj.putTemplateID(detid.rawId(), thisID)))
251  edm::LogPrint("SiPixelTemplateDBObjectUploader")
252  << " Could not fill endcap det unit" << side << ", disk " << disk << ", blade " << blade << ", and panel "
253  << panel << ".\n";
254  edm::LogPrint("SiPixelTemplateDBObjectUploader") << "This is an endcap element with: side " << side << ", disk "
255  << disk << ", blade " << blade << ", and panel " << panel;
256  } else {
257  continue;
258  }
259 
260  //Print out the assignment of this detID
261  short mapnum;
262  mapnum = obj.getTemplateID(detid.rawId());
263  edm::LogPrint("SiPixelTemplateDBObjectUploader")
264  << "The DetID: " << detid.rawId() << " is mapped to the template: " << mapnum << "\n";
265  }
266  }
267 
268  //--- Create a new IOV
270  if (!poolDbService.isAvailable()) // Die if not available
271  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
272  if (poolDbService->isNewTagRequest("SiPixelTemplateDBObjectRcd"))
273  poolDbService->writeOneIOV(obj, poolDbService->beginOfTime(), "SiPixelTemplateDBObjectRcd");
274  else
275  poolDbService->writeOneIOV(obj, poolDbService->currentTime(), "SiPixelTemplateDBObjectRcd");
276 }
277 
279 
const GeomDetEnumerators::SubDetector geomDetSubDetector(int subdet) const
const edm::EventSetup & c
bool putTemplateID(const uint32_t &detid, short &value)
short getTemplateID(const uint32_t &detid) const
SiPixelTemplateDBObjectUploader(const edm::ParameterSet &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int pxfDisk(const DetId &id) const
unsigned int pxbLadder(const DetId &id) const
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
unsigned int pxbModule(const DetId &id) const
Log< level::Error, false > LogError
constexpr std::array< uint8_t, layerIndexSize > layer
bool getData(T &iHolder) const
Definition: EventSetup.h:128
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopologyToken_
int iEvent
Definition: GenABIO.cc:224
bool isNewTagRequest(const std::string &recordName)
bool isAvailable() const
Definition: Service.h:40
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
Log< level::Warning, true > LogPrint
unsigned int pxbLayer(const DetId &id) const
Log< level::Info, false > LogInfo
Definition: DetId.h:17
void setVersion(float version)
edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryToken_
void setNumOfTempl(int numOfTempl)
trackerTopologyToken_(iC.esConsumes())
void analyze(const edm::Event &, const edm::EventSetup &) override
unsigned int pxfSide(const DetId &id) const
std::string fullPath() const
Definition: FileInPath.cc:161
trackerGeometryToken_(iC.esConsumes())
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:157
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
unsigned int pxfPanel(const DetId &id) const
unsigned int pxfBlade(const DetId &id) const
tuple module
Definition: callgraph.py:69