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