CMS 3D CMS Logo

CastorTestAnalysis.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Forward
4 // Class : CastorTestAnalysis
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: P. Katsas
10 // Created: 02/2007
11 //
12 
20 
24 
28 
29 #include "G4SDManager.hh"
30 #include "G4Step.hh"
31 #include "G4Track.hh"
32 #include "G4Event.hh"
33 #include "G4PrimaryVertex.hh"
34 #include "G4VProcess.hh"
35 #include "G4HCofThisEvent.hh"
36 #include "G4UserEventAction.hh"
37 
38 #include <CLHEP/Units/GlobalSystemOfUnits.h>
39 #include <CLHEP/Units/GlobalPhysicalConstants.h>
40 #include <CLHEP/Random/Randomize.h>
41 
42 #include "TROOT.h"
43 #include "TFile.h"
44 #include "TH1.h"
45 #include "TH2.h"
46 #include "TProfile.h"
47 #include "TNtuple.h"
48 #include "TRandom.h"
49 #include "TLorentzVector.h"
50 #include "TUnixSystem.h"
51 #include "TSystem.h"
52 #include "TMath.h"
53 #include "TF1.h"
54 
55 #include <cassert>
56 #include <cmath>
57 #include <iostream>
58 #include <iomanip>
59 #include <map>
60 #include <string>
61 #include <vector>
62 
63 //#define EDM_ML_DEBUG
64 
66  public Observer<const BeginOfJob *>,
67  public Observer<const BeginOfRun *>,
68  public Observer<const EndOfRun *>,
69  public Observer<const BeginOfEvent *>,
70  public Observer<const EndOfEvent *>,
71  public Observer<const G4Step *> {
72 public:
74  ~CastorTestAnalysis() override;
75 
76 private:
77  // observer classes
78  void update(const BeginOfJob *run) override;
79  void update(const BeginOfRun *run) override;
80  void update(const EndOfRun *run) override;
81  void update(const BeginOfEvent *evt) override;
82  void update(const EndOfEvent *evt) override;
83  void update(const G4Step *step) override;
84 
85 private:
87  void Finish();
88 
89  int verbosity;
94 
97 
98  TNtuple *castorstepntuple;
100 
102 
106 
107  Float_t castorsteparray[14];
108  Float_t castoreventarray[11];
109 };
110 
126 };
127 
140 };
141 
143  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("CastorTestAnalysis");
144  verbosity = m_Anal.getParameter<int>("Verbosity");
145  doNTcastorstep = m_Anal.getParameter<int>("StepNtupleFlag");
146  doNTcastorevent = m_Anal.getParameter<int>("EventNtupleFlag");
147  stepNtFileName = m_Anal.getParameter<std::string>("StepNtupleFileName");
148  eventNtFileName = m_Anal.getParameter<std::string>("EventNtupleFileName");
149 
150  if (verbosity > 0) {
151  edm::LogVerbatim("ForwardSim") << std::endl;
152  edm::LogVerbatim("ForwardSim") << "============================================================================";
153  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis:: Initialized as observer";
154  if (doNTcastorstep > 0) {
155  edm::LogVerbatim("ForwardSim") << " Step Ntuple will be created";
156  edm::LogVerbatim("ForwardSim") << " Step Ntuple file: " << stepNtFileName;
157  }
158  if (doNTcastorevent > 0) {
159  edm::LogVerbatim("ForwardSim") << " Event Ntuple will be created";
160  edm::LogVerbatim("ForwardSim") << " Step Ntuple file: " << stepNtFileName;
161  }
162  edm::LogVerbatim("ForwardSim") << "============================================================================";
163  edm::LogVerbatim("ForwardSim") << std::endl;
164  }
165  if (doNTcastorstep > 0)
167  new TNtuple("NTcastorstep", "NTcastorstep", "evt:trackid:charge:pdgcode:x:y:z:stepl:stepe:eta:phi:vpx:vpy:vpz");
168 
169  if (doNTcastorevent > 0)
170  castoreventntuple = new TNtuple(
171  "NTcastorevent", "NTcastorevent", "evt:ihit:detector:sector:module:enem:totalenergy:hitenergy:x:y:z");
172 }
173 
175  //destructor of CastorTestAnalysis
176 
177  Finish();
178  if (verbosity > 0) {
179  edm::LogVerbatim("ForwardSim") << std::endl << "End of CastorTestAnalysis";
180  }
181 
182  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: End of process";
183 }
184 
185 //=================================================================== per EVENT
186 void CastorTestAnalysis::update(const BeginOfJob *job) { edm::LogVerbatim("ForwardSim") << " Starting new job "; }
187 
188 //==================================================================== per RUN
190  edm::LogVerbatim("ForwardSim") << std::endl << "CastorTestAnalysis: Starting Run";
191  if (doNTcastorstep) {
192  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: output step root file created";
193  TString stepfilename = stepNtFileName;
194  castorOutputStepFile = new TFile(stepfilename, "RECREATE");
195  }
196 
197  if (doNTcastorevent) {
198  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: output event root file created";
199  TString stepfilename = eventNtFileName;
200  castorOutputEventFile = new TFile(stepfilename, "RECREATE");
201  }
202 
203  eventIndex = 0;
204 }
205 
207  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: Processing Event Number: " << eventIndex;
208  eventIndex++;
209  stepIndex = 0;
210 }
211 
212 void CastorTestAnalysis::update(const G4Step *aStep) {
213  stepIndex++;
214 
215  if (doNTcastorstep) {
216  G4StepPoint *preStepPoint = aStep->GetPreStepPoint();
217  // G4StepPoint * postStepPoint= aStep->GetPostStepPoint();
218  G4double stepL = aStep->GetStepLength();
219  G4double stepE = aStep->GetTotalEnergyDeposit();
220 
221  if (verbosity >= 2)
222  edm::LogVerbatim("ForwardSim") << "Step " << stepL << ", " << stepE;
223 
224  G4Track *theTrack = aStep->GetTrack();
225  G4int theTrackID = theTrack->GetTrackID();
226  G4double theCharge = theTrack->GetDynamicParticle()->GetCharge();
227  // G4String particleType = theTrack->GetDefinition()->GetParticleName();
228  G4int pdgcode = theTrack->GetDefinition()->GetPDGEncoding();
229 
230  const G4ThreeVector &vert_mom = theTrack->GetVertexMomentumDirection();
231  G4double vpx = vert_mom.x();
232  G4double vpy = vert_mom.y();
233  G4double vpz = vert_mom.z();
234  double eta = 0.5 * log((1. + vpz) / (1. - vpz));
235  double phi = atan2(vpy, vpx);
236 
237  const G4ThreeVector &hitPoint = preStepPoint->GetPosition();
238 
239  // Fill-in ntuple
240  // castorsteparray[ntcastors_evt] = (*evt)()->GetEventID();
242  castorsteparray[ntcastors_trackid] = (float)theTrackID;
245  castorsteparray[ntcastors_x] = hitPoint.x();
246  castorsteparray[ntcastors_y] = hitPoint.y();
247  castorsteparray[ntcastors_z] = hitPoint.z();
255 
256  /*
257  edm::LogVerbatim("ForwardSim") << "TrackID: " << theTrackID;
258  edm::LogVerbatim("ForwardSim") << " StepN: "<< theTrack->GetCurrentStepNumber();
259  edm::LogVerbatim("ForwardSim") << " ParentID: " << aStep->GetTrack()->GetParentID();
260  edm::LogVerbatim("ForwardSim") << " PDG: " << pdgcode;
261  edm::LogVerbatim("ForwardSim") << " X,Y,Z (mm): " << theTrack->GetPosition().x() << "," << theTrack->GetPosition().y() << "," << theTrack->GetPosition().z();
262  edm::LogVerbatim("ForwardSim") << " KE (MeV): " << theTrack->GetKineticEnergy();
263  edm::LogVerbatim("ForwardSim") << " Total EDep (MeV): " << aStep->GetTotalEnergyDeposit();
264  edm::LogVerbatim("ForwardSim") << " StepLength (mm): " << aStep->GetStepLength();
265  edm::LogVerbatim("ForwardSim") << " TrackLength (mm): " << theTrack->GetTrackLength();
266 
267  if ( theTrack->GetNextVolume() != 0 )
268  edm::LogVerbatim("ForwardSim") <<" NextVolume: " << theTrack->GetNextVolume()->GetName();
269  else
270  edm::LogVerbatim("ForwardSim") <<" NextVolume: OutOfWorld";
271 
272  if(aStep->GetPostStepPoint()->GetProcessDefinedStep() != NULL)
273  edm::LogVerbatim("ForwardSim") << " ProcessName: "<< aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName();
274  else
275  edm::LogVerbatim("ForwardSim") <<" ProcessName: UserLimit";
276 
277 
278  edm::LogVerbatim("ForwardSim") << std::endl;
279  */
280 
281 #ifdef EDM_ML_DEBUG
282  if (theTrack->GetNextVolume() != 0)
283  edm::LogVerbatim("ForwardSim") << " NextVolume: " << theTrack->GetNextVolume()->GetName();
284  else
285  edm::LogVerbatim("ForwardSim") << " NextVolume: OutOfWorld";
286 #endif
287 
288  //fill ntuple with step level information
290  }
291 }
292 
293 //================= End of EVENT ===============
295  // Look for the Hit Collection
296  edm::LogVerbatim("ForwardSim") << std::endl
297  << "CastorTest::update(EndOfEvent * evt) - event #" << (*evt)()->GetEventID();
298 
299  // access to the G4 hit collections
300  G4HCofThisEvent *allHC = (*evt)()->GetHCofThisEvent();
301  edm::LogVerbatim("ForwardSim") << "update(*evt) --> accessed all HC";
302 
303  int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("CastorFI");
304 
305  CaloG4HitCollection *theCAFI = (CaloG4HitCollection *)allHC->GetHC(CAFIid);
306 
308  // CastorNumberingScheme *theCastorNumScheme = new CastorNumberingScheme();
309 
310  /*
311  unsigned int volumeID=0;
312  int det, zside, sector, zmodule;
313  std::map<int,float,std::less<int> > themap;
314  double totalEnergy = 0;
315  double hitEnergy = 0;
316  double en_in_fi = 0.;
317  double en_in_pl = 0.;
318 */
319  // double en_in_bu = 0.;
320  // double en_in_tu = 0.;
321 
322  if (doNTcastorevent) {
323  eventGlobalHit = 0;
324  // int eventGlobalHit = 0 ;
325 
326  // Check FI TBranch for Hits
327  if (theCAFI->entries() > 0)
328  getCastorBranchData(theCAFI);
329 
330  // Find Primary info:
331  int trackID = 0;
332 #ifdef EDM_ML_DEBUG
333  int particleType = 0;
334 #endif
335  G4PrimaryParticle *thePrim = nullptr;
336  G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
337  edm::LogVerbatim("ForwardSim") << "Event has " << nvertex << " vertex";
338  if (nvertex == 0)
339  edm::LogVerbatim("ForwardSim") << "CASTORTest End Of Event ERROR: no vertex";
340 
341  for (int i = 0; i < nvertex; i++) {
342  G4PrimaryVertex *avertex = (*evt)()->GetPrimaryVertex(i);
343  if (avertex == nullptr) {
344  edm::LogVerbatim("ForwardSim") << "CASTORTest End Of Event ERR: pointer to vertex = 0";
345  continue;
346  }
347  edm::LogVerbatim("ForwardSim") << "Vertex number :" << i;
348  int npart = avertex->GetNumberOfParticle();
349  if (npart == 0)
350  edm::LogVerbatim("ForwardSim") << "CASTORTest End Of Event ERR: no primary!";
351  if (thePrim == nullptr)
352  thePrim = avertex->GetPrimary(trackID);
353  }
354 
355  double px = 0., py = 0., pz = 0., pInit = 0;
356 #ifdef EDM_ML_DEBUG
357  double eta = 0., phi = 0.;
358 #endif
359  if (thePrim != nullptr) {
360  px = thePrim->GetPx();
361  py = thePrim->GetPy();
362  pz = thePrim->GetPz();
363  pInit = sqrt(pow(px, 2.) + pow(py, 2.) + pow(pz, 2.));
364  if (pInit == 0) {
365  edm::LogVerbatim("ForwardSim") << "CASTORTest End Of Event ERR: primary has p=0 ";
366 #ifdef EDM_ML_DEBUG
367  } else {
368  float costheta = pz / pInit;
369  float theta = acos(std::min(std::max(costheta, float(-1.)), float(1.)));
370  eta = -log(tan(theta / 2));
371 
372  if (px != 0)
373  phi = atan(py / px);
374 #endif
375  }
376 #ifdef EDM_ML_DEBUG
377  particleType = thePrim->GetPDGcode();
378 #endif
379  } else {
380  edm::LogVerbatim("ForwardSim") << "CASTORTest End Of Event ERR: could not find primary ";
381  }
382 #ifdef EDM_ML_DEBUG
383  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: Particle Type " << particleType << " p/eta/phi " << pInit
384  << ", " << eta << ", " << phi;
385 #endif
386  }
387 
388  int iEvt = (*evt)()->GetEventID();
389  if (iEvt < 10)
390  edm::LogVerbatim("ForwardSim") << " CastorTest Event " << iEvt;
391  else if ((iEvt < 100) && (iEvt % 10 == 0))
392  edm::LogVerbatim("ForwardSim") << " CastorTest Event " << iEvt;
393  else if ((iEvt < 1000) && (iEvt % 100 == 0))
394  edm::LogVerbatim("ForwardSim") << " CastorTest Event " << iEvt;
395  else if ((iEvt < 10000) && (iEvt % 1000 == 0))
396  edm::LogVerbatim("ForwardSim") << " CastorTest Event " << iEvt;
397 
398  edm::LogVerbatim("ForwardSim") << std::endl << "===>>> Done writing user histograms ";
399 }
400 
402 
403 //===================================================================
405  int nentries = hc->entries();
406 
407  if (nentries > 0) {
408  unsigned int volumeID = 0;
409  int det = 0, zside, sector, zmodule;
410  std::map<int, float, std::less<int> > themap;
411  double totalEnergy = 0;
412  double hitEnergy = 0;
413  double en_in_sd = 0.;
414 
415  for (int ihit = 0; ihit < nentries; ihit++) {
416  CaloG4Hit *aHit = (*hc)[ihit];
417  totalEnergy += aHit->getEnergyDeposit();
418  }
419 
420  for (int ihit = 0; ihit < nentries; ihit++) {
421  CaloG4Hit *aHit = (*hc)[ihit];
422  volumeID = aHit->getUnitID();
423  hitEnergy = aHit->getEnergyDeposit();
424  en_in_sd += aHit->getEnergyDeposit();
425  // double enEm = aHit->getEM();
426  // double enHad = aHit->getHadr();
427 
428  themap[volumeID] += aHit->getEnergyDeposit();
429  // int det, zside, sector, zmodule;
430  theCastorNumScheme->unpackIndex(volumeID, zside, sector, zmodule);
431 
432  // det = 2 ; // det=2/3 for CAFI/CAPL
433 
435  // castoreventarray[ntcastore_ihit] = (float)ihit;
440  castoreventarray[ntcastore_enem] = en_in_sd;
441  castoreventarray[ntcastore_enhad] = totalEnergy;
446  // castoreventarray[ntcastore_x] = aHit->getEntry().x();
447  // castoreventarray[ntcastore_y] = aHit->getEntry().y();
448  // castoreventarray[ntcastore_z] = aHit->getEntry().z();
449 
451 
452  eventGlobalHit++;
453  }
454  } // nentries > 0
455 }
456 
457 //===================================================================
458 
460  if (doNTcastorstep) {
461  castorOutputStepFile->cd();
462  castorstepntuple->Write();
463  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: Ntuple step written";
464  castorOutputStepFile->Close();
465  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: Step file closed";
466  }
467 
468  if (doNTcastorevent) {
469  castorOutputEventFile->cd();
470  castoreventntuple->Write("", TObject::kOverwrite);
471  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: Ntuple event written";
472  castorOutputEventFile->Close();
473  edm::LogVerbatim("ForwardSim") << "CastorTestAnalysis: Event file closed";
474  }
475 }
476 
479 
Log< level::Info, true > LogVerbatim
ntcastors_elements
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double npart
Definition: HydjetWrapper.h:46
int zside(DetId const &)
ntcastore_elements
static void unpackIndex(const uint32_t &idx, int &z, int &sector, int &zmodule)
CastorNumberingScheme * theCastorNumScheme
math::XYZPoint getPosition() const
Definition: CaloG4Hit.h:52
T sqrt(T t)
Definition: SSEVec.h:19
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
double getEnergyDeposit() const
Definition: CaloG4Hit.h:79
uint32_t getUnitID() const
Definition: CaloG4Hit.h:66
CastorTestAnalysis(const edm::ParameterSet &p)
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
step
Definition: StallMonitor.cc:98
Geom::Theta< T > theta() const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
void getCastorBranchData(const CaloG4HitCollection *hc)