CMS 3D CMS Logo

WriteCTPPSPixGainCalibrations.cc
Go to the documentation of this file.
6 
10 //CTPPS Gain Calibration Conditions Object
14 //CTPPS tracker DetId
16 #include "TFile.h"
17 #include "TH2F.h"
18 #include <iostream>
19 #include <vector>
20 #include <string>
21 
22 //
23 // class declaration
24 //
25 
27 public:
30 
31  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
32 
33 private:
34  void beginJob() override;
35  void analyze(const edm::Event&, const edm::EventSetup&) override;
36  void endJob() override;
37  void getHistos();
38  void fillDB();
39  void getGainsPedsFromHistos(uint32_t detid,
40  int rocId,
41  int index,
42  std::vector<double>& peds,
43  std::vector<double>& gains,
44  std::map<int, int>& myindxmap,
45  int nrocs);
46  void setDummyFullPlane(std::vector<float>& peds, std::vector<float>& gains, int npixplane);
47  // ----------Member data ---------------------------
50  bool m_usedummy;
51  int npfitmin;
52  double gainlow, gainhigh;
54  std::map<uint32_t, std::vector<std::string> > detidHistoNameMap;
55  // std::map<uint32_t, std::vector<std::string> > detidSlopeNameMap;
56  // std::map<uint32_t, std::vector<std::string> > detidInterceptNameMap;
57  // std::map<uint32_t, std::vector<std::string> > detidChi2NameMap;
58  // std::map<uint32_t, std::vector<std::string> > detidNpfitNameMap;
59  std::map<uint32_t, std::vector<int> > detidROCsPresent;
60 };
61 
62 //
63 // constructors and destructor
64 //
66  : m_record(iConfig.getUntrackedParameter<std::string>("record", "CTPPSPixelGainCalibrationsRcd")),
67  m_inputHistosFileName(iConfig.getUntrackedParameter<std::string>("inputrootfile", "inputfile.root")),
68  m_usedummy(iConfig.getUntrackedParameter<bool>("useDummyValues", true)),
69  npfitmin(iConfig.getUntrackedParameter<int>("minimumNpfit", 3)),
70  gainlow(iConfig.getUntrackedParameter<double>("gainLowLimit", 0.0)),
71  gainhigh(iConfig.getUntrackedParameter<double>("gainHighLimit", 100.0)) {}
72 
74  // do anything here that needs to be done at desctruction time
75  // (e.g. close files, deallocate resources etc.)
76 }
77 
78 //
79 // member functions
80 //
81 
82 // ------------ method called for each event ------------
84  // using namespace edm;
85 }
86 
87 // ------------ method called once each job just before starting event loop ------------
89 
90 // ------------ method called once each job just after ending the event loop ------------
92  getHistos();
93  fillDB();
94 }
95 
96 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
98  //The following says we do not know what parameters are allowed so do no validation
99  // Please change this to state exactly what you do use, even if it is no parameters
101  desc.setUnknown();
102  descriptions.addDefault(desc);
103 }
104 
106  // std::cout <<"Parsing file " <<m_inputHistosFileName << std::endl;
107  m_inputRootFile = new TFile(m_inputHistosFileName.c_str());
108  m_inputRootFile->cd();
109 
110  int sector[2] = {45, 56}; // arm 0 is sector 45 and arm 1 is sector 56
111  int nsec = 2;
112  int station[2] = {0, 2}; // for each arm
113  int nst = 2;
114  //int pot[6]={3}; // index of the pot within the 6 pot configuration (vertical top or bottom and horizontal)
115  int npt = 6;
116 
117  for (int i = 0; i < nsec; i++)
118  for (int st = 0; st < nst; st++)
119  for (int pot = 0; pot < npt; pot++) {
120  int arm = i;
121 
122  //Check which pots present
123  char temppathrp[100];
124  sprintf(temppathrp, "CTPPS/CTPPS_SEC%d/CTPPS_SEC%d_RP%d%d%d", sector[i], sector[i], arm, station[st], pot);
125  if (!m_inputRootFile->Get(temppathrp))
126  continue;
127 
128  for (int plane = 0; plane < 6; plane++) {
129  //Check which planes present
130  char temppathplane[100];
131  sprintf(temppathplane,
132  "CTPPS/CTPPS_SEC%d/CTPPS_SEC%d_RP%d%d%d/CTPPS_SEC%d_RP%d%d%d_PLN%d",
133  sector[i],
134  sector[i],
135  arm,
136  station[st],
137  pot,
138  sector[i],
139  arm,
140  station[st],
141  pot,
142  plane);
143 
144  // do not skip the missing plane, instead put dummy values
145  // if(!m_inputRootFile->Get(temppathplane)) continue;
146 
147  CTPPSPixelDetId mytempid(arm, station[st], pot, plane);
148  std::vector<std::string> histnamevec;
149  std::vector<int> listrocs;
150  for (int roc = 0; roc < 6; roc++) {
151  char temppathhistos[200];
152 
153  sprintf(
154  temppathhistos,
155  "CTPPS/CTPPS_SEC%d/CTPPS_SEC%d_RP%d%d%d/CTPPS_SEC%d_RP%d%d%d_PLN%d/CTPPS_SEC%d_RP%d%d%d_PLN%d_ROC%d",
156  sector[i],
157  sector[i],
158  arm,
159  station[st],
160  pot,
161  sector[i],
162  arm,
163  station[st],
164  pot,
165  plane,
166  sector[i],
167  arm,
168  station[st],
169  pot,
170  plane,
171  roc);
172 
173  std::string pathhistos(temppathhistos);
174  std::string pathslope = pathhistos + "_Slope2D";
175  std::string pathintercept = pathhistos + "_Intercept2D";
176  if (m_inputRootFile->Get(pathslope.c_str()) && m_inputRootFile->Get(pathintercept.c_str())) {
177  histnamevec.push_back(pathhistos);
178  listrocs.push_back(roc);
179  }
180  }
181  detidHistoNameMap[mytempid.rawId()] = histnamevec;
182  detidROCsPresent[mytempid.rawId()] = listrocs;
183  edm::LogInfo("CTPPSPixGainsCalibrationWriter")
184  << "Raw DetId = " << mytempid.rawId() << " Arm = " << arm << " Sector = " << sector[arm]
185  << " Station = " << station[st] << " Pot = " << pot << " Plane = " << plane;
186  }
187  }
188 }
189 
191  CTPPSPixelGainCalibrations gainCalibsTest;
192  CTPPSPixelGainCalibrations gainCalibsTest1;
193 
194  // std::cout<<"Here! "<<std::endl;
195 
196  for (std::map<uint32_t, std::vector<int> >::iterator it = detidROCsPresent.begin(); it != detidROCsPresent.end();
197  ++it) {
198  uint32_t tempdetid = it->first;
199  std::vector<int> rocs = it->second;
200  unsigned int nrocs = rocs.size();
201  std::map<int, int> mapIPixIndx;
202 
203  std::vector<double> gainsFromHistos;
204  std::vector<double> pedsFromHistos;
205 
206  CTPPSPixelGainCalibration tempPGCalib;
207 
208  for (unsigned int i = 0; i < nrocs; i++) {
209  getGainsPedsFromHistos(tempdetid, i, rocs[i], pedsFromHistos, gainsFromHistos, mapIPixIndx, nrocs);
210  }
211 
212  std::vector<float> orderedGains;
213  std::vector<float> orderedPeds;
214  for (unsigned int k = 0; k < nrocs * 52 * 80; k++) {
215  int indx = mapIPixIndx[k];
216  float tmpped = pedsFromHistos[indx];
217  float tmpgain = gainsFromHistos[indx];
218  orderedGains.push_back(tmpgain);
219  orderedPeds.push_back(tmpped);
220  tempPGCalib.putData(k, tmpped, tmpgain);
221  }
222 
223  if (nrocs == 0) {
224  edm::LogWarning("CTPPSPixGainsCalibrationWriter") << " plane with detID =" << tempdetid << " is empty";
225  setDummyFullPlane(orderedPeds, orderedGains, 6 * 52 * 80);
226  }
227 
228  gainCalibsTest.setGainCalibration(tempdetid, orderedPeds, orderedGains);
229  // std::cout << "Here detid = "<<tempdetid <<std::endl;
230  gainCalibsTest1.setGainCalibration(tempdetid, tempPGCalib);
231  // std::cout << "Here again"<<std::endl;
232  }
233  // std::cout<<" Here 3!"<<std::endl;
235  if (!mydbservice.isAvailable()) {
236  edm::LogError("CTPPSPixGainsCalibrationWriter") << "Db Service Unavailable";
237  return;
238  }
239  mydbservice->writeOneIOV(gainCalibsTest, mydbservice->currentTime(), m_record);
240 }
241 
243  std::vector<float>& gains,
244  int npixplane) {
245  for (int i = 0; i < npixplane; ++i) {
246  peds.push_back(20.);
247  gains.push_back(0.5);
248  }
249 }
250 
252  int ROCindex,
253  int rocId,
254  std::vector<double>& peds,
255  std::vector<double>& gains,
256  std::map<int, int>& mymap,
257  int nrocs) {
258  CTPPSPixelIndices modulepixels(52 * nrocs / 2, 160);
259 
260  std::string tmpslopename = detidHistoNameMap[detid][ROCindex] + "_Slope2D";
261  std::string tmpitcpname = detidHistoNameMap[detid][ROCindex] + "_Intercept2D";
262  std::string tmpchi2name = detidHistoNameMap[detid][ROCindex] + "_Chisquare2D";
263  std::string tmpnpfitsname = detidHistoNameMap[detid][ROCindex] + "_Npfits2D";
264  TH2D* tempslope = (TH2D*)m_inputRootFile->Get(tmpslopename.c_str());
265  TH2D* tempintrcpt = (TH2D*)m_inputRootFile->Get(tmpitcpname.c_str());
266  // TH2D * tempchi2 = (TH2D*) m_inputRootFile->Get(tmpchi2name.c_str());
267  TH2D* tempnpfit = (TH2D*)m_inputRootFile->Get(tmpnpfitsname.c_str());
268  int ncols = tempslope->GetNbinsX();
269  int nrows = tempslope->GetNbinsY();
270  if (nrows != 80 || ncols != 52)
271  edm::LogWarning("CTPPSPixGainsCalibrationWriter")
272  << "Something wrong ncols = " << ncols << " and nrows = " << nrows;
273 
274  for (int jrow = 0; jrow < nrows;
275  ++jrow) // when scanning through the 2d histo make sure to avoid underflow bin i or j ==0
276  for (int icol = 0; icol < ncols; ++icol) {
277  double tmpslp = tempslope->GetBinContent(icol + 1, jrow + 1);
278  double tmpgain = (tmpslp == 0.0) ? 0.0 : 1.0 / tmpslp;
279  double tmpped = tempintrcpt->GetBinContent(icol + 1, jrow + 1);
280  // check for noisy/badly calibrated pixels
281  int tmpnpfit = tempnpfit->GetBinContent(icol + 1, jrow + 1);
282  //double tmpchi2 = tempchi2 -> GetBinContent(icol+1,jrow+1);
283  if (tmpnpfit < npfitmin || tmpgain < gainlow || tmpgain > gainhigh) {
284  // std::cout << " *** Badly calibrated pixel, NPoints = "<<tmpnpfit << " setting dummy values gain = 0.5 and ped =20.0 ***" <<std::endl;
285  // std::cout << " **** bad Pixel column icol = "<<icol <<" and jrow = "<<jrow <<" Name= "<< tmpslopename <<std::endl;
286  if (m_usedummy) {
287  tmpgain = 1.0 / 2.0;
288  tmpped = 20.0;
289  }
290  // else leave as is and set noisy in mask?
291  }
292 
293  gains.push_back(tmpgain);
294  peds.push_back(tmpped);
295  int modCol = -1;
296  int modRow = -1;
297  modulepixels.transformToModule(icol, jrow, rocId, modCol, modRow);
298  int indx = gains.size() - 1;
299  int pixIndx = modCol + modRow * (52 * nrocs / 2);
300  mymap[pixIndx] = indx;
301  }
302 }
303 
304 //define this as a plug-in
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Log< level::Error, false > LogError
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
std::map< uint32_t, std::vector< std::string > > detidHistoNameMap
void setGainCalibration(const uint32_t &DetId, const CTPPSPixelGainCalibration &PixGains)
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void analyze(const edm::Event &, const edm::EventSetup &) override
constexpr float gains[NGAINS]
Definition: EcalConstants.h:11
Log< level::Info, false > LogInfo
void getGainsPedsFromHistos(uint32_t detid, int rocId, int index, std::vector< double > &peds, std::vector< double > &gains, std::map< int, int > &myindxmap, int nrocs)
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
WriteCTPPSPixGainCalibrations(const edm::ParameterSet &)
int transformToModule(const int colROC, const int rowROC, const int rocId, int &col, int &row) const
void setDummyFullPlane(std::vector< float > &peds, std::vector< float > &gains, int npixplane)
bool isAvailable() const
Definition: Service.h:40
Log< level::Warning, false > LogWarning
void putData(uint32_t ipix, float ped, float gain)
std::map< uint32_t, std::vector< int > > detidROCsPresent