CMS 3D CMS Logo

HcalTB02Analysis.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HcalTestBeam
4 // Class : HcalTB02Analysis
5 //
6 // Implementation:
7 // Main analysis class for Hcal Test Beam 2002 Analysis
8 //
9 // Original Author:
10 // Created: Sun May 21 10:14:34 CEST 2006
11 //
12 
13 // system include files
14 #include <cmath>
15 #include <iostream>
16 #include <iomanip>
17 #include <memory>
18 #include <map>
19 #include <vector>
20 #include <string>
21 
22 // user include files
23 // to retreive hits
33 
38 
41 #include "HcalTB02Histo.h"
42 
43 #include "G4HCofThisEvent.hh"
44 #include "G4SDManager.hh"
45 #include "G4Step.hh"
46 #include "G4Track.hh"
47 #include "G4ThreeVector.hh"
48 #include "G4VProcess.hh"
49 
50 #include "CLHEP/Random/RandGaussQ.h"
51 #include "CLHEP/Units/GlobalSystemOfUnits.h"
52 #include "CLHEP/Units/GlobalPhysicalConstants.h"
53 #include "Randomize.hh"
54 
55 namespace CLHEP {
56  class HepRandomEngine;
57 }
58 
59 class HcalTB02Analysis : public SimProducer, public Observer<const BeginOfEvent*>, public Observer<const EndOfEvent*> {
60 public:
62  ~HcalTB02Analysis() override;
63 
64  void produce(edm::Event&, const edm::EventSetup&) override;
65 
66 private:
67  HcalTB02Analysis(const HcalTB02Analysis&) = delete; // stop default
68  const HcalTB02Analysis& operator=(const HcalTB02Analysis&) = delete;
69 
70  // observer methods
71  void update(const BeginOfEvent* evt) override;
72  void update(const EndOfEvent* evt) override;
73 
75  void clear();
76  void finish();
77 
78 private:
79  // Private Tuples
81 
82  // to read from parameter set
83  bool hcalOnly;
85  std::vector<std::string> names;
86 
87  //To be saved
88  std::map<int, float> energyInScints, energyInCrystals;
89  std::map<int, float> primaries;
91  double eta, phi, pInit, incidentEnergy;
92  float SEnergy, E7x7Matrix, E5x5Matrix;
93  float SEnergyN, E7x7MatrixN, E5x5MatrixN;
94  int maxTime;
96  float xSEnergy, xSEnergyN;
97  float xE3x3Matrix, xE5x5Matrix;
98  float xE3x3MatrixN, xE5x5MatrixN;
99 };
100 
101 //
102 // constructors and destructor
103 //
104 
106  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("HcalTB02Analysis");
107  hcalOnly = m_Anal.getUntrackedParameter<bool>("HcalClusterOnly", true);
108  names = m_Anal.getParameter<std::vector<std::string> >("Names");
109 
110  produces<HcalTB02HistoClass>();
111 
112  edm::LogInfo("HcalTBSim") << "HcalTB02Analysis:: Initialised as observer of "
113  << "BeginOfJob/BeginOfEvent/EndOfEvent with "
114  << "Parameter values:\n \thcalOnly = " << hcalOnly;
115 
116  histo = new HcalTB02Histo(m_Anal);
117 }
118 
120  finish();
121  delete histo;
122 }
123 
124 //
125 // member functions
126 //
127 
129  std::unique_ptr<HcalTB02HistoClass> product(new HcalTB02HistoClass);
130  fillEvent(*product);
131  e.put(std::move(product));
132 }
133 
135  edm::LogInfo("HcalTBSim") << "HcalTB02Analysis: =====> Begin of event = " << (*evt)()->GetEventID();
136  clear();
137 }
138 
140  CLHEP::HepRandomEngine* engine = G4Random::getTheEngine();
141  CLHEP::RandGaussQ randGauss(*engine);
142 
143  // Look for the Hit Collection
144  LogDebug("HcalTBSim") << "HcalTB02Analysis::Fill event " << (*evt)()->GetEventID();
145 
146  // access to the G4 hit collections
147  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
148  int ihit = 0;
149 
150  // HCAL
151  std::string sd = names[0];
152  int HCHCid = G4SDManager::GetSDMpointer()->GetCollectionID(sd);
153  CaloG4HitCollection* theHCHC = (CaloG4HitCollection*)allHC->GetHC(HCHCid);
154  LogDebug("HcalTBSim") << "HcalTB02Analysis :: Hit Collection for " << sd << " of ID " << HCHCid << " is obtained at "
155  << theHCHC;
156 
157  int nentries = 0;
158  nentries = theHCHC->entries();
159  if (nentries == 0)
160  return;
161 
162  int xentries = 0;
163  int XTALSid = 0;
164  CaloG4HitCollection* theXTHC = nullptr;
165 
166  if (!hcalOnly) {
167  // XTALS
168  sd = names[1];
169  XTALSid = G4SDManager::GetSDMpointer()->GetCollectionID(sd);
170  theXTHC = (CaloG4HitCollection*)allHC->GetHC(XTALSid);
171  LogDebug("HcalTBSim") << "HcalTB02Analysis :: Hit Collection for " << sd << " of ID " << XTALSid
172  << " is obtained at " << theXTHC;
173  xentries = theXTHC->entries();
174  if (xentries == 0)
175  return;
176  }
177 
178  LogDebug("HcalTBSim") << "HcalTB02Analysis :: There are " << nentries << " HCal hits, and" << xentries
179  << " xtal hits";
180 
181  float ETot = 0., xETot = 0.;
182  int scintID = 0, xtalID = 0;
183 
184  // HCAL
186 
187  if (HCHCid >= 0 && theHCHC != nullptr) {
188  for (ihit = 0; ihit < nentries; ihit++) {
189  CaloG4Hit* aHit = (*theHCHC)[ihit];
190  scintID = aHit->getUnitID();
191  int layer = org->getlayerID(scintID);
192  float enEm = aHit->getEM();
193  float enhad = aHit->getHadr();
194 
195  if (layer == 0) {
196  enEm = enEm / 2.;
197  enhad = enhad / 2.;
198  }
199 
200  energyInScints[scintID] += enEm + enhad;
201  primaries[aHit->getTrackID()] += enEm + enhad;
202  float time = aHit->getTimeSliceID();
203  if (time > maxTime)
204  maxTime = (int)time;
205  histo->fillAllTime(time);
206  }
207 
208  //
209  // Profile
210  //
211 
212  float TowerEne[8][18], TowerEneCF[8][18], LayerEne[19], EnRing[100];
213  for (int iphi = 0; iphi < 8; iphi++) {
214  for (int jeta = 0; jeta < 18; jeta++) {
215  TowerEne[iphi][jeta] = 0.;
216  TowerEneCF[iphi][jeta] = 0.;
217  }
218  }
219 
220  for (int ilayer = 0; ilayer < 19; ilayer++)
221  LayerEne[ilayer] = 0.;
222  for (int iring = 0; iring < 100; iring++)
223  EnRing[iring] = 0.;
224 
225  for (std::map<int, float>::iterator is = energyInScints.begin(); is != energyInScints.end(); is++) {
226  ETot = (*is).second;
227 
228  int layer = org->getlayerID((*is).first);
229 
230  if ((layer != 17) && (layer != 18)) {
231  float eta = org->getetaID((*is).first);
232  float phi = org->getphiID((*is).first);
233 
234  SEnergy += ETot;
235  TowerEne[(int)phi][(int)eta] += ETot;
236 
237  TowerEneCF[(int)phi][(int)eta] += ETot * (1. + 0.1 * randGauss.fire());
238  double dR = 0.08727 * std::sqrt((eta - 8.) * (eta - 8.) + (phi - 3.) * (phi - 3.));
239  EnRing[(int)(dR / 0.01)] += ETot;
240  }
241 
242  LayerEne[layer] += ETot;
243  }
244 
245  for (int ilayer = 0; ilayer < 19; ilayer++) {
246  histo->fillProfile(ilayer, LayerEne[ilayer] / GeV);
247  }
248 
249  for (int iring = 0; iring < 100; iring++)
250  histo->fillTransProf(0.01 * iring + 0.005, EnRing[iring] / GeV);
251 
252  for (int iphi = 0; iphi < 8; iphi++) {
253  for (int jeta = 0; jeta < 18; jeta++) {
254  SEnergyN += TowerEneCF[iphi][jeta] + 3. * randGauss.fire(); // QGSP
255 
256  double Rand = 3. * randGauss.fire(); // QGSP
257 
258  if ((iphi >= 0) && (iphi < 7)) {
259  if ((jeta >= 5) && (jeta < 12)) {
260  E7x7Matrix += TowerEne[iphi][jeta];
261  E7x7MatrixN += TowerEneCF[iphi][jeta] + Rand;
262 
263  if ((iphi >= 1) && (iphi < 6)) {
264  if ((jeta >= 6) && (jeta < 11)) {
265  E5x5Matrix += TowerEne[iphi][jeta];
266  E5x5MatrixN += TowerEneCF[iphi][jeta] + Rand;
267  }
268  }
269  }
270  }
271  }
272  }
273 
274  //
275  // Find Primary info:
276  //
277  int trackID = 0;
278  G4PrimaryParticle* thePrim = nullptr;
279  G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
280  LogDebug("HcalTBSim") << "HcalTB02Analysis :: Event has " << nvertex << " vertex";
281  if (nvertex == 0)
282  edm::LogWarning("HcalTBSim") << "HcalTB02Analysis:: End Of Event "
283  << "ERROR: no vertex";
284 
285  for (int i = 0; i < nvertex; i++) {
286  G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
287  if (avertex == nullptr) {
288  edm::LogWarning("HcalTBSim") << "HcalTB02Analysis:: End Of Event "
289  << "ERROR: pointer to vertex = 0";
290  } else {
291  int npart = avertex->GetNumberOfParticle();
292  LogDebug("HcalTBSim") << "HcalTB02Analysis::Vertex number :" << i << " with " << npart << " particles";
293  if (thePrim == nullptr)
294  thePrim = avertex->GetPrimary(trackID);
295  }
296  }
297 
298  double px = 0., py = 0., pz = 0.;
299 
300  if (thePrim != nullptr) {
301  px = thePrim->GetPx();
302  py = thePrim->GetPy();
303  pz = thePrim->GetPz();
304  pInit = std::sqrt(pow(px, 2.) + pow(py, 2.) + pow(pz, 2.));
305  if (pInit == 0) {
306  edm::LogWarning("HcalTBSim") << "HcalTB02Analysis:: End Of Event "
307  << " ERROR: primary has p=0 ";
308  } else {
309  float costheta = pz / pInit;
310  float theta = acos(std::min(std::max(costheta, float(-1.)), float(1.)));
311  eta = -log(tan(theta / 2));
312  if (px != 0)
313  phi = atan(py / px);
314  }
315  particleType = thePrim->GetPDGcode();
316  } else {
317  LogDebug("HcalTBSim") << "HcalTB02Analysis:: End Of Event ERROR: could"
318  << " not find primary ";
319  }
320 
321  CaloG4Hit* firstHit = (*theHCHC)[0];
322  incidentEnergy = (firstHit->getIncidentEnergy() / GeV);
323 
324  } // number of Hits > 0
325 
326  if (!hcalOnly) {
327  // XTALS
328 
329  if (XTALSid >= 0 && theXTHC != nullptr) {
330  for (int xihit = 0; xihit < xentries; xihit++) {
331  CaloG4Hit* xaHit = (*theXTHC)[xihit];
332 
333  float xenEm = xaHit->getEM();
334  float xenhad = xaHit->getHadr();
335  xtalID = xaHit->getUnitID();
336 
337  energyInCrystals[xtalID] += xenEm + xenhad;
338  }
339 
340  float xCrysEne[7][7];
341  for (int irow = 0; irow < 7; irow++) {
342  for (int jcol = 0; jcol < 7; jcol++) {
343  xCrysEne[irow][jcol] = 0.;
344  }
345  }
346 
347  for (std::map<int, float>::iterator is = energyInCrystals.begin(); is != energyInCrystals.end(); is++) {
348  int xtalID = (*is).first;
349  xETot = (*is).second;
350 
351  int irow = (int)(xtalID / 100.);
352  int jcol = (int)(xtalID - 100. * irow);
353 
354  xSEnergy += xETot;
355  xCrysEne[irow][jcol] = xETot;
356 
357  float dR = std::sqrt(0.01619 * 0.01619 * (jcol - 3) * (jcol - 3) + 0.01606 * 0.01606 * (irow - 3) * (irow - 3));
358  histo->fillTransProf(dR, xETot * 1.05);
359 
360  if ((irow > 0) && (irow < 6)) {
361  if ((jcol > 0) && (jcol < 6)) {
362  xE5x5Matrix += xCrysEne[irow][jcol];
363  xE5x5MatrixN += xCrysEne[irow][jcol] + 108.5 * randGauss.fire();
364 
365  if ((irow > 1) && (irow < 5)) {
366  if ((jcol > 1) && (jcol < 5)) {
367  xE3x3Matrix += xCrysEne[irow][jcol];
368  xE3x3MatrixN += xCrysEne[irow][jcol] + 108.5 * randGauss.fire();
369  }
370  }
371  }
372  }
373  }
374 
375  if (!hcalOnly) {
376  // assert(theXTHC);
377  if (theXTHC != nullptr) {
378  CaloG4Hit* xfirstHit = (*theXTHC)[0];
379  xIncidentEnergy = xfirstHit->getIncidentEnergy() / GeV;
380  }
381  }
382 
383  } // number of Hits > 0
384  }
385 
386  int iEvt = (*evt)()->GetEventID();
387  if (iEvt < 10)
388  std::cout << " Event " << iEvt << std::endl;
389  else if ((iEvt < 100) && (iEvt % 10 == 0))
390  std::cout << " Event " << iEvt << std::endl;
391  else if ((iEvt < 1000) && (iEvt % 100 == 0))
392  std::cout << " Event " << iEvt << std::endl;
393  else if ((iEvt < 10000) && (iEvt % 1000 == 0))
394  std::cout << " Event " << iEvt << std::endl;
395 
396  delete org;
397 }
398 
400  //Beam information
401  product.set_Nprim(float(primaries.size()));
402  product.set_partType(particleType);
403  product.set_Einit(pInit / GeV);
404  product.set_eta(eta);
405  product.set_phi(phi);
406  product.set_Eentry(incidentEnergy);
407 
408  //Calorimeter energy
409  product.set_ETot(SEnergy / GeV);
410  product.set_E7x7(E7x7Matrix / GeV);
411  product.set_E5x5(E5x5Matrix / GeV);
412  product.set_ETotN(SEnergyN / GeV);
413  product.set_E7x7N(E7x7MatrixN / GeV);
414  product.set_E5x5N(E5x5MatrixN / GeV);
415  product.set_NUnit(float(energyInScints.size()));
416  product.set_Ntimesli(float(maxTime));
417 
418  //crystal information
419  product.set_xEentry(xIncidentEnergy);
420  product.set_xNUnit(float(energyInCrystals.size()));
421  product.set_xETot(xSEnergy / GeV);
422  product.set_xETotN(xSEnergyN / GeV);
423  product.set_xE5x5(xE5x5Matrix / GeV);
424  product.set_xE3x3(xE3x3Matrix / GeV);
425  product.set_xE5x5N(xE5x5MatrixN / GeV);
426  product.set_xE3x3N(xE3x3MatrixN / GeV);
427 }
428 
430  primaries.clear();
431  particleType = 0;
432  pInit = eta = phi = incidentEnergy = 0;
433 
434  SEnergy = E7x7Matrix = E5x5Matrix = SEnergyN = 0;
435  E7x7MatrixN = E5x5MatrixN = 0;
436  energyInScints.clear();
437  maxTime = 0;
438 
439  xIncidentEnergy = 0;
440  energyInCrystals.clear();
441  xSEnergy = xSEnergyN = xE5x5Matrix = xE3x3Matrix = 0;
442  xE5x5MatrixN = xE3x3MatrixN = 0;
443 }
444 
446  /*
447  //Profile
448  std::ofstream oFile;
449  oFile.open("profile.dat");
450  float st[19] = {0.8,
451  0.4, 0.4, 0.4, 0.4, 0.4,
452  0.4, 0.4, 0.4, 0.4, 0.4,
453  0.4, 0.4, 0.4, 0.4, 0.4,
454  0.8, 1.0, 1.0};
455 
456  //cm of material (brass) in front of scintillator layer i:
457 
458  float w[19] = {7.45, //iron !
459  6.00, 6.00, 6.00, 6.00, 6.00, //brass
460  6.00, 6.00, 6.00, 6.00, 6.60, //brass
461  6.60, 6.60, 6.60, 6.60, 6.60, //brass
462  8.90, 20.65, 19.5}; //brass,iron !
463 
464  for (int ilayer = 0; ilayer<19; ilayer++) {
465 
466  // Histogram mean and sigma calculated from the ROOT histos
467  edm::LogInfo("HcalTBSim") << "Layer number: " << ilayer << " Mean = "
468  << histo->getMean(ilayer) << " sigma = "
469  << histo->getRMS(ilayer) << " LThick= "
470  << w[ilayer] << " SThick= " << st[ilayer];
471 
472  oFile << ilayer << " " << histo->getMean(ilayer) << " "
473  << histo->getRMS(ilayer) << " " << w[ilayer] << " " << st[ilayer]
474  << std::endl;
475 
476  }
477  oFile.close();
478  */
479 }
480 
#define LogDebug(id)
void set_xE3x3N(float v)
std::string fileNameTuple
void set_E5x5N(float v)
T getParameter(std::string const &) const
void set_Ntimesli(float v)
T getUntrackedParameter(std::string const &, T const &) const
void set_NUnit(float v)
#define DEFINE_SIMWATCHER(type)
void set_Eentry(float v)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
const double GeV
Definition: MathUtil.h:16
std::map< int, float > energyInScints
void set_xEentry(float v)
void set_xETotN(float v)
void set_Nprim(float v)
void set_E7x7(float v)
~HcalTB02Analysis() override
def fillEvent(tree, event)
Definition: ntuple.py:18
void set_ETotN(float v)
Geom::Theta< T > theta() const
double getIncidentEnergy() const
Definition: CaloG4Hit.h:61
double npart
Definition: HydjetWrapper.h:46
void set_Einit(float v)
void produce(edm::Event &, const edm::EventSetup &) override
void set_xETot(float v)
void set_E7x7N(float v)
const std::string names[nVars_]
void fillEvent(HcalTB02HistoClass &)
void set_xE3x3(float v)
HcalTB02Analysis(const edm::ParameterSet &p)
void update(const BeginOfEvent *evt) override
This routine will be called when the appropriate signal arrives.
HcalTB02Histo * histo
void set_xE5x5N(float v)
T sqrt(T t)
Definition: SSEVec.h:19
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:151
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
T min(T a, T b)
Definition: MathUtil.h:58
std::map< int, float > primaries
int getTrackID() const
Definition: CaloG4Hit.h:64
double sd
void set_E5x5(float v)
void set_partType(float v)
int getTimeSliceID() const
Definition: CaloG4Hit.h:67
double getEM() const
Definition: CaloG4Hit.h:55
std::vector< std::string > names
#define update(a, b)
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
void set_ETot(float v)
void set_xE5x5(float v)
uint32_t getUnitID() const
Definition: CaloG4Hit.h:65
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
def move(src, dest)
Definition: eostools.py:511
double getHadr() const
Definition: CaloG4Hit.h:58
void set_xNUnit(float v)