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