CMS 3D CMS Logo

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