HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
DigiPlotter_New.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HGCal/DigiPlotter_New
4 // Class: DigiPlotter_New
5 //
6 /**\class DigiPlotter_New DigiPlotter_New.cc HGCal/DigiPlotter_New/plugins/DigiPlotter_New.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 // user include files
27 #include "FWCore/Framework/interface/Frameworkfwd.h"
28 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
29 #include "FWCore/Framework/interface/Event.h"
30 #include "FWCore/Framework/interface/MakerMacros.h"
31 #include "FWCore/ParameterSet/interface/ParameterSet.h"
32 #include "FWCore/ServiceRegistry/interface/Service.h"
33 #include "HGCal/DataFormats/interface/HGCalTBRecHitCollections.h"
34 #include "HGCal/DataFormats/interface/HGCalTBDetId.h"
35 #include "HGCal/DataFormats/interface/HGCalTBRecHit.h"
36 #include "HGCal/Geometry/interface/HGCalTBCellVertices.h"
37 #include "HGCal/Geometry/interface/HGCalTBTopology.h"
38 #include "HGCal/Geometry/interface/HGCalTBGeometryParameters.h"
39 #include "CommonTools/UtilAlgos/interface/TFileService.h"
40 #include "HGCal/DataFormats/interface/HGCalTBDataFrameContainers.h"
41 #include "HGCal/CondObjects/interface/HGCalElectronicsMap.h"
42 #include "HGCal/DataFormats/interface/HGCalTBElectronicsId.h"
43 #include "HGCal/DataFormats/interface/SKIROCParameters.h"
44 #define MAXVERTICES 6
45 
46 class Pedestal
47 {
48 public:
49  std::vector<std::pair<double, unsigned int> > _pedestals;
50  Pedestal(){};
51 
52  void Sum(HGCalTBDetId detId, double pedestal)
53  {
54  _pedestals[detId.cellType()].first += pedestal;
55  _pedestals[detId.cellType()].second++;
56  }
57 
58  float GetPedestal(HGCalTBDetId detId)
59  {
60  /// \todo here the logic can be adapted if one want to use MouseBites and HalfCells
61  // for example:
62  // if(detId.cellType()==HGCalTBDetId::kCellHalf || detId.cellType()==HGCalTBDetId::kCellMouseBite)
63  // return _pedestals[HGCalTBDetId::kCellMouseBite].first+_pedestals[HGCalTBDetId::kCellHalf].first/(_pedestals[HGCalTBDetId::kCellMouseBite].second+_pedestals[HGCalTBDetId::kCellHalf].second);
64  return _pedestals[detId.cellType()].first / _pedestals[detId.cellType()].second;
65  }
66 };
67 
68 //
69 // class declaration
70 //
71 
72 // If the analyzer does not use TFileService, please remove
73 // the template argument to the base class so the class inherits
74 // from edm::one::EDAnalyzer<> and also remove the line from
75 // constructor "usesResource("TFileService");"
76 // This will improve performance in multithreaded jobs.
77 
78 
79 //usesResource("TFileService");
80 edm::Service<TFileService> fs;
81 
82 class DigiPlotter_New : public edm::one::EDAnalyzer<edm::one::SharedResources>
83 {
84 
85 public:
86  explicit DigiPlotter_New(const edm::ParameterSet&);
87  ~DigiPlotter_New();
88  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
89 private:
90  virtual void beginJob() override;
91  void analyze(const edm::Event& , const edm::EventSetup&) override;
92  virtual void endJob() override;
93  void InitTH2Poly(TH2Poly& poly);
94  // ----------member data ---------------------------
95  Pedestal _pedestals[2]; // one set of pedestals per skiroc
96  HGCalTBTopology IsCellValid;
97  HGCalTBCellVertices TheCell;
98  int sensorsize = 128;// The geometry for a 256 cell sensor hasnt been implemted yet. Need a picture to do this.
99  std::vector<std::pair<double, double>> CellXY;
100  std::pair<double, double> CellCentreXY;
101  std::vector<std::pair<double, double>>::const_iterator it;
102  const static int NSAMPLES = 2;
103  const static int NLAYERS = 1;
104  int Sensor_Iu = 0;
105  int Sensor_Iv = 0;
106 };
107 
108 //
109 // constants, enums and typedefs
110 //
111 
112 //
113 // static data member definitions
114 //
115 
116 //
117 // constructors and destructor
118 //
119 DigiPlotter_New::DigiPlotter_New(const edm::ParameterSet& iConfig)
120 {
121  //now do what ever initialization is needed
122  consumesMany<SKIROC2DigiCollection>();
123 }//contructor ends here
124 
125 
126 DigiPlotter_New::~DigiPlotter_New()
127 {
128 
129  // do anything here that needs to be done at desctruction time
130  // (e.g. close files, deallocate resources etc.)
131 
132 }
133 
134 
135 void DigiPlotter_New::InitTH2Poly(TH2Poly& poly)
136 {
137  double HexX[MAXVERTICES] = {0.};
138  double HexY[MAXVERTICES] = {0.};
139 
140  for(int iv = -7; iv < 8; iv++) {
141  for(int iu = -7; iu < 8; iu++) {
142  if(!IsCellValid.iu_iv_valid(NLAYERS, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize)) continue;
143  CellXY = TheCell.GetCellCoordinatesForPlots(NLAYERS, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize);
144  assert(CellXY.size() == 4 || CellXY.size() == 6);
145  unsigned int iVertex = 0;
146  for(it = CellXY.begin(); it != CellXY.end(); it++) {
147  HexX[iVertex] = it->first;
148  HexY[iVertex] = it->second;
149  ++iVertex;
150  }
151 //Somehow cloning of the TH2Poly was not working. Need to look at it. Currently physically booked another one.
152  poly.AddBin(CellXY.size(), HexX, HexY);
153  }//loop over iu
154  }//loop over iv
155 }
156 
157 //
158 // member functions
159 //
160 
161 // ------------ method called for each event ------------
162 void
163 DigiPlotter_New::analyze(const edm::Event& event, const edm::EventSetup& setup)
164 {
165  int evId = event.id().event() - 1;
166 
167  using namespace edm;
168  using namespace std;
169  char name[300], title[300];
170  std::vector<edm::Handle<SKIROC2DigiCollection> > ski;
171  event.getManyByType(ski);
172 // int Event = event.id().event();
173 
174  if(!ski.empty()) {
175 
176  std::vector<edm::Handle<SKIROC2DigiCollection> >::iterator i;
177  double Average_Pedestal_SKI1_Event1 = 0, Average_Pedestal_SKI1_Event2 = 0, Average_Pedestal_SKI2_Event1 = 0, Average_Pedestal_SKI2_Event2 = 0;
178  int Cell_SKI1_counter1 = 0, Cell_SKI1_counter2 = 0, Cell_SKI2_counter1 = 0, Cell_SKI2_counter2 = 0;
179 // int counter1=0, counter2=0;
180  for(i = ski.begin(); i != ski.end(); i++) {
181  const SKIROC2DigiCollection& Coll = *(*i);
182 #ifdef DEBUG
183  if(DEBUG) cout << "SKIROC2 Digis: " << i->provenance()->branchName() << endl;
184 #endif
185 //////////////////////////////////Evaluate average pedestal per event to subtract out//////////////////////////////////
186  for(SKIROC2DigiCollection::const_iterator k = Coll.begin(); k != Coll.end(); k++) {
187  const SKIROC2DataFrame& skiFrame = *k ;
188  HGCalTBDetId detId = skiFrame.detid();
189 // int iSample = 0; // using only 1 sample ///\todo to be generalized
190  int n_layer = (detId).layer();
191  int n_sensor_IU = (detId).sensorIU();
192  int n_sensor_IV = (detId).sensorIV();
193  int n_cell_iu = (detId).iu();
194  int n_cell_iv = (detId).iv();
195 #ifdef DEBUG
196  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;
197 #endif
198  if(!IsCellValid.iu_iv_valid(n_layer, n_sensor_IU, n_sensor_IV, n_cell_iu, n_cell_iv, sensorsize)) continue;
199 
200  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots(n_layer, n_sensor_IU, n_sensor_IV, n_cell_iu, n_cell_iv, sensorsize);
201 // double iux = (CellCentreXY.first < 0 ) ? (CellCentreXY.first + 0.0001) : (CellCentreXY.first - 0.0001) ;
202  double iyy = (CellCentreXY.second < 0 ) ? (CellCentreXY.second + 0.0001) : (CellCentreXY.second - 0.0001);
203 
204 // _pedestals[detId.skiIndexInSensor()].Sum(detId, skiFrame[iSample].adcLow());
205 
206  if(n_layer == 1){
207  if(iyy < 0){
208  Average_Pedestal_SKI1_Event1+= skiFrame[0].adcHigh();
209  Cell_SKI1_counter1++;
210  }
211  else if(iyy > 0){
212  Average_Pedestal_SKI2_Event1+= skiFrame[0].adcHigh();
213  Cell_SKI2_counter1++;
214  }
215  }
216 
217  if(n_layer == 2){
218  if(iyy < 0){
219  Average_Pedestal_SKI1_Event2+= skiFrame[0].adcHigh();
220  Cell_SKI1_counter2++;
221  }
222  else if(iyy > 0){
223  Average_Pedestal_SKI2_Event2+= skiFrame[0].adcHigh();
224  Cell_SKI2_counter2++;
225  }
226  }
227 
228 
229  }
230 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
231 //
232 
233  TH2Poly *h_digi_layer[MAXLAYERS], *highGain_hpoly[MAXLAYERS];
234  if(evId%10 == 0){
235  for(unsigned int iLayer = 0; iLayer < MAXLAYERS; ++iLayer) {
236 
237  h_digi_layer[iLayer] = fs->make<TH2Poly>();
238  sprintf(name, "FullLayer_ADC%i_Layer%i_Event%i", 0, iLayer + 1, evId);
239  sprintf(title, "Sum of adc counts per cell for ADC%i Layer%i Event%i", 0, iLayer + 1, evId);
240  h_digi_layer[iLayer]->SetName(name);
241  h_digi_layer[iLayer]->SetTitle(title);
242  InitTH2Poly(*h_digi_layer[iLayer]);
243 
244  highGain_hpoly[iLayer] = fs->make<TH2Poly>();
245  sprintf(name, "FullLayer_ADC%i_Layer%i_Event%i", 1, iLayer + 1, evId);
246  sprintf(title, "Sum of adc counts per cell for ADC%i Layer%i Event%i", 1, iLayer + 1, evId);
247  highGain_hpoly[iLayer]->SetName(name);
248  highGain_hpoly[iLayer]->SetTitle(title);
249  InitTH2Poly(*highGain_hpoly[iLayer]);
250  }
251  }
252 
253  for(SKIROC2DigiCollection::const_iterator j = Coll.begin(); j != Coll.end(); j++) {
254 // bool flag_calib = false;
255  const SKIROC2DataFrame& SKI = *j ;
256 
257  int iSample = 0; // using only 1 sample ///\todo to be generalized
258  int n_layer = (SKI.detid()).layer();
259  int n_sensor_IU = (SKI.detid()).sensorIU();
260  int n_sensor_IV = (SKI.detid()).sensorIV();
261  int n_cell_iu = (SKI.detid()).iu();
262  int n_cell_iv = (SKI.detid()).iv();
263  HGCalTBDetId detId = SKI.detid();
264  if(!IsCellValid.iu_iv_valid(n_layer, n_sensor_IU, n_sensor_IV, n_cell_iu, n_cell_iv, sensorsize)) continue;
265 
266 
267 #ifdef DEBUG
268  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;
269 #endif
270 
271 
272  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots(n_layer, n_sensor_IU, n_sensor_IV, n_cell_iu, n_cell_iv, sensorsize);
273 
274  double iux = (CellCentreXY.first < 0 ) ? (CellCentreXY.first + 0.0001) : (CellCentreXY.first - 0.0001) ;
275  double iyy = (CellCentreXY.second < 0 ) ? (CellCentreXY.second + 0.0001) : (CellCentreXY.second - 0.0001);
276 
277  if(detId.cellType() == 1 || detId.cellType() == 4) {
278  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 <<" Layers= "<<MAXLAYERS<<endl;
279  if(evId%10 == 0) h_digi_layer[n_layer-1]->Fill(iux, iyy, 0.5 * (SKI[iSample].adcLow()));
280  } else {
281  if(evId%10 == 0) h_digi_layer[n_layer-1]->Fill(iux, iyy, (SKI[iSample].adcLow()));
282  }
283 
284  if(evId%10 == 0 ){
285  if(n_layer == 1 && iyy<0) highGain_hpoly[n_layer-1]->Fill(iux , iyy, (SKI[iSample].adcHigh() - Average_Pedestal_SKI1_Event1/Cell_SKI1_counter1));
286  if(n_layer == 1 && iyy>0) highGain_hpoly[n_layer-1]->Fill(iux , iyy, (SKI[iSample].adcHigh() - Average_Pedestal_SKI2_Event1/Cell_SKI2_counter1));
287  if(n_layer == 2 && iyy<0) highGain_hpoly[n_layer-1]->Fill(iux , iyy, (SKI[iSample].adcHigh() - Average_Pedestal_SKI1_Event2/Cell_SKI1_counter2));
288  if(n_layer == 2 && iyy>0) highGain_hpoly[n_layer-1]->Fill(iux , iyy, (SKI[iSample].adcHigh() - Average_Pedestal_SKI2_Event2/Cell_SKI2_counter2));
289 
290  }
291  }
292 
293  }
294  } else {
295  edm::LogWarning("DQM") << "No SKIROC2 Digis";
296  }
297 
298 }//analyze method ends here
299 
300 
301 // ------------ method called once each job just before starting event loop ------------
302 void
303 DigiPlotter_New::beginJob()
304 {
305 
306 }
307 
308 // ------------ method called once each job just after ending the event loop ------------
309 void
310 DigiPlotter_New::endJob()
311 {
312 }
313 
314 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
315 void
316 DigiPlotter_New::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
317 {
318  //The following says we do not know what parameters are allowed so do no validation
319  // Please change this to state exactly what you do use, even if it is no parameters
320  edm::ParameterSetDescription desc;
321  desc.setUnknown();
322  descriptions.addDefault(desc);
323 }
324 
325 //define this as a plug-in
326 DEFINE_FWK_MODULE(DigiPlotter_New);
DEFINE_FWK_MODULE(Pedestals)
#define MAXVERTICES
HGCalTBDetId detid() const
Get the detector id.
edm::Service< TFileService > fs
std::vector< std::pair< double, double > > GetCellCoordinatesForPlots(int layer, int sensor_iu, int sensor_iv, int iu, int iv, int sensorsize)
#define MAXLAYERS
int cellType() const
cell type
Definition: HGCalTBDetId.h:75