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