HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
RecHitPlotter_HighGain_New.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HGCal/RecHitPlotter_HighGain_New
4 // Class: RecHitPlotter_HighGain_New
5 //
6 /**\class RecHitPlotter_HighGain_New RecHitPlotter_HighGain_New.cc HGCal/RecHitPlotter_HighGain_New/plugins/RecHitPlotter_HighGain_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 <fstream>
24 #include "TH2Poly.h"
25 #include "TProfile.h"
26 #include "TH1F.h"
27 #include "TH2F.h"
28 // user include files
29 #include "FWCore/Framework/interface/Frameworkfwd.h"
30 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
31 #include "FWCore/Framework/interface/Event.h"
32 #include "FWCore/Framework/interface/MakerMacros.h"
33 #include "FWCore/ParameterSet/interface/ParameterSet.h"
34 #include "FWCore/ServiceRegistry/interface/Service.h"
35 #include "DataFormats/Math/interface/Error.h"
36 #include "DataFormats/Math/interface/Vector3D.h"
37 #include "DataFormats/Math/interface/Point3D.h"
38 #include "HGCal/DataFormats/interface/HGCalTBRecHitCollections.h"
39 #include "HGCal/DataFormats/interface/HGCalTBDetId.h"
40 #include "HGCal/DataFormats/interface/HGCalTBRecHit.h"
41 #include "HGCal/DataFormats/interface/HGCalTBTrackCollection.h"
42 #include "HGCal/DataFormats/interface/HGCalTBTrack.h"
43 #include "HGCal/Geometry/interface/HGCalTBCellVertices.h"
44 #include "HGCal/Geometry/interface/HGCalTBTopology.h"
45 #include "CommonTools/UtilAlgos/interface/TFileService.h"
46 #include "HGCal/CondObjects/interface/HGCalElectronicsMap.h"
47 #include "HGCal/CondObjects/interface/HGCalCondObjectTextIO.h"
48 #include "HGCal/DataFormats/interface/HGCalTBElectronicsId.h"
49 #include "HGCal/Geometry/interface/HGCalTBGeometryParameters.h"
50 #define MAXVERTICES 6
51 
52 using namespace std;
53 
54 // class declaration
55 //
56 
57 // If the analyzer does not use TFileService, please remove
58 // the template argument to the base class so the class inherits
59 // from edm::one::EDAnalyzer<> and also remove the line from
60 // constructor "usesResource("TFileService");"
61 // This will improve performance in multithreaded jobs.
62 
63 static const double delta = 0.00001;//Add/subtract delta = 0.00001 to x,y of a cell centre so the TH2Poly::Fill doesnt have a problem at the edges where the centre of a half-hex cell passes through the sennsor boundary line.
64 double Return_RMS(double mean_sq, double mean)
65 {
66  return sqrt(mean_sq - mean * mean);
67 }
68 bool DoCommonMode = 1;
69 bool PED = 0;
70 bool UP = 1;
71 
72  edm::Service<TFileService> fs;
73 class RecHitPlotter_HighGain_New : public edm::one::EDAnalyzer<edm::one::SharedResources>
74 {
75 
76 public:
77  explicit RecHitPlotter_HighGain_New(const edm::ParameterSet&);
78  ~RecHitPlotter_HighGain_New();
79  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
80 
81 private:
82  virtual void beginJob() override;
83  void analyze(const edm::Event& , const edm::EventSetup&) override;
84  virtual void endJob() override;
85  void InitTH2Poly(TH2Poly& poly);
86  ofstream ofs;
87  // ----------member data ---------------------------
88  edm::EDGetToken HGCalTBRecHitCollection_;
89  edm::EDGetToken HGCalTBTrackCollection_;
90  HGCalTBTopology IsCellValid;
91  HGCalTBCellVertices TheCell;
92  std::string mapfile_ = "HGCal/CondObjects/data/map_FNAL_SB2_Layer16.txt";
93  struct {
94  HGCalElectronicsMap emap_;
95  } essource_;
96  int sensorsize = 128;// The geometry for a 256 cell sensor hasnt been implemted yet. Need a picture to do this.
97  std::vector<std::pair<double, double>> CellXY;
98  std::pair<double, double> CellCentreXY;
99  std::vector<std::pair<double, double>>::const_iterator it;
100  const static int NLAYERS = 1;
101  double Mean_SUM[128] = {0.};
102  double Mean_SQ_SUM[128] = {0.};
103  double Mean_PROD_SUM[128][128] = { {0.} };
104  double Diff_IJ_SUM[128][128] = { {0.} };
105  double Mean_i = 0.;
106  double Mean_j = 0.;
107  double RMS_i = 0.;
108  double RMS_j = 0.;
109  double Correlation = 0.;
110  double Covariance = 0.;
111  TH2F *Covar_hist;
112  TH2F *Correl_hist;
113  TH2F *DiffIJ_hist;
114 //TH2D *Covar_hist = new TH2D("Covar_hist","Covar_hist",nphistar_bins-1,phistar_var,nphistar_bins-1,phistar_var);
115 //TH2D *Correl_hist = new TH2D("Correl_hist","Correl_hist",nphistar_bins-1,phistar_var,nphistar_bins-1,phistar_var);
116 
117 
118  int Sensor_Iu = 0;
119  int Sensor_Iv = 0;
120  double ADC_Chan[128][9000];
121  TH1F *h_RecHit_layer_summed[MAXLAYERS];
122  TH1F* Sum_Cluster_ADC;
123  TH1F* Sum_Cluster_Max;
124  TH1F* AllCells_Ped;
125  TH1F* AllCells_CM;
126  TProfile* SKI1_Ped_Event;
127  TProfile* SKI2_Ped_Event;
128  TH1F* CG_X;
129  TH1F* CG_Y;
130  char name[50], title[50];
131  double ExtrapolateZ = 20.;// distance along Z the tracks are extrapolated by(in cm) to hit the first layer.
132 };
133 
134 //
135 // constants, enums and typedefs
136 //
137 
138 //
139 // static data member definitions
140 //
141 
142 //
143 // constructors and destructor
144 //
145 RecHitPlotter_HighGain_New::RecHitPlotter_HighGain_New(const edm::ParameterSet& iConfig)
146 {
147  //now do what ever initialization is needed
148  HGCalTBRecHitCollection_ = consumes<HGCalTBRecHitCollection>(iConfig.getParameter<edm::InputTag>("HGCALTBRECHITS"));
149 //Booking 2 "hexagonal" histograms to display the sum of Rechits and the Occupancy(Hit > 5 GeV) in 1 sensor in 1 layer. To include all layers soon. Also the 1D Rechits per cell in a sensor is booked here.
150  Covar_hist = fs->make<TH2F>("Covar_hist", "Covar_hist", 128, 0, 128, 128, 0, 128);
151  Correl_hist = fs->make<TH2F>("Correl_hist", "Correl_hist", 128, 0, 128, 128, 0, 128);
152  DiffIJ_hist = fs->make<TH2F>("DiffIJ_hist", "DiffIJ_hist", 128, 0, 128, 128, 0, 128);
153  CG_X = fs->make<TH1F>("CG_X", "CG X[cm]", 100, -5, 5);
154  CG_Y = fs->make<TH1F>("CG_Y", "CG Y[cm]", 100, -5, 5);
155  AllCells_Ped = fs->make<TH1F>("AllCells_Ped", "AllCells_Ped", 500, -250, 250);
156  AllCells_CM = fs->make<TH1F>("AllCells_CM", "AllCells_CM", 500, -250, 250);
157  SKI1_Ped_Event = fs->make<TProfile>("SKI1_Ped_Event", "Profile per event of pedestal for SKI1", 5000, 1, 5000, 0, 400);
158  SKI2_Ped_Event = fs->make<TProfile>("SKI2_Ped_Event", "Profile per event of pedestal for SKI2", 5000, 1, 5000, 0, 400);
159  Sum_Cluster_ADC = fs->make<TH1F>("Sum_Cluster_ADC", "Sum_Cluster_ADC", 1000, -4000., 4000.);
160  Sum_Cluster_Max = fs->make<TH1F>("Sum_Cluster_Max", "Sum_Cluster_Max", 1000, -4000., 4000.);
161  for(int nlayers = 0; nlayers < MAXLAYERS; nlayers++) {
162  sprintf(name, "FullLayer_RecHits_Layer%i_Summed", nlayers + 1);
163  sprintf(title, "Sum of RecHits Layer%i Summed over the cells", nlayers + 1);
164  h_RecHit_layer_summed[nlayers] = fs->make<TH1F>(name, title, 4000, -2000., 2000.);
165  h_RecHit_layer_summed[nlayers]->GetXaxis()->SetTitle("RecHits[GeV]");
166  }//loop over layers end here
167 
168 
169 }//contructor ends here
170 
171 
172 RecHitPlotter_HighGain_New::~RecHitPlotter_HighGain_New()
173 {
174 
175  // do anything here that needs to be done at desctruction time
176  // (e.g. close files, deallocate resources etc.)
177 
178 }
179 
180 
181 //
182 // member functions
183 //
184 
185 void RecHitPlotter_HighGain_New::InitTH2Poly(TH2Poly& poly)
186 {
187  double HexX[MAXVERTICES] = {0.};
188  double HexY[MAXVERTICES] = {0.};
189 
190  for(int iv = -7; iv < 8; iv++) {
191  for(int iu = -7; iu < 8; iu++) {
192  if(!IsCellValid.iu_iv_valid(NLAYERS, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize)) continue;
193  CellXY = TheCell.GetCellCoordinatesForPlots(NLAYERS, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize);
194  assert(CellXY.size() == 4 || CellXY.size() == 6);
195  unsigned int iVertex = 0;
196  for(it = CellXY.begin(); it != CellXY.end(); it++) {
197  HexX[iVertex] = it->first;
198  HexY[iVertex] = it->second;
199  ++iVertex;
200  }
201 //Somehow cloning of the TH2Poly was not working. Need to look at it. Currently physically booked another one.
202  poly.AddBin(CellXY.size(), HexX, HexY);
203  }//loop over iu
204  }//loop over iv
205  }
206 //
207 
208 // ------------ method called for each event ------------
209 void
210 RecHitPlotter_HighGain_New::analyze(const edm::Event& event, const edm::EventSetup& setup)
211 {
212 
213  using namespace edm;
214  using namespace std;
215 
216  edm::Handle<HGCalTBRecHitCollection> Rechits;
217  event.getByToken(HGCalTBRecHitCollection_, Rechits);
218  edm::Handle<HGCalTBRecHitCollection> Rechits1;
219  event.getByToken(HGCalTBRecHitCollection_, Rechits1);
220 
221  double Average_Pedestal_Per_Event1_Full = 0, Average_Pedestal_Per_Event1_Half = 0, Average_Pedestal_Per_Event2_Full = 0, Average_Pedestal_Per_Event2_Half = 0;
222  int Cell_counter1_Full = 0, Cell_counter2_Full = 0, Cell_counter1_Half = 0, Cell_counter2_Half = 0;
223  double iux_Max = 0., iyy_Max = 0.;
224  int ADC_TMP = 0;
225 
226  int Sum_Cluster_Tmp = 0;
227 
228 /*
229  for(auto Track : *Tracks) {
230  cout << endl << " Event/Trigger = " << event.id().event() << " Track intercept X= " << Track.vertex().X() << " Track intercept Y= " << Track.vertex().Y() << " Slope X= " << Track.momentum().X() << " Slope Y= " << Track.momentum().Y() << " Track hit X= " << Track.pointAt(ExtrapolateZ).X() << " Track hit Y= " << Track.pointAt(ExtrapolateZ).Y() << endl;
231  }
232 */
233 
234  for(auto RecHit1 : *Rechits1) {
235  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots((RecHit1.id()).layer(), (RecHit1.id()).sensorIU(), (RecHit1.id()).sensorIV(), (RecHit1.id()).iu(), (RecHit1.id()).iv(), sensorsize);
236  double iux = (CellCentreXY.first < 0 ) ? (CellCentreXY.first + delta) : (CellCentreXY.first - delta) ;
237  double iyy = (CellCentreXY.second < 0 ) ? (CellCentreXY.second + delta) : (CellCentreXY.second - delta);
238  if((RecHit1.energyHigh() > ADC_TMP)) {
239 
240  ADC_TMP = RecHit1.energyHigh();
241  iux_Max = iux;
242  iyy_Max = iyy;
243  }
244  if(RecHit1.energyHigh() > 20.) continue;
245 // if(UP && fabs(iux - iux_Max) > 0. && fabs(iyy - iyy_Max) > 0. && RecHit1.energyHigh()< 2000000.){
246  if(((RecHit1.id()).cellType() == 0) || ((RecHit1.id()).cellType() == 5)) {
247 
248  if(( iyy >= -0.25 )) {
249  Cell_counter1_Full++;
250  Average_Pedestal_Per_Event1_Full += RecHit1.energyHigh();
251  }
252 
253  if((iyy <= -0.5)) {
254  Cell_counter2_Full++;
255  Average_Pedestal_Per_Event2_Full += RecHit1.energyHigh();
256  }
257 
258 /*
259  Cell_counter1_Full++;
260  Average_Pedestal_Per_Event1_Full += RecHit1.energyHigh();
261  Cell_counter2_Full++;
262  Average_Pedestal_Per_Event2_Full += RecHit1.energyHigh();
263 */
264  }
265 
266  if(((RecHit1.id()).cellType() == 2) || ((RecHit1.id()).cellType() == 3) || ((RecHit1.id()).cellType() == 4) ) {
267 
268  if(((iyy >= -0.25 ))) {
269  Cell_counter1_Half++;
270  Average_Pedestal_Per_Event1_Half += RecHit1.energyHigh();
271  }
272  if(((iyy <= -0.5))) {
273  Cell_counter2_Half++;
274  Average_Pedestal_Per_Event2_Half += RecHit1.energyHigh();
275  }
276  }
277 
278 // }
279 
280 
281  if(!UP) {
282  if(((RecHit1.id()).cellType() == 0)) {
283 
284  if((iux <= -0.25 && iux >= -3.25) && (iyy <= -0.25 && iyy >= -5.25 ) && (RecHit1.id()).cellType() == 0) {
285  Cell_counter1_Full++;
286  Average_Pedestal_Per_Event1_Full += RecHit1.energyHigh();
287  }
288 
289  if((iux >= -0.25 && iux <= 3.25) && (iyy <= -0.25 && iyy >= -5.25 ) && (RecHit1.id()).cellType() == 0) {
290  Cell_counter2_Full++;
291  Average_Pedestal_Per_Event2_Full += RecHit1.energyHigh();
292  }
293  }
294 
295  if(((RecHit1.id()).cellType() == 2) || ((RecHit1.id()).cellType() == 3) || ((RecHit1.id()).cellType() == 4) ) {
296 
297  if((iux <= 0.25 && iyy >= -0.25 ) || (iux < -0.5) ) {
298  Cell_counter1_Half++;
299  Average_Pedestal_Per_Event1_Half += RecHit1.energyHigh();
300  }
301  if((iux >= 0.25 && iyy <= -0.5) || (iux >= 0.5) ) {
302  Cell_counter2_Half++;
303  Average_Pedestal_Per_Event2_Half += RecHit1.energyHigh();
304  }
305  }
306 
307  }
308 
309 
310  }
311  if(ADC_TMP > 50.) {
312  CG_X->Fill(iux_Max);
313  CG_Y->Fill(iyy_Max);
314  Sum_Cluster_Max->Fill(ADC_TMP);
315 // cout<<endl<<" X= "<<CG_X<<" Y= "<<CG_Y<<" Max ADC= "<<ADC_TMP<<endl;
316  }
317 
318  TH2Poly *h_RecHit_layer[MAXLAYERS];
319  int evId = event.id().event() - 1;
320  int iLayer = (evId%2400)/150;
321  h_RecHit_layer[iLayer] = fs->make<TH2Poly>();
322  sprintf(name, "FullLayer_ADC%i_Layer%i_Event%i", 0, iLayer + 1, evId);
323  sprintf(title, "ADC counts in Layer%i",iLayer + 1);
324  h_RecHit_layer[iLayer]->SetName(name);
325  h_RecHit_layer[iLayer]->SetTitle(title);
326  InitTH2Poly(*h_RecHit_layer[iLayer]);
327 
328  for(auto RecHit : *Rechits) {
329  if(!IsCellValid.iu_iv_valid((RecHit.id()).layer(), (RecHit.id()).sensorIU(), (RecHit.id()).sensorIV(), (RecHit.id()).iu(), (RecHit.id()).iv(), sensorsize)) continue;
330  int n_layer = (RecHit.id()).layer();
331  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots((RecHit.id()).layer(), (RecHit.id()).sensorIU(), (RecHit.id()).sensorIV(), (RecHit.id()).iu(), (RecHit.id()).iv(), sensorsize);
332  double iux = (CellCentreXY.first < 0 ) ? (CellCentreXY.first + delta) : (CellCentreXY.first - delta) ;
333  double iyy = (CellCentreXY.second < 0 ) ? (CellCentreXY.second + delta) : (CellCentreXY.second - delta);
334  uint32_t EID = essource_.emap_.detId2eid(RecHit.id());
335  HGCalTBElectronicsId eid(EID);
336  AllCells_Ped->Fill(RecHit.energyHigh());
337 if(RecHit.energyHigh() > 55) cout<<endl<<" Energy= "<<RecHit.energyHigh()<<" u= "<<iux<<" v= "<<iyy<<" event number= "<<event.id().event()<<endl;
338 // if((RecHit.id()).iu() == 4 && (RecHit.id()).iv() == 2) continue;
339  if(!DoCommonMode) {
340  h_RecHit_layer[n_layer - 1]->Fill(iux , iyy, RecHit.energyHigh());
341  }
342  if(((RecHit.id()).cellType() == 0 ) || ((RecHit.id()).cellType() == 5) ) {
343  if((iyy >= -0.25 ) ) {
344  if(!PED && DoCommonMode) {
345  if((RecHit.id()).cellType() == 0){
346  h_RecHit_layer[n_layer - 1]->Fill(iux , iyy, (RecHit.energyHigh() - (Average_Pedestal_Per_Event1_Full / (Cell_counter1_Full))) );
347 
348  }
349  Sum_Cluster_Tmp += (RecHit.energyHigh());
350  }
351  h_RecHit_layer_summed[n_layer - 1]->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event1_Full / (Cell_counter1_Full)));
352  if(DoCommonMode) {
353  AllCells_CM->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event1_Full / (Cell_counter1_Full)));
354  }
355 // Sum_Cluster_ADC->Fill(RecHit.energyHigh()- (Average_Pedestal_Per_Event1_Full/(Cell_counter1_Full)));
356 // CG_X->Fill(iux);
357 // CG_Y->Fill(iyy);
358  } else if(( iyy < -0.50 )) {
359  if(!PED && DoCommonMode) {
360  if((RecHit.id()).cellType() == 0) h_RecHit_layer[n_layer - 1]->Fill(iux , iyy, (RecHit.energyHigh() - (Average_Pedestal_Per_Event2_Full / (Cell_counter2_Full))) );
361  Sum_Cluster_Tmp += (RecHit.energyHigh());
362 
363  }
364  h_RecHit_layer_summed[n_layer - 1]->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event2_Full / (Cell_counter2_Full)));
365  if(DoCommonMode) {
366  AllCells_CM->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event2_Full / (Cell_counter2_Full)));
367  }
368 // Sum_Cluster_ADC->Fill(RecHit.energyHigh()- (Average_Pedestal_Per_Event2_Full/(Cell_counter2_Full)));
369 // CG_X->Fill(iux);
370 // CG_Y->Fill(iyy);
371  }
372  }
373  if((RecHit.id()).cellType() == 1 ) {
374  if(!PED) h_RecHit_layer[n_layer - 1]->Fill(iux , iyy, RecHit.energyHigh());
375  if(DoCommonMode) {
376  AllCells_CM->Fill(RecHit.energyHigh());
377  }
378  }
379  if(((RecHit.id()).cellType() != 5) && ((RecHit.id()).cellType() != 1) && ((RecHit.id()).cellType() != 0)) {
380  if((iyy >= -0.25 ) ) {
381  if(!PED && DoCommonMode){
382  h_RecHit_layer[n_layer - 1]->Fill(iux , iyy, RecHit.energyHigh() - (Average_Pedestal_Per_Event1_Half / Cell_counter1_Half) );
383 
384  }
385 // h_RecHit_layer_summed[n_layer - 1]->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event1_Half / Cell_counter1_Half));
386  if(DoCommonMode) {
387  AllCells_CM->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event1_Half / (Cell_counter1_Half)));
388  }
389  }
390  if(( iyy < -0.50 )) {
391  if(!PED && DoCommonMode){
392  h_RecHit_layer[n_layer - 1]->Fill(iux , iyy, RecHit.energyHigh() - (Average_Pedestal_Per_Event2_Half / Cell_counter2_Half) );
393 
394  }
395  h_RecHit_layer_summed[n_layer - 1]->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event2_Half / Cell_counter2_Half));
396  if(DoCommonMode) {
397  AllCells_CM->Fill(RecHit.energyHigh() - (Average_Pedestal_Per_Event2_Half / (Cell_counter2_Half)));
398  }
399  }
400 
401  }
402 
403  }
404 
405  if(Sum_Cluster_Tmp > 2. ) Sum_Cluster_ADC->Fill(Sum_Cluster_Tmp);
406 
407 
408 }//analyze method ends here
409 
410 
411 // ------------ method called once each job just before starting event loop ------------
412 void
413 RecHitPlotter_HighGain_New::beginJob()
414 {
415  HGCalCondObjectTextIO io(0);
416  edm::FileInPath fip(mapfile_);
417  if (!io.load(fip.fullPath(), essource_.emap_)) {
418  throw cms::Exception("Unable to load electronics map");
419  }
420  ofs.open("ShowerMax_X_Y_Ped_20532.txt", std::ofstream::out);
421 }
422 
423 // ------------ method called once each job just after ending the event loop ------------
424 void
425 RecHitPlotter_HighGain_New::endJob()
426 {
427 
428  ofs.close();
429 
430 }
431 
432 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
433 void
434 RecHitPlotter_HighGain_New::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
435 {
436  //The following says we do not know what parameters are allowed so do no validation
437  // Please change this to state exactly what you do use, even if it is no parameters
438  edm::ParameterSetDescription desc;
439  desc.setUnknown();
440  descriptions.addDefault(desc);
441 }
442 
443 //define this as a plug-in
444 DEFINE_FWK_MODULE(RecHitPlotter_HighGain_New);
DEFINE_FWK_MODULE(Pedestals)
edm::Service< TFileService > fs
#define MAXLAYERS
double delta
double Return_RMS(double mean_sq, double mean)
provides the conversion between electronics Id to DetId
#define MAXVERTICES