CMS 3D CMS Logo

CastorPedestalsAnalysis.cc
Go to the documentation of this file.
1 // Original Author: Steven Won
2 // Original Author: Alan Campbell for castor
3 // Created: Fri May 2 15:34:43 CEST 2008
4 // Written to replace the combination of HcalPedestalAnalyzer and HcalPedestalAnalysis
5 // This code runs 1000x faster and produces all outputs from a single run
6 // (ADC, fC in .txt plus an .xml file)
7 //
8 #include <memory>
10 
12  : castorDigiCollectionTag(ps.getParameter<edm::InputTag>("castorDigiCollectionTag")) {
13  hiSaveFlag = ps.getUntrackedParameter<bool>("hiSaveFlag", false);
14  dumpXML = ps.getUntrackedParameter<bool>("dumpXML", false);
15  verboseflag = ps.getUntrackedParameter<bool>("verbose", false);
16  firstTS = ps.getUntrackedParameter<int>("firstTS", 0);
17  lastTS = ps.getUntrackedParameter<int>("lastTS", 9);
18  firsttime = true;
19 }
20 
22  CastorPedestals* rawPedsItem = new CastorPedestals(true);
23  CastorPedestalWidths* rawWidthsItem = new CastorPedestalWidths(true);
24  CastorPedestals* rawPedsItemfc = new CastorPedestals(false);
25  CastorPedestalWidths* rawWidthsItemfc = new CastorPedestalWidths(false);
26 
27  //Calculate pedestal constants
28  std::cout << "Calculating Pedestal constants...\n";
29  std::vector<NewPedBunch>::iterator bunch_it;
30  for (bunch_it = Bunches.begin(); bunch_it != Bunches.end(); ++bunch_it) {
31  if (bunch_it->usedflag) {
32  if (verboseflag)
33  std::cout << "Analyzing channel sector= " << bunch_it->detid.sector()
34  << " module = " << bunch_it->detid.module() << std::endl;
35  //pedestal constant is the mean
36  bunch_it->cap[0] /= bunch_it->num[0][0];
37  bunch_it->cap[1] /= bunch_it->num[1][1];
38  bunch_it->cap[2] /= bunch_it->num[2][2];
39  bunch_it->cap[3] /= bunch_it->num[3][3];
40  bunch_it->capfc[0] /= bunch_it->num[0][0];
41  bunch_it->capfc[1] /= bunch_it->num[1][1];
42  bunch_it->capfc[2] /= bunch_it->num[2][2];
43  bunch_it->capfc[3] /= bunch_it->num[3][3];
44  //widths are the covariance matrix--assumed symmetric
45  bunch_it->sig[0][0] = (bunch_it->prod[0][0] / bunch_it->num[0][0]) - (bunch_it->cap[0] * bunch_it->cap[0]);
46  bunch_it->sig[0][1] = (bunch_it->prod[0][1] / bunch_it->num[0][1]) - (bunch_it->cap[0] * bunch_it->cap[1]);
47  bunch_it->sig[0][2] = (bunch_it->prod[0][2] / bunch_it->num[0][2]) - (bunch_it->cap[0] * bunch_it->cap[2]);
48  bunch_it->sig[0][3] = (bunch_it->prod[0][3] / bunch_it->num[0][3]) - (bunch_it->cap[0] * bunch_it->cap[3]);
49  bunch_it->sig[1][0] = (bunch_it->prod[1][0] / bunch_it->num[1][0]) - (bunch_it->cap[1] * bunch_it->cap[0]);
50  bunch_it->sig[1][1] = (bunch_it->prod[1][1] / bunch_it->num[1][1]) - (bunch_it->cap[1] * bunch_it->cap[1]);
51  bunch_it->sig[1][2] = (bunch_it->prod[1][2] / bunch_it->num[1][2]) - (bunch_it->cap[1] * bunch_it->cap[2]);
52  bunch_it->sig[1][3] = (bunch_it->prod[1][3] / bunch_it->num[1][3]) - (bunch_it->cap[1] * bunch_it->cap[3]);
53  bunch_it->sig[2][0] = (bunch_it->prod[2][0] / bunch_it->num[2][0]) - (bunch_it->cap[2] * bunch_it->cap[0]);
54  bunch_it->sig[2][1] = (bunch_it->prod[2][1] / bunch_it->num[2][1]) - (bunch_it->cap[2] * bunch_it->cap[1]);
55  bunch_it->sig[2][2] = (bunch_it->prod[2][2] / bunch_it->num[2][2]) - (bunch_it->cap[2] * bunch_it->cap[2]);
56  bunch_it->sig[2][3] = (bunch_it->prod[2][3] / bunch_it->num[2][3]) - (bunch_it->cap[2] * bunch_it->cap[3]);
57  bunch_it->sig[3][0] = (bunch_it->prod[3][0] / bunch_it->num[3][0]) - (bunch_it->cap[3] * bunch_it->cap[0]);
58  bunch_it->sig[3][1] = (bunch_it->prod[3][1] / bunch_it->num[3][1]) - (bunch_it->cap[3] * bunch_it->cap[1]);
59  bunch_it->sig[3][2] = (bunch_it->prod[3][2] / bunch_it->num[3][2]) - (bunch_it->cap[3] * bunch_it->cap[2]);
60  bunch_it->sig[3][3] = (bunch_it->prod[3][3] / bunch_it->num[3][3]) - (bunch_it->cap[3] * bunch_it->cap[3]);
61 
62  bunch_it->sigfc[0][0] =
63  (bunch_it->prodfc[0][0] / bunch_it->num[0][0]) - (bunch_it->capfc[0] * bunch_it->capfc[0]);
64  bunch_it->sigfc[0][1] =
65  (bunch_it->prodfc[0][1] / bunch_it->num[0][1]) - (bunch_it->capfc[0] * bunch_it->capfc[1]);
66  bunch_it->sigfc[0][2] =
67  (bunch_it->prodfc[0][2] / bunch_it->num[0][2]) - (bunch_it->capfc[0] * bunch_it->capfc[2]);
68  bunch_it->sigfc[0][3] =
69  (bunch_it->prodfc[0][3] / bunch_it->num[0][3]) - (bunch_it->capfc[0] * bunch_it->capfc[3]);
70  bunch_it->sigfc[1][0] =
71  (bunch_it->prodfc[1][0] / bunch_it->num[1][0]) - (bunch_it->capfc[1] * bunch_it->capfc[0]);
72  bunch_it->sigfc[1][1] =
73  (bunch_it->prodfc[1][1] / bunch_it->num[1][1]) - (bunch_it->capfc[1] * bunch_it->capfc[1]);
74  bunch_it->sigfc[1][2] =
75  (bunch_it->prodfc[1][2] / bunch_it->num[1][2]) - (bunch_it->capfc[1] * bunch_it->capfc[2]);
76  bunch_it->sigfc[1][3] =
77  (bunch_it->prodfc[1][3] / bunch_it->num[1][3]) - (bunch_it->capfc[1] * bunch_it->capfc[3]);
78  bunch_it->sigfc[2][0] =
79  (bunch_it->prodfc[2][0] / bunch_it->num[2][0]) - (bunch_it->capfc[2] * bunch_it->capfc[0]);
80  bunch_it->sigfc[2][1] =
81  (bunch_it->prodfc[2][1] / bunch_it->num[2][1]) - (bunch_it->capfc[2] * bunch_it->capfc[1]);
82  bunch_it->sigfc[2][2] =
83  (bunch_it->prodfc[2][2] / bunch_it->num[2][2]) - (bunch_it->capfc[2] * bunch_it->capfc[2]);
84  bunch_it->sigfc[2][3] =
85  (bunch_it->prodfc[2][3] / bunch_it->num[2][3]) - (bunch_it->capfc[2] * bunch_it->capfc[3]);
86  bunch_it->sigfc[3][0] =
87  (bunch_it->prodfc[3][0] / bunch_it->num[3][0]) - (bunch_it->capfc[3] * bunch_it->capfc[0]);
88  bunch_it->sigfc[3][1] =
89  (bunch_it->prodfc[3][1] / bunch_it->num[3][1]) - (bunch_it->capfc[3] * bunch_it->capfc[1]);
90  bunch_it->sigfc[3][2] =
91  (bunch_it->prodfc[3][2] / bunch_it->num[3][2]) - (bunch_it->capfc[3] * bunch_it->capfc[2]);
92  bunch_it->sigfc[3][3] =
93  (bunch_it->prodfc[3][3] / bunch_it->num[3][3]) - (bunch_it->capfc[3] * bunch_it->capfc[3]);
94 
95  for (int i = 0; i != 3; i++) {
96  CASTORMeans->Fill(bunch_it->cap[i]);
97  CASTORWidths->Fill(bunch_it->sig[i][i]);
98  }
99 
100  //if(bunch_it->detid.subdet() == 1){
101 
102  int fillphi = bunch_it->detid.sector();
103  //if (bunch_it->detid.depth()==4) fillphi++;
104 
105  // dephist[bunch_it->detid.module()-1]->Fill(bunch_it->detid.ieta(),fillphi,
106  // (bunch_it->cap[0]+bunch_it->cap[1]+bunch_it->cap[2]+bunch_it->cap[3])/4);
107  dephist->Fill(bunch_it->detid.module(),
108  fillphi,
109  (bunch_it->cap[0] + bunch_it->cap[1] + bunch_it->cap[2] + bunch_it->cap[3]) / 4);
110 
111  const CastorPedestal item(bunch_it->detid,
112  bunch_it->cap[0],
113  bunch_it->cap[1],
114  bunch_it->cap[2],
115  bunch_it->cap[3],
116  bunch_it->sig[0][0],
117  bunch_it->sig[1][1],
118  bunch_it->sig[2][2],
119  bunch_it->sig[3][3]);
120  rawPedsItem->addValues(item);
121  CastorPedestalWidth widthsp(bunch_it->detid);
122  widthsp.setSigma(0, 0, bunch_it->sig[0][0]);
123  widthsp.setSigma(0, 1, bunch_it->sig[0][1]);
124  widthsp.setSigma(0, 2, bunch_it->sig[0][2]);
125  widthsp.setSigma(0, 3, bunch_it->sig[0][3]);
126  widthsp.setSigma(1, 0, bunch_it->sig[1][0]);
127  widthsp.setSigma(1, 1, bunch_it->sig[1][1]);
128  widthsp.setSigma(1, 2, bunch_it->sig[1][2]);
129  widthsp.setSigma(1, 3, bunch_it->sig[1][3]);
130  widthsp.setSigma(2, 0, bunch_it->sig[2][0]);
131  widthsp.setSigma(2, 1, bunch_it->sig[2][1]);
132  widthsp.setSigma(2, 2, bunch_it->sig[2][2]);
133  widthsp.setSigma(2, 3, bunch_it->sig[2][3]);
134  widthsp.setSigma(3, 0, bunch_it->sig[3][0]);
135  widthsp.setSigma(3, 1, bunch_it->sig[3][1]);
136  widthsp.setSigma(3, 2, bunch_it->sig[3][2]);
137  widthsp.setSigma(3, 3, bunch_it->sig[3][3]);
138  rawWidthsItem->addValues(widthsp);
139 
140  const CastorPedestal itemfc(bunch_it->detid,
141  bunch_it->capfc[0],
142  bunch_it->capfc[1],
143  bunch_it->capfc[2],
144  bunch_it->capfc[3],
145  bunch_it->sigfc[0][0],
146  bunch_it->sigfc[1][1],
147  bunch_it->sigfc[2][2],
148  bunch_it->sigfc[3][3]);
149  rawPedsItemfc->addValues(itemfc);
150  CastorPedestalWidth widthspfc(bunch_it->detid);
151  widthspfc.setSigma(0, 0, bunch_it->sigfc[0][0]);
152  widthspfc.setSigma(0, 1, bunch_it->sigfc[0][1]);
153  widthspfc.setSigma(0, 2, bunch_it->sigfc[0][2]);
154  widthspfc.setSigma(0, 3, bunch_it->sigfc[0][3]);
155  widthspfc.setSigma(1, 0, bunch_it->sigfc[1][0]);
156  widthspfc.setSigma(1, 1, bunch_it->sigfc[1][1]);
157  widthspfc.setSigma(1, 2, bunch_it->sigfc[1][2]);
158  widthspfc.setSigma(1, 3, bunch_it->sigfc[1][3]);
159  widthspfc.setSigma(2, 0, bunch_it->sigfc[2][0]);
160  widthspfc.setSigma(2, 1, bunch_it->sigfc[2][1]);
161  widthspfc.setSigma(2, 2, bunch_it->sigfc[2][2]);
162  widthspfc.setSigma(2, 3, bunch_it->sigfc[2][3]);
163  widthspfc.setSigma(3, 0, bunch_it->sigfc[3][0]);
164  widthspfc.setSigma(3, 1, bunch_it->sigfc[3][1]);
165  widthspfc.setSigma(3, 2, bunch_it->sigfc[3][2]);
166  widthspfc.setSigma(3, 3, bunch_it->sigfc[3][3]);
167  rawWidthsItemfc->addValues(widthspfc);
168  }
169  }
170 
171  // dump the resulting list of pedestals into a file
172  std::ofstream outStream1(pedsADCfilename.c_str());
173  CastorDbASCIIIO::dumpObject(outStream1, (*rawPedsItem));
174  std::ofstream outStream2(widthsADCfilename.c_str());
175  CastorDbASCIIIO::dumpObject(outStream2, (*rawWidthsItem));
176 
177  std::ofstream outStream3(pedsfCfilename.c_str());
178  CastorDbASCIIIO::dumpObject(outStream3, (*rawPedsItemfc));
179  std::ofstream outStream4(widthsfCfilename.c_str());
180  CastorDbASCIIIO::dumpObject(outStream4, (*rawWidthsItemfc));
181 
182  if (dumpXML) {
183  std::ofstream outStream5(XMLfilename.c_str());
184  // CastorCondXML::dumpObject (outStream5, runnum, runnum, runnum, XMLtag, 1, (*rawPedsItem), (*rawWidthsItem));
185  }
186 
187  if (hiSaveFlag) {
188  theFile->Write();
189  } else {
190  theFile->cd();
191  theFile->cd("CASTOR");
192  CASTORMeans->Write();
193  CASTORWidths->Write();
194  }
195  theFile->cd();
196  dephist->Write();
197  dephist->SetDrawOption("colz");
198  dephist->GetXaxis()->SetTitle("module");
199  dephist->GetYaxis()->SetTitle("sector");
200 
201  //for (int n=0; n!= 4; n++)
202  //{
203  //dephist[n]->Write();
204  //dephist[n]->SetDrawOption("colz");
205  //dephist[n]->GetXaxis()->SetTitle("i#eta");
206  //dephist[n]->GetYaxis()->SetTitle("i#phi");
207  //}
208 
209  std::stringstream tempstringout;
210  tempstringout << runnum;
211  std::string name1 = tempstringout.str() + "_pedplots_1d.png";
212  std::string name2 = tempstringout.str() + "_pedplots_2d.png";
213 
214  TStyle* theStyle = new TStyle("style", "null");
215  theStyle->SetPalette(1, nullptr);
216  theStyle->SetCanvasDefH(1200); //Height of canvas
217  theStyle->SetCanvasDefW(1600); //Width of canvas
218 
219  gStyle = theStyle;
220  /*
221  TCanvas * c1 = new TCanvas("c1","graph",1);
222  c1->Divide(2,2);
223  c1->cd(1);
224  CASTORMeans->Draw();
225  c1->SaveAs(name1.c_str());
226 
227  theStyle->SetOptStat("n");
228  gStyle = theStyle;
229 
230  TCanvas * c2 = new TCanvas("c2","graph",1);
231  // c2->Divide(2,2);
232  c2->cd(1);
233  dephist->Draw();
234  dephist->SetDrawOption("colz");
235  //c2->cd(2);
236  //dephist[1]->Draw();
237  //dephist[1]->SetDrawOption("colz");
238  //c2->cd(3);
239  //dephist[2]->Draw();
240  //dephist[2]->SetDrawOption("colz");
241  //c2->cd(4);
242  //dephist[3]->Draw();
243  //dephist[3]->SetDrawOption("colz");
244  c2->SaveAs(name2.c_str());
245 */
246  std::cout << "Writing ROOT file... ";
247  theFile->Close();
248  std::cout << "ROOT file closed.\n";
249 }
250 
251 // ------------ method called to for each event ------------
253  using namespace edm;
254  using namespace std;
255 
258 
260  iSetup.get<CastorDbRecord>().get(conditions);
261 
262  const CastorQIEShape* shape = conditions->getCastorShape();
263 
264  if (firsttime) {
265  runnum = e.id().run();
266  std::string runnum_string;
267  std::stringstream tempstringout;
268  tempstringout << runnum;
269  runnum_string = tempstringout.str();
270  ROOTfilename = runnum_string + "-peds_ADC.root";
271  pedsADCfilename = runnum_string + "-peds_ADC.txt";
272  pedsfCfilename = runnum_string + "-peds_fC.txt";
273  widthsADCfilename = runnum_string + "-widths_ADC.txt";
274  widthsfCfilename = runnum_string + "-widths_fC.txt";
275  XMLfilename = runnum_string + "-peds_ADC_complete.xml";
276  XMLtag = "Castor_pedestals_" + runnum_string;
277 
278  theFile = new TFile(ROOTfilename.c_str(), "RECREATE");
279  theFile->cd();
280  // Create sub-directories
281  theFile->mkdir("CASTOR");
282  theFile->cd();
283 
284  CASTORMeans = new TH1F("All Ped Means CASTOR", "All Ped Means CASTOR", 100, 0, 9);
285  CASTORWidths = new TH1F("All Ped Widths CASTOR", "All Ped Widths CASTOR", 100, 0, 3);
286 
287  dephist = new TH2F("Pedestals (ADC)", "All Castor", 14, 0., 14.5, 16, .5, 16.5);
288  // dephist[0] = new TH2F("Pedestals (ADC)","Depth 1",89, -44, 44, 72, .5, 72.5);
289  // dephist[1] = new TH2F("Pedestals (ADC)","Depth 2",89, -44, 44, 72, .5, 72.5);
290  // dephist[2] = new TH2F("Pedestals (ADC)","Depth 3",89, -44, 44, 72, .5, 72.5);
291  // dephist[3] = new TH2F("Pedestals (ADC)","Depth 4",89, -44, 44, 72, .5, 72.5);
292 
294  iSetup.get<CastorElectronicsMapRcd>().get(refEMap);
295  const CastorElectronicsMap* myRefEMap = refEMap.product();
296  std::vector<HcalGenericDetId> listEMap = myRefEMap->allPrecisionId();
297  for (std::vector<HcalGenericDetId>::const_iterator it = listEMap.begin(); it != listEMap.end(); ++it) {
298  HcalGenericDetId mygenid(it->rawId());
299  if (mygenid.isHcalCastorDetId()) {
300  NewPedBunch a;
301  HcalCastorDetId chanid(mygenid.rawId());
302  a.detid = chanid;
303  a.usedflag = false;
304  string type;
305  type = "CASTOR";
306  for (int i = 0; i != 4; i++) {
307  a.cap[i] = 0;
308  a.capfc[i] = 0;
309  for (int j = 0; j != 4; j++) {
310  a.sig[i][j] = 0;
311  a.sigfc[i][j] = 0;
312  a.prod[i][j] = 0;
313  a.prodfc[i][j] = 0;
314  a.num[i][j] = 0;
315  }
316  }
317  Bunches.push_back(a);
318  }
319  }
320  firsttime = false;
321  }
322 
323  std::vector<NewPedBunch>::iterator bunch_it;
324 
325  for (CastorDigiCollection::const_iterator j = castor->begin(); j != castor->end(); ++j) {
326  const CastorDataFrame digi = (const CastorDataFrame)(*j);
327  for (bunch_it = Bunches.begin(); bunch_it != Bunches.end(); ++bunch_it)
328  if (bunch_it->detid.rawId() == digi.id().rawId())
329  break;
330  bunch_it->usedflag = true;
331  for (int ts = firstTS; ts != lastTS + 1; ts++) {
332  const CastorQIECoder* coder = conditions->getCastorCoder(digi.id().rawId());
333  bunch_it->num[digi.sample(ts).capid()][digi.sample(ts).capid()] += 1;
334  bunch_it->cap[digi.sample(ts).capid()] += digi.sample(ts).adc();
335  double charge1 = coder->charge(*shape, digi.sample(ts).adc(), digi.sample(ts).capid());
336  bunch_it->capfc[digi.sample(ts).capid()] += charge1;
337  bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts).capid()] +=
338  (digi.sample(ts).adc() * digi.sample(ts).adc());
339  bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts).capid()] += charge1 * charge1;
340  if ((ts + 1 < digi.size()) && (ts + 1 < lastTS)) {
341  bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts + 1).capid()] +=
342  digi.sample(ts).adc() * digi.sample(ts + 1).adc();
343  double charge2 = coder->charge(*shape, digi.sample(ts + 1).adc(), digi.sample(ts + 1).capid());
344  bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts + 1).capid()] += charge1 * charge2;
345  bunch_it->num[digi.sample(ts).capid()][digi.sample(ts + 1).capid()] += 1;
346  }
347  if ((ts + 2 < digi.size()) && (ts + 2 < lastTS)) {
348  bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts + 2).capid()] +=
349  digi.sample(ts).adc() * digi.sample(ts + 2).adc();
350  double charge2 = coder->charge(*shape, digi.sample(ts + 2).adc(), digi.sample(ts + 2).capid());
351  bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts + 2).capid()] += charge1 * charge2;
352  bunch_it->num[digi.sample(ts).capid()][digi.sample(ts + 2).capid()] += 1;
353  }
354  if ((ts + 3 < digi.size()) && (ts + 3 < lastTS)) {
355  bunch_it->prod[digi.sample(ts).capid()][digi.sample(ts + 3).capid()] +=
356  digi.sample(ts).adc() * digi.sample(ts + 3).adc();
357  double charge2 = coder->charge(*shape, digi.sample(ts + 3).adc(), digi.sample(ts + 3).capid());
358  bunch_it->prodfc[digi.sample(ts).capid()][digi.sample(ts + 3).capid()] += charge1 * charge2;
359  bunch_it->num[digi.sample(ts).capid()][digi.sample(ts + 3).capid()] += 1;
360  }
361  }
362  }
363 
364  //this is the last brace
365 }
366 
367 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
type
Definition: HCALResponse.h:21
T getUntrackedParameter(std::string const &, T const &) const
void setSigma(int fCapId1, int fCapId2, float fSigma)
std::vector< NewPedBunch > Bunches
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
const HcalQIESample & sample(int i) const
access a sample
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
std::vector< T >::const_iterator const_iterator
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const CastorQIEShape * getCastorShape() const
float charge(const CastorQIEShape &fShape, unsigned fAdc, unsigned fCapId) const
ADC [0..127] + capid [0..3] -> fC conversion.
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:480
constexpr int adc() const
get the ADC sample
Definition: HcalQIESample.h:59
HcalCastorDetId detid
const_iterator end() const
bool dumpObject(std::ostream &fOutput, const CastorPedestals &fObject)
std::vector< HcalGenericDetId > allPrecisionId() const
CastorPedestalsAnalysis(const edm::ParameterSet &ps)
bool addValues(const Item &myItem)
edm::EventID id() const
Definition: EventBase.h:59
constexpr int capid() const
get the Capacitor id
Definition: HcalQIESample.h:65
HLT enums.
double a
Definition: hdecay.h:121
T get() const
Definition: EventSetup.h:71
const HcalCastorDetId & id() const
int size() const
total number of samples in the digi
T const * product() const
Definition: ESHandle.h:86
const_iterator begin() const
const CastorQIECoder * getCastorCoder(const HcalGenericDetId &fId) const