CMS 3D CMS Logo

CastorShowerLibraryMaker.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ShowerLibraryProducer
4 // Class : CastorShowerLibraryMaker
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: P. Katsas
10 // Created: 02/2007
11 //
12 // Adapted by W. Carvalho , 02/2009
13 //
15 
18 
24 
25 // Classes for shower library Root file
28 
37 
40 
42 
43 #include "G4RunManager.hh"
44 #include "G4SDManager.hh"
45 #include "G4Step.hh"
46 #include "G4Track.hh"
47 #include "G4Event.hh"
48 #include "G4PrimaryVertex.hh"
49 #include "G4VProcess.hh"
50 #include "G4HCofThisEvent.hh"
51 #include "G4UserEventAction.hh"
52 
53 #include <CLHEP/Random/Randomize.h>
54 #include <CLHEP/Units/SystemOfUnits.h>
55 #include <CLHEP/Units/PhysicalConstants.h>
56 
57 #include "TROOT.h"
58 #include "TFile.h"
59 #include "TH1.h"
60 #include "TH2.h"
61 #include "TProfile.h"
62 #include "TNtuple.h"
63 #include "TRandom.h"
64 #include "TLorentzVector.h"
65 #include "TUnixSystem.h"
66 #include "TSystem.h"
67 #include "TMath.h"
68 #include "TF1.h"
69 
70 #include <cassert>
71 #include <cmath>
72 #include <cstdlib>
73 #include <iomanip>
74 #include <iostream>
75 #include <map>
76 #include <set>
77 #include <sstream>
78 #include <string>
79 #include <vector>
80 
81 typedef std::vector<std::vector<std::vector<std::vector<CastorShowerEvent> > > > SLBin3D; // bin in energy, eta and phi
82 
84  public Observer<const BeginOfJob*>,
85  public Observer<const BeginOfRun*>,
86  public Observer<const EndOfRun*>,
87  public Observer<const BeginOfEvent*>,
88  public Observer<const EndOfEvent*>,
89  public Observer<const G4Step*> {
90 public:
92  ~CastorShowerLibraryMaker() override;
93 
94 private:
95  typedef int ebin;
96  typedef int etabin;
97  typedef int phibin;
98  // private structures
99  struct ShowerLib {
101  SLBin3D SLCollection; // the showers
102  std::vector<double> SLEnergyBins;
103  std::vector<double> SLEtaBins;
104  std::vector<double> SLPhiBins;
105  unsigned int nEvtPerBinE;
106  unsigned int nEvtPerBinEta;
107  unsigned int nEvtPerBinPhi;
108  std::vector<int> nEvtInBinE;
109  std::vector<std::vector<int> > nEvtInBinEta;
110  std::vector<std::vector<std::vector<int> > > nEvtInBinPhi;
111  };
112 
113  // observer classes
114  void update(const BeginOfJob* run) override;
115  void update(const BeginOfRun* run) override;
116  void update(const EndOfRun* run) override;
117  void update(const BeginOfEvent* evt) override;
118  void update(const EndOfEvent* evt) override;
119  void update(const G4Step* step) override;
120 
121 private:
122  void Finish();
123 
124  // Job general parameters
127 
128  unsigned int NPGParticle; // number of particles requested to Particle Gun
129  std::vector<int> PGParticleIDs; //p. gun particle IDs
130  bool DoHadSL; // true if hadronic SL should be produced
131  bool DoEmSL; // true if electromag. SL should be produced
132  bool InsideCastor; // true if particle step inside CASTOR
133  bool DeActivatePhysicsProcess; //cfg parameter: True if phys. proc. should be off from IP to Castor
134  std::vector<G4PrimaryParticle*> thePrims; // list of primaries for this event
135 
136  // Pointers for user defined class objects to be stored to Root file
143  ShowerLib* SLShowerptr; // pointer to the current shower collection (above)
144  std::map<int, std::set<int> > MapOfSecondaries; // map to hold all secondaries ID keyed by
145  // the PDG code of the primary
146 
147  std::map<int, G4ThreeVector> PrimaryMomentum;
148  std::map<int, G4ThreeVector> PrimaryPosition;
149  double MaxEta; // limits the eta region, the lower limit is given by the SL bins
150  double MaxPhi; // limits the phi region, the lower limit is given by the SL bins
151  // private methods
152  int FindEnergyBin(double e);
153  int FindEtaBin(double eta);
154  int FindPhiBin(double phi);
155  bool SLacceptEvent(int, int, int);
156  bool IsSLReady();
157  void GetKinematics(G4PrimaryParticle*, double& px, double& py, double& pz, double& pInit, double& eta, double& phi);
158  void GetKinematics(int, double& px, double& py, double& pz, double& pInit, double& eta, double& phi);
159 
160  std::vector<G4PrimaryParticle*> GetPrimary(const G4Event*);
162  void InitSLHolder(ShowerLib&);
163 
164  void printSLstatus(int, int, int);
165  int& SLnEvtInBinE(int ebin);
166  int& SLnEvtInBinEta(int ebin, int etabin);
167  int& SLnEvtInBinPhi(int ebin, int etabin, int phibin);
168  bool SLisEBinFilled(int ebin);
169  bool SLisEtaBinFilled(int ebin, int etabin);
170  bool SLisPhiBinFilled(int ebin, int etabin, int phibin);
171  void KillSecondaries(const G4Step* step);
172  void GetMissingEnergy(CaloG4HitCollection*, double&, double&);
173 
174  // Root pointers
175  TFile* theFile;
176  TTree* theTree;
177 
179  int stepIndex; // ignore, please
180 };
181 
183  : NPGParticle(0),
184  DoHadSL(false),
185  DoEmSL(false),
186  DeActivatePhysicsProcess(false),
187  emShower(nullptr),
188  hadShower(nullptr) {
189  MapOfSecondaries.clear();
190  hadInfo = nullptr;
191  emInfo = nullptr;
192  edm::ParameterSet p_SLM = p.getParameter<edm::ParameterSet>("CastorShowerLibraryMaker");
193  verbosity = p_SLM.getParameter<int>("Verbosity");
194  eventNtFileName = p_SLM.getParameter<std::string>("EventNtupleFileName");
195  hadSLHolder.nEvtPerBinPhi = p_SLM.getParameter<int>("nhadEvents");
196  emSLHolder.nEvtPerBinPhi = p_SLM.getParameter<int>("nemEvents");
197  hadSLHolder.SLEnergyBins = p_SLM.getParameter<std::vector<double> >("SLhadEnergyBins");
198  hadSLHolder.SLEtaBins = p_SLM.getParameter<std::vector<double> >("SLhadEtaBins");
199  hadSLHolder.SLPhiBins = p_SLM.getParameter<std::vector<double> >("SLhadPhiBins");
200  emSLHolder.SLEnergyBins = p_SLM.getParameter<std::vector<double> >("SLemEnergyBins");
201  emSLHolder.SLEtaBins = p_SLM.getParameter<std::vector<double> >("SLemEtaBins");
202  emSLHolder.SLPhiBins = p_SLM.getParameter<std::vector<double> >("SLemPhiBins");
203  PGParticleIDs = p_SLM.getParameter<std::vector<int> >("PartID");
204  DeActivatePhysicsProcess = p_SLM.getParameter<bool>("DeActivatePhysicsProcess");
205  MaxPhi = p_SLM.getParameter<double>("SLMaxPhi");
206  MaxEta = p_SLM.getParameter<double>("SLMaxEta");
207  //
208  NPGParticle = PGParticleIDs.size();
209  for (unsigned int i = 0; i < PGParticleIDs.size(); i++) {
210  switch (std::abs(PGParticleIDs.at(i))) {
211  case 11:
212  case 22:
213  DoEmSL = true;
214  break;
215  default:
216  DoHadSL = true;
217  }
218  }
223 
224  edm::LogVerbatim("HcalSim") << "============================================================================";
225  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker:: Initialized as observer";
226  edm::LogVerbatim("HcalSim") << " Event Ntuple will be created";
227  edm::LogVerbatim("HcalSim") << " Event Ntuple file: " << eventNtFileName;
228  edm::LogVerbatim("HcalSim") << " Number of Hadronic events in E bins: " << hadSLHolder.nEvtPerBinE;
229  edm::LogVerbatim("HcalSim") << " Number of Hadronic events in Eta bins: " << hadSLHolder.nEvtPerBinEta;
230  edm::LogVerbatim("HcalSim") << " Number of Hadronic events in Phi bins: " << hadSLHolder.nEvtPerBinPhi;
231  edm::LogVerbatim("HcalSim") << " Number of Electromag. events in E bins: " << emSLHolder.nEvtPerBinE;
232  edm::LogVerbatim("HcalSim") << " Number of Electromag. events in Eta bins: " << emSLHolder.nEvtPerBinEta;
233  edm::LogVerbatim("HcalSim") << " Number of Electromag. events in Phi bins: " << emSLHolder.nEvtPerBinPhi;
234  edm::LogVerbatim("HcalSim") << "============================================================================\n";
235 
236  // Initializing the SL collections
239 }
241  int nBinsE, nBinsEta, nBinsPhi, nEvtPerBinPhi;
242  nBinsE = showerholder.SLEnergyBins.size();
243  nBinsEta = showerholder.SLEtaBins.size();
244  nBinsPhi = showerholder.SLPhiBins.size();
245  nEvtPerBinPhi = showerholder.nEvtPerBinPhi;
246  //
247  // Info
248  //
249  showerholder.SLInfo.Energy.setNEvts(nEvtPerBinPhi * nBinsPhi * nBinsEta * nBinsE);
250  showerholder.SLInfo.Energy.setNEvtPerBin(nEvtPerBinPhi * nBinsPhi * nBinsEta);
251  showerholder.SLInfo.Energy.setNBins(nBinsE);
252  showerholder.SLInfo.Energy.setBin(showerholder.SLEnergyBins);
253  //
254  showerholder.SLInfo.Eta.setNEvts(nEvtPerBinPhi * nBinsPhi * nBinsEta);
255  showerholder.SLInfo.Eta.setNEvtPerBin(nEvtPerBinPhi * nBinsPhi);
256  showerholder.SLInfo.Eta.setNBins(nBinsEta);
257  showerholder.SLInfo.Eta.setBin(showerholder.SLEtaBins);
258  //
259  showerholder.SLInfo.Phi.setNEvts(nEvtPerBinPhi * nBinsPhi);
260  showerholder.SLInfo.Phi.setNEvtPerBin(nEvtPerBinPhi);
261  showerholder.SLInfo.Phi.setNBins(nBinsPhi);
262  showerholder.SLInfo.Phi.setBin(showerholder.SLPhiBins);
263  //
264  // Shower
265  showerholder.SLCollection.assign(nBinsE, std::vector<std::vector<std::vector<CastorShowerEvent> > >());
266  showerholder.nEvtInBinE.assign(nBinsE, 0);
267  showerholder.nEvtInBinEta.assign(nBinsE, std::vector<int>(0));
268  showerholder.nEvtInBinPhi.assign(nBinsE, std::vector<std::vector<int> >());
269  for (int i = 0; i < nBinsE; i++) {
270  showerholder.SLCollection.at(i).assign(nBinsEta, std::vector<std::vector<CastorShowerEvent> >());
271  showerholder.nEvtInBinEta.at(i).assign(nBinsEta, 0);
272  showerholder.nEvtInBinPhi.at(i).assign(nBinsEta, std::vector<int>(0));
273  for (int j = 0; j < nBinsEta; j++) {
274  showerholder.SLCollection.at(i).at(j).assign(nBinsPhi, std::vector<CastorShowerEvent>());
275  showerholder.nEvtInBinPhi.at(i).at(j).assign(nBinsPhi, 0);
276  for (int k = 0; k < nBinsPhi; k++)
277  showerholder.SLCollection.at(i).at(j).at(k).assign(nEvtPerBinPhi, CastorShowerEvent());
278  }
279  }
280 }
281 
282 //===============================================================================================
283 
285  Finish();
286 
287  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker: End of process";
288 }
289 
290 //=================================================================== per EVENT
292  edm::LogVerbatim("HcalSim") << " CastorShowerLibraryMaker::Starting new job ";
293 }
294 
295 //==================================================================== per RUN
297  edm::LogVerbatim("HcalSim") << "\nCastorShowerLibraryMaker: Starting Run";
298 
299  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker: output event root file created";
300 
301  TString eventfilename = eventNtFileName;
302  theFile = new TFile(eventfilename, "RECREATE");
303  theTree = new TTree("CastorCherenkovPhotons", "Cherenkov Photons");
304 
305  Int_t split = 1;
306  Int_t bsize = 64000;
308  emShower = new CastorShowerEvent();
311  // Create Branchs
312  theTree->Branch("emShowerLibInfo.", "CastorShowerLibraryInfo", &emInfo, bsize, split);
313  theTree->Branch("emParticles.", "CastorShowerEvent", &emShower, bsize, split);
314  theTree->Branch("hadShowerLibInfo.", "CastorShowerLibraryInfo", &hadInfo, bsize, split);
315  theTree->Branch("hadParticles.", "CastorShowerEvent", &hadShower, bsize, split);
316 
317  // set the Info for electromagnetic shower
318  // set the energy bins info
323  // set the eta bins info
328  // set the eta bins info
333  // The same for the hadronic shower
334  // set the energy bins info
339  // set the eta bins info
344  // set the eta bins info
349  // int flag = theTree->GetBranch("CastorShowerLibInfo")->Fill();
350  // Loop on all leaves of this branch to fill Basket buffer.
351  // The function returns the number of bytes committed to the memory basket.
352  // If a write error occurs, the number of bytes returned is -1.
353  // If no data are written, because e.g. the branch is disabled,
354  // the number of bytes returned is 0.
355  // if(flag==-1) {
356  // edm::LogVerbatim("CastorAnalyzer") << " WARNING: Error writing to Branch \"CastorShowerLibInfo\" \n" ;
357  // } else
358  // if(flag==0) {
359  // edm::LogVerbatim("CastorAnalyzer") << " WARNING: No data written to Branch \"CastorShowerLibInfo\" \n" ;
360  // }
361 
362  // Initialize "accounting" variables
363 
364  eventIndex = 0;
365 }
366 
367 //=================================================================== per EVENT
369  eventIndex++;
370  stepIndex = 0;
371  InsideCastor = false;
372  PrimaryMomentum.clear();
373  PrimaryPosition.clear();
374  int NAccepted = 0;
375  // reset the pointers to the shower objects
376  SLShowerptr = nullptr;
377  MapOfSecondaries.clear();
378  thePrims.clear();
379  G4EventManager* e_mgr = G4EventManager::GetEventManager();
380  if (IsSLReady()) {
381  printSLstatus(-1, -1, -1);
382  update((EndOfRun*)nullptr);
383  return;
384  }
385 
386  thePrims = GetPrimary((*evt)());
387  for (unsigned int i = 0; i < thePrims.size(); i++) {
388  G4PrimaryParticle* thePrim = thePrims.at(i);
389  int particleType = thePrim->GetPDGcode();
390 
391  std::string SLType("");
392  if (particleType == 11) {
394  SLType = "Electromagnetic";
395  } else {
397  SLType = "Hadronic";
398  }
399  double px = 0., py = 0., pz = 0., pInit = 0., eta = 0., phi = 0.;
400  GetKinematics(thePrim, px, py, pz, pInit, eta, phi);
401  int ebin = FindEnergyBin(pInit);
402  int etabin = FindEtaBin(eta);
403  int phibin = FindPhiBin(phi);
404  if (verbosity)
405  edm::LogVerbatim("HcalSim") << "\n========================================================================"
406  << "\nBeginOfEvent: E : " << pInit << "\t bin : " << ebin
407  << "\n Eta : " << eta << "\t bin : " << etabin
408  << "\n Phi : " << phi << "\t bin : " << phibin
409  << "\n========================================================================";
410 
411  if (ebin < 0 || etabin < 0 || phibin < 0)
412  continue;
413  bool accept = false;
414  if (!(SLacceptEvent(ebin, etabin, phibin))) {
415  /*
416 // To increase the chance of a particle arriving at CASTOR inside a not full bin,
417 // check if there is available phase space in the neighboring bins
418  unsigned int ebin_min = std::max(0,ebin-3);
419  unsigned int eta_bin_min = std::max(0,etabin-2);
420  unsigned int eta_bin_max = std::min(etabin,etabin+2);
421  unsigned int phi_bin_min = std::max(0,phibin-2);
422  unsigned int phi_bin_max = std::min(phibin,phibin+2);
423  for(unsigned int i_ebin=ebin_min;i_ebin<=(unsigned int)ebin;i_ebin++) {
424  for (unsigned int i_etabin=eta_bin_min;i_etabin<=eta_bin_max;i_etabin++) {
425  for (unsigned int i_phibin=phi_bin_min;i_phibin<=phi_bin_max;i_phibin++) {
426  if (SLacceptEvent((int)i_ebin,(int)i_etabin,(int)i_phibin)) {accept=true;break;}
427  }
428  if (accept) break;
429  }
430  if (accept) break;
431  }
432 */
433  if (!accept)
434  edm::LogVerbatim("CastorShowerLibraryMaker")
435  << "Event not accepted for ebin=" << ebin << ",etabin=" << etabin << ",phibin=" << phibin;
436  } else {
437  accept = true;
438  }
439  if (accept)
440  NAccepted++;
441  }
442 
443  if (NAccepted == 0) {
444  const_cast<G4Event*>((*evt)())->SetEventAborted();
445  const_cast<G4Event*>((*evt)())->KeepTheEvent((G4bool) false);
446  e_mgr->AbortCurrentEvent();
447  }
448  SLShowerptr = nullptr;
449  //
450  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker: Processing Event Number: " << eventIndex;
451 }
452 
453 //=================================================================== per STEP
454 void CastorShowerLibraryMaker::update(const G4Step* aStep) {
455  static thread_local int CurrentPrimary = 0;
456  G4Track* trk = aStep->GetTrack();
457  int pvec_size;
458  if (trk->GetCurrentStepNumber() == 1) {
459  if (trk->GetParentID() == 0) {
460  CurrentPrimary = (int)trk->GetDynamicParticle()->GetPDGcode();
461  if (CurrentPrimary == 0)
462  SimG4Exception("CastorShowerLibraryMaker::update(G4Step) -> Primary particle undefined");
463  InsideCastor = false;
464  // Deactivate the physics process
466  G4ProcessManager* p_mgr = trk->GetDefinition()->GetProcessManager();
467  G4ProcessVector* pvec = p_mgr->GetProcessList();
468  pvec_size = pvec->size();
469  for (int i = 0; i < pvec_size; i++) {
470  G4VProcess* proc = (*pvec)(i);
471  if (proc->GetProcessName() != "Transportation" && proc->GetProcessName() != "Decay") {
472  edm::LogVerbatim("HcalSim") << "DeActivating process: " << proc->GetProcessName();
473  p_mgr->SetProcessActivation(proc, false);
474  }
475  }
476  }
477  // move track to z of CASTOR
478  G4ThreeVector pos;
479  pos.setZ(-14390);
480  double t = std::abs((pos.z() - trk->GetPosition().z())) / trk->GetVelocity();
481  double r = (pos.z() - trk->GetPosition().z()) / trk->GetMomentum().cosTheta();
482  pos.setX(r * sin(trk->GetMomentum().theta()) * cos(trk->GetMomentum().phi()) + trk->GetPosition().x());
483  pos.setY(r * sin(trk->GetMomentum().theta()) * sin(trk->GetMomentum().phi()) + trk->GetPosition().y());
484  trk->SetPosition(pos);
485  trk->SetGlobalTime(trk->GetGlobalTime() + t);
486  trk->AddTrackLength(r);
487  } else if (!InsideCastor) {
488  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker::update(G4Step) -> Killing spurious track";
489  trk->SetTrackStatus(fKillTrackAndSecondaries);
490  return;
491  }
492  MapOfSecondaries[CurrentPrimary].insert((int)trk->GetTrackID());
493  }
494  // Checks if primary already inside CASTOR
495  std::string CurVolume = trk->GetVolume()->GetName();
496  if (!InsideCastor && (
497  //CurVolume=="C3EF"||CurVolume=="C4EF"||CurVolume=="CAEL"||
498  //CurVolume=="CAHL"||CurVolume=="C3HF"||CurVolume=="C4HF")) {
499  //CurVolume=="CastorB"||
500  CurVolume == "CAST")) {
501  //CurVolume=="CAIR")) {
502  InsideCastor = true;
503  // Activate the physics process
504  if (trk->GetParentID() == 0 && DeActivatePhysicsProcess) {
505  G4ProcessManager* p_mgr = trk->GetDefinition()->GetProcessManager();
506  G4ProcessVector* pvec = p_mgr->GetProcessList();
507  pvec_size = pvec->size();
508  for (int i = 0; i < pvec_size; i++) {
509  G4VProcess* proc = (*pvec)(i);
510  if (proc->GetProcessName() != "Transportation" && proc->GetProcessName() != "Decay") {
511  edm::LogVerbatim("HcalSim") << " Activating process: " << proc->GetProcessName();
512  p_mgr->SetProcessActivation(proc, true);
513  }
514  }
515  }
516  //PrimaryMomentum[CurrentPrimary]=aStep->GetPreStepPoint()->GetMomentum();
517  // check fiducial eta and phi
518  if (trk->GetMomentum().phi() > MaxPhi || trk->GetMomentum().eta() > MaxEta) {
519  trk->SetTrackStatus(fKillTrackAndSecondaries);
520  InsideCastor = false;
521  return;
522  }
523  PrimaryMomentum[CurrentPrimary] = trk->GetMomentum();
524  PrimaryPosition[CurrentPrimary] = trk->GetPosition();
525  KillSecondaries(aStep);
526  return;
527  }
528  // Kill the secondaries if they have been produced before entering castor
529  if (CurrentPrimary != 0 && trk->GetParentID() == 0 && !InsideCastor) {
530  KillSecondaries(aStep);
531  if (verbosity) {
532  double pre_phi = aStep->GetPreStepPoint()->GetMomentum().phi();
533  double cur_phi = trk->GetMomentum().phi();
534  if (pre_phi != cur_phi) {
535  edm::LogVerbatim("HcalSim") << "Primary track phi : " << pre_phi << " changed in current step: " << cur_phi
536  << " by processes:";
537  const G4VProcess* proc = aStep->GetPreStepPoint()->GetProcessDefinedStep();
538  edm::LogVerbatim("HcalSim") << " " << proc->GetProcessName() << " In volume " << CurVolume;
539  }
540  }
541  }
542 
543  //==============================================
544  /*
545 */
546  /*
547  if(aStep->IsFirstStepInVolume()) {
548  edm::LogVerbatim("CastorShowerLibraryMaker") << "CastorShowerLibraryMaker::update(const G4Step * aStep):"
549  << "\n IsFirstStepInVolume , "
550  << "time = " << aStep->GetTrack()->GetGlobalTime() ;
551  }
552  stepIndex++;
553 */
554 }
555 
556 //================= End of EVENT ===============
558  // check if the job is done!
559  if ((*evt)()->IsAborted()) {
560  edm::LogVerbatim("HcalSim") << "\n========================================================================"
561  << "\nEndOfEvent: EVENT ABORTED"
562  << "\n========================================================================";
563  return;
564  }
565  //DynamicRangeFlatRandomEGunProducer* pgun = edm::DynamicRangeFlatRandomEGunKernel::get_instance();
566  //edm::LogVerbatim("HcalSim") << pgun->EGunMaxE();
567  /*
568  edm::LogVerbatim("HcalSim") << "Minimum energy in Particle Gun : " << pgun->EGunMinE() << "\nMaximum energy in Particle Gun : " << pgun->EGunMaxE();
569 */
570  if (verbosity)
571  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker: End of Event: " << eventIndex;
572  // Get the pointer to the primary particle
573  if (thePrims.empty()) {
574  edm::LogVerbatim("CastorShowerLibraryMaker") << "No valid primary particle found. Skipping event";
575  return;
576  }
577  // access to the G4 hit collections
578  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
579  int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("CastorFI");
580  CaloG4HitCollection* theCAFI = (CaloG4HitCollection*)allHC->GetHC(CAFIid);
581  if (verbosity)
582  edm::LogVerbatim("CastorShowerLibraryMaker") << " update(*evt) --> accessed all HC ";
583  edm::LogVerbatim("CastorShowerLibraryMaker") << "Found " << theCAFI->entries() << " hits in G4HitCollection";
584  if (theCAFI->entries() == 0) {
585  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n Empty G4HitCollection";
586  return;
587  }
588 
589  // Loop over primaries
590  int NEvtAccepted = 0;
591  int NHitInEvent = 0;
592  for (unsigned int i = 0; i < thePrims.size(); i++) {
593  G4PrimaryParticle* thePrim = thePrims.at(i);
594  if (!thePrim) {
595  edm::LogVerbatim("CastorShowerLibraryMaker") << "nullptr Pointer to the primary";
596  continue;
597  }
598  // Check primary particle type
599  int particleType = thePrim->GetPDGcode();
600 
601  // set the pointer to the shower collection
602  std::string SLType("");
603  if (particleType == 11) {
605  SLType = "Electromagnetic";
606  } else {
608  SLType = "Hadronic";
609  }
610  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n Primary (thePrim) trackID is " << thePrim->GetTrackID() << "\n";
611 
612  // Obtain primary particle's initial momentum (pInit)
613  double px = 0., py = 0., pz = 0., pInit = 0., eta = 0., phi = 0.;
614  GetKinematics(particleType, px, py, pz, pInit, eta, phi);
615  // Check if current event falls into any bin
616  // first: energy
617  if (pInit == 0) {
618  edm::LogVerbatim("CastorShowerLibraryMaker") << "Primary did not hit CASTOR";
619  continue;
620  }
621  int ebin = FindEnergyBin(pInit);
622  int etabin = FindEtaBin(eta);
623  int phibin = FindPhiBin(phi);
624  edm::LogVerbatim("HcalSim") << SLType;
626  if (!SLacceptEvent(ebin, etabin, phibin)) {
627  edm::LogVerbatim("CastorShowerLibraryMaker")
628  << "Event not accepted for ebin=" << ebin << ",etabin=" << etabin << ",phibin=" << phibin << "(" << pInit
629  << "," << eta << "," << phi << ")";
630  continue;
631  }
632  //
633  // event passed. Fill the vector accordingly
634  //
635  // Look for the Hit Collection
636  edm::LogVerbatim("CastorShowerLibraryMaker")
637  << "\n CastorShowerLibraryMaker::update(EndOfEvent * evt) - event #" << (*evt)()->GetEventID();
638 
639  /*
640  edm::LogVerbatim("HcalSim") << "Number of collections : " << allHC->GetNumberOfCollections();
641  for(int ii = 0;ii<allHC->GetNumberOfCollections();ii++)
642  edm::LogVerbatim("HcalSim") << "Name of collection " << ii << " : " << allHC->GetHC(ii)->GetName();
643 */
644 
645  CastorShowerEvent* shower = nullptr;
646  int cur_evt_idx = SLShowerptr->nEvtInBinPhi.at(ebin).at(etabin).at(phibin);
647  shower = &(SLShowerptr->SLCollection.at(ebin).at(etabin).at(phibin).at(cur_evt_idx));
648 
649  // Get Hit information
650  if (FillShowerEvent(theCAFI, shower, particleType)) {
651  // Primary particle information
652  /*
653  edm::LogVerbatim("CastorShowerLibraryMaker") << "New SL event: Primary = " << particleType << "; Energy = " << pInit << "; Eta = " << eta << "; Phi = " << phi << "; Nhits = " << shower->getNhit();
654 */
655  shower->setPrimE(pInit);
656  shower->setPrimEta(eta);
657  shower->setPrimPhi(phi);
661  SLnEvtInBinE(ebin)++;
664  NHitInEvent += shower->getNhit();
665  NEvtAccepted++;
666  } else {
667  shower->Clear();
668  }
669  }
670  // Check for unassociated energy
671  int thecafi_entries = theCAFI->entries();
672  if (NEvtAccepted == int(thePrims.size()) && thecafi_entries != NHitInEvent) {
673  edm::LogWarning("HcalSim") << "WARNING: Inconsistent Number of Hits -> Hits in collection: " << theCAFI->entries()
674  << " Hits in the showers: " << NHitInEvent;
675  double miss_energy = 0;
676  double tot_energy = 0;
677  GetMissingEnergy(theCAFI, miss_energy, tot_energy);
678  if (miss_energy > 0) {
679  edm::LogVerbatim("HcalSim") << "Total missing energy: " << miss_energy
680  << " for an incident energy: " << tot_energy;
681  }
682  }
683 }
684 
685 //========================= End of RUN ======================
687  // Fill the tree with the collected objects
688  if (!IsSLReady())
689  SimG4Exception("\n\nShower Library NOT READY.\n\n");
690 
691  unsigned int ibine, ibineta, ibinphi, ievt; // indexes for em shower
692  unsigned int jbine, jbineta, jbinphi, jevt; // indexes for had shower
693 
694  ibine = ibineta = ibinphi = ievt = jbine = jbineta = jbinphi = jevt = 0;
695 
696  int nEvtInTree = 0;
697  int nEMevt = emSLHolder.nEvtPerBinE * emSLHolder.SLEnergyBins.size();
698  int nHadevt = hadSLHolder.nEvtPerBinE * hadSLHolder.SLEnergyBins.size();
699  int maxEvtInTree = std::max(nEMevt, nHadevt);
700 
703 
704  while (nEvtInTree < maxEvtInTree) {
705  if (emShower)
706  emShower->Clear();
707  if (hadShower)
708  hadShower->Clear();
709  while (ibine < emSLHolder.SLEnergyBins.size() && nEMevt > 0) {
710  emShower = &(emSLHolder.SLCollection.at(ibine).at(ibineta).at(ibinphi).at(ievt));
711  ievt++;
712  if (ievt == emSLHolder.nEvtPerBinPhi) {
713  ievt = 0;
714  ibinphi++;
715  }
716  if (ibinphi == emSLHolder.SLPhiBins.size()) {
717  ibinphi = 0;
718  ibineta++;
719  }
720  if (ibineta == emSLHolder.SLEtaBins.size()) {
721  ibineta = 0;
722  ibine++;
723  }
724  break;
725  }
726  while (jbine < hadSLHolder.SLEnergyBins.size() && nHadevt > 0) {
727  hadShower = &(hadSLHolder.SLCollection.at(jbine).at(jbineta).at(jbinphi).at(jevt));
728  jevt++;
729  if (jevt == hadSLHolder.nEvtPerBinPhi) {
730  jevt = 0;
731  jbinphi++;
732  }
733  if (jbinphi == hadSLHolder.SLPhiBins.size()) {
734  jbinphi = 0;
735  jbineta++;
736  }
737  if (jbineta == hadSLHolder.SLEtaBins.size()) {
738  jbineta = 0;
739  jbine++;
740  }
741  break;
742  }
743  theTree->Fill();
744  nEvtInTree++;
745  if (nEvtInTree == 1) {
746  theTree->SetBranchStatus("emShowerLibInfo.", false);
747  theTree->SetBranchStatus("hadShowerLibInfo.", false);
748  }
749  }
750  // check if run is nullptr and exit
751  if (run == nullptr)
752  throw SimG4Exception("\n\nNumber of needed trigger events reached in CastorShowerLibraryMaker\n\n");
753 }
754 
755 //============================================================
757  // if (doNTcastorevent) {
758 
759  theFile->cd();
760  theTree->Write("", TObject::kOverwrite);
761  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker: Ntuple event written";
762  theFile->Close();
763  edm::LogVerbatim("HcalSim") << "CastorShowerLibraryMaker: Event file closed";
764 
765  // Delete pointers to objects, now that TTree has been written and TFile closed
766  // delete info;
767  // delete emShower;
768  // delete hadShower;
769  // }
770 }
772  //
773  // returns the integer index of the energy bin, taken from SLenergies vector
774  // returns -1 if ouside valid range
775  //
776  if (!SLShowerptr) {
777  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nFindEnergyBin can be called only after BeginOfEvent\n\n";
778  throw SimG4Exception("\n\nnullptr Pointer to the shower library.\n\n");
779  }
780  const std::vector<double>& SLenergies = SLShowerptr->SLEnergyBins;
781  if (energy >= SLenergies.back())
782  return SLenergies.size() - 1;
783 
784  unsigned int i = 0;
785  for (; i < SLenergies.size() - 1; i++)
786  if (energy >= SLenergies.at(i) && energy < SLenergies.at(i + 1))
787  return (int)i;
788 
789  // now i points to the last but 1 bin
790  if (energy >= SLenergies.at(i))
791  return (int)i;
792  // energy outside bin range
793  return -1;
794 }
796  //
797  // returns the integer index of the eta bin, taken from SLetas vector
798  // returns -1 if ouside valid range
799  //
800  if (!SLShowerptr) {
801  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nFindEtaBin can be called only after BeginOfEvent\n\n";
802  throw SimG4Exception("\n\nnullptr Pointer to the shower library.\n\n");
803  }
804  const std::vector<double>& SLetas = SLShowerptr->SLEtaBins;
805  if (eta >= SLetas.back())
806  return SLetas.size() - 1;
807  unsigned int i = 0;
808  for (; i < SLetas.size() - 1; i++)
809  if (eta >= SLetas.at(i) && eta < SLetas.at(i + 1))
810  return (int)i;
811  // now i points to the last but 1 bin
812  if (eta >= SLetas.at(i))
813  return (int)i;
814  // eta outside bin range
815  return -1;
816 }
818  //
819  // returns the integer index of the phi bin, taken from SLphis vector
820  // returns -1 if ouside valid range
821  //
822  // needs protection in case phi is outside range -pi,pi
823  //
824  if (!SLShowerptr) {
825  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nFindPhiBin can be called only after BeginOfEvent\n\n";
826  throw SimG4Exception("\n\nnullptr Pointer to the shower library.\n\n");
827  }
828  const std::vector<double>& SLphis = SLShowerptr->SLPhiBins;
829  if (phi >= SLphis.back())
830  return SLphis.size() - 1;
831  unsigned int i = 0;
832  for (; i < SLphis.size() - 1; i++)
833  if (phi >= SLphis.at(i) && phi < SLphis.at(i + 1))
834  return (int)i;
835  // now i points to the last but 1 bin
836  if (phi >= SLphis.at(i))
837  return (int)i;
838  // phi outside bin range
839  return -1;
840 }
842  // at this point, the pointer to the shower library should be nullptr
843  if (SLShowerptr) {
844  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nIsSLReady must be called when a new event starts.\n\n";
845  throw SimG4Exception("\n\nNOT nullptr Pointer to the shower library.\n\n");
846  }
847  // it is enough to check if all the energy bin is filled
848  if (DoEmSL) {
850  for (unsigned int i = 0; i < SLShowerptr->SLEnergyBins.size(); i++) {
851  if (!SLisEBinFilled(i)) {
852  SLShowerptr = nullptr;
853  return false;
854  }
855  }
856  }
857  if (DoHadSL) {
859  for (unsigned int i = 0; i < SLShowerptr->SLEnergyBins.size(); i++) {
860  if (!SLisEBinFilled(i)) {
861  SLShowerptr = nullptr;
862  return false;
863  }
864  }
865  }
866  SLShowerptr = nullptr;
867  return true;
868 }
870  int thePrim, double& px, double& py, double& pz, double& pInit, double& eta, double& phi) {
871  if (thePrim == 0)
872  return;
873  if (PrimaryMomentum.find(thePrim) == PrimaryMomentum.end())
874  return;
875  px = PrimaryMomentum[thePrim].x() / GeV;
876  py = PrimaryMomentum[thePrim].y() / GeV;
877  pz = PrimaryMomentum[thePrim].z() / GeV;
878  pInit = PrimaryMomentum[thePrim].mag() / GeV;
879  if (pInit == 0)
880  return;
881  double costheta = pz / pInit;
882  double theta = acos(std::min(std::max(costheta, double(-1.)), double(1.)));
883  eta = -log(tan(theta / 2.0));
884  phi = (px == 0 && py == 0) ? 0 : atan2(py, px); // the recommended way of calculating phi
885  phi = PrimaryMomentum[thePrim].phi();
886 }
888  G4PrimaryParticle* thePrim, double& px, double& py, double& pz, double& pInit, double& eta, double& phi) {
889  px = py = pz = phi = eta = 0.0;
890  if (thePrim == nullptr)
891  return;
892  px = thePrim->GetMomentum().x() / GeV;
893  py = thePrim->GetMomentum().y() / GeV;
894  pz = thePrim->GetMomentum().z() / GeV;
895  pInit = thePrim->GetMomentum().mag() / GeV;
896  //pInit = sqrt(pow(px,2.)+pow(py,2.)+pow(pz,2.));
897  if (pInit == 0)
898  return;
899  double costheta = pz / pInit;
900  double theta = acos(std::min(std::max(costheta, double(-1.)), double(1.)));
901  eta = -log(tan(theta / 2.0));
902  phi = (px == 0 && py == 0) ? 0 : atan2(py, px); // the recommended way of calculating phi
903  phi = thePrim->GetMomentum().phi();
904  //if (px!=0) phi=atan(py/px);
905 }
906 std::vector<G4PrimaryParticle*> CastorShowerLibraryMaker::GetPrimary(const G4Event* evt) {
907  // Find Primary info:
908  int trackID = 0;
909  std::vector<G4PrimaryParticle*> thePrims;
910  G4PrimaryParticle* thePrim = nullptr;
911  G4int nvertex = evt->GetNumberOfPrimaryVertex();
912  edm::LogVerbatim("CastorShowerLibraryMaker") << "Event has " << nvertex << " vertex";
913  if (nvertex != 1) {
914  edm::LogVerbatim("CastorShowerLibraryMaker") << "CastorShowerLibraryMaker::GetPrimary ERROR: no vertex";
915  return thePrims;
916  }
917 
918  for (int i = 0; i < nvertex; i++) {
919  G4PrimaryVertex* avertex = evt->GetPrimaryVertex(i);
920  if (avertex == nullptr) {
921  edm::LogVerbatim("CastorShowerLibraryMaker")
922  << "CastorShowerLibraryMaker::GetPrimary ERROR: pointer to vertex = 0";
923  continue;
924  }
925  unsigned int npart = avertex->GetNumberOfParticle();
926  if (npart != NPGParticle)
927  continue;
928  for (unsigned int j = 0; j < npart; j++) {
929  unsigned int k = 0;
930  //int test_pID = 0;
931  trackID = j;
932  thePrim = avertex->GetPrimary(trackID);
933  while (k < NPGParticle && PGParticleIDs.at(k++) != thePrim->GetPDGcode()) {
934  ;
935  };
936  if (k > NPGParticle)
937  continue; // ID not found in the requested particles
938  thePrims.push_back(thePrim);
939  }
940  }
941  return thePrims;
942 }
944  if (!SLShowerptr) {
945  edm::LogVerbatim("CastorShowerLibraryInfo") << "nullptr shower pointer. Printing both";
946  edm::LogVerbatim("HcalSim") << "Electromagnetic";
948  this->printSLstatus(ebin, etabin, phibin);
949  edm::LogVerbatim("HcalSim") << "Hadronic";
951  this->printSLstatus(ebin, etabin, phibin);
952  SLShowerptr = nullptr;
953  return;
954  }
955  int nBinsE = SLShowerptr->SLEnergyBins.size();
956  int nBinsEta = SLShowerptr->SLEtaBins.size();
957  int nBinsPhi = SLShowerptr->SLPhiBins.size();
958  std::vector<double> SLenergies = SLShowerptr->SLEnergyBins;
959  std::ostringstream st1;
960  for (int n = 0; n < 11 + (nBinsEta * nBinsPhi); n++)
961  st1 << "=";
962  edm::LogVerbatim("HcalSim") << st1.str();
963  for (int i = 0; i < nBinsE; i++) {
964  std::ostringstream st1;
965  st1 << "E bin " << std::setw(6) << SLenergies.at(i) << " : ";
966  for (int j = 0; j < nBinsEta; j++) {
967  for (int k = 0; k < nBinsPhi; k++) {
968  (SLisPhiBinFilled(i, j, k)) ? st1 << "1" : st1 << "-";
969  }
970  if (j < nBinsEta - 1)
971  st1 << "|";
972  }
973  st1 << " (" << SLnEvtInBinE(i) << " events)";
974  edm::LogVerbatim("HcalSim") << st1.str();
975  if (ebin != i)
976  continue;
977  std::ostringstream st2;
978  st2 << " ";
979  for (int j = 0; j < nBinsEta; j++) {
980  for (int k = 0; k < nBinsPhi; k++) {
981  (ebin == i && etabin == j && phibin == k) ? st2 << "^" : st2 << " ";
982  }
983  if (j < nBinsEta - 1)
984  st2 << " ";
985  }
986  edm::LogVerbatim("HcalSim") << st2.str();
987  }
988  std::ostringstream st2;
989  for (int n = 0; n < 11 + (nBinsEta * nBinsPhi); n++)
990  st2 << "=";
991  edm::LogVerbatim("HcalSim") << st2.str();
992 }
994  if (SLShowerptr == nullptr) {
995  edm::LogVerbatim("CastorShowerLibraryMaker::SLacceptEvent:") << "Error. nullptr pointer to CastorShowerEvent";
996  return false;
997  }
998  if (ebin < 0 || ebin >= int(SLShowerptr->SLEnergyBins.size()))
999  return false;
1000  if (SLisEBinFilled(ebin))
1001  return false;
1002 
1003  if (etabin < 0 || etabin >= int(SLShowerptr->SLEtaBins.size()))
1004  return false;
1006  return false;
1007 
1008  if (phibin < 0 || phibin >= int(SLShowerptr->SLPhiBins.size()))
1009  return false;
1011  return false;
1012  return true;
1013 }
1015  unsigned int volumeID = 0;
1016  double en_in_fi = 0.;
1017  //double totalEnergy = 0;
1018 
1019  int nentries = theCAFI->entries();
1020 
1021  // Compute Total Energy in CastorFI volume
1022  /*
1023  for(int ihit = 0; ihit < nentries; ihit++) {
1024  CaloG4Hit* aHit = (*theCAFI)[ihit];
1025  totalEnergy += aHit->getEnergyDeposit();
1026  }
1027 */
1028  if (!shower) {
1029  edm::LogVerbatim("CastorShowerLibraryMaker") << "Error. nullptr pointer to CastorShowerEvent";
1030  return false;
1031  }
1032 
1033  CastorNumberingScheme* theCastorNumScheme = new CastorNumberingScheme();
1034  // Hit position
1037  int nHits;
1038  nHits = 0;
1039  for (int ihit = 0; ihit < nentries; ihit++) {
1040  CaloG4Hit* aHit = (*theCAFI)[ihit];
1041  int hit_particleID = aHit->getTrackID();
1042  if (MapOfSecondaries[ipart].find(hit_particleID) == MapOfSecondaries[ipart].end()) {
1043  if (verbosity)
1044  edm::LogVerbatim("CastorShowerLibraryMaker") << "Skipping hit from trackID " << hit_particleID;
1045  continue;
1046  }
1047  volumeID = aHit->getUnitID();
1048  double hitEnergy = aHit->getEnergyDeposit();
1049  en_in_fi += aHit->getEnergyDeposit();
1050  float time = aHit->getTimeSlice();
1051  int zside, sector, zmodule;
1052  theCastorNumScheme->unpackIndex(volumeID, zside, sector, zmodule);
1053  entry = aHit->getEntry();
1054  position = aHit->getPosition();
1055  if (verbosity)
1056  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n side , sector , module = " << zside << " , " << sector
1057  << " , " << zmodule << "\n nphotons = " << hitEnergy;
1058 
1059  if (verbosity)
1060  edm::LogVerbatim("CastorShowerLibraryMaker")
1061  << "\n packIndex = " << theCastorNumScheme->packIndex(zside, sector, zmodule);
1062 
1063  if (verbosity && time > 100.) {
1064  edm::LogVerbatim("CastorShowerLibraryMaker")
1065  << "\n nentries = " << nentries << "\n time[" << ihit << "] = " << time << "\n trackID[" << ihit
1066  << "] = " << aHit->getTrackID() << "\n volumeID[" << ihit << "] = " << volumeID << "\n nphotons[" << ihit
1067  << "] = " << hitEnergy << "\n side, sector, module = " << zside << ", " << sector << ", " << zmodule
1068  << "\n packIndex " << theCastorNumScheme->packIndex(zside, sector, zmodule) << "\n X,Y,Z = " << entry.x()
1069  << "," << entry.y() << "," << entry.z();
1070  }
1071  if (verbosity)
1072  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n Incident Energy = " << aHit->getIncidentEnergy() << " \n";
1073 
1074  // CaloG4Hit information
1075  shower->setDetID(volumeID);
1076  shower->setHitPosition(position);
1077  shower->setNphotons(hitEnergy);
1078  shower->setTime(time);
1079  nHits++;
1080  }
1081  // Write number of hits to CastorShowerEvent instance
1082  if (nHits == 0) {
1083  edm::LogVerbatim("CastorShowerLibraryMaker") << "No hits found for this track (trackID=" << ipart << ").";
1084  if (theCastorNumScheme)
1085  delete theCastorNumScheme;
1086  return false;
1087  }
1088  shower->setNhit(nHits);
1089 
1090  // update the event counters
1091  if (theCastorNumScheme)
1092  delete theCastorNumScheme;
1093  return true;
1094 }
1096  if (!SLShowerptr) {
1097  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nSLnEvtInBinE can be called only after BeginOfEvent\n\n";
1098  throw SimG4Exception("\n\nnullptr Pointer to the shower library.");
1099  }
1100  return SLShowerptr->nEvtInBinE.at(ebin);
1101 }
1102 
1104  if (!SLShowerptr) {
1105  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nSLnEvtInBinEta can be called only after BeginOfEvent\n\n";
1106  throw SimG4Exception("\n\nnullptr Pointer to the shower library.");
1107  }
1108  return SLShowerptr->nEvtInBinEta.at(ebin).at(etabin);
1109 }
1110 
1112  if (!SLShowerptr) {
1113  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nSLnEvtInBinPhi can be called only after BeginOfEvent\n\n";
1114  throw SimG4Exception("\n\nnullptr Pointer to the shower library.");
1115  }
1116  return SLShowerptr->nEvtInBinPhi.at(ebin).at(etabin).at(phibin);
1117 }
1119  if (!SLShowerptr) {
1120  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nSLisEBinFilled can be called only after BeginOfEvent\n\n";
1121  throw SimG4Exception("\n\nnullptr Pointer to the shower library.");
1122  }
1124  return false;
1125  return true;
1126 }
1128  if (!SLShowerptr) {
1129  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nSLisEtaBinFilled can be called only after BeginOfEvent\n\n";
1130  throw SimG4Exception("\n\nnullptr Pointer to the shower library.");
1131  }
1133  return false;
1134  return true;
1135 }
1137  if (!SLShowerptr) {
1138  edm::LogVerbatim("CastorShowerLibraryMaker") << "\n\nSLisPhiBinFilled can be called only after BeginOfEvent\n\n";
1139  throw SimG4Exception("\n\nnullptr Pointer to the shower library.");
1140  }
1142  return false;
1143  return true;
1144 }
1146  const G4TrackVector* p_sec = aStep->GetSecondary();
1147  for (int i = 0; i < int(p_sec->size()); i++) {
1148  edm::LogVerbatim("HcalSim") << "Killing track ID " << p_sec->at(i)->GetTrackID()
1149  << " and its secondaries Produced by Process "
1150  << p_sec->at(i)->GetCreatorProcess()->GetProcessName() << " in the volume "
1151  << aStep->GetTrack()->GetVolume()->GetName();
1152  p_sec->at(i)->SetTrackStatus(fKillTrackAndSecondaries);
1153  }
1154 }
1155 
1156 void CastorShowerLibraryMaker::GetMissingEnergy(CaloG4HitCollection* theCAFI, double& miss_energy, double& tot_energy) {
1157  // Get the total deposited energy and the one from hit not associated to a primary
1158  miss_energy = 0;
1159  tot_energy = 0;
1160  int nhits = theCAFI->entries();
1161  for (int ihit = 0; ihit < nhits; ihit++) {
1162  int id = (*theCAFI)[ihit]->getTrackID();
1163  tot_energy += (*theCAFI)[ihit]->getEnergyDeposit();
1164  int hit_prim = 0;
1165  for (unsigned int i = 0; i < thePrims.size(); i++) {
1166  int particleType = thePrims.at(i)->GetPDGcode();
1168  hit_prim = particleType;
1169  }
1170  if (hit_prim == 0) {
1171  edm::LogVerbatim("HcalSim") << "Track ID " << id << " produced a hit which is not associated with a primary.";
1172  miss_energy += (*theCAFI)[ihit]->getEnergyDeposit();
1173  }
1174  }
1175 }
1176 
1179 
void Clear(Option_t *option="") override
Log< level::Info, true > LogVerbatim
int getTrackID() const
Definition: CaloG4Hit.h:64
void setNBins(unsigned int n)
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void setTime(float t)
void setPrimEta(float eta)
void setPrimZ(float z)
void setDetID(unsigned int id)
int & SLnEvtInBinEta(int ebin, int etabin)
void setPrimPhi(float phi)
void setBin(double val)
std::vector< G4PrimaryParticle * > thePrims
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Trktree trk
Definition: Trktree.cc:2
bool FillShowerEvent(CaloG4HitCollection *, CastorShowerEvent *, int)
std::map< int, G4ThreeVector > PrimaryPosition
void KillSecondaries(const G4Step *step)
double npart
Definition: HydjetWrapper.h:48
int zside(DetId const &)
std::vector< std::vector< std::vector< std::vector< CastorShowerEvent > > > > SLBin3D
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:31
bool SLisEtaBinFilled(int ebin, int etabin)
static void unpackIndex(const uint32_t &idx, int &z, int &sector, int &zmodule)
void setNEvtPerBin(unsigned int n)
std::map< int, std::set< int > > MapOfSecondaries
std::vector< G4PrimaryParticle * > GetPrimary(const G4Event *)
CastorShowerLibraryInfo * hadInfo
math::XYZPoint getPosition() const
Definition: CaloG4Hit.h:52
math::XYZPoint getEntry() const
Definition: CaloG4Hit.h:46
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< std::vector< std::vector< int > > > nEvtInBinPhi
CastorShowerLibraryInfo * emInfo
CastorShowerLibraryMaker(const edm::ParameterSet &p)
void setPrimE(float e)
void setPrimY(float y)
void GetMissingEnergy(CaloG4HitCollection *, double &, double &)
void GetKinematics(G4PrimaryParticle *, double &px, double &py, double &pz, double &pInit, double &eta, double &phi)
void setPrimX(float x)
int & SLnEvtInBinPhi(int ebin, int etabin, int phibin)
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
std::vector< std::vector< int > > nEvtInBinEta
double getEnergyDeposit() const
Definition: CaloG4Hit.h:79
uint32_t getUnitID() const
Definition: CaloG4Hit.h:66
void setNEvts(unsigned int n)
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
static int position[264][3]
Definition: ReadPGInfo.cc:289
bool SLisPhiBinFilled(int ebin, int etabin, int phibin)
std::map< int, G4ThreeVector > PrimaryMomentum
double getTimeSlice() const
Definition: CaloG4Hit.h:67
step
Definition: StallMonitor.cc:83
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
Log< level::Warning, false > LogWarning
static uint32_t packIndex(int z, int sector, int zmodule)
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
void setNphotons(float np)
double getIncidentEnergy() const
Definition: CaloG4Hit.h:61
void setNhit(unsigned int i)
void setHitPosition(const Point &p)
unsigned int getNhit()
MPlex< T, D1, D2, N > atan2(const MPlex< T, D1, D2, N > &y, const MPlex< T, D1, D2, N > &x)
Definition: Matriplex.h:648