HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
RecHitPlotter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HGCal/RecHitPlotter
4 // Class: RecHitPlotter
5 //
6 /**\class RecHitPlotter RecHitPlotter.cc HGCal/RecHitPlotter/plugins/RecHitPlotter.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 // user include files
26 #include "FWCore/Framework/interface/Frameworkfwd.h"
27 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
28 #include "FWCore/Framework/interface/Event.h"
29 #include "FWCore/Framework/interface/MakerMacros.h"
30 #include "FWCore/ParameterSet/interface/ParameterSet.h"
31 #include "FWCore/ServiceRegistry/interface/Service.h"
32 #include "HGCal/DataFormats/interface/HGCalTBRecHitCollections.h"
33 #include "HGCal/DataFormats/interface/HGCalTBDetId.h"
34 #include "HGCal/DataFormats/interface/HGCalTBRecHit.h"
35 #include "HGCal/Geometry/interface/HGCalTBCellVertices.h"
36 #include "HGCal/Geometry/interface/HGCalTBTopology.h"
37 #include "CommonTools/UtilAlgos/interface/TFileService.h"
38 
39 //
40 // class declaration
41 //
42 
43 // If the analyzer does not use TFileService, please remove
44 // the template argument to the base class so the class inherits
45 // from edm::one::EDAnalyzer<> and also remove the line from
46 // constructor "usesResource("TFileService");"
47 // This will improve performance in multithreaded jobs.
48 
49 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.
50 
51 class RecHitPlotter : public edm::one::EDAnalyzer<edm::one::SharedResources>
52 {
53 
54 public:
55  explicit RecHitPlotter(const edm::ParameterSet&);
56  ~RecHitPlotter();
57  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
58 
59 private:
60  virtual void beginJob() override;
61  void analyze(const edm::Event& , const edm::EventSetup&) override;
62  virtual void endJob() override;
63 
64  // ----------member data ---------------------------
65  edm::EDGetToken HGCalTBRecHitCollection_;
66  HGCalTBTopology IsCellValid;
67  HGCalTBCellVertices TheCell;
68  int sensorsize = 128;// The geometry for a 256 cell sensor hasnt been implemted yet. Need a picture to do this.
69  std::vector<std::pair<double, double>> CellXY;
70  std::pair<double, double> CellCentreXY;
71  std::vector<std::pair<double, double>>::const_iterator it;
72  const static int NLAYERS = 2;
73  TH2Poly *h_RecHit_layer[NLAYERS];
74  TH1F *h_RecHit_layer_summed[NLAYERS];
75  TH2Poly *h_RecHit_layer_Occupancy[NLAYERS];
76  const static int cellx = 15;
77  const static int celly = 15;
78  int Sensor_Iu = 0;
79  int Sensor_Iv = 0;
80  TH1F *h_RecHit_layer_cell[NLAYERS][cellx][celly];
81  char name[50], title[50];
82 };
83 
84 //
85 // constants, enums and typedefs
86 //
87 
88 //
89 // static data member definitions
90 //
91 
92 //
93 // constructors and destructor
94 //
95 RecHitPlotter::RecHitPlotter(const edm::ParameterSet& iConfig)
96 {
97  //now do what ever initialization is needed
98  usesResource("TFileService");
99  edm::Service<TFileService> fs;
100  HGCalTBRecHitCollection_ = consumes<HGCalTBRecHitCollection>(iConfig.getParameter<edm::InputTag>("HGCALTBRECHITS"));
101 
102 //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.
103  const int HalfHexVertices = 4;
104  double HalfHexX[HalfHexVertices] = {0.};
105  double HalfHexY[HalfHexVertices] = {0.};
106  const int FullHexVertices = 6;
107  double FullHexX[FullHexVertices] = {0.};
108  double FullHexY[FullHexVertices] = {0.};
109  int iii = 0;
110  for(int nlayers = 0; nlayers < NLAYERS; nlayers++) {
111  sprintf(name, "FullLayer_RecHits_Layer%i", nlayers + 1);
112  sprintf(title, "Sum of RecHits Layer%i", nlayers + 1);
113  h_RecHit_layer[nlayers] = fs->make<TH2Poly>();
114  h_RecHit_layer[nlayers]->SetName(name);
115  h_RecHit_layer[nlayers]->SetTitle(title);
116  sprintf(name, "FullLayer_RecHits_Layer%i_Summed", nlayers + 1);
117  sprintf(title, "Sum of RecHits Layer%i Summed over the cells", nlayers + 1);
118  h_RecHit_layer_summed[nlayers] = fs->make<TH1F>(name, title, 200, -100., 100.);
119  h_RecHit_layer_summed[nlayers]->GetXaxis()->SetTitle("RecHits[GeV]");
120  sprintf(name, "FullLayer_Occupancy_Layer%i", nlayers + 1);
121  sprintf(title, "Sum of Occupancy Layer%i", nlayers + 1);
122  h_RecHit_layer_Occupancy[nlayers] = fs->make<TH2Poly>();
123  h_RecHit_layer_Occupancy[nlayers]->SetName(name);
124  h_RecHit_layer_Occupancy[nlayers]->SetTitle(title);
125  for(int iv = -7; iv < 8; iv++) {
126  for(int iu = -7; iu < 8; iu++) {
127  if(!IsCellValid.iu_iv_valid(nlayers, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize)) continue;
128 //Some thought needs to be put in about the binning and limits of this 1D histogram, probably different for beam type Fermilab and cern.
129  sprintf(name, "Cell_RecHits_u_%i_v_%i_Layer%i", iu, iv, nlayers + 1);
130  sprintf(title, "RecHits for Cell_u_%i_v_%i Layer%i", iu, iv, nlayers + 1);
131  h_RecHit_layer_cell[nlayers][iu + 7][iv + 7] = fs->make<TH1F>(name, title, 200, -100., 100.); // need to finalize binning
132  h_RecHit_layer_cell[nlayers][iu + 7][iv + 7]->GetXaxis()->SetTitle("RecHits[GeV]");
133  CellXY = TheCell.GetCellCoordinatesForPlots(nlayers, Sensor_Iu, Sensor_Iv, iu, iv, sensorsize);
134  int NumberOfCellVertices = CellXY.size();
135  iii = 0;
136  if(NumberOfCellVertices == 4) {
137  for(it = CellXY.begin(); it != CellXY.end(); it++) {
138  HalfHexX[iii] = it->first;
139  HalfHexY[iii++] = it->second;
140  }
141 //Somehow cloning of the TH2Poly was not working. Need to look at it. Currently physically booked another one.
142  h_RecHit_layer[nlayers]->AddBin(NumberOfCellVertices, HalfHexX, HalfHexY);
143  h_RecHit_layer_Occupancy[nlayers]->AddBin(NumberOfCellVertices, HalfHexX, HalfHexY);
144  } else if(NumberOfCellVertices == 6) {
145  iii = 0;
146  for(it = CellXY.begin(); it != CellXY.end(); it++) {
147  FullHexX[iii] = it->first;
148  FullHexY[iii++] = it->second;
149  }
150  h_RecHit_layer[nlayers]->AddBin(NumberOfCellVertices, FullHexX, FullHexY);
151  h_RecHit_layer_Occupancy[nlayers]->AddBin(NumberOfCellVertices, FullHexX, FullHexY);
152  }
153 
154  }
155  }
156  }//loop over layers end here
157 
158 
159 }//contructor ends here
160 
161 
162 RecHitPlotter::~RecHitPlotter()
163 {
164 
165  // do anything here that needs to be done at desctruction time
166  // (e.g. close files, deallocate resources etc.)
167 
168 }
169 
170 
171 //
172 // member functions
173 //
174 
175 // ------------ method called for each event ------------
176 void
177 RecHitPlotter::analyze(const edm::Event& event, const edm::EventSetup& setup)
178 {
179 
180  using namespace edm;
181  using namespace std;
182 
183  edm::Handle<HGCalTBRecHitCollection> Rechits;
184  event.getByToken(HGCalTBRecHitCollection_, Rechits);
185  edm::Handle<HGCalTBRecHitCollection> Rechits1;
186  event.getByToken(HGCalTBRecHitCollection_, Rechits1);
187 
188  double Average_Pedestal_Per_Event1_Full = 0;
189  int Cell_counter1_Full = 0;
190  for(auto RecHit1 : *Rechits1) {
191  Average_Pedestal_Per_Event1_Full+=RecHit1.energyHigh();
192  Cell_counter1_Full++;
193  }
194 
195  for(auto RecHit : *Rechits) {
196  if(!IsCellValid.iu_iv_valid((RecHit.id()).layer(), (RecHit.id()).sensorIU(), (RecHit.id()).sensorIV(), (RecHit.id()).iu(), (RecHit.id()).iv(), sensorsize)) continue;
197  int n_layer = (RecHit.id()).layer();
198  int n_cell_type = (RecHit.id()).cellType();
199  double n_cell_area = IsCellValid.Cell_Area(n_cell_type);
200 //We now obtain the cartesian coordinates of the cell corresponding to an iu,iv. This may either be a full hex, a half hex or an invalid cell. If a cell is invalid based on the iu,iv index -123456 is returned for its x,y vertices
201  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots((RecHit.id()).layer(), (RecHit.id()).sensorIU(), (RecHit.id()).sensorIV(), (RecHit.id()).iu(), (RecHit.id()).iv(), sensorsize);
202  double iux = (CellCentreXY.first < 0 ) ? (CellCentreXY.first + delta) : (CellCentreXY.first - delta) ;
203  double iyy = (CellCentreXY.second < 0 ) ? (CellCentreXY.second + delta) : (CellCentreXY.second - delta);
204  h_RecHit_layer[n_layer - 1]->Fill(iux , iyy, RecHit.energyHigh());
205  h_RecHit_layer_summed[n_layer - 1]->Fill(RecHit.energyHigh());
206 //The energyHigh threshold for the occupancy has been hardcoded here. Need to decide what a good choice is. Maybe dynamic per cell depending on the pedestal
207  if(RecHit.energyHigh() > 5) h_RecHit_layer_Occupancy[n_layer - 1]->Fill(iux , iyy, 1. / n_cell_area);
208 // There will be several array indices iu, iv that wont be filled due to it being invalid. Can think of alternate array filling.
209  h_RecHit_layer_cell[n_layer - 1][7 + (RecHit.id()).iu()][7 + (RecHit.id()).iv()]->Fill(RecHit.energyHigh() - Average_Pedestal_Per_Event1_Full/Cell_counter1_Full);
210  }
211 
212 
213 }//analyze method ends here
214 
215 
216 // ------------ method called once each job just before starting event loop ------------
217 void
218 RecHitPlotter::beginJob()
219 {
220 
221 }
222 
223 // ------------ method called once each job just after ending the event loop ------------
224 void
225 RecHitPlotter::endJob()
226 {
227 }
228 
229 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
230 void
231 RecHitPlotter::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
232 {
233  //The following says we do not know what parameters are allowed so do no validation
234  // Please change this to state exactly what you do use, even if it is no parameters
235  edm::ParameterSetDescription desc;
236  desc.setUnknown();
237  descriptions.addDefault(desc);
238 }
239 
240 //define this as a plug-in
241 DEFINE_FWK_MODULE(RecHitPlotter);
DEFINE_FWK_MODULE(Pedestals)
edm::Service< TFileService > fs
double delta