CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CSCAFEBThrAnalysis.cc
Go to the documentation of this file.
6 #include "TMath.h"
7 
8 class CSCFitAFEBThr;
9 
11  hist_file = nullptr; // set to null
12 
13  nmbev = 0;
14  nmbev_no_wire = 0;
15 
16  npulses = 100;
17  vecDac.clear();
18  BegDac = 1;
19  EndDac = 29;
20  EvDac = 1;
21  StepDac = 1;
22  indDac = 0;
23 
24  vecDacOccup.assign(150, 0);
25  m_wire_dac.clear();
26  m_res_for_db.clear();
27  mh_ChanEff.clear();
28  mh_FirstTime.clear();
29  mh_AfebDac.clear();
30 }
31 
34  hist_file = new TFile(histoFileName.c_str(), "RECREATE");
35  hist_file->cd();
36 }
37 
38 void CSCAFEBThrAnalysis::bookForId(int flag, const int& idint, const std::string& idstring) {
39  hist_file->cd();
40 
41  std::ostringstream ss;
42 
43  if (flag == 100) {
44  ss << idint << "_Anode_First_Time";
45  mh_FirstTime[idint] = new TH2F(ss.str().c_str(), "", 675, 0.0, 675.0, 50, 0.0, 10.0);
46  mh_FirstTime[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
47  mh_FirstTime[idint]->GetYaxis()->SetTitle("Anode First Time Bin");
48  mh_FirstTime[idint]->SetOption("BOX");
49  ss.str(""); // clear
50  }
51 
52  if (flag == 101) {
53  ss << idint << "_Anode_Chan_Eff";
54  mh_ChanEff[idint] = new TH1F(ss.str().c_str(), "", 675, 0.0, 675.0);
55  mh_ChanEff[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
56  mh_ChanEff[idint]->GetYaxis()->SetTitle("Entries");
57  ss.str(""); // clear
58  }
59 
60  if (flag == 200) {
61  ss << idint << "_Anode_AfebDac";
62  mh_AfebDac[idint] = new TH2F(ss.str().c_str(), "", 75, 0.0, 75.0, 50, 0.0, 50.0);
63  mh_AfebDac[idint]->GetXaxis()->SetTitle("Threshold DAC");
64  mh_AfebDac[idint]->GetYaxis()->SetTitle("AFEB Channel Occupancy");
65  mh_AfebDac[idint]->SetOption("COL");
66  ss.str(""); // clear
67  }
68 
69  if (flag == 300) {
70  ss << idint << "_Anode_AfebThrPar";
71  mh_AfebThrPar[idint] = new TH2F(ss.str().c_str(), "", 700, 0.0, 700.0, 50, 0.0, 50.0);
72  mh_AfebThrPar[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
73  mh_AfebThrPar[idint]->GetYaxis()->SetTitle("AFEB Channel Threshold (DAC)");
74  mh_AfebThrPar[idint]->SetOption("BOX");
75  ss.str(""); // clear
76  }
77 
78  if (flag == 400) {
79  ss << idint << "_Anode_AfebNoisePar";
80  mh_AfebNoisePar[idint] = new TH2F(ss.str().c_str(), "", 700, 0.0, 700.0, 50, 0.0, 5.0);
81  mh_AfebNoisePar[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
82  mh_AfebNoisePar[idint]->GetYaxis()->SetTitle("AFEB Channel Noise (DAC)");
83  mh_AfebNoisePar[idint]->SetOption("BOX");
84  ss.str(""); // clear
85  }
86 
87  if (flag == 500) {
88  ss << idint << "_Anode_AfebNDF";
89  mh_AfebNDF[idint] = new TH2F(ss.str().c_str(), "", 700, 0.0, 700.0, 25, -5.0, 20.0);
90  mh_AfebNDF[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
91  mh_AfebNDF[idint]->GetYaxis()->SetTitle("AFEB Channel Fit NDF");
92  mh_AfebNDF[idint]->SetOption("BOX");
93  ss.str(""); // clear
94  }
95 
96  if (flag == 600) {
97  ss << idint << "_Anode_AfebChi2perNDF";
98  mh_AfebChi2perNDF[idint] = new TH2F(ss.str().c_str(), "", 700, 0.0, 700.0, 50, 0.0, 10.0);
99  mh_AfebChi2perNDF[idint]->GetXaxis()->SetTitle("(AFEB-1)*16+ch");
100  mh_AfebChi2perNDF[idint]->GetYaxis()->SetTitle("AFEB Channel Fit Chi2/NDF");
101  mh_AfebChi2perNDF[idint]->SetOption("BOX");
102  ss.str(""); // clear
103  }
104 }
105 
106 void CSCAFEBThrAnalysis::hf1ForId(std::map<int, TH1*>& mp, int flag, const int& id, float& x, float w) {
107  std::map<int, TH1*>::iterator h;
108  h = mp.find(id);
109  if (h == mp.end()) {
110  bookForId(flag, id, "");
111  h = mp.find(id);
112  }
113  h->second->Fill(x, w);
114 }
115 
116 void CSCAFEBThrAnalysis::hf2ForId(std::map<int, TH2*>& mp, int flag, const int& id, float& x, float& y, float w) {
117  std::map<int, TH2*>::iterator h;
118  h = mp.find(id);
119  if (h == mp.end()) {
120  bookForId(flag, id, "");
121  h = mp.find(id);
122  }
123  h->second->Fill(x, y, w);
124 }
125 
126 /* Analyze the hits */
128  std::ostringstream ss;
129  std::map<int, std::vector<int> >::iterator intIt;
130  std::map<int, std::vector<std::vector<int> > >::iterator wiredacIt;
131 
132  std::vector<int> vec;
133  int afeb, ch;
134  float x, y;
135 
136  m_wire_ev.clear();
137 
139 
140  nmbev++;
141  //indDac=(nmbev-1)/EvDac;
142  float dac = BegDac + StepDac * indDac;
143  if (vecDac.size() <= indDac)
144  vecDac.push_back(dac);
145 
146  //std::cout<<" Event "<<nmbev;
147  //std::cout<<" "<<indDac<<" "<<vecDac[indDac]<<std::endl;
148 
149  //Anode wires
150 
152  if (wirecltn.begin() == wirecltn.end())
153  nmbev_no_wire++;
154 
155  if (wirecltn.begin() != wirecltn.end()) {
157 
158  for (wiredetUnitIt = wirecltn.begin(); wiredetUnitIt != wirecltn.end(); ++wiredetUnitIt) {
159  const CSCDetId& id = (*wiredetUnitIt).first;
160 
161  const int idchamber = id.endcap() * 10000 + id.station() * 1000 + id.ring() * 100 + id.chamber();
162  const int idlayer =
163  id.endcap() * 100000 + id.station() * 10000 + id.ring() * 1000 + id.chamber() * 10 + id.layer();
164 
165  // std::cout<<idchamber<<" "<<idlayer<<std::endl;
166 
167  const int maxwire = csctoafeb.getMaxWire(id.station(), id.ring());
168  std::vector<int> wireplane(maxwire, 0);
169 
170  const CSCWireDigiCollection::Range& range = (*wiredetUnitIt).second;
171  for (CSCWireDigiCollection::const_iterator digiIt = range.first; digiIt != range.second; ++digiIt) {
172  int iwire = (*digiIt).getWireGroup();
173  if (iwire <= maxwire) {
174  if (wireplane[iwire - 1] == 0) {
175  wireplane[iwire - 1] = (*digiIt).getBeamCrossingTag() + 1;
176  ch = csctoafeb.getAfebCh(id.layer(), (*digiIt).getWireGroup());
177  afeb = csctoafeb.getAfebPos(id.layer(), (*digiIt).getWireGroup());
178 
180 
181  x = (afeb - 1) * 16 + ch;
182  y = wireplane[iwire - 1];
183  hf2ForId(mh_FirstTime, 100, idchamber, x, y, 1.0);
184 
185  } // end if wireplane[iwire-1]==0
186  } // end if iwire<=csctoafeb.getMaxWire(id.station(),id.ring()
187  } // end of for digis in layer
188 
190 
191  if (m_wire_ev.find(idlayer) == m_wire_ev.end())
192  m_wire_ev[idlayer] = wireplane;
193 
194  } // end of cycle on detUnit
195 
197 
198  for (intIt = m_wire_ev.begin(); intIt != m_wire_ev.end(); ++intIt) {
199  const int idwirev = (*intIt).first;
200  const std::vector<int> wiretemp = (*intIt).second; // What for?
201  int nsize = EndDac - BegDac + 1;
202  std::vector<int> zer(nsize, 0);
203 
204  wiredacIt = m_wire_dac.find(idwirev);
205  if (wiredacIt == m_wire_dac.end()) {
206  for (unsigned int j = 0; j < wiretemp.size(); j++)
207  m_wire_dac[idwirev].push_back(zer);
208  wiredacIt = m_wire_dac.find(idwirev);
209  // std::cout<<idwirev<<" "<<wiredacIt->second.size()<<" "<<
210  // wiredacIt->second[0].size()<<std::endl;
211  }
212  for (unsigned int i = 0; i < (*intIt).second.size(); i++)
213  if ((*intIt).second[i] > 0)
214  wiredacIt->second[i][indDac] = wiredacIt->second[i][indDac] + 1;
215  } // end of adding hits to the map of wire/DAC
216  } // end of if wire collection not empty
217  indDac++;
218  if (dac == (float)EndDac)
219  indDac = 0;
220 } // end of void CSCAFEBThrAnalysis
221 
223  float x, y;
224 
225  //This is for DB transfer
226  CSCobject cn{};
227 
228  std::map<int, std::vector<std::vector<int> > >::iterator mwiredacIt;
229  std::map<int, std::vector<std::vector<float> > >::iterator mresfordbIt;
230  std::vector<int>::iterator vecIt;
231 
232  std::cout << "Events analyzed " << nmbev << std::endl;
233  std::cout << "Events no anodes " << nmbev_no_wire << std::endl << std::endl;
234 
235  std::cout << "DAC occupancy" << std::endl;
236  size_t ndacsize = EndDac - BegDac + 1;
237  for (size_t i = 0; i < ndacsize; i++)
238  std::cout << " " << vecDacOccup[i];
239  std::cout << "\n\n" << std::endl;
240 
241  std::vector<float> inputx;
242  std::vector<float> inputy;
243 
244  std::vector<float> mypar(2, 0.0);
245  std::vector<float> ermypar(2, 0.0);
246  float ercorr, chisq, edm;
247  int ndf, niter;
248 
249  int ch, afeb, idchmb;
250 
251  CSCFitAFEBThr* fitAnodeThr;
252 
253  std::vector<float> fitres(4, 0);
254 
255  // std::cout<<"Threshold curves:\n"<<std::endl;
256 
257  for (mwiredacIt = m_wire_dac.begin(); mwiredacIt != m_wire_dac.end(); ++mwiredacIt) {
258  int idwiredac = (*mwiredacIt).first;
259 
260  int layer = idwiredac - (idwiredac / 10) * 10;
261  idchmb = idwiredac / 10;
262 
263  for (int unsigned iwire = 0; iwire < mwiredacIt->second.size(); iwire++) {
264  afeb = csctoafeb.getAfebPos(layer, iwire + 1);
265  ch = csctoafeb.getAfebCh(layer, iwire + 1);
266  int afebid = (idwiredac / 10) * 100 + csctoafeb.getAfebPos(layer, iwire + 1);
267 
268  indDac = 0;
269  for (vecIt = mwiredacIt->second[iwire].begin(); vecIt != mwiredacIt->second[iwire].end(); ++vecIt) {
270  x = vecDac[indDac];
271  y = *vecIt;
272  hf2ForId(mh_AfebDac, 200, afebid, x, y, 1.0);
273 
275  if (indDac == 0) {
276  x = (afeb - 1) * 16 + ch;
277  y = *vecIt;
278  hf1ForId(mh_ChanEff, 101, idchmb, x, y);
279  }
280 
281  inputx.push_back(x);
282  inputy.push_back(y);
283 
284  indDac++;
285  }
286  // end of DAC cycle to form vectors of input data (inputx,inputy)for fit
287 
288  // std::cout<<afebid<<" "<<ch<<std::endl;
289  // for(unsigned int i=0;i<inputx.size();i++)
290  // std::cout<<" "<<inputy[i];
291  // std::cout<<std::endl;
292 
293  for (unsigned int i = 0; i < 2; i++) {
294  mypar[i] = 0.0;
295  ermypar[i] = 0.0;
296  }
297  ercorr = 0.0;
298  chisq = 0.0;
299  ndf = 0;
300  niter = 0;
301  edm = 0.0;
302 
304  fitAnodeThr = new CSCFitAFEBThr();
305 
306  fitAnodeThr->ThresholdNoise(inputx, inputy, npulses, vecDacOccup, mypar, ermypar, ercorr, chisq, ndf, niter, edm);
307  delete fitAnodeThr;
308 
309  // std::cout<<"Fit "<<mypar[0]<<" "<<mypar[1]<<" "<<ndf<<" "<<chisq
310  // <<std::endl<<std::endl;
311 
313 
314  x = (afeb - 1) * 16 + ch;
315 
317  y = mypar[0];
318  hf2ForId(mh_AfebThrPar, 300, idchmb, x, y, 1.0);
319 
321  y = mypar[1];
322  hf2ForId(mh_AfebNoisePar, 400, idchmb, x, y, 1.0);
323 
325  y = ndf;
326  hf2ForId(mh_AfebNDF, 500, idchmb, x, y, 1.0);
327 
329  y = 0.0;
330  if (ndf > 0)
331  y = chisq / (float)ndf;
332  hf2ForId(mh_AfebChi2perNDF, 600, idchmb, x, y, 1.0);
333 
335  fitres[0] = mypar[0];
336  fitres[1] = mypar[1];
337  fitres[2] = ndf;
338  fitres[3] = 0.0;
339  if (ndf > 0)
340  fitres[3] = chisq / (float)ndf;
341 
343 
344  mresfordbIt = m_res_for_db.find(idwiredac);
345  if (mresfordbIt == m_res_for_db.end())
346  m_res_for_db[idwiredac].push_back(fitres);
347  else
348  m_res_for_db[idwiredac].push_back(fitres);
349 
350  inputx.clear();
351  inputy.clear();
352 
353  } // end for(int iwire=0)
354  } // end of iteration thru m_wire_dac map
355 
356  std::cout << "Size of map for DB " << m_res_for_db.size() << std::endl;
357 
358  std::cout << "The following CSCs will go to DB" << std::endl << std::endl;
359 
360  for (mresfordbIt = m_res_for_db.begin(); mresfordbIt != m_res_for_db.end(); ++mresfordbIt) {
361  int idlayer = (*mresfordbIt).first;
362  idchmb = idlayer / 10;
363  int layer = idlayer - idchmb * 10;
364  std::cout << "CSC " << idchmb << " Layer " << layer << " " << (*mresfordbIt).second.size() << std::endl;
365  }
366 
367  for (mresfordbIt = m_res_for_db.begin(); mresfordbIt != m_res_for_db.end(); ++mresfordbIt) {
368  int idlayer = (*mresfordbIt).first;
369 
370  //This is for DB transfer
371  int size = (*mresfordbIt).second.size();
372  cn.obj[idlayer].resize(size);
373 
374  for (unsigned int i = 0; i < (*mresfordbIt).second.size(); i++) {
375  std::cout << idlayer << " " << i + 1 << " ";
376 
377  for (int j = 0; j < 4; j++)
378  std::cout << (*mresfordbIt).second[i][j] << " ";
379  std::cout << std::endl;
380 
381  //This is for DB transfer
382  cn.obj[idlayer][i].resize(4);
383  cn.obj[idlayer][i][0] = (*mresfordbIt).second[i][0];
384  cn.obj[idlayer][i][1] = (*mresfordbIt).second[i][1];
385  cn.obj[idlayer][i][2] = (*mresfordbIt).second[i][2];
386  cn.obj[idlayer][i][3] = (*mresfordbIt).second[i][3];
387  }
388  }
389 
390  //send data to DB
391  //dbon->cdbon_last_run("afeb_thresholds",&run);
392  //std::cout<<"Last AFEB thresholds run "<<run<<" for run file "<<myname<<" saved "<<myTime<<std::endl;
393  //if(debug) dbon->cdbon_write(cn,"afeb_thresholds",run+1,myTime);
394 
395  if (hist_file != nullptr) { // if there was a histogram file...
396  hist_file->Write(); // write out the histrograms
397  delete hist_file; // close and delete the file
398  hist_file = nullptr; // set to zero to clean up
399  std::cout << "Hist. file was closed\n";
400  }
401 
402  std::cout << " End of CSCAFEBThrAnalysis" << std::endl;
403 }
int getAfebCh(int layer, int wiregroup) const
return AFEB channel number
Definition: CSCToAFEB.cc:14
std::map< int, TH2 * > mh_FirstTime
const double w
Definition: UKUtility.cc:23
tuple histoFileName
Definition: diJetCalib.py:108
std::map< int, std::vector< std::vector< float > > > m_res_for_db
std::map< int, std::vector< int > > m_wire_ev
Maps - per event, threshold curve, fit results.
virtual bool ThresholdNoise(const std::vector< float > &inputx, const std::vector< float > &inputy, const int &npulses, std::vector< int > &dacoccup, std::vector< float > &mypar, std::vector< float > &ermypar, float &ercorr, float &chisq, int &ndf, int &niter, float &edm) const
constexpr std::array< uint8_t, layerIndexSize > layer
const uint16_t range(const Frame &aFrame)
int endcap() const
Definition: CSCDetId.h:85
std::map< int, TH1 * > mh_ChanEff
Histogram maps.
std::vector< int > vecDacOccup
std::map< int, TH2 * > mh_AfebChi2perNDF
uint16_t const *__restrict__ x
Definition: gpuClustering.h:39
int getMaxWire(int station, int ring) const
return max. number of wiregroups per layer
Definition: CSCToAFEB.cc:53
std::map< int, TH2 * > mh_AfebDac
std::vector< float > vecDac
const CSCToAFEB csctoafeb
Layer, wire to AFEB, channel conversion.
std::pair< const_iterator, const_iterator > Range
std::vector< DigiType >::const_iterator const_iterator
std::map< int, TH2 * > mh_AfebNDF
int nmbev
Statistics.
void hf1ForId(std::map< int, TH1 * > &mp, int flag, const int &id, float &x, float w)
TFile * hist_file
ROOT hist file.
int getAfebPos(int layer, int wiregroup) const
return AFEB position number
Definition: CSCToAFEB.cc:20
std::map< int, TH2 * > mh_AfebNoisePar
std::map< int, std::vector< std::vector< int > > > m_wire_dac
tuple cout
Definition: gather_cfg.py:144
void setup(const std::string &histoFileName)
void analyze(const CSCWireDigiCollection &wirecltn)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
std::map< int, TH2 * > mh_AfebThrPar
void hf2ForId(std::map< int, TH2 * > &mp, int flag, const int &id, float &x, float &y, float w)
tuple size
Write out results.
void bookForId(int flag, const int &idint, const std::string &ids)