CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalTB04Analysis.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HcalTestBeam
4 // Class : HcalTB04Analysis
5 //
6 // Implementation:
7 // Main analysis class for Hcal Test Beam 2004 Analysis
8 //
9 // Original Author:
10 // Created: Tue May 16 10:14:34 CEST 2006
11 // $Id: HcalTB04Analysis.cc,v 1.1 2012/07/02 04:44:40 sunanda Exp $
12 //
13 
14 // system include files
15 #include <cmath>
16 #include <iostream>
17 #include <iomanip>
18 
19 // user include files
21 
26 
27 // to retreive hits
37 
44 
47 #include "CLHEP/Random/RandGaussQ.h"
49 
50 #include "G4SDManager.hh"
51 #include "G4VProcess.hh"
52 #include "G4HCofThisEvent.hh"
53 #include "CLHEP/Units/GlobalSystemOfUnits.h"
54 #include "CLHEP/Units/GlobalPhysicalConstants.h"
55 
56 //
57 // constructors and destructor
58 //
59 
61  histo(0) {
62 
63  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("HcalTB04Analysis");
64  hcalOnly = m_Anal.getParameter<bool>("HcalOnly");
65  mode = m_Anal.getParameter<int>("Mode");
66  type = m_Anal.getParameter<int>("Type");
67  ecalNoise = m_Anal.getParameter<double>("EcalNoise");
68  scaleHB0 = m_Anal.getParameter<double>("ScaleHB0");
69  scaleHB16 = m_Anal.getParameter<double>("ScaleHB16");
70  scaleHO = m_Anal.getParameter<double>("ScaleHO");
71  scaleHE0 = m_Anal.getParameter<double>("ScaleHE0");
72  names = m_Anal.getParameter<std::vector<std::string> >("Names");
73  beamOffset =-m_Anal.getParameter<double>("BeamPosition")*cm;
74  double fMinEta = m_Anal.getParameter<double>("MinEta");
75  double fMaxEta = m_Anal.getParameter<double>("MaxEta");
76  double fMinPhi = m_Anal.getParameter<double>("MinPhi");
77  double fMaxPhi = m_Anal.getParameter<double>("MaxPhi");
78  double beamEta = (fMaxEta+fMinEta)/2.;
79  double beamPhi = (fMaxPhi+fMinPhi)/2.;
80  double beamThet= 2*atan(exp(-beamEta));
81  if (beamPhi < 0) beamPhi += twopi;
82  iceta = (int)(beamEta/0.087) + 1;
83  icphi = (int)(fabs(beamPhi)/0.087) + 5;
84  if (icphi > 72) icphi -= 73;
85 
86  produces<PHcalTB04Info>();
87 
88  beamline_RM = new G4RotationMatrix;
89  beamline_RM->rotateZ(-beamPhi);
90  beamline_RM->rotateY(-beamThet);
91 
92  edm::LogInfo("HcalTBSim") << "HcalTB04:: Initialised as observer of BeginOf"
93  << "Job/BeginOfRun/BeginOfEvent/G4Step/EndOfEvent"
94  << " with Parameter values:\n \thcalOnly = "
95  << hcalOnly << "\tecalNoise = " << ecalNoise
96  << "\n\tMode = " << mode << " (0: HB2 Standard; "
97  << "1:HB2 Segmented)" << "\tType = " << type
98  << " (0: HB; 1 HE; 2 HB+HE)\n\tbeamOffset = "
99  << beamOffset << "\ticeta = " << iceta
100  << "\ticphi = " << icphi << "\n\tbeamline_RM = "
101  << *beamline_RM;
102 
103  init();
104 
105  myQie = new HcalQie(p);
106  histo = new HcalTB04Histo(m_Anal);
107 }
108 
110 
111  edm::LogInfo("HcalTBSim") << "\n --------> Total number of selected entries"
112  << " : " << count << "\nPointers:: QIE " << myQie
113  << " Histo " << histo;
114  if (myQie) {
115  delete myQie;
116  myQie = 0;
117  }
118  if (histo) {
119  delete histo;
120  histo = 0;
121  }
122 }
123 
124 //
125 // member functions
126 //
127 
129 
130  std::auto_ptr<PHcalTB04Info> product(new PHcalTB04Info);
131  fillEvent(*product);
132  e.put(product);
133 }
134 
136 
138  nTower = idTower.size();
139  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis:: Save information from "
140  << nTower << " HCal towers";
141  idHcal.reserve(nTower);
142  for (int i=0; i<nTower; i++) {
143  int id = unitID(idTower[i]);
144  idHcal.push_back(id);
145  LogDebug("HcalTBSim") << "\tTower[" << i << "] Original " << std::hex
146  << idTower[i] << " Stored " << idHcal[i] << std::dec;
147  }
148 
149  if (!hcalOnly) {
150  int det = 10;
151  uint32_t id1;
152  nCrystal = 0;
153  for (int lay=1; lay<8; lay++) {
154  for (int icr=1; icr<8; icr++) {
155  id1 = HcalTestNumbering::packHcalIndex(det,0,1,icr,lay,1);
156  int id = unitID(id1);
157  idEcal.push_back(id1);
158  idXtal.push_back(id);
159  nCrystal++;
160  }
161  }
162  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis:: Save information from "
163  << nCrystal << " ECal Crystals";
164  for (int i=0; i<nCrystal; i++) {
165  LogDebug("HcalTBSim") << "\tCrystal[" << i << "] Original " << std::hex
166  << idEcal[i] << " Stored " << idXtal[i] <<std::dec;
167  }
168  }
169  // Profile vectors
170  eseta.reserve(5);
171  eqeta.reserve(5);
172  esphi.reserve(3);
173  eqphi.reserve(3);
174  eslay.reserve(20);
175  eqlay.reserve(20);
176  for (int i=0; i<5; i++) {
177  eseta.push_back(0.);
178  eqeta.push_back(0.);
179  }
180  for (int i=0; i<3; i++) {
181  esphi.push_back(0.);
182  eqphi.push_back(0.);
183  }
184  for (int i=0; i<20; i++) {
185  eslay.push_back(0.);
186  eqlay.push_back(0.);
187  }
188 
189  // counter
190  count = 0;
191  evNum = 0;
192  clear();
193 }
194 
196 
197  int irun = (*run)()->GetRunID();
198  edm::LogInfo("HcalTBSim") <<" =====> Begin of Run = " << irun;
199 
200  G4SDManager* sd = G4SDManager::GetSDMpointerIfExist();
201  if (sd != 0) {
202  std::string sdname = names[0];
203  G4VSensitiveDetector* aSD = sd->FindSensitiveDetector(sdname);
204  if (aSD==0) {
205  edm::LogWarning("HcalTBSim") << "HcalTB04Analysis::beginOfRun: No SD"
206  << " with name " << sdname << " in this "
207  << "Setup";
208  } else {
209  HCalSD* theCaloSD = dynamic_cast<HCalSD*>(aSD);
210  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis::beginOfRun: Finds SD "
211  << "with name " << theCaloSD->GetName()
212  << " in this Setup";
214  theCaloSD->setNumberingScheme(org);
215  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis::beginOfRun: set a "
216  << "new numbering scheme";
217  }
218  if (!hcalOnly) {
219  sdname = names[1];
220  aSD = sd->FindSensitiveDetector(sdname);
221  if (aSD==0) {
222  edm::LogWarning("HcalTBSim") << "HcalTB04Analysis::beginOfRun: No SD"
223  << " with name " << sdname << " in this "
224  << "Setup";
225  } else {
226  ECalSD* theCaloSD = dynamic_cast<ECalSD*>(aSD);
227  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis::beginOfRun: Finds SD "
228  << "with name " << theCaloSD->GetName()
229  << " in this Setup";
231  theCaloSD->setNumberingScheme(org);
232  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis::beginOfRun: set a "
233  << "new numbering scheme";
234  }
235  }
236  } else {
237  edm::LogWarning("HcalTBSim") << "HcalTB04Analysis::beginOfRun: Could "
238  << "not get SD Manager!";
239  }
240 
241 }
242 
244 
245  evNum = (*evt) ()->GetEventID ();
246  clear();
247  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis: =====> Begin of event = "
248  << evNum;
249 }
250 
251 void HcalTB04Analysis::update(const G4Step * aStep) {
252 
253  if (aStep != NULL) {
254  //Get Step properties
255  G4ThreeVector thePreStepPoint = aStep->GetPreStepPoint()->GetPosition();
256  G4ThreeVector thePostStepPoint;
257 
258  // Get Tracks properties
259  G4Track* aTrack = aStep->GetTrack();
260  int trackID = aTrack->GetTrackID();
261  int parentID = aTrack->GetParentID();
262  G4ThreeVector position = aTrack->GetPosition();
263  G4ThreeVector momentum = aTrack->GetMomentum();
264  G4String partType = aTrack->GetDefinition()->GetParticleType();
265  G4String partSubType = aTrack->GetDefinition()->GetParticleSubType();
266  int partPDGEncoding = aTrack->GetDefinition()->GetPDGEncoding();
267 #ifdef ddebug
268  bool isPDGStable = aTrack->GetDefinition()->GetPDGStable();
269 #endif
270  double pDGlifetime = aTrack->GetDefinition()->GetPDGLifeTime();
271  double gammaFactor = aStep->GetPreStepPoint()->GetGamma();
272 
273  if (!pvFound) { //search for v1
274  double stepDeltaEnergy = aStep->GetDeltaEnergy ();
275  double kinEnergy = aTrack->GetKineticEnergy ();
276 
277  // look for DeltaE > 10% kinEnergy of particle, or particle death - Ek=0
278  if (trackID == 1 && parentID == 0 &&
279  ((kinEnergy == 0.) || (fabs (stepDeltaEnergy / kinEnergy) > 0.1))) {
280  pvType = -1;
281  if (kinEnergy == 0.) {
282  pvType = 0;
283  } else {
284  if (fabs (stepDeltaEnergy / kinEnergy) > 0.1) pvType = 1;
285  }
286  pvFound = true;
288  pvMomentum = momentum;
289  // Rotated coord.system:
290  pvUVW = (*beamline_RM)*(pvPosition);
291 
292  //Volume name requires some checks:
293  G4String thePostPVname = "NoName";
294  G4StepPoint * thePostPoint = aStep->GetPostStepPoint ();
295  if (thePostPoint) {
296  thePostStepPoint = thePostPoint->GetPosition();
297  G4VPhysicalVolume * thePostPV = thePostPoint->GetPhysicalVolume ();
298  if (thePostPV) thePostPVname = thePostPV->GetName ();
299  }
300 #ifdef ddebug
301  LogDebug("HcalTBSim") << "HcalTB04Analysis:: V1 found at: "
302  << thePostStepPoint << " G4VPhysicalVolume: "
303  << thePostPVname;
304 #endif
305  LogDebug("HcalTBSim") << "HcalTB04Analysis::fill_v1Pos: Primary Track "
306  << "momentum: " << pvMomentum << " psoition "
307  << pvPosition << " u/v/w " << pvUVW;
308  }
309  } else {
310  // watch for secondaries originating @v1, including the surviving primary
311  if ((trackID != 1 && parentID == 1 &&
312  (aTrack->GetCurrentStepNumber () == 1) &&
313  (thePreStepPoint == pvPosition)) ||
314  (trackID == 1 && thePreStepPoint == pvPosition)) {
315 #ifdef ddebug
316  LogDebug("HcalTBSim") << "HcalTB04Analysis::A secondary... PDG:"
317  << partPDGEncoding << " TrackID:" << trackID
318  << " ParentID:" << parentID << " stable: "
319  << isPDGStable << " Tau: " << pDGlifetime
320  << " cTauGamma="
321  << c_light*pDGlifetime*gammaFactor*1000.
322  << "um" << " GammaFactor: " << gammaFactor;
323 #endif
324  secTrackID.push_back(trackID);
325  secPartID.push_back(partPDGEncoding);
326  secMomentum.push_back(momentum);
327  secEkin.push_back(aTrack->GetKineticEnergy());
328 
329  // Check for short-lived secondaries: cTauGamma<100um
330  double ctaugamma_um = c_light * pDGlifetime * gammaFactor * 1000.;
331  if ((ctaugamma_um>0.) && (ctaugamma_um<100.)) {//short-lived secondary
332  shortLivedSecondaries.push_back(trackID);
333  } else {//normal secondary - enter into the V1-calorimetric tree
334  // histos->fill_v1cSec (aTrack);
335  }
336  }
337  // Also watch for tertiary particles coming from
338  // short-lived secondaries from V1
339  if (aTrack->GetCurrentStepNumber() == 1) {
340  if (shortLivedSecondaries.size() > 0) {
341  int pid = parentID;
342  std::vector<int>::iterator pos1= shortLivedSecondaries.begin();
343  std::vector<int>::iterator pos2 = shortLivedSecondaries.end();
344  std::vector<int>::iterator pos;
345  for (pos = pos1; pos != pos2; pos++) {
346  if (*pos == pid) {//ParentID is on the list of short-lived
347  // secondary
348 #ifdef ddebug
349  LogDebug("HcalTBSim") << "HcalTB04Analysis:: A tertiary... PDG:"
350  << partPDGEncoding << " TrackID:" <<trackID
351  << " ParentID:" << parentID << " stable: "
352  << isPDGStable << " Tau: " << pDGlifetime
353  << " cTauGamma="
354  << c_light*pDGlifetime*gammaFactor*1000.
355  << "um GammaFactor: " << gammaFactor;
356 #endif
357  }
358  }
359  }
360  }
361  }
362  }
363 }
364 
366 
367  count++;
368 
369  //fill the buffer
370  LogDebug("HcalTBSim") << "HcalTB04Analysis::Fill event "
371  << (*evt)()->GetEventID();
372  fillBuffer (evt);
373 
374  //QIE analysis
375  LogDebug("HcalTBSim") << "HcalTB04Analysis::Do QIE analysis with "
376  << hcalHitCache.size() << " hits";
377  qieAnalysis();
378 
379  //Energy in Crystal Matrix
380  if (!hcalOnly) {
381  LogDebug("HcalTBSim") << "HcalTB04Analysis::Do Xtal analysis with "
382  << ecalHitCache.size() << " hits";
383  xtalAnalysis();
384  }
385 
386  //Final Analysis
387  LogDebug("HcalTBSim") << "HcalTB04Analysis::Final analysis";
388  finalAnalysis();
389 
390  int iEvt = (*evt)()->GetEventID();
391  if (iEvt < 10)
392  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis:: Event " << iEvt;
393  else if ((iEvt < 100) && (iEvt%10 == 0))
394  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis:: Event " << iEvt;
395  else if ((iEvt < 1000) && (iEvt%100 == 0))
396  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis:: Event " << iEvt;
397  else if ((iEvt < 10000) && (iEvt%1000 == 0))
398  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis:: Event " << iEvt;
399 }
400 
402 
403  std::vector<CaloHit> hhits, hhitl;
404  int idHC, j;
405  CaloG4HitCollection* theHC;
406  std::map<int,float,std::less<int> > primaries;
407  double etot1=0, etot2=0;
408 
409  // Look for the Hit Collection of HCal
410  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
411  std::string sdName = names[0];
412  idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
413  theHC = (CaloG4HitCollection*) allHC->GetHC(idHC);
414  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Hit Collection for " << sdName
415  << " of ID " << idHC << " is obtained at " << theHC;
416 
417  if (idHC >= 0 && theHC > 0) {
418  hhits.reserve(theHC->entries());
419  hhitl.reserve(theHC->entries());
420  for (j = 0; j < theHC->entries(); j++) {
421  CaloG4Hit* aHit = (*theHC)[j];
422  double e = aHit->getEnergyDeposit()/GeV;
423  double time = aHit->getTimeSlice();
424  math::XYZPoint pos = aHit->getEntry();
425  unsigned int id = aHit->getUnitID();
426  double theta = pos.theta();
427  double eta = -log(tan(theta * 0.5));
428  double phi = pos.phi();
429  int det, z, group, ieta, iphi, layer;
430  HcalTestNumbering::unpackHcalIndex(id,det,z,group,ieta,iphi,layer);
431  double jitter = time-timeOfFlight(det,layer,eta);
432  if (jitter<0) jitter = 0;
433  if (e < 0 || e > 1.) e = 0;
434  double escl = e * scale(det,layer);
435  unsigned int idx= HcalTBNumberingScheme::getUnitID(id,mode);
436  CaloHit hit(det,layer,escl,eta,phi,jitter,idx);
437  hhits.push_back(hit);
438  CaloHit hitl(det,layer,escl,eta,phi,jitter,id);
439  hhitl.push_back(hitl);
440  primaries[aHit->getTrackID()]+= e;
441  etot1 += escl;
442 #ifdef ddebug
443  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Hcal Hit i/p " << j
444  << " ID 0x" << std::hex << id << " 0x" << idx
445  << std::dec << " time " << std::setw(6) << time
446  << " " << std::setw(6) << jitter << " theta "
447  << std::setw(8) << theta << " eta " << std::setw(8)
448  << eta << " phi " << std::setw(8) << phi << " e "
449  << std::setw(8) << e << " " << std::setw(8) <<escl;
450 #endif
451  }
452  }
453 
454  // Add hits in the same channel within same time slice
455  std::vector<CaloHit>::iterator itr;
456  int nHit = hhits.size();
457  std::vector<CaloHit*> hits(nHit);
458  for (j = 0, itr = hhits.begin(); itr != hhits.end(); j++, itr++) {
459  hits[j] = &hhits[j];
460  }
461  sort(hits.begin(),hits.end(),CaloHitIdMore());
462  std::vector<CaloHit*>::iterator k1, k2;
463  int nhit = 0;
464  for (k1 = hits.begin(); k1 != hits.end(); k1++) {
465  int det = (**k1).det();
466  int layer = (**k1).layer();
467  double ehit = (**k1).e();
468  double eta = (**k1).eta();
469  double phi = (**k1).phi();
470  double jitter = (**k1).t();
471  uint32_t unitID = (**k1).id();
472  int jump = 0;
473  for (k2 = k1+1; k2 != hits.end() && fabs(jitter-(**k2).t())<1 &&
474  unitID==(**k2).id(); k2++) {
475  ehit += (**k2).e();
476  jump++;
477  }
478  nhit++;
479  CaloHit hit(det, layer, ehit, eta, phi, jitter, unitID);
480  hcalHitCache.push_back(hit);
481  etot2 += ehit;
482  k1 += jump;
483 #ifdef ddebug
484  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Hcal Hit store " << nhit
485  << " ID 0x" << std::hex << unitID << std::dec
486  << " time " << std::setw(6) << jitter << " eta "
487  << std::setw(8) << eta << " phi " << std::setw(8)
488  << phi << " e " << std::setw(8) << ehit;
489 #endif
490  }
491  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Stores " << nhit << " HCal hits"
492  << " from " << nHit << " input hits E(Hcal) " << etot1
493  << " " << etot2;
494 
495  //Repeat for Hit in each layer (hhits and hhitl sizes are the same)
496  for (j = 0, itr = hhitl.begin(); itr != hhitl.end(); j++, itr++) {
497  hits[j] = &hhitl[j];
498  }
499  sort(hits.begin(),hits.end(),CaloHitIdMore());
500  int nhitl = 0;
501  double etotl = 0;
502  for (k1 = hits.begin(); k1 != hits.end(); k1++) {
503  int det = (**k1).det();
504  int layer = (**k1).layer();
505  double ehit = (**k1).e();
506  double eta = (**k1).eta();
507  double phi = (**k1).phi();
508  double jitter = (**k1).t();
509  uint32_t unitID = (**k1).id();
510  int jump = 0;
511  for (k2 = k1+1; k2 != hits.end() && fabs(jitter-(**k2).t())<1 &&
512  unitID==(**k2).id(); k2++) {
513  ehit += (**k2).e();
514  jump++;
515  }
516  nhitl++;
517  CaloHit hit(det, layer, ehit, eta, phi, jitter, unitID);
518  hcalHitLayer.push_back(hit);
519  etotl += ehit;
520  k1 += jump;
521 #ifdef ddebug
522  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Hcal Hit store " << nhitl
523  << " ID 0x" << std::hex << unitID << std::dec
524  << " time " << std::setw(6) << jitter << " eta "
525  << std::setw(8) << eta << " phi " << std::setw(8)
526  << phi << " e " << std::setw(8) << ehit;
527 #endif
528  }
529  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Stores " << nhitl << " HCal "
530  << "hits from " << nHit << " input hits E(Hcal) "
531  << etot1 << " " << etotl;
532 
533  // Look for the Hit Collection of ECal
534  std::vector<CaloHit> ehits;
535  sdName= names[1];
536  idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
537  theHC = (CaloG4HitCollection*) allHC->GetHC(idHC);
538  etot1 = etot2 = 0;
539  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Hit Collection for " << sdName
540  << " of ID " << idHC << " is obtained at " << theHC;
541  if (idHC >= 0 && theHC > 0) {
542  ehits.reserve(theHC->entries());
543  for (j = 0; j < theHC->entries(); j++) {
544  CaloG4Hit* aHit = (*theHC)[j];
545  double e = aHit->getEnergyDeposit()/GeV;
546  double time = aHit->getTimeSlice();
547  math::XYZPoint pos = aHit->getEntry();
548  unsigned int id = aHit->getUnitID();
549  double theta = pos.theta();
550  double eta = -log(tan(theta * 0.5));
551  double phi = pos.phi();
552  if (e < 0 || e > 100000.) e = 0;
553  int det, z, group, ieta, iphi, layer;
554  HcalTestNumbering::unpackHcalIndex(id,det,z,group,ieta,iphi,layer);
555  CaloHit hit(det,0,e,eta,phi,time,id);
556  ehits.push_back(hit);
557  primaries[aHit->getTrackID()]+= e;
558  etot1 += e;
559 #ifdef ddebug
560  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Ecal Hit i/p " << j
561  << " ID 0x" << std::hex << id << std::dec
562  << " time " << std::setw(6) << time << " theta "
563  << std::setw(8) << theta << " eta " <<std::setw(8)
564  << eta << " phi " << std::setw(8) << phi << " e "
565  << std::setw(8) << e;
566 #endif
567  }
568  }
569 
570  // Add hits in the same channel within same time slice
571  nHit = ehits.size();
572  std::vector<CaloHit*> hite(nHit);
573  for (j = 0, itr = ehits.begin(); itr != ehits.end(); j++, itr++) {
574  hite[j] = &ehits[j];
575  }
576  sort(hite.begin(),hite.end(),CaloHitIdMore());
577  nhit = 0;
578  for (k1 = hite.begin(); k1 != hite.end(); k1++) {
579  int det = (**k1).det();
580  int layer = (**k1).layer();
581  double ehit = (**k1).e();
582  double eta = (**k1).eta();
583  double phi = (**k1).phi();
584  double jitter = (**k1).t();
585  uint32_t unitID = (**k1).id();
586  int jump = 0;
587  for (k2 = k1+1; k2 != hite.end() && fabs(jitter-(**k2).t())<1 &&
588  unitID==(**k2).id(); k2++) {
589  ehit += (**k2).e();
590  jump++;
591  }
592  nhit++;
593  CaloHit hit(det, layer, ehit, eta, phi, jitter, unitID);
594  ecalHitCache.push_back(hit);
595  etot2 += ehit;
596  k1 += jump;
597 #ifdef ddebug
598  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Ecal Hit store " << nhit
599  << " ID 0x" << std::hex << unitID << std::dec
600  << " time " << std::setw(6) << jitter << " eta "
601  << std::setw(8) << eta << " phi " << std::setw(8)
602  << phi << " e " << std::setw(8) << ehit;
603 #endif
604  }
605  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Stores " << nhit << " ECal hits"
606  << " from " << nHit << " input hits E(Ecal) " << etot1
607  << " " << etot2;
608 
609  // Find Primary info:
610  nPrimary = (int)(primaries.size());
611  int trackID = 0;
612  G4PrimaryParticle* thePrim=0;
613  int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
614  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Event has " << nvertex
615  << " verteices";
616  if (nvertex<=0)
617  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis::EndOfEvent ERROR: no "
618  << "vertex found for event " << evNum;
619 
620  for (int i = 0 ; i<nvertex; i++) {
621  G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
622  if (avertex == 0) {
623  edm::LogInfo("HcalTBSim") << "HcalTB04Analysis::EndOfEvent ERR: pointer "
624  << "to vertex = 0 for event " << evNum;
625  } else {
626  LogDebug("HcalTBSim") << "HcalTB04Analysis::Vertex number :" << i << " "
627  << avertex->GetPosition();
628  int npart = avertex->GetNumberOfParticle();
629  if (npart == 0)
630  edm::LogWarning("HcalTBSim") << "HcalTB04Analysis::End Of Event ERR: "
631  << "no primary!";
632  if (thePrim==0) thePrim=avertex->GetPrimary(trackID);
633  }
634  }
635 
636  if (thePrim != 0) {
637  double px = thePrim->GetPx();
638  double py = thePrim->GetPy();
639  double pz = thePrim->GetPz();
640  double p = std::sqrt(pow(px,2.)+pow(py,2.)+pow(pz,2.));
641  pInit = p/GeV;
642  if (p==0)
643  edm::LogWarning("HcalTBSim") << "HcalTB04Analysis:: EndOfEvent ERR: "
644  << "primary has p=0 ";
645  else {
646  double costheta = pz/p;
647  double theta = acos(std::min(std::max(costheta,-1.),1.));
648  etaInit = -log(tan(theta/2));
649  if (px != 0 || py != 0) phiInit = atan2(py,px);
650  }
651  particleType = thePrim->GetPDGcode();
652  } else
653  edm::LogWarning("HcalTBSim") << "HcalTB04Analysis::EndOfEvent ERR: could "
654  << "not find primary";
655 
656 }
657 
659 
660  int hittot = hcalHitCache.size();
661  if (hittot<=0) hittot = 1;
662  std::vector<CaloHit> hits(hittot);
663  std::vector<int> todo(nTower,0);
664 
665  LogDebug("HcalTBSim") << "HcalTB04Analysis::qieAnalysis: Size "
666  << hits.size() << " " << todo.size() << " "
667  << idTower.size() << " " << esimh.size() << " "
668  << eqie.size();
669  // Loop over all HCal hits
670  for (unsigned int k1 = 0; k1 < hcalHitCache.size(); k1++) {
671  CaloHit hit = hcalHitCache[k1];
672  uint32_t id = hit.id();
673  int nhit = 0;
674  double esim = hit.e();
675  hits[nhit] = hit;
676  for (unsigned int k2 = k1+1; k2 < hcalHitCache.size(); k2++) {
677  hit = hcalHitCache[k2];
678  if (hit.id() == id) {
679  nhit++;
680  hits[nhit] = hit;
681  esim += hit.e();
682  }
683  }
684  k1 += nhit;
685  nhit++;
686  std::vector<int> cd = myQie->getCode(nhit,hits);
687  double eq = myQie->getEnergy(cd);
688  LogDebug("HcalTBSim") << "HcalTB04Analysis:: ID 0x" << std::hex << id
689  << std::dec << " registers " << esim << " energy "
690  << "from " << nhit << " hits starting with hit # "
691  << k1 << " energy with noise " << eq;
692  for (int k2 = 0; k2 < nTower; k2++) {
693  if (id == idTower[k2]) {
694  todo[k2] = 1;
695  esimh[k2] = esim;
696  eqie[k2] = eq;
697  }
698  }
699  }
700 
701  // Towers with no hit
702  for (int k2 = 0; k2 < nTower; k2++) {
703  if (todo[k2] == 0) {
704  std::vector<int> cd = myQie->getCode(0,hits);
705  double eq = myQie->getEnergy(cd);
706  esimh[k2] = 0;
707  eqie[k2] = eq;
708 #ifdef ddebug
709  LogDebug("HcalTBSim") << "HcalTB04Analysis:: ID 0x" << std::hex
710  << idTower[k2] << std::dec << " registers "
711  << esimh[k2] << " energy from hits and energy "
712  << "after QIE analysis " << eqie[k2];
713 #endif
714  }
715  }
716 }
717 
719 
721  if ( ! rng.isAvailable()) {
722  throw cms::Exception("Configuration")
723  << "HcalTB04Analysis requires the RandomNumberGeneratorService\n"
724  << "which is not present in the configuration file. "
725  << "You must add the service\n in the configuration file or "
726  << "remove the modules that require it.";
727  }
728  CLHEP::RandGaussQ randGauss(rng->getEngine());
729 
730  // Crystal Data
731  std::vector<int> iok(nCrystal,0);
732  LogDebug("HcalTBSim") << "HcalTB04Analysis::xtalAnalysis: Size " <<iok.size()
733  << " " << idEcal.size() << " " << esime.size() << " "
734  << enois.size();
735  for (unsigned int k1 = 0; k1 < ecalHitCache.size(); k1++) {
736  uint32_t id = ecalHitCache[k1].id();
737  int nhit = 0;
738  double esim = ecalHitCache[k1].e();
739  for (unsigned int k2 = k1+1; k2 < ecalHitCache.size(); k2++) {
740  if (ecalHitCache[k2].id() == id) {
741  nhit++;
742  esim += ecalHitCache[k2].e();
743  }
744  }
745  k1 += nhit;
746  nhit++;
747  double eq = esim + randGauss.fire(0., ecalNoise);
748 #ifdef ddebug
749  LogDebug("HcalTBSim") << "HcalTB04Analysis:: ID 0x" << std::hex << id
750  << std::dec << " registers " << esim << " energy "
751  << "from " << nhit << " hits starting with hit # "
752  << k1 << " energy with noise " << eq;
753 #endif
754  for (int k2 = 0; k2 < nCrystal; k2++) {
755  if (id == idEcal[k2]) {
756  iok[k2] = 1;
757  esime[k2] = esim;
758  enois[k2] = eq;
759  }
760  }
761  }
762 
763  // Crystals with no hit
764  for (int k2 = 0; k2 < nCrystal; k2++) {
765  if (iok[k2] == 0) {
766  esime[k2] = 0;
767  enois[k2] = randGauss.fire(0., ecalNoise);
768 #ifdef ddebug
769  LogDebug("HcalTBSim") << "HcalTB04Analysis:: ID 0x" << std::hex
770  << idEcal[k2] << std::dec << " registers "
771  << esime[k2] << " energy from hits and energy from"
772  << " noise " << enois[k2];
773 #endif
774  }
775  }
776 }
777 
779 
780  //Beam Information
782 
783  // Total Energy
784  eecals = ehcals = eecalq = ehcalq = 0.;
785  for (int i=0; i<nTower; i++) {
786  ehcals += esimh[i];
787  ehcalq += eqie[i];
788  }
789  for (int i=0; i<nCrystal; i++) {
790  eecals += esime[i];
791  eecalq += enois[i];
792  }
793  etots = eecals + ehcals;
794  etotq = eecalq + ehcalq;
795  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Energy deposit at Sim Level "
796  << "(Total) " << etots << " (ECal) " << eecals
797  << " (HCal) " << ehcals << "\nHcalTB04Analysis:: "
798  << "Energy deposit at Qie Level (Total) " << etotq
799  << " (ECal) " << eecalq << " (HCal) " << ehcalq;
800  histo->fillEdep(etots, eecals, ehcals, etotq, eecalq, ehcalq);
801 
802  // Lateral Profile
803  for (int i=0; i<5; i++) {
804  eseta[i] = 0.;
805  eqeta[i] = 0.;
806  }
807  for (int i=0; i<3; i++) {
808  esphi[i] = 0.;
809  eqphi[i] = 0.;
810  }
811  double e1=0, e2=0;
812  unsigned int id;
813  for (int i=0; i<nTower; i++) {
814  int det, z, group, ieta, iphi, layer;
815  id = idTower[i];
816  HcalTestNumbering::unpackHcalIndex(id,det,z,group,ieta,iphi,layer);
817  iphi -= (icphi - 1);
818  if (icphi > 4) {
819  if (ieta == 0) ieta = 2;
820  else ieta =-1;
821  } else {
822  ieta = ieta - iceta + 2;
823  }
824  if (iphi >= 0 && iphi < 3 && ieta >= 0 && ieta < 5) {
825  eseta[ieta] += esimh[i];
826  esphi[iphi] += esimh[i];
827  e1 += esimh[i];
828  eqeta[ieta] += eqie[i];
829  eqphi[iphi] += eqie[i];
830  e2 += eqie[i];
831  }
832  }
833  for (int i=0; i<3; i++) {
834  if (e1>0) esphi[i] /= e1;
835  if (e2>0) eqphi[i] /= e2;
836  }
837  for (int i=0; i<5; i++) {
838  if (e1>0) eseta[i] /= e1;
839  if (e2>0) eqeta[i] /= e2;
840  }
841  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Energy fraction along Eta and"
842  << " Phi (Sim/Qie)";
843  for (int i=0; i<5; i++)
844  LogDebug("HcalTBSim") << "HcalTB04Analysis:: [" << i << "] Eta Sim = "
845  << eseta[i] << " Qie = " << eqeta[i] << " Phi Sim = "
846  << esphi[i] << " Qie = " << eqphi[i];
848 
849  // Longitudianl profile
850  for (int i=0; i<20; i++) {
851  eslay[i] = 0.;
852  eqlay[i] = 0.;
853  }
854  e1=0; e2=0;
855  for (int i=0; i<nTower; i++) {
856  int det, z, group, ieta, iphi, layer;
857  id = idTower[i];
858  HcalTestNumbering::unpackHcalIndex(id,det,z,group,ieta,iphi,layer);
859  iphi -= (icphi - 1);
860  layer -= 1;
861  if (iphi >= 0 && iphi < 3 && layer >= 0 && layer < 20) {
862  eslay[layer] += esimh[i];
863  e1 += esimh[i];
864  eqlay[layer] += eqie[i];
865  e2 += eqie[i];
866  }
867  }
868  for (int i=0; i<20; i++) {
869  if (e1>0) eslay[i] /= e1;
870  if (e2>0) eqlay[i] /= e2;
871  }
872  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Energy fraction along Layer";
873  for (int i=0; i<20; i++)
874  LogDebug("HcalTBSim") << "HcalTB04Analysis:: [" << i << "] Sim = "
875  << eslay[i] << " Qie = " << eqlay[i];
876  histo->fillLongProf(eslay, eqlay);
877 }
878 
879 
881 
882  //Setup the ID's
883  product.setIDs(idHcal, idXtal);
884 
885  //Beam Information
887 
888  //Energy deposits in the crystals and towers
889  product.setEdepHcal(esimh, eqie);
890  product.setEdepHcal(esime, enois);
891 
892  // Total Energy
893  product.setEdep(etots, eecals, ehcals, etotq, eecalq, ehcalq);
894 
895  // Lateral Profile
896  product.setTrnsProf(eseta,eqeta,esphi,eqphi);
897 
898  // Longitudianl profile
899  product.setLongProf(eslay, eqlay);
900 
901  //Save Hits
902  int i, nhit=0;
903  std::vector<CaloHit>::iterator itr;
904  for (i=0, itr=ecalHitCache.begin(); itr!=ecalHitCache.end(); i++,itr++) {
905  uint32_t id = itr->id();
906  int det, z, group, ieta, iphi, lay;
907  HcalTestNumbering::unpackHcalIndex(id,det,z,group,ieta,iphi,lay);
908  product.saveHit(det, lay, ieta, iphi, itr->e(), itr->t());
909  nhit++;
910 #ifdef debug
911  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Save Hit " << std::setw(3)
912  << i+1 << " ID 0x" << std::hex << group << std::dec
913  << " " << std::setw(2) << det << " " << std::setw(2)
914  << lay << " " << std::setw(1) << z << " "
915  << std::setw(3) << ieta << " " << std::setw(3) <<iphi
916  << " T " << std::setw(6) << itr->t() << " E "
917  << std::setw(6) << itr->e();
918 #endif
919  }
920  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Saves " << nhit
921  << " hits from Crystals";
922  int hit = nhit;
923  nhit = 0;
924 
925  for (i=hit, itr=hcalHitCache.begin(); itr!=hcalHitCache.end(); i++,itr++) {
926  uint32_t id = itr->id();
927  int det, z, group, ieta, iphi, lay;
928  HcalTestNumbering::unpackHcalIndex(id,det,z,group,ieta,iphi,lay);
929  product.saveHit(det, lay, ieta, iphi, itr->e(), itr->t());
930  nhit++;
931 #ifdef debug
932  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Save Hit " << std::setw(3)
933  << i+1 << " ID 0x" << std::hex << group << std::dec
934  << " " << std::setw(2) << det << " " << std::setw(2)
935  << lay << " " << std::setw(1) << z << " "
936  << std::setw(3) << ieta << " " << std::setw(3) <<iphi
937  << " T " << std::setw(6) << itr->t() << " E "
938  << std::setw(6) << itr->e();
939 #endif
940  }
941  LogDebug("HcalTBSim") << "HcalTB04Analysis:: Saves " << nhit
942  << " hits from HCal";
943 
944  //Vertex associated quantities
945  product.setVtxPrim(evNum, pvType, pvPosition.x(), pvPosition.y(),
946  pvPosition.z(), pvUVW.x(), pvUVW.y(), pvUVW.z(),
947  pvMomentum.x(), pvMomentum.y(), pvMomentum.z());
948  for (unsigned int i = 0; i < secTrackID.size(); i++) {
949  product.setVtxSec(secTrackID[i], secPartID[i], secMomentum[i].x(),
950  secMomentum[i].y(), secMomentum[i].z(), secEkin[i]);
951  }
952 }
953 
955  pvFound = false;
956  pvType =-2;
957  pvPosition = G4ThreeVector();
958  pvMomentum = G4ThreeVector();
959  pvUVW = G4ThreeVector();
960  secTrackID.clear();
961  secPartID.clear();
962  secMomentum.clear();
963  secEkin.clear();
964  shortLivedSecondaries.clear();
965 
966  ecalHitCache.erase(ecalHitCache.begin(), ecalHitCache.end());
967  hcalHitCache.erase(hcalHitCache.begin(), hcalHitCache.end());
968  hcalHitLayer.erase(hcalHitLayer.begin(), hcalHitLayer.end());
969  nPrimary = particleType = 0;
970  pInit = etaInit = phiInit = 0;
971 
972  esimh.clear();
973  eqie.clear();
974  esimh.reserve(nTower);
975  eqie.reserve(nTower);
976  for (int i=0; i<nTower; i++) {
977  esimh.push_back(0.);
978  eqie.push_back(0.);
979  }
980  esime.clear();
981  enois.clear();
982  esime.reserve(nCrystal);
983  enois.reserve(nCrystal);
984  for (int i=0; i<nCrystal; i++) {
985  esime.push_back(0.);
986  enois.push_back(0.);
987  }
988 }
989 
990 int HcalTB04Analysis::unitID(uint32_t id) {
991 
992  int det, z, group, ieta, iphi, lay;
993  HcalTestNumbering::unpackHcalIndex(id,det,z,group,ieta,iphi,lay);
994  group = (det&15)<<20;
995  group += ((lay-1)&31)<<15;
996  group += (z&1)<<14;
997  group += (ieta&127)<<7;
998  group += (iphi&127);
999  return group;
1000 }
1001 
1002 double HcalTB04Analysis::scale(int det, int layer) {
1003 
1004  double tmp = 1.;
1005  if (det == static_cast<int>(HcalBarrel)) {
1006  if (layer == 1) tmp = scaleHB0;
1007  else if (layer == 17) tmp = scaleHB16;
1008  else if (layer > 17) tmp = scaleHO;
1009  } else {
1010  if (layer <= 2) tmp = scaleHE0;
1011  }
1012  return tmp;
1013 }
1014 
1015 double HcalTB04Analysis::timeOfFlight(int det, int layer, double eta) {
1016 
1017  double theta = 2.0*atan(exp(-eta));
1018  double dist = beamOffset;
1019  if (det == static_cast<int>(HcalBarrel)) {
1020  const double rLay[19] = {
1021  1836.0, 1902.0, 1962.0, 2022.0, 2082.0, 2142.0, 2202.0, 2262.0, 2322.0,
1022  2382.0, 2448.0, 2514.0, 2580.0, 2646.0, 2712.0, 2776.0, 2862.5, 3847.0,
1023  4052.0};
1024  if (layer>0 && layer<=19) dist += rLay[layer-1]*mm/sin(theta);
1025  } else {
1026  const double zLay[19] = {
1027  4034.0, 4032.0, 4123.0, 4210.0, 4297.0, 4384.0, 4471.0, 4558.0, 4645.0,
1028  4732.0, 4819.0, 4906.0, 4993.0, 5080.0, 5167.0, 5254.0, 5341.0, 5428.0,
1029  5515.0};
1030  if (layer>0 && layer<=19) dist += zLay[layer-1]*mm/cos(theta);
1031  }
1032 
1033  double tmp = dist/c_light/ns;
1034 #ifdef ddebug
1035  LogDebug("HcalTBSim") << "HcalTB04Analysis::timeOfFlight " << tmp
1036  << " for det/lay " << det << " " << layer
1037  << " eta/theta " << eta << " " << theta/deg
1038  << " dist " << dist;
1039 #endif
1040  return tmp;
1041 }
1042 
#define LogDebug(id)
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
double getEnergy(std::vector< int >)
Definition: HcalQie.cc:403
void update(const BeginOfRun *run)
This routine will be called when the appropriate signal arrives.
int i
Definition: DBlmapReader.cc:9
#define DEFINE_SIMWATCHER(type)
std::vector< double > secEkin
void setLongProf(const std::vector< double > &es, const std::vector< double > &eq)
std::vector< int > idHcal
void fillPrimary(double energy, double eta, double phi)
std::vector< CaloHit > hcalHitLayer
std::vector< double > eqeta
std::vector< double > eqie
void fillTrnsProf(const std::vector< double > &es1, const std::vector< double > &eq1, const std::vector< double > &es2, const std::vector< double > &eq2)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
virtual void produce(edm::Event &, const edm::EventSetup &)
void fillBuffer(const EndOfEvent *evt)
std::vector< int > shortLivedSecondaries
Geom::Theta< T > theta() const
std::vector< int > secTrackID
#define NULL
Definition: scimark2.h:8
double npart
Definition: HydjetWrapper.h:45
std::vector< double > eqphi
void setPrimary(int primary, int id, double energy, double eta, double phi)
#define min(a, b)
Definition: mlp_lapack.h:161
T eta() const
void setNumberingScheme(HcalNumberingScheme *)
Definition: HCalSD.cc:599
float float float z
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
void setEdep(double simtot, double sime, double simh, double digtot, double dige, double digh)
void setTrnsProf(const std::vector< double > &es1, const std::vector< double > &eq1, const std::vector< double > &es2, const std::vector< double > &eq2)
void saveHit(int det, int lay, int eta, int phi, double e, double t)
std::vector< int > secPartID
Definition: HCalSD.h:33
static uint32_t packHcalIndex(int det, int z, int depth, int eta, int phi, int lay)
std::vector< uint32_t > idEcal
std::vector< double > eseta
void setIDs(const std::vector< int > &, const std::vector< int > &)
uint32_t id() const
Definition: CaloHit.h:28
void setVtxSec(int id, int pdg, double px, double py, double pz, double ek)
const T & max(const T &a, const T &b)
G4ThreeVector pvUVW
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
void setEdepHcal(const std::vector< double > &esim, const std::vector< double > &edig)
T sqrt(T t)
Definition: SSEVec.h:48
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
bool isAvailable() const
Definition: Service.h:47
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
int j
Definition: DBlmapReader.cc:9
static void unpackHcalIndex(const uint32_t &idx, int &det, int &z, int &depth, int &eta, int &phi, int &lay)
G4RotationMatrix * beamline_RM
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
std::vector< double > esime
static std::vector< uint32_t > getUnitIDs(const int type, const int mode)
double scale(int det, int layer)
std::vector< double > esphi
double timeOfFlight(int det, int layer, double eta)
std::vector< int > getCode(int, std::vector< CaloHit >)
Definition: HcalQie.cc:299
unsigned int id
Definition: ECalSD.h:26
Float e1
Definition: deltaR.h:22
std::vector< CaloHit > hcalHitCache
int getTrackID() const
Definition: CaloG4Hit.h:68
static uint32_t getUnitID(const uint32_t id, const int mode)
HcalTB04Analysis(const edm::ParameterSet &p)
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
double sd
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
std::vector< int > idXtal
std::vector< CaloHit > ecalHitCache
double e() const
Definition: CaloHit.h:24
std::vector< double > enois
Float e2
Definition: deltaR.h:23
void setVtxPrim(int evNum, int type, double x, double y, double z, double u, double v, double w, double px, double py, double pz)
std::vector< G4ThreeVector > secMomentum
int unitID(uint32_t id)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
HcalTB04Histo * histo
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
G4ThreeVector pvMomentum
double getTimeSlice() const
Definition: CaloG4Hit.h:70
virtual ~HcalTB04Analysis()
std::vector< uint32_t > idTower
std::vector< double > eslay
math::XYZPoint getEntry() const
Definition: CaloG4Hit.h:50
std::vector< double > eqlay
Definition: DDAxes.h:10
uint32_t getUnitID() const
Definition: CaloG4Hit.h:69
G4ThreeVector pvPosition
void setNumberingScheme(EcalNumberingScheme *)
Definition: ECalSD.cc:264
std::vector< std::string > names
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
void fillEdep(double etots, double eecals, double ehcals, double etotq, double eecalq, double ehcalq)
void fillEvent(PHcalTB04Info &)
double getEnergyDeposit() const
Definition: CaloG4Hit.h:81
std::vector< double > esimh
void fillLongProf(const std::vector< double > &es, const std::vector< double > &eq)
Definition: DDAxes.h:10