HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
DigiPlotter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HGCal/DigiPlotter
4 // Class: DigiPlotter
5 //
6 /**\class DigiPlotter DigiPlotter.cc HGCal/DigiPlotter/plugins/DigiPlotter.cc
7 
8  Description: [one line class summary]
9 
10  Implementation:
11  [Notes on implementation]
12 */
13 //
14 // Original Author: Rajdeep Mohan Chatterjee
15 // Created: Mon, 15 Feb 2016 09:47:43 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 #include <iostream>
23 #include "TH2Poly.h"
24 #include "TH1F.h"
25 #include "TProfile.h"
26 #include <fstream>
27 // user include files
28 #include "FWCore/Framework/interface/Frameworkfwd.h"
29 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
30 #include "FWCore/Framework/interface/Event.h"
31 #include "FWCore/Framework/interface/MakerMacros.h"
32 #include "FWCore/ParameterSet/interface/ParameterSet.h"
33 #include "FWCore/ServiceRegistry/interface/Service.h"
34 #include "HGCal/DataFormats/interface/HGCalTBRecHitCollections.h"
35 #include "HGCal/DataFormats/interface/HGCalTBDetId.h"
36 #include "HGCal/DataFormats/interface/HGCalTBRecHit.h"
37 #include "HGCal/Geometry/interface/HGCalTBCellVertices.h"
38 #include "HGCal/Geometry/interface/HGCalTBTopology.h"
39 #include "CommonTools/UtilAlgos/interface/TFileService.h"
40 #include "HGCal/CondObjects/interface/HGCalElectronicsMap.h"
41 #include "HGCal/CondObjects/interface/HGCalCondObjectTextIO.h"
42 #include "HGCal/DataFormats/interface/HGCalTBElectronicsId.h"
43 #include "HGCal/DataFormats/interface/HGCalTBDataFrameContainers.h"
44 #include "HGCal/Geometry/interface/HGCalTBGeometryParameters.h"
45 
46  using namespace std;
47 
48 
49 //
50 // class declaration
51 //
52 
53 // If the analyzer does not use TFileService, please remove
54 // the template argument to the base class so the class inherits
55 // from edm::one::EDAnalyzer<> and also remove the line from
56 // constructor "usesResource("TFileService");"
57 // This will improve performance in multithreaded jobs.
58 
59 class DigiPlotter : public edm::one::EDAnalyzer<edm::one::SharedResources>
60 {
61 
62 public:
63  explicit DigiPlotter(const edm::ParameterSet&);
64  ~DigiPlotter();
65  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
66 private:
67  virtual void beginJob() override;
68  void analyze(const edm::Event& , const edm::EventSetup&) override;
69  virtual void endJob() override;
70  // ----------member data ---------------------------
71  bool DEBUG = 0;
72  HGCalTBTopology IsCellValid;
73  HGCalTBCellVertices TheCell;
74  std::string mapfile_ = "HGCal/CondObjects/data/map_FNAL_SB2_Layer16.txt";
75  struct {
76  HGCalElectronicsMap emap_;
77  } essource_;
78  int sensorsize = 128;// The geometry for a 256 cell sensor hasnt been implemted yet. Need a picture to do this.
79  std::vector<std::pair<double, double>> CellXY;
80  std::pair<double, double> CellCentreXY;
81  std::vector<std::pair<double, double>>::const_iterator it;
82  const static int NSAMPLES = 2;
83  TH2Poly *h_digi_layer[NSAMPLES][MAXLAYERS];
84  TH1F *h_digi_layer_summed[NSAMPLES][MAXLAYERS];
85  TProfile *h_digi_layer_profile[NSAMPLES][MAXLAYERS];
86  const static int cellx = 15;
87  const static int celly = 15;
88  int Sensor_Iu = 0;
89  int Sensor_Iv = 0;
90  TH2F* Noise_2D_Profile[NSAMPLES][MAXLAYERS];
91  TH1F *h_digi_layer_channel[MAXSKIROCS][64][NSAMPLES];
92 // TH1F *h_digi_layer_cell_event[NSAMPLES][MAXLAYERS][cellx][celly][512];
93  char name[50], title[50];
94  double ADC_Sum_SKI_Layer[2][MAXLAYERS][2]; // 2 SKIROCs per layer, High gain and low gain ADC HARD CODED
95  int Cell_Count_SKI_Layer[2][4]; // 2 SKIROCs per layer, High gain and low gain ADC HARD CODED
96 
97 };
98 
99 //
100 // constants, enums and typedefs
101 //
102 
103 //
104 // static data member definitions
105 //
106 
107 //
108 // constructors and destructor
109 //
110 DigiPlotter::DigiPlotter(const edm::ParameterSet& iConfig)
111 {
112  //now do what ever initialization is needed
113  usesResource("TFileService");
114  edm::Service<TFileService> fs;
115  consumesMany<SKIROC2DigiCollection>();
116  const int HalfHexVertices = 4;
117  double HalfHexX[HalfHexVertices] = {0.};
118  double HalfHexY[HalfHexVertices] = {0.};
119  const int FullHexVertices = 6;
120  double FullHexX[FullHexVertices] = {0.};
121  double FullHexY[FullHexVertices] = {0.};
122  for(int nsample = 0; nsample < NSAMPLES; nsample++) {
123  for(int nlayers = 0; nlayers < MAXLAYERS; nlayers++) {
124  sprintf(name, "Noise_2D_Profile_ADC%i_Layer%i", nsample, nlayers);
125  sprintf(title, "Noise 2D Profile ADC%i Layer%i", nsample, nlayers);
126  Noise_2D_Profile[nsample][nlayers] = fs->make<TH2F>(name,title,128,0,127,500,-250,250);
127  }
128  }
129  for(int ISkiroc = 1; ISkiroc <= MAXSKIROCS; ISkiroc++) {
130  for(int Channel = 0; Channel < 64; Channel++) {
131  for(int iii = 0; iii < NSAMPLES; iii++) {
132  sprintf(name, "Ski_%i_Channel_%i_ADC%i", ISkiroc, Channel, iii);
133  sprintf(title, "Ski %i Channel %i ADC%i", ISkiroc, Channel, iii);
134  h_digi_layer_channel[ISkiroc - 1][Channel][iii] = fs->make<TH1F>(name, title, 4096, 0., 4095.);
135  }
136  }
137  }
138  int iii = 0;
139  for(int nsample = 0; nsample < NSAMPLES; nsample++) {
140  for(int nlayers = 0; nlayers < MAXLAYERS; nlayers++) {
141 //Booking a "hexagonal" histograms to display the sum of Digis for NSAMPLES, in 1 SKIROC in 1 layer. To include all layers soon. Also the 1D Digis per cell in a sensor is booked here for NSAMPLES.
142  sprintf(name, "FullLayer_ADC%i_Layer%i", nsample, nlayers + 1);
143  sprintf(title, "Sum of adc counts per cell for ADC%i Layer%i", nsample, nlayers + 1);
144  h_digi_layer[nsample][nlayers] = fs->make<TH2Poly>();
145  h_digi_layer[nsample][nlayers]->SetName(name);
146  h_digi_layer[nsample][nlayers]->SetTitle(title);
147  sprintf(name, "FullLayer_ADC%i_Layer%i_summed", nsample, nlayers + 1);
148  sprintf(title, "Sum of adc counts for all cells in ADC%i Layer%i", nsample, nlayers + 1);
149  h_digi_layer_summed[nsample][nlayers] = fs->make<TH1F>(name, title, 4096, 0., 4095.);
150  h_digi_layer_summed[nsample][nlayers]->GetXaxis()->SetTitle("Digis[adc counts]");
151  sprintf(name, "FullLayer_ADC%i_Layer%i_profile", nsample, nlayers + 1);
152  sprintf(title, "profile of adc counts for all cells in ADC%i Layer%i", nsample, nlayers + 1);
153  h_digi_layer_profile[nsample][nlayers] = fs->make<TProfile>(name, title, 128, 0, 127, 0., 4095.);
154  h_digi_layer_profile[nsample][nlayers]->GetXaxis()->SetTitle("Channel #");
155  h_digi_layer_profile[nsample][nlayers]->GetYaxis()->SetTitle("ADC counts");
156 
157 
158  for(int iv = -7; iv < 8; iv++) {
159  for(int iu = -7; iu < 8; iu++) {
160  if(!IsCellValid.iu_iv_valid(nlayers, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize)) continue;
161  CellXY = TheCell.GetCellCoordinatesForPlots(nlayers, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize);
162  int NumberOfCellVertices = CellXY.size();
163  iii = 0;
164  if(NumberOfCellVertices == 4) {
165  for(it = CellXY.begin(); it != CellXY.end(); it++) {
166  HalfHexX[iii] = it->first;
167  HalfHexY[iii++] = it->second;
168  }
169 //Somehow cloning of the TH2Poly was not working. Need to look at it. Currently physically booked another one.
170  h_digi_layer[nsample][nlayers]->AddBin(NumberOfCellVertices, HalfHexX, HalfHexY);
171  } else if(NumberOfCellVertices == 6) {
172  iii = 0;
173  for(it = CellXY.begin(); it != CellXY.end(); it++) {
174  FullHexX[iii] = it->first;
175  FullHexY[iii++] = it->second;
176  }
177  h_digi_layer[nsample][nlayers]->AddBin(NumberOfCellVertices, FullHexX, FullHexY);
178  }
179 
180  }//loop over iu
181  }//loop over iv
182  }//loop over nlayers
183  }//loop over nsamples
184 }//contructor ends here
185 
186 
187 DigiPlotter::~DigiPlotter()
188 {
189 
190  // do anything here that needs to be done at desctruction time
191  // (e.g. close files, deallocate resources etc.)
192 
193 }
194 
195 
196 //
197 // member functions
198 //
199 
200 // ------------ method called for each event ------------
201 void
202 DigiPlotter::analyze(const edm::Event& event, const edm::EventSetup& setup)
203 {
204  using namespace edm;
205  std::vector<edm::Handle<SKIROC2DigiCollection> > ski;
206  event.getManyByType(ski);
207 // int Event = event.id().event();
208 /*
209  for(int ski=0;ski<2;ski++){
210  for(int layers =0; layers<MAXLAYERS; layers++){
211  Cell_Count_SKI_Layer[ski][layers] = 0;
212  for(int samples =0; samples<2; samples++){
213  ADC_Sum_SKI_Layer[ski][layers][samples] = 0.; // 2 SKIROCs per layer, High gain and low gain ADC HARD CODED
214  }
215  }
216  }
217 */
218  if(!ski.empty()) {
219 
220  std::vector<edm::Handle<SKIROC2DigiCollection> >::iterator i;
221  int counter1 = 0, counter2 = 0;
222  for(i = ski.begin(); i != ski.end(); i++) {
223  const SKIROC2DigiCollection& Coll = *(*i);
224 
225 //////////////////////////////////Evaluate average pedestal per event to subtract out//////////////////////////////////
226  for(SKIROC2DigiCollection::const_iterator k = Coll.begin(); k != Coll.end(); k++) {
227  const SKIROC2DataFrame& SKI_1 = *k ;
228  int n_layer = (SKI_1.detid()).layer();
229  int n_sensor_IU = (SKI_1.detid()).sensorIU();
230  int n_sensor_IV = (SKI_1.detid()).sensorIV();
231  int n_cell_iu = (SKI_1.detid()).iu();
232  int n_cell_iv = (SKI_1.detid()).iv();
233  uint32_t EID = essource_.emap_.detId2eid(SKI_1.detid());
234  HGCalTBElectronicsId eid(EID);
235  if(DEBUG) cout << endl << " Layer = " << n_layer << " Sensor IU = " << n_sensor_IU << " Sensor IV = " << n_sensor_IV << " Cell iu = " << n_cell_iu << " Cell iu = " << n_cell_iv << endl;
236  if(!IsCellValid.iu_iv_valid(n_layer, n_sensor_IU, n_sensor_IV, n_cell_iu, n_cell_iv, sensorsize)) continue;
237 // ADC_Sum_SKI_Layer[eid.iskiroc() - 2*(n_layer - 1) - 1][n_layer - 1][1] += SKI_1[0].adcHigh();
238 // ADC_Sum_SKI_Layer[eid.iskiroc() - 2*(n_layer - 1) - 1][n_layer - 1][0] += SKI_1[0].adcLow();
239 // Cell_Count_SKI_Layer[eid.iskiroc() - 2*(n_layer - 1) - 1][n_layer - 1] += 1;
240  }
241 
242 
243 
244 // cout << "SKIROC2 Digis: " << i->provenance()->branchName() << endl;
245  for(SKIROC2DigiCollection::const_iterator j = Coll.begin(); j != Coll.end(); j++) {
246  const SKIROC2DataFrame& SKI = *j ;
247  int n_layer = (SKI.detid()).layer();
248  int n_sensor_IU = (SKI.detid()).sensorIU();
249  int n_sensor_IV = (SKI.detid()).sensorIV();
250  int n_cell_iu = (SKI.detid()).iu();
251  int n_cell_iv = (SKI.detid()).iv();
252  uint32_t EID = essource_.emap_.detId2eid(SKI.detid());
253  HGCalTBElectronicsId eid(EID);
254  if(DEBUG) cout << endl << " Layer = " << n_layer << " Sensor IU = " << n_sensor_IU << " Sensor IV = " << n_sensor_IV << " Cell iu = " << n_cell_iu << " Cell iu = " << n_cell_iv << endl;
255  if(!IsCellValid.iu_iv_valid(n_layer, n_sensor_IU, n_sensor_IV, n_cell_iu, n_cell_iv, sensorsize)) continue;
256  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots(n_layer, n_sensor_IU, n_sensor_IV, n_cell_iu, n_cell_iv, sensorsize);
257  double iux = (CellCentreXY.first < 0 ) ? (CellCentreXY.first + 0.0001) : (CellCentreXY.first - 0.0001) ;
258  double iyy = (CellCentreXY.second < 0 ) ? (CellCentreXY.second + 0.0001) : (CellCentreXY.second - 0.0001);
259  int nsample = 0;
260  h_digi_layer[nsample][n_layer - 1]->Fill(iux , iyy, SKI[nsample].adcLow());
261  h_digi_layer_profile[nsample][n_layer - 1]->Fill(counter1++, SKI[nsample].adcLow(), 1);
262 // h_digi_layer_summed[nsample][n_layer - 1]->Fill(ADC_Sum_SKI_Layer[eid.iskiroc() - 2*(n_layer - 1) - 1][n_layer - 1][0]);
263  if(eid.iskiroc() > 0) h_digi_layer_channel[eid.iskiroc() - 1][eid.ichan()][nsample]->Fill(SKI[nsample].adcLow());
264  nsample = 1;
265  h_digi_layer[nsample][n_layer - 1]->Fill(iux , iyy, SKI[nsample - 1].adcHigh());
266  h_digi_layer_profile[nsample][n_layer - 1]->Fill(counter2++, SKI[nsample - 1].adcHigh(), 1);
267 // h_digi_layer_summed[nsample][n_layer - 1]->Fill(ADC_Sum_SKI_Layer[eid.iskiroc() - 2*(n_layer - 1) - 1][n_layer - 1][1]);
268 // if(((SKI.detid()).cellType() != 4) && (eid.ichan() == 0) ) cout<<endl<<"SKIROC= "<<eid.iskiroc()<<" Chan= "<<eid.ichan()<<" u= "<<n_cell_iu<<" v = "<<n_cell_iv<<endl;
269  if(eid.iskiroc() > 0) h_digi_layer_channel[eid.iskiroc() - 1][eid.ichan()][nsample]->Fill(SKI[nsample - 1].adcHigh());
270  }
271 
272  }
273  } else {
274  edm::LogWarning("DQM") << "No SKIROC2 Digis";
275  }
276 
277 }//analyze method ends here
278 
279 
280 // ------------ method called once each job just before starting event loop ------------
281 void
282 DigiPlotter::beginJob()
283 {
284  HGCalCondObjectTextIO io(0);
285  edm::FileInPath fip(mapfile_);
286  if (!io.load(fip.fullPath(), essource_.emap_)) {
287  throw cms::Exception("Unable to load electronics map");
288  }
289 }
290 
291 // ------------ method called once each job just after ending the event loop ------------
292 void
293 DigiPlotter::endJob()
294 {
295  int Code = 0;
296  int SENSOR_IX = 0;
297  int SENSOR_IV = 0;
298  ofstream fs1, fs2;
299  fs1.open("/afs/cern.ch/work/r/rchatter/FNAL_June_TestBeam/CMSSW_8_0_1/src/HGCal/Ped_HighGain_L16.txt");
300  fs1<<"SCHEME_CODE 0"<<endl;
301  fs1<<"# CODE LAYER SENSOR_IX SENSOR_IV IX IV TYPE VALUE"<<endl;
302  fs2.open("/afs/cern.ch/work/r/rchatter/FNAL_June_TestBeam/CMSSW_8_0_1/src/HGCal/Ped_LowGain_L16.txt");
303  fs2<<"SCHEME_CODE 0"<<endl;
304  fs2<<"# CODE LAYER SENSOR_IX SENSOR_IV IX IV TYPE VALUE"<<endl;
305 
306  for(int ISkiroc = 1; ISkiroc <= MAXSKIROCS; ISkiroc++) {
307  for(int Channel = 0; Channel < 64; Channel++) {
308  HGCalTBElectronicsId ElId(ISkiroc,Channel);
309  HGCalTBDetId DetId = essource_.emap_.eid2detId(ElId);
310  if(DetId.layer() != 0){
311  fs1<<" "<<Code<<" "<<DetId.layer()<<" "<<SENSOR_IX<<" "<<SENSOR_IV<<" "<<DetId.iu()<<" "<<DetId.iv()<<" "<<" "<<DetId.cellType()<<" "<<h_digi_layer_channel[ISkiroc - 1][Channel][1]->GetMean()<<endl;
312  fs2<<" "<<Code<<" "<<DetId.layer()<<" "<<SENSOR_IX<<" "<<SENSOR_IV<<" "<<DetId.iu()<<" "<<DetId.iv()<<" "<<" "<<DetId.cellType()<<" "<<h_digi_layer_channel[ISkiroc - 1][Channel][0]->GetMean()<<endl;
313  }
314  }
315  }
316 }
317 
318 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
319 void
320 DigiPlotter::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
321 {
322  //The following says we do not know what parameters are allowed so do no validation
323  // Please change this to state exactly what you do use, even if it is no parameters
324  edm::ParameterSetDescription desc;
325  desc.setUnknown();
326  descriptions.addDefault(desc);
327 }
328 
329 //define this as a plug-in
330 DEFINE_FWK_MODULE(DigiPlotter);
int layer() const
get the layer #
Definition: HGCalTBDetId.h:81
DEFINE_FWK_MODULE(Pedestals)
HGCalTBDetId detid() const
Get the detector id.
edm::Service< TFileService > fs
#define MAXLAYERS
int cellType() const
cell type
Definition: HGCalTBDetId.h:75
int iv() const
Definition: HGCalTBDetId.h:56
#define MAXSKIROCS
int iu() const
get the absolute value of the cell #&#39;s
Definition: HGCalTBDetId.h:51
provides the conversion between electronics Id to DetId