HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Layer_Sum_Analyzer.cc
Go to the documentation of this file.
1 /* Need full layer, 7 cell cluster, and 19 cell cluster histograms for each layer
2  * also need each for all layers summed
3  * use ADC to MIP conversion of 1 MIP = 10 ADC Counts */
4 
5 /**
6  @Author: Ryan Quinn <ryan>
7  7 July 2016
8  quinn@physics.umn.edu
9 */
10 
11 
12 
13 // system include files
14 #include <memory>
15 #include <iostream>
16 #include "TH2Poly.h"
17 #include "TH1F.h"
18 #include "TF1.h"
19 #include <sstream>
20 #include <fstream>
21 #include <math.h>
22 // user include files
23 #include "FWCore/Framework/interface/Frameworkfwd.h"
24 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
25 #include "FWCore/Framework/interface/Event.h"
26 #include "FWCore/Framework/interface/MakerMacros.h"
27 #include "FWCore/ParameterSet/interface/ParameterSet.h"
28 #include "FWCore/ServiceRegistry/interface/Service.h"
29 #include "HGCal/DataFormats/interface/HGCalTBRecHitCollections.h"
30 #include "HGCal/DataFormats/interface/HGCalTBDetId.h"
31 #include "HGCal/DataFormats/interface/HGCalTBRecHit.h"
32 #include "HGCal/Geometry/interface/HGCalTBCellVertices.h"
33 #include "HGCal/Geometry/interface/HGCalTBTopology.h"
34 #include "HGCal/Geometry/interface/HGCalTBGeometryParameters.h"
35 #include "CommonTools/UtilAlgos/interface/TFileService.h"
36 #include "HGCal/CondObjects/interface/HGCalElectronicsMap.h"
37 #include "HGCal/CondObjects/interface/HGCalCondObjectTextIO.h"
38 #include "HGCal/DataFormats/interface/HGCalTBElectronicsId.h"
39 #include "HGCal/DataFormats/interface/HGCalTBDataFrameContainers.h"
40 #include "HGCal/Geometry/interface/HGCalTBCellVertices.h"
41 #include "HGCal/Geometry/interface/HGCalTBCellParameters.h"
42 #include "HGCal/Geometry/interface/HGCalTBSpillParameters.h"
43 
44 // chooses which particle to look at. Inverts threshold filtering.
45 // if nothing is selected, electrons are the default
46 const bool ELECTRONS(1);// uses > *CELLS_THRESHOLD
47 const bool PIONS(0);// uses > PION_*CELLS_THRESHOLD and < *CELLS_THRESHOLD
48 
49 double Layer_Z[16] = {1.2,2.,3.5,4.3,5.8,6.3,8.7,9.5,11.4,12.2,13.8,14.6,16.6,17.4,20.,20.8};
50 
51 double ADCtoMIP[16] = {1.};
52 
53 //double ADCtoMIP[16] = {16.02,16.85,15.36,14.73,10.66,15.64,16.52,14.24,10.07,14.42,16.14,17.33,16.61,16.84,15.79,16.43};// one MIP is equal to _ADCtoMIP_ ADC Counts
54 //double LayerWeight[16] = {0.6374029601923652, 0.7392021202456731, 0.6374273268336504, 0.7392021202456731, 0.6374273268336504, 0.8861075434658853, 0.8487578715427883, 1.0330129666860974, 0.8487578715427883, 1.0330129666860974, 0.8487578715427883, 1.5226977107534714, 1.2714189609610644, 1.5226977107534714, 1.2714189609610644, 1.5226977107534714};// X0 weights
55 
56 //double LayerWeight[16] = {1.4091566745180932, 0.7020676448403224, 0.6054055986179145, 0.7020676448403224, 0.6054055986179145, 0.8415931435769973, 0.8061197656138868, 0.9811186423136724, 0.8061197656138868, 0.9811186423136724, 0.8061197656138868, 1.4462036381025891, 1.2075480996058319, 1.4462036381025891, 1.2075480996058319, 1.4462036381025891};
57 
58 double LayerWeight[16] = {1.};
59 
60 //double LayerWeight[16] = {0.4847555727337982, 1.0214605968539232, 0.4847555727337982, 1.0214605968539232, 0.4847555727337982, 1.1420105918768606, 0.6423912113800805, 1.2625605868997982, 0.6423912113800805, 1.2625605868997982, 0.6423912113800805, 1.6643939036429232, 0.9576624886726451, 1.6643939036429232, 0.9576624886726451, 1.6643939036429232};// dE/dx weights
61 
62 double LayerSumWeight = 1.;
63 const int CMTHRESHOLD = 30;// anything less than this value is added to the commonmode sum
64 
65 // applied to all layers sum after commonmode subtraction and the ADC to MIP conversion
66 const double ALLCELLS_THRESHOLD = 50.;
67 const double SEVENCELLS_THRESHOLD = 50.;
68 const double NINETEENCELLS_THRESHOLD = 50.;
69 const double PION_ALLCELLS_THRESHOLD = 15.;
70 const double PION_7CELLS_THRESHOLD = -100.;
71 const double PION_19CELLS_THRESHOLD = -100.;
72 
73 
74 using namespace std;
75 
76 
77 
78 class Layer_Sum_Analyzer : public edm::one::EDAnalyzer<edm::one::SharedResources>
79 {
80 
81 public:
82  explicit Layer_Sum_Analyzer(const edm::ParameterSet&);
83  ~Layer_Sum_Analyzer();
84  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
85 
86 private:
87  virtual void beginJob() override;
88  void analyze(const edm::Event& , const edm::EventSetup&) override;
89  virtual void endJob() override;
90 
91  // ----------member data ---------------------------
92  edm::EDGetToken HGCalTBRecHitCollection_;
93  struct {
94  HGCalElectronicsMap emap_;
95  } essource_;
96  string mapfile_ = "HGCal/CondObjects/data/map_FNAL_SB2_Layer16.txt";
97  int sensorsize = 128;
98  std::vector<std::pair<double, double>> CellXY;
99  std::pair<double, double> CellCentreXY;
100  HGCalTBCellVertices TheCell;
101  double maxdist = (1 + sqrt (3) / 2) * HGCAL_TB_CELL::FULL_CELL_SIDE;
102  TH1F *h_sum_layer[MAXLAYERS], *h_layer_seven[MAXLAYERS], *h_layer_nineteen[MAXLAYERS], *h_sum_all, *h_seven_all, *h_nineteen_all;
103  TH1F *h_x_layer[MAXLAYERS], *h_y_layer[MAXLAYERS];
104  TH2F *h_x_y_layer[MAXLAYERS];
105  TH2F *HighGain_LowGain_2D;
106  int SPILL = 0, EVENT = 0, LAYER = 0;
107  double AllCells[MAXLAYERS][EVENTSPERSPILL * SPILLS] = {{0.}};
108  double SevenCells[MAXLAYERS][EVENTSPERSPILL * SPILLS] = {{0.}};
109  double NineteenCells[MAXLAYERS][EVENTSPERSPILL * SPILLS] = {{0.}};
110  double X_Layer[MAXLAYERS][EVENTSPERSPILL * SPILLS] = {{0.}};
111  double Y_Layer[MAXLAYERS][EVENTSPERSPILL * SPILLS] = {{0.}};
112  double Time_Stamp[EVENTSPERSPILL * SPILLS] = {0.};
113  double Delta_Time_Stamp[EVENTSPERSPILL * SPILLS] = {0.};
114  double Time_Temp = 0.;
115 };
116 
117 
118 
119 Layer_Sum_Analyzer::Layer_Sum_Analyzer(const edm::ParameterSet& iConfig)
120 {
121 
122  // initialization
123  usesResource("TFileService");
124  edm::Service<TFileService> fs;
125  HGCalTBRecHitCollection_ = consumes<HGCalTBRecHitCollection>(iConfig.getParameter<edm::InputTag>("HGCALTBRECHITS"));
126 
127  //booking the histos
128  for(int layer = 0; layer < MAXLAYERS; layer++){
129  stringstream name, sevenname, nineteenname, Xname, Yname, X_Y_name;
130  name << "AllCells_Sum_Layer" << layer + 1;
131  sevenname << "Cells7_Sum_Layer" << layer + 1;
132  nineteenname << "Cells19_Sum_Layer" << layer + 1;
133  Xname << "X_Layer" << layer + 1;
134  Yname << "Y_Layer" << layer + 1;
135  X_Y_name<<"X_Y_Layer"<< layer + 1;
136 
137  h_sum_layer[layer] = fs->make<TH1F>(name.str().c_str(), name.str().c_str(), 622, -10, 612);
138  h_layer_seven[layer] = fs->make<TH1F>(sevenname.str().c_str(), sevenname.str().c_str(), 622, -10, 612);
139  h_layer_nineteen[layer] = fs->make<TH1F>(nineteenname.str().c_str(), nineteenname.str().c_str(), 622, -10, 612);
140  h_x_layer[layer] = fs->make<TH1F>(Xname.str().c_str(), Xname.str().c_str(),2000,-10.,10. );
141  h_y_layer[layer] = fs->make<TH1F>(Yname.str().c_str(), Yname.str().c_str(),2000,-10.,10. );
142  h_x_y_layer[layer] = fs->make<TH2F>(X_Y_name.str().c_str(), X_Y_name.str().c_str(),2000,-10.,10.,2000,-10.,10. );
143  }
144 
145  h_sum_all = fs->make<TH1F>("AllCells_Sum_AllLayers", "AllCells_Sum_AllLayers", 40010, -10, 40000);
146  h_sum_all->Sumw2();
147  h_seven_all = fs->make<TH1F>("Cells7_Sum_AllLayers", "7Cells_Sum_AllLayers", 40010, -10, 40000);
148  h_seven_all->Sumw2();
149  h_nineteen_all = fs->make<TH1F>("Cells19_Sum_AllLayers", "19Cells_Sum_AllLayers", 40010, -10, 40000);
150  h_nineteen_all->Sumw2();
151  HighGain_LowGain_2D = fs->make<TH2F>("HighGain_LowGain_2D","HighGain_LowGain_2D",4000,0,4000,4000,0,4000);
152 }//constructor ends here
153 
154 
155 Layer_Sum_Analyzer::~Layer_Sum_Analyzer()
156 {
157 
158  // do anything here that needs to be done at desctruction time
159  // (e.g. close files, deallocate resources etc.)
160 
161 }
162 
163 
164 // ------------ method called for each event ------------
165 void
166 Layer_Sum_Analyzer::analyze(const edm::Event& event, const edm::EventSetup& setup)
167 {
168 
169 
170  if(((event.id()).event() - 1) % (EVENTSPERSPILL * MAXLAYERS) == 0 && (event.id()).event() != 1){
171  SPILL++;
172  }
173  LAYER = (((event.id()).event() - 1) / EVENTSPERSPILL) % MAXLAYERS;
174  EVENT = ((event.id()).event() - 1) % EVENTSPERSPILL + EVENTSPERSPILL * SPILL;
175  if(EVENT == 1){
176  Time_Temp = event.time().value();
177  Time_Stamp[EVENT] = Time_Temp;
178  Delta_Time_Stamp[EVENT] = 0.;
179  }
180  else{
181  Time_Stamp[EVENT] = event.time().value();
182  Delta_Time_Stamp[EVENT] = event.time().value() - Time_Temp;
183  Time_Temp = event.time().value();
184  }
185  //opening Rechits
186  edm::Handle<HGCalTBRecHitCollection> Rechits;
187  event.getByToken(HGCalTBRecHitCollection_, Rechits);
188 
189  // looping over each rechit to fill histogram
190  bool FIRST(1);
191  double commonmode, max, max_x, max_y;
192  commonmode = max = max_x = max_y = 0.;
193  int cm_num = 0;
194  for(auto Rechit : *Rechits){
195 
196  //getting electronics ID
197  uint32_t EID = essource_.emap_.detId2eid(Rechit.id());
198  HGCalTBElectronicsId eid(EID);
199 
200  //getting X and Y coordinates
201  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots((Rechit.id()).layer(), (Rechit.id()).sensorIU(), (Rechit.id()).sensorIV(), (Rechit.id()).iu(), (Rechit.id()).iv(), sensorsize);
202  HighGain_LowGain_2D->Fill(Rechit.energyLow(),Rechit.energyHigh());
203  if((Rechit.id()).cellType() != 0) continue;
204 
205  if(FIRST){
206 
207  max = Rechit.energyHigh();
208  max_x = CellCentreXY.first;
209  max_y = CellCentreXY.second;
210  FIRST = 0;
211  }
212 
213  if(Rechit.energyHigh() > max){
214 
215  max = Rechit.energyHigh();
216  max_x = CellCentreXY.first;
217  max_y = CellCentreXY.second;
218  }
219 
220  if((Rechit.energyHigh())/ADCtoMIP[LAYER] <= CMTHRESHOLD){
221 
222  commonmode += Rechit.energyHigh();
223  cm_num++;
224  }
225 
226  }//Rechit loop ends here
227 
228  commonmode /= cm_num;
229 
230  edm::Handle<HGCalTBRecHitCollection> Rechits1;
231  event.getByToken(HGCalTBRecHitCollection_, Rechits1);
232 
233  // looping over each rechit to fill histogram
234  double allcells_sum, sevencells_sum, nineteencells_sum, radius;
235  allcells_sum = sevencells_sum = nineteencells_sum = radius = 0.;
236  double x_tmp = 0., y_tmp = 0.;
237  int num, sevennum, nineteennum;
238  num = sevennum = nineteennum = 0;
239  for(auto Rechit1 : *Rechits1){
240 
241  //getting electronics ID
242  uint32_t EID = essource_.emap_.detId2eid(Rechit1.id());
243  HGCalTBElectronicsId eid(EID);
244 
245  //getting X and Y coordinates
246  CellCentreXY = TheCell.GetCellCentreCoordinatesForPlots((Rechit1.id()).layer(), (Rechit1.id()).sensorIU(), (Rechit1.id()).sensorIV(), (Rechit1.id()).iu(), (Rechit1.id()).iv(), sensorsize);
247 
248  if((Rechit1.id()).cellType() != 0) continue;
249 
250  radius = sqrt( pow(CellCentreXY.first - max_x, 2) + pow(CellCentreXY.second - max_y, 2) );
251 
252  if(((Rechit1.energyHigh() - commonmode)/ADCtoMIP[LAYER]) > CMTHRESHOLD)
253  allcells_sum += (Rechit1.energyHigh() - commonmode) / ADCtoMIP[LAYER];
254  num++;
255 
256  if((radius < maxdist && sevennum < 7) && ((Rechit1.energyHigh() - commonmode)/ADCtoMIP[LAYER] > CMTHRESHOLD)){
257 
258  sevencells_sum += (Rechit1.energyHigh() - commonmode) / ADCtoMIP[LAYER];
259  sevennum++;
260  }
261 
262  if((radius < 1.95 * maxdist && nineteennum < 19) && ((Rechit1.energyHigh() - commonmode)/ADCtoMIP[LAYER] > CMTHRESHOLD)){
263 
264 // nineteencells_sum += (LayerWeight[LAYER]*(Rechit1.energyHigh() - commonmode))/ ADCtoMIP[LAYER];
265  nineteencells_sum += ((Rechit1.energyHigh() - commonmode))/ ADCtoMIP[LAYER];
266 
267  x_tmp += CellCentreXY.first*((Rechit1.energyHigh() - commonmode) / ADCtoMIP[LAYER]);
268  y_tmp += CellCentreXY.second*((Rechit1.energyHigh() - commonmode) / ADCtoMIP[LAYER]);
269  nineteennum++;
270  }
271  }
272 
273  AllCells[LAYER][EVENT] = allcells_sum;
274  SevenCells[LAYER][EVENT] = sevencells_sum;
275  NineteenCells[LAYER][EVENT] = nineteencells_sum;
276  if(nineteennum> 1 && nineteencells_sum > 0) X_Layer[LAYER][EVENT] = x_tmp/nineteencells_sum ;
277  if(nineteennum> 1 && nineteencells_sum > 0) Y_Layer[LAYER][EVENT] = y_tmp/nineteencells_sum ;
278 
279 }// analyze ends here
280 
281 
282 // ------------ method called once each job just before starting event loop ------------
283 void
284 Layer_Sum_Analyzer::beginJob()
285 {
286  HGCalCondObjectTextIO io(0);
287  edm::FileInPath fip(mapfile_);
288  if (!io.load(fip.fullPath(), essource_.emap_)) {
289  throw cms::Exception("Unable to load electronics map");
290  }
291 
292  for(int iii = 0; iii< 16;iii++)
293  ADCtoMIP[iii] = ADCtoMIP[iii]/1.3; // Converting response to 120 GeV protons to MIPs
294 /*
295  for(int iii= 0; iii<16;iii++){
296  LayerWeight[iii] += 0.8;
297  LayerSumWeight += LayerWeight[iii];
298  }
299 */
300 
301 
302 }
303 
304 // ------------ method called once each job just after ending the event loop ------------
305 void
306 Layer_Sum_Analyzer::endJob()
307 {
308 
309  double allcells, sevencells, nineteencells;
310  bool doAllCells, do7Cells, do19Cells;
311  ofstream fs1;
312  fs1.open("/home/daq/CMSSW_8_0_1/src/HGCal/HGC_CERN_Time_Synch.txt");
313  fs1<<"# "<<"Event Num"<<"\t"<<"Time(us)"<<"\t"<<"Delta t(us)"<<"\t"<<"Cluster x[cm]"<<"\t"<<"Cluster y[cm]"<<endl;
314  for(int event = 0; event < (SPILL + 1) * EVENTSPERSPILL; event++){
315  allcells = sevencells = nineteencells = 0.;
316  doAllCells = do7Cells = do19Cells = false;
317 // fs1<<event+1<<"\t"<<200*Time_Stamp[event+1]/1000.<<"\t"<<200*Delta_Time_Stamp[event+1]/1000.<<endl;//division by 1000 to covert from ns --> us
318 
319  for(int layer = 0; layer < MAXLAYERS; layer++){
320  allcells += AllCells[layer][event];
321  sevencells += SevenCells[layer][event];
322  nineteencells += NineteenCells[layer][event];
323  }
324 
325  if(ELECTRONS || !PIONS){
326  if(allcells >= ALLCELLS_THRESHOLD){
327  h_sum_all->Fill(allcells);
328  doAllCells = true;
329  }
330  if(sevencells >= SEVENCELLS_THRESHOLD){
331  h_seven_all->Fill(sevencells);
332  do7Cells = true;
333  }
334  if(nineteencells >= NINETEENCELLS_THRESHOLD){
335  h_nineteen_all->Fill(nineteencells/LayerSumWeight);
336  do19Cells = true;
337  }
338  }
339 
340  if(PIONS){
341  if(allcells <= ALLCELLS_THRESHOLD && allcells >= PION_ALLCELLS_THRESHOLD){
342  h_sum_all->Fill(allcells);
343  doAllCells = true;
344  }
345  if(sevencells <= SEVENCELLS_THRESHOLD && allcells >= PION_7CELLS_THRESHOLD){
346  h_seven_all->Fill(sevencells);
347  do7Cells = true;
348  }
349  if(nineteencells <= NINETEENCELLS_THRESHOLD && allcells >= PION_19CELLS_THRESHOLD){
350  h_nineteen_all->Fill(nineteencells/LayerSumWeight);
351  do19Cells = true;
352  }
353  }
354 
355 
356  for(int layer = 0; layer < MAXLAYERS; layer++){
357  if(doAllCells){
358  h_sum_layer[layer]->Fill(AllCells[layer][event]);
359  }
360  if(do7Cells){
361  h_layer_seven[layer]->Fill(SevenCells[layer][event]);
362  }
363  if(do19Cells){
364  h_layer_nineteen[layer]->Fill(NineteenCells[layer][event]);
365  h_x_layer[layer]->Fill(X_Layer[layer][event]);
366  h_y_layer[layer]->Fill(Y_Layer[layer][event]);
367  h_x_y_layer[layer]->Fill(X_Layer[layer][event],Y_Layer[layer][event]);
368  cout<<endl<<" "<<layer<<" "<<X_Layer[layer][event]<<" "<<Y_Layer[layer][event]<<endl;
369  fs1<<event+1<<"\t"<<200*Time_Stamp[event+1]/1000.<<"\t"<<200*Delta_Time_Stamp[event+1]/1000.<<"\t"<<X_Layer[layer][event]<<"\t"<<Y_Layer[layer][event]<<endl;
370 // fs1<<event<<" "<<layer<<" "<<X_Layer[layer][event]<<" "<<Y_Layer[layer][event]<<" "<<Layer_Z[layer]<<" "<<endl;
371  }
372  }
373  }
374 fs1.close();
375 }
376 
377 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
378 void
379 Layer_Sum_Analyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
380 {
381  //The following says we do not know what parameters are allowed so do no validation
382  // Please change this to state exactly what you do use, even if it is no parameters
383  edm::ParameterSetDescription desc;
384  desc.setUnknown();
385  descriptions.addDefault(desc);
386 }
387 
388 //define this as a plug-in
389 DEFINE_FWK_MODULE(Layer_Sum_Analyzer);
DEFINE_FWK_MODULE(Pedestals)
const double PION_ALLCELLS_THRESHOLD
const double PION_19CELLS_THRESHOLD
edm::Service< TFileService > fs
double LayerWeight[16]
const bool PIONS(0)
const double PION_7CELLS_THRESHOLD
const double ALLCELLS_THRESHOLD
#define MAXLAYERS
const double NINETEENCELLS_THRESHOLD
const bool ELECTRONS(1)
#define EVENTSPERSPILL
double LayerSumWeight
const double SEVENCELLS_THRESHOLD
#define SPILLS
provides the conversion between electronics Id to DetId
double ADCtoMIP[16]
double Layer_Z[16]
const int CMTHRESHOLD