CMS 3D CMS Logo

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