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