CMS 3D CMS Logo

SiPixelGenErrorDBObjectUploader.cc
Go to the documentation of this file.
1 #include <cstdio>
2 #include <fstream>
3 #include <iostream>
4 #include <memory>
5 
8 
11 
16 
21 
24 
28 
32 
34 public:
37 
38  typedef std::vector<std::string> vstring;
39 
40 private:
41  void beginJob() override;
42  void analyze(const edm::Event&, const edm::EventSetup&) override;
43  void endJob() override;
44 
47  float theVersion;
48  float theMagField;
49  std::vector<uint32_t> theDetIds;
52  std::vector<uint32_t> theBarrelGenErrIds;
53  std::vector<uint32_t> theEndcapGenErrIds;
57 };
58 
60  : theGenErrorCalibrations(iConfig.getParameter<vstring>("siPixelGenErrorCalibrations")),
61  theGenErrorBaseString(iConfig.getParameter<std::string>("theGenErrorBaseString")),
62  theVersion(iConfig.getParameter<double>("Version")),
63  theMagField(iConfig.getParameter<double>("MagField")),
64  theBarrelLocations(iConfig.getParameter<std::vector<std::string> >("barrelLocations")),
65  theEndcapLocations(iConfig.getParameter<std::vector<std::string> >("endcapLocations")),
66  theBarrelGenErrIds(iConfig.getParameter<std::vector<uint32_t> >("barrelGenErrIds")),
67  theEndcapGenErrIds(iConfig.getParameter<std::vector<uint32_t> >("endcapGenErrIds")),
68  useVectorIndices(iConfig.getUntrackedParameter<bool>("useVectorIndices", false)),
69  trackerGeometryToken_(esConsumes()),
70  trackerTopologyToken_(esConsumes()) {}
71 
73 
75 
77  //--- Make the POOL-ORA object to store the database object
79 
80  // Local variables
81  int m;
82 
83  // Set the number of GenErrors to be passed to the dbobject
84  obj.setNumOfTempl(theGenErrorCalibrations.size());
85 
86  // Set the version of the GenError dbobject - this is an external parameter
87  obj.setVersion(theVersion);
88 
89  // Open the GenError 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("GenError Info") << "Opened GenError File: " << file.fullPath().c_str();
95 
96  // Local variables
97  char title_char[80], c;
99  float tempstore;
100  int iter, j, k;
101 
102  // GenErrors 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 
113  for (j = 0; j < 80; j += 4) {
114  temp.c[0] = title_char[j];
115  temp.c[1] = title_char[j + 1];
116  temp.c[2] = title_char[j + 2];
117  temp.c[3] = title_char[j + 3];
118  obj.push_back(temp.f);
119  obj.setMaxIndex(obj.maxIndex() + 1);
120  }
121 
122  // Check if the magnetic field is the same as in the header of the input files
123  for (k = 0; k < 80; k++) {
124  if ((title_char[k] == '@') && (title_char[k - 1] == 'T')) {
125  double localMagField = (((int)title_char[k - 4]) - 48) * 10 + ((int)title_char[k - 2]) - 48;
126  if (theMagField != localMagField) {
127  edm::LogPrint("SiPixelGenErrorDBObjectUploader")
128  << "\n -------- WARNING -------- \n Magnetic field in the cfg is " << theMagField << "T while it is "
129  << title_char[k - 4] << title_char[k - 2] << title_char[k - 1]
130  << " in the header \n ------------------------- \n " << std::endl;
131  }
132  }
133  }
134 
135  // Fill the dbobject
136  in_file >> tempstore;
137  while (!in_file.eof()) {
138  obj.setMaxIndex(obj.maxIndex() + 1);
139  obj.push_back(tempstore);
140  in_file >> tempstore;
141  }
142 
143  in_file.close();
144  } else {
145  // If file didn't open, report this
146  edm::LogError("SiPixelGenErrorDBObjectUploader") << "Error opening File " << file.fullPath().c_str();
147  }
148  }
149 
150  //get TrackerGeometry from the event setup
152  const TrackerGeometry* tGeo = &iSetup.getData(trackerGeometryToken_);
153 
154  // Use the TrackerTopology class for layer/disk etc. number
155  const TrackerTopology* tTopo = &iSetup.getData(trackerTopologyToken_);
156 
157  // Check if we are using Phase-1 or Phase-2 geometry
158  int phase = 0;
160  phase = 1;
161  } else if (pDD->isThere(GeomDetEnumerators::P2PXB) && pDD->isThere(GeomDetEnumerators::P2PXEC) == true) {
162  phase = 2;
163  }
164  edm::LogPrint("SiPixelGenErrorDBObjectUploader") << "Phase-" << phase << " geometry is used" << std::endl;
165 
166  //Loop over the detector elements and put the GenError IDs in place
167  for (const auto& it : pDD->detUnits()) {
168  if (it != nullptr) {
169  // Here is the actual looping step over all DetIds:
170  DetId detid = it->geographicalId();
171  unsigned int layer = 0, ladder = 0, disk = 0, side = 0, blade = 0, panel = 0, module = 0;
172  // Some extra variables that can be used for Phase 1 - comment in if needed
173  // unsigned int shl=0, sec=0, half=0, flipped=0, ring=0;
174  short thisID = 10000;
175  unsigned int iter;
176 
177  // Now we sort them into the Barrel and Endcap:
178  //Barrel Pixels first
179  if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) ||
180  (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXB)) {
181  //Get the layer, ladder, and module corresponding to this DetID
182  layer = tTopo->pxbLayer(detid.rawId());
183  ladder = tTopo->pxbLadder(detid.rawId());
184  module = tTopo->pxbModule(detid.rawId());
185 
186  if (useVectorIndices) {
187  --layer;
188  --ladder;
189  --module;
190  }
191 
192  //Assign template IDs
193  //Loop over all the barrel locations
194  for (iter = 0; iter < theBarrelLocations.size(); ++iter) {
195  //get the string of this barrel location
196  std::string loc_string = theBarrelLocations[iter];
197  //find where the delimiters are
198  unsigned int first_delim_pos = loc_string.find('_');
199  unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
200  //get the layer, ladder, and module as unsigned ints
201  unsigned int checklayer = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
202  unsigned int checkladder =
203  (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
204  unsigned int checkmodule = (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, 5));
205  //check them against the desired layer, ladder, and module
206  if (ladder == checkladder && layer == checklayer && module == checkmodule)
207  //if they match, set the template ID
208  thisID = (short)theBarrelGenErrIds[iter];
209  }
210 
211  if (thisID == 10000 || (!obj.putGenErrorID(detid.rawId(), thisID)))
212  edm::LogPrint("SiPixelGenErrorDBObjectUploader")
213  << " Could not fill barrel layer " << layer << ", module " << module << "\n";
214  edm::LogPrint("SiPixelGenErrorDBObjectUploader")
215  << "This is a barrel element with: layer " << layer << ", ladder " << ladder << " and module " << module;
216  }
217  //Now endcaps
218  else if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap)) ||
219  (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXEC)) {
220  //Get the DetID's disk, blade, side, panel, and module
221  disk = tTopo->pxfDisk(detid.rawId()); //1,2,3
222  blade = tTopo->pxfBlade(detid.rawId()); //1-56 (Ring 1 is 1-22, Ring 2 is 23-56)
223  side = tTopo->pxfSide(detid.rawId()); //side=1 for -z, 2 for +z
224  panel = tTopo->pxfPanel(detid.rawId()); //panel=1,2
225 
226  if (useVectorIndices) {
227  --disk;
228  --blade;
229  --side;
230  --panel;
231  }
232 
233  //Assign IDs
234  //Loop over all the endcap locations
235  for (iter = 0; iter < theEndcapLocations.size(); ++iter) {
236  //get the string of this barrel location
237  std::string loc_string = theEndcapLocations[iter];
238  //find where the delimiters are
239  unsigned int first_delim_pos = loc_string.find('_');
240  unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
241  unsigned int third_delim_pos = loc_string.find('_', second_delim_pos + 1);
242  //get the disk, blade, side, panel, and module as unsigned ints
243  unsigned int checkdisk = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
244  unsigned int checkblade =
245  (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
246  unsigned int checkside =
247  (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, third_delim_pos - second_delim_pos - 1));
248  unsigned int checkpanel = (unsigned int)stoi(loc_string.substr(third_delim_pos + 1, 5));
249  //check them against the desired disk, blade, side, panel, and module
250  if (disk == checkdisk && blade == checkblade && side == checkside && panel == checkpanel)
251  //if they match, set the template ID
252  thisID = (short)theEndcapGenErrIds[iter];
253  }
254 
255  if (thisID == 10000 || (!obj.putGenErrorID(detid.rawId(), thisID)))
256  edm::LogPrint("SiPixelGenErrorDBObjectUploader")
257  << " Could not fill endcap det unit" << side << ", disk " << disk << ", blade " << blade << ", panel "
258  << panel << ".\n";
259  edm::LogPrint("SiPixelGenErrorDBObjectUploader") << "This is an endcap element with: side " << side << ", disk "
260  << disk << ", blade " << blade << ", panel " << panel;
261  } else {
262  continue;
263  }
264 
265  //Print out the assignment of this DetID
266  short mapnum;
267  mapnum = obj.getGenErrorID(detid.rawId());
268  edm::LogPrint("SiPixelGenErrorDBObjectUploader")
269  << "The DetID: " << detid.rawId() << " is mapped to the template: " << mapnum << "\n";
270  }
271  }
272 
273  //--- Create a new IOV
275  if (!poolDbService.isAvailable()) // Die if not available
276  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
277  if (poolDbService->isNewTagRequest("SiPixelGenErrorDBObjectRcd"))
278  poolDbService->writeOneIOV(obj, poolDbService->beginOfTime(), "SiPixelGenErrorDBObjectRcd");
279  else
280  poolDbService->writeOneIOV(obj, poolDbService->currentTime(), "SiPixelGenErrorDBObjectRcd");
281 }
282 
284 
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
unsigned int pxbLayer(const DetId &id) const
void analyze(const edm::Event &, const edm::EventSetup &) override
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
unsigned int pxfBlade(const DetId &id) const
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopologyToken_
const DetContainer & detUnits() const override
Returm a vector of all GeomDet.
unsigned int pxbLadder(const DetId &id) const
Log< level::Error, false > LogError
edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryToken_
constexpr std::array< uint8_t, layerIndexSize< TrackerTraits > > layer
int iEvent
Definition: GenABIO.cc:224
SiPixelGenErrorDBObjectUploader(const edm::ParameterSet &)
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)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
Log< level::Warning, true > LogPrint
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
unsigned int pxfPanel(const DetId &id) const
Log< level::Info, false > LogInfo
Definition: DetId.h:17
unsigned int pxfSide(const DetId &id) const
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