CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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/Random/Randomize.h>
52 #include <CLHEP/Units/GlobalSystemOfUnits.h>
53 #include <CLHEP/Units/GlobalPhysicalConstants.h>
54 
55 //#define EDM_ML_DEBUG
56 
57 namespace CLHEP {
58  class HepRandomEngine;
59 }
60 
61 class HcalTB02Analysis : public SimProducer, public Observer<const BeginOfEvent*>, public Observer<const EndOfEvent*> {
62 public:
64  HcalTB02Analysis(const HcalTB02Analysis&) = delete; // stop default
65  const HcalTB02Analysis& operator=(const HcalTB02Analysis&) = delete;
66  ~HcalTB02Analysis() override;
67 
68  void produce(edm::Event&, const edm::EventSetup&) override;
69 
70 private:
71  // observer methods
72  void update(const BeginOfEvent* evt) override;
73  void update(const EndOfEvent* evt) override;
74 
76  void clear();
77  void finish();
78 
79 private:
80  // Private Tuples
81  std::unique_ptr<HcalTB02Histo> histo;
82 
83  // to read from parameter set
84  bool hcalOnly;
86  std::vector<std::string> names;
87 
88  //To be saved
89  std::map<int, float> energyInScints, energyInCrystals;
90  std::map<int, float> primaries;
95  int maxTime;
100 };
101 
102 //
103 // constructors and destructor
104 //
105 
107  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("HcalTB02Analysis");
108  hcalOnly = m_Anal.getUntrackedParameter<bool>("HcalClusterOnly", true);
109  names = m_Anal.getParameter<std::vector<std::string> >("Names");
110 
111  produces<HcalTB02HistoClass>();
112 
113  edm::LogVerbatim("HcalTBSim") << "HcalTB02Analysis:: Initialised as observer of "
114  << "BeginOfJob/BeginOfEvent/EndOfEvent with "
115  << "Parameter values:\n \thcalOnly = " << hcalOnly;
116 
117  histo = std::make_unique<HcalTB02Histo>(m_Anal);
118 }
119 
121 
122 //
123 // member functions
124 //
125 
127  std::unique_ptr<HcalTB02HistoClass> product(new HcalTB02HistoClass);
128  fillEvent(*product);
129  e.put(std::move(product));
130 }
131 
133 #ifdef EDM_ML_DEBUG
134  edm::LogVerbatim("HcalTBSim") << "HcalTB02Analysis: =====> Begin of event = " << (*evt)()->GetEventID();
135 #endif
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
185  std::unique_ptr<HcalTB02HcalNumberingScheme> org(new HcalTB02HcalNumberingScheme());
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  edm::LogVerbatim("HcalTBSim") << " Event " << iEvt;
389  else if ((iEvt < 100) && (iEvt % 10 == 0))
390  edm::LogVerbatim("HcalTBSim") << " Event " << iEvt;
391  else if ((iEvt < 1000) && (iEvt % 100 == 0))
392  edm::LogVerbatim("HcalTBSim") << " Event " << iEvt;
393  else if ((iEvt < 10000) && (iEvt % 1000 == 0))
394  edm::LogVerbatim("HcalTBSim") << " Event " << iEvt;
395 }
396 
398  //Beam information
399  product.set_Nprim(float(primaries.size()));
400  product.set_partType(particleType);
401  product.set_Einit(pInit / GeV);
402  product.set_eta(eta);
403  product.set_phi(phi);
404  product.set_Eentry(incidentEnergy);
405 
406  //Calorimeter energy
407  product.set_ETot(SEnergy / GeV);
408  product.set_E7x7(E7x7Matrix / GeV);
409  product.set_E5x5(E5x5Matrix / GeV);
410  product.set_ETotN(SEnergyN / GeV);
411  product.set_E7x7N(E7x7MatrixN / GeV);
412  product.set_E5x5N(E5x5MatrixN / GeV);
413  product.set_NUnit(float(energyInScints.size()));
414  product.set_Ntimesli(float(maxTime));
415 
416  //crystal information
417  product.set_xEentry(xIncidentEnergy);
418  product.set_xNUnit(float(energyInCrystals.size()));
419  product.set_xETot(xSEnergy / GeV);
420  product.set_xETotN(xSEnergyN / GeV);
421  product.set_xE5x5(xE5x5Matrix / GeV);
422  product.set_xE3x3(xE3x3Matrix / GeV);
423  product.set_xE5x5N(xE5x5MatrixN / GeV);
424  product.set_xE3x3N(xE3x3MatrixN / GeV);
425 }
426 
428  primaries.clear();
429  particleType = 0;
430  pInit = eta = phi = incidentEnergy = 0;
431 
433  E7x7MatrixN = E5x5MatrixN = 0;
434  energyInScints.clear();
435  maxTime = 0;
436 
437  xIncidentEnergy = 0;
438  energyInCrystals.clear();
441 }
442 
444  /*
445  //Profile
446  std::ofstream oFile;
447  oFile.open("profile.dat");
448  float st[19] = {0.8,
449  0.4, 0.4, 0.4, 0.4, 0.4,
450  0.4, 0.4, 0.4, 0.4, 0.4,
451  0.4, 0.4, 0.4, 0.4, 0.4,
452  0.8, 1.0, 1.0};
453 
454  //cm of material (brass) in front of scintillator layer i:
455 
456  float w[19] = {7.45, //iron !
457  6.00, 6.00, 6.00, 6.00, 6.00, //brass
458  6.00, 6.00, 6.00, 6.00, 6.60, //brass
459  6.60, 6.60, 6.60, 6.60, 6.60, //brass
460  8.90, 20.65, 19.5}; //brass,iron !
461 
462  for (int ilayer = 0; ilayer<19; ilayer++) {
463 
464  // Histogram mean and sigma calculated from the ROOT histos
465  edm::LogVerbatim("HcalTBSim") << "Layer number: " << ilayer << " Mean = "
466  << histo->getMean(ilayer) << " sigma = "
467  << histo->getRMS(ilayer) << " LThick= "
468  << w[ilayer] << " SThick= " << st[ilayer];
469 
470  oFile << ilayer << " " << histo->getMean(ilayer) << " "
471  << histo->getRMS(ilayer) << " " << w[ilayer] << " " << st[ilayer]
472  << std::endl;
473 
474  }
475  oFile.close();
476  */
477 }
478 
void set_xE3x3N(float v)
std::string fileNameTuple
Log< level::Info, true > LogVerbatim
void set_E5x5N(float v)
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:133
const double GeV
Definition: MathUtil.h:16
static std::vector< std::string > checklist log
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
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)
const HcalTB02Analysis & operator=(const HcalTB02Analysis &)=delete
void produce(edm::Event &, const edm::EventSetup &) override
void set_xETot(float v)
void set_E7x7N(float v)
constexpr std::array< uint8_t, layerIndexSize > layer
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.
void set_xE5x5N(float v)
std::unique_ptr< HcalTB02Histo > histo
T sqrt(T t)
Definition: SSEVec.h:19
def move
Definition: eostools.py:511
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
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void set_E5x5(float v)
void set_partType(float v)
int getTimeSliceID() const
Definition: CaloG4Hit.h:68
double getEM() const
Definition: CaloG4Hit.h:55
std::vector< std::string > names
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
void set_ETot(float v)
void set_xE5x5(float v)
uint32_t getUnitID() const
Definition: CaloG4Hit.h:66
Log< level::Warning, false > LogWarning
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
double getHadr() const
Definition: CaloG4Hit.h:58
std::map< int, float > energyInCrystals
#define LogDebug(id)
void set_xNUnit(float v)