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