CMS 3D CMS Logo

ZdcTestAnalysis.cc
Go to the documentation of this file.
1 // File: ZdcTestAnalysis.cc
3 // Date: 03.06 Edmundo Garcia
4 // Description: simulation analysis steering code
5 //
10 
14 
22 
23 #include "G4SDManager.hh"
24 #include "G4Step.hh"
25 #include "G4Track.hh"
26 #include "G4Event.hh"
27 #include "G4PrimaryVertex.hh"
28 #include "G4VProcess.hh"
29 #include "G4HCofThisEvent.hh"
30 #include "G4UserEventAction.hh"
31 
32 #include <CLHEP/Units/SystemOfUnits.h>
33 #include <CLHEP/Units/GlobalPhysicalConstants.h>
34 #include <CLHEP/Random/Randomize.h>
35 
36 #include "TROOT.h"
37 #include "TH1.h"
38 #include "TH2.h"
39 #include "TProfile.h"
40 #include "TNtuple.h"
41 #include "TRandom.h"
42 #include "TLorentzVector.h"
43 #include "TUnixSystem.h"
44 #include "TSystem.h"
45 #include "TMath.h"
46 #include "TF1.h"
47 #include "TFile.h"
48 
49 #include <cassert>
50 #include <cmath>
51 #include <iostream>
52 #include <iomanip>
53 #include <map>
54 #include <string>
55 #include <vector>
56 
57 class ZdcTestAnalysis : public SimWatcher,
58  public Observer<const BeginOfJob*>,
59  public Observer<const BeginOfRun*>,
60  public Observer<const EndOfRun*>,
61  public Observer<const BeginOfEvent*>,
62  public Observer<const EndOfEvent*>,
63  public Observer<const G4Step*> {
64 public:
66  ~ZdcTestAnalysis() override;
67 
68 private:
69  // observer classes
70  void update(const BeginOfJob* run) override;
71  void update(const BeginOfRun* run) override;
72  void update(const EndOfRun* run) override;
73  void update(const BeginOfEvent* evt) override;
74  void update(const EndOfEvent* evt) override;
75  void update(const G4Step* step) override;
76 
77  void finish();
78 
79  int verbosity;
84 
87 
88  TNtuple* zdcstepntuple;
89  TNtuple* zdceventntuple;
90 
92  int stepIndex;
93 
94  Float_t zdcsteparray[18];
95  Float_t zdceventarray[16];
96 };
97 
117 };
118 
136 };
137 
139  //constructor
140  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("ZdcTestAnalysis");
141  verbosity = m_Anal.getParameter<int>("Verbosity");
142  doNTzdcstep = m_Anal.getParameter<int>("StepNtupleFlag");
143  doNTzdcevent = m_Anal.getParameter<int>("EventNtupleFlag");
144  stepNtFileName = m_Anal.getParameter<std::string>("StepNtupleFileName");
145  eventNtFileName = m_Anal.getParameter<std::string>("EventNtupleFileName");
146 
147  if (verbosity > 0)
148  edm::LogVerbatim("ZdcAnalysis") << "\n============================================================================";
149  edm::LogVerbatim("ZdcAnalysis") << "ZdcTestAnalysis:: Initialized as observer";
150  if (doNTzdcstep > 0) {
151  edm::LogVerbatim("ZdcAnalysis") << " Step Ntuple will be created";
152  edm::LogVerbatim("ZdcAnalysis") << " Step Ntuple file: " << stepNtFileName;
153  }
154  if (doNTzdcevent > 0) {
155  edm::LogVerbatim("ZdcAnalysis") << " Event Ntuple will be created";
156  edm::LogVerbatim("ZdcAnalysis") << " Step Ntuple file: " << stepNtFileName;
157  }
158  edm::LogVerbatim("ZdcAnalysis") << "============================================================================"
159  << std::endl;
160 
161  if (doNTzdcstep > 0)
162  zdcstepntuple =
163  new TNtuple("NTzdcstep",
164  "NTzdcstep",
165  "evt:trackid:charge:pdgcode:x:y:z:stepl:stepe:eta:phi:vpx:vpy:vpz:idx:idl:pvtype:ncherphot");
166 
167  if (doNTzdcevent > 0)
169  new TNtuple("NTzdcevent",
170  "NTzdcevent",
171  "evt:ihit:fiberid:zside:subdet:layer:fiber:channel:enem:enhad:hitenergy:x:y:z:time:etot");
172 
173  //theZdcSD = new ZdcSD("ZDCHITSB", new ZdcNumberingScheme());
174 }
175 
177  // destructor
178  finish();
179 }
180 
182  //job
183  edm::LogVerbatim("ZdcAnalysis") << "beggining of job";
184 }
185 
186 //==================================================================== per RUN
188  //run
189 
190  edm::LogVerbatim("ZdcAnalysis") << "\nZdcTestAnalysis: Begining of Run";
191  if (doNTzdcstep) {
192  edm::LogVerbatim("ZdcAnalysis") << "ZDCTestAnalysis: output step file created";
193  TString stepfilename = stepNtFileName;
194  zdcOutputStepFile = new TFile(stepfilename, "RECREATE");
195  }
196 
197  if (doNTzdcevent) {
198  edm::LogVerbatim("ZdcAnalysis") << "ZDCTestAnalysis: output event file created";
199  TString stepfilename = eventNtFileName;
200  zdcOutputEventFile = new TFile(stepfilename, "RECREATE");
201  }
202 
203  eventIndex = 0;
204 }
205 
207  //event
208  edm::LogVerbatim("ZdcAnalysis") << "ZdcTest: Processing Event Number: " << eventIndex;
209  eventIndex++;
210  stepIndex = 0;
211 }
212 
213 void ZdcTestAnalysis::update(const G4Step* aStep) {
214  //step;
215  stepIndex++;
216 
217  if (doNTzdcstep) {
218  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
219  // G4StepPoint * postStepPoint= aStep->GetPostStepPoint();
220  G4double stepL = aStep->GetStepLength();
221  G4double stepE = aStep->GetTotalEnergyDeposit();
222 
223  if (verbosity >= 2)
224  edm::LogVerbatim("ZdcAnalysis") << "Step " << stepL << "," << stepE;
225 
226  G4Track* theTrack = aStep->GetTrack();
227  G4int theTrackID = theTrack->GetTrackID();
228  G4double theCharge = theTrack->GetDynamicParticle()->GetCharge();
229  G4String particleType = theTrack->GetDefinition()->GetParticleName();
230  G4int pdgcode = theTrack->GetDefinition()->GetPDGEncoding();
231 
232  const G4ThreeVector& vert_mom = theTrack->GetVertexMomentumDirection();
233  G4double vpx = vert_mom.x();
234  G4double vpy = vert_mom.y();
235  G4double vpz = vert_mom.z();
236  double eta = 0.5 * log((1. + vpz) / (1. - vpz));
237  double phi = atan2(vpy, vpx);
238 
239  const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
240  G4ThreeVector localPoint = theTrack->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
241 
242  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
243  int idx = touch->GetReplicaNumber(0);
244  int idLayer = -1;
245  int thePVtype = -1;
246 
247  int historyDepth = touch->GetHistoryDepth();
248 
249  if (historyDepth > 0) {
250  std::vector<int> theReplicaNumbers(historyDepth);
251  std::vector<G4VPhysicalVolume*> thePhysicalVolumes(historyDepth);
252  std::vector<G4String> thePVnames(historyDepth);
253  std::vector<G4LogicalVolume*> theLogicalVolumes(historyDepth);
254  std::vector<G4String> theLVnames(historyDepth);
255  std::vector<G4Material*> theMaterials(historyDepth);
256  std::vector<G4String> theMaterialNames(historyDepth);
257 
258  for (int jj = 0; jj < historyDepth; jj++) {
259  theReplicaNumbers[jj] = touch->GetReplicaNumber(jj);
260  thePhysicalVolumes[jj] = touch->GetVolume(jj);
261  thePVnames[jj] = thePhysicalVolumes[jj]->GetName();
262  theLogicalVolumes[jj] = thePhysicalVolumes[jj]->GetLogicalVolume();
263  theLVnames[jj] = theLogicalVolumes[jj]->GetName();
264  theMaterials[jj] = theLogicalVolumes[jj]->GetMaterial();
265  theMaterialNames[jj] = theMaterials[jj]->GetName();
266  if (verbosity >= 2)
267  edm::LogVerbatim("ZdcAnalysis") << " GHD " << jj << ": " << theReplicaNumbers[jj] << "," << thePVnames[jj]
268  << "," << theLVnames[jj] << "," << theMaterialNames[jj];
269  }
270 
271  idLayer = theReplicaNumbers[1];
272  if (thePVnames[0] == "ZDC_EMLayer")
273  thePVtype = 1;
274  else if (thePVnames[0] == "ZDC_EMAbsorber")
275  thePVtype = 2;
276  else if (thePVnames[0] == "ZDC_EMFiber")
277  thePVtype = 3;
278  else if (thePVnames[0] == "ZDC_HadLayer")
279  thePVtype = 7;
280  else if (thePVnames[0] == "ZDC_HadAbsorber")
281  thePVtype = 8;
282  else if (thePVnames[0] == "ZDC_HadFiber")
283  thePVtype = 9;
284  else if (thePVnames[0] == "ZDC_PhobosLayer")
285  thePVtype = 11;
286  else if (thePVnames[0] == "ZDC_PhobosAbsorber")
287  thePVtype = 12;
288  else if (thePVnames[0] == "ZDC_PhobosFiber")
289  thePVtype = 13;
290  else {
291  thePVtype = 0;
292  if (verbosity >= 2)
293  edm::LogVerbatim("ZdcAnalysis") << " pvtype=0 hd=" << historyDepth << " " << theReplicaNumbers[0] << ","
294  << thePVnames[0] << "," << theLVnames[0] << "," << theMaterialNames[0];
295  }
296  } else if (historyDepth == 0) {
297  int theReplicaNumber = touch->GetReplicaNumber(0);
298  G4VPhysicalVolume* thePhysicalVolume = touch->GetVolume(0);
299  const G4String& thePVname = thePhysicalVolume->GetName();
300  G4LogicalVolume* theLogicalVolume = thePhysicalVolume->GetLogicalVolume();
301  const G4String& theLVname = theLogicalVolume->GetName();
302  G4Material* theMaterial = theLogicalVolume->GetMaterial();
303  const G4String& theMaterialName = theMaterial->GetName();
304  if (verbosity >= 2)
305  edm::LogVerbatim("ZdcAnalysis") << " hd=0 " << theReplicaNumber << "," << thePVname << "," << theLVname << ","
306  << theMaterialName;
307  } else {
308  edm::LogVerbatim("ZdcAnalysis") << " hd<0: hd=" << historyDepth;
309  }
310 
311  double NCherPhot = -1;
313  zdcsteparray[ntzdcs_trackid] = (float)theTrackID;
315  zdcsteparray[ntzdcs_pdgcode] = (float)pdgcode;
316  zdcsteparray[ntzdcs_x] = hitPoint.x();
317  zdcsteparray[ntzdcs_y] = hitPoint.y();
318  zdcsteparray[ntzdcs_z] = hitPoint.z();
319  zdcsteparray[ntzdcs_stepl] = stepL;
320  zdcsteparray[ntzdcs_stepe] = stepE;
323  zdcsteparray[ntzdcs_vpx] = vpx;
324  zdcsteparray[ntzdcs_vpy] = vpy;
325  zdcsteparray[ntzdcs_vpz] = vpz;
327  zdcsteparray[ntzdcs_idl] = (float)idLayer;
328  zdcsteparray[ntzdcs_pvtype] = thePVtype;
329  zdcsteparray[ntzdcs_ncherphot] = NCherPhot;
331  }
332 }
333 
335  //end of event
336 
337  // Look for the Hit Collection
338  edm::LogVerbatim("ZdcAnalysis") << "\nZdcTest::upDate(const EndOfEvent * evt) - event #" << (*evt)()->GetEventID()
339  << "\n # of aSteps followed in event = " << stepIndex;
340 
341  // access to the G4 hit collections
342  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
343  edm::LogVerbatim("ZdcAnalysis") << " accessed all HC";
344 
345  int theZDCHCid = G4SDManager::GetSDMpointer()->GetCollectionID("ZDCHITS");
346  edm::LogVerbatim("ZdcAnalysis") << " - theZDCHCid = " << theZDCHCid;
347 
348  CaloG4HitCollection* theZDCHC = (CaloG4HitCollection*)allHC->GetHC(theZDCHCid);
349  edm::LogVerbatim("ZdcAnalysis") << " - theZDCHC = " << theZDCHC;
350 
351  //float ETot = 0.;
352  int maxTime = 0;
353  int fiberID = 0;
354  unsigned int unsignedfiberID = 0;
355  std::map<int, float, std::less<int> > energyInFibers;
356  std::map<int, float, std::less<int> > primaries;
357  float totalEnergy = 0;
358  int nentries = theZDCHC->entries();
359  edm::LogVerbatim("ZdcAnalysis") << " theZDCHC has " << nentries << " entries";
360 
361  if (doNTzdcevent) {
362  if (nentries > 0) {
363  for (int ihit = 0; ihit < nentries; ihit++) {
364  CaloG4Hit* caloHit = (*theZDCHC)[ihit];
365  totalEnergy += caloHit->getEnergyDeposit();
366  }
367 
368  for (int ihit = 0; ihit < nentries; ihit++) {
369  CaloG4Hit* aHit = (*theZDCHC)[ihit];
370  fiberID = aHit->getUnitID();
371  unsignedfiberID = aHit->getUnitID();
372  double enEm = aHit->getEM();
373  double enHad = aHit->getHadr();
374  math::XYZPoint hitPoint = aHit->getPosition();
375  double hitEnergy = aHit->getEnergyDeposit();
376  if (verbosity >= 1)
377  edm::LogVerbatim("ZdcAnalysis")
378  << " entry #" << ihit << ": fiberID=0x" << std::hex << fiberID << std::dec << "; enEm=" << enEm
379  << "; enHad=" << enHad << "; hitEnergy=" << hitEnergy << "z=" << hitPoint.z();
380  energyInFibers[fiberID] += enEm + enHad;
381  primaries[aHit->getTrackID()] += enEm + enHad;
382  float time = aHit->getTimeSliceID();
383  if (time > maxTime)
384  maxTime = (int)time;
385 
386  int thesubdet, thelayer, thefiber, thechannel, thez;
387  ZdcNumberingScheme::unpackZdcIndex(fiberID, thesubdet, thelayer, thefiber, thechannel, thez);
388  int unsignedsubdet, unsignedlayer, unsignedfiber, unsignedchannel, unsignedz;
390  unsignedfiberID, unsignedsubdet, unsignedlayer, unsignedfiber, unsignedchannel, unsignedz);
391 
392  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
393  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
394  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
395  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
396 
399  zdceventarray[ntzdce_fiberid] = (float)fiberID;
401  zdceventarray[ntzdce_subdet] = (float)thesubdet;
402  zdceventarray[ntzdce_layer] = (float)thelayer;
403  zdceventarray[ntzdce_fiber] = (float)thefiber;
404  zdceventarray[ntzdce_channel] = (float)thechannel;
405  zdceventarray[ntzdce_enem] = enEm;
406  zdceventarray[ntzdce_enhad] = enHad;
407  zdceventarray[ntzdce_hitenergy] = hitEnergy;
408  zdceventarray[ntzdce_x] = hitPoint.x();
409  zdceventarray[ntzdce_y] = hitPoint.y();
410  zdceventarray[ntzdce_z] = hitPoint.z();
412  zdceventarray[ntzdce_etot] = totalEnergy;
414  }
415 
416  /*
417  for (std::map<int, float, std::less<int> >::iterator is = energyInFibers.begin(); is != energyInFibers.end();
418  is++) {
419  ETot = (*is).second;
420  }
421  */
422 
423  // Find Primary info:
424  int trackID = 0;
425  G4PrimaryParticle* thePrim = nullptr;
426  G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
427  edm::LogVerbatim("ZdcAnalysis") << "Event has " << nvertex << " vertex";
428  if (nvertex == 0)
429  edm::LogVerbatim("ZdcAnalysis") << "ZdcTest End Of Event ERROR: no vertex";
430 
431  for (int i = 0; i < nvertex; i++) {
432  G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
433  if (avertex == nullptr) {
434  edm::LogVerbatim("ZdcAnalysis") << "ZdcTest End Of Event ERR: pointer to vertex = 0";
435  } else {
436  edm::LogVerbatim("ZdcAnalysis") << "Vertex number :" << i;
437  int npart = avertex->GetNumberOfParticle();
438  if (npart == 0)
439  edm::LogVerbatim("ZdcAnalysis") << "ZdcTest End Of Event ERR: no primary!";
440  if (thePrim == nullptr)
441  thePrim = avertex->GetPrimary(trackID);
442  }
443  }
444 
445  double px = 0., py = 0., pz = 0.;
446  double pInit = 0.;
447 
448  if (thePrim != nullptr) {
449  px = thePrim->GetPx();
450  py = thePrim->GetPy();
451  pz = thePrim->GetPz();
452  pInit = sqrt(pow(px, 2.) + pow(py, 2.) + pow(pz, 2.));
453  if (pInit == 0) {
454  edm::LogVerbatim("ZdcAnalysis") << "ZdcTest End Of Event ERR: primary has p=0 ";
455  }
456  } else {
457  edm::LogVerbatim("ZdcAnalysis") << "ZdcTest End Of Event ERR: could not find primary ";
458  }
459 
460  } // nentries > 0
461 
462  } // createNTzdcevent
463 
464  int iEvt = (*evt)()->GetEventID();
465  if (iEvt < 10)
466  edm::LogVerbatim("ZdcAnalysis") << " ZdcTest Event " << iEvt;
467  else if ((iEvt < 100) && (iEvt % 10 == 0))
468  edm::LogVerbatim("ZdcAnalysis") << " ZdcTest Event " << iEvt;
469  else if ((iEvt < 1000) && (iEvt % 100 == 0))
470  edm::LogVerbatim("ZdcAnalysis") << " ZdcTest Event " << iEvt;
471  else if ((iEvt < 10000) && (iEvt % 1000 == 0))
472  edm::LogVerbatim("ZdcAnalysis") << " ZdcTest Event " << iEvt;
473 }
474 
476 
478  if (doNTzdcstep) {
479  zdcOutputStepFile->cd();
480  zdcstepntuple->Write();
481  edm::LogVerbatim("ZdcAnalysis") << "ZdcTestAnalysis: Ntuple step written for event: " << eventIndex;
482  zdcOutputStepFile->Close();
483  edm::LogVerbatim("ZdcAnalysis") << "ZdcTestAnalysis: Step file closed";
484  }
485 
486  if (doNTzdcevent) {
487  zdcOutputEventFile->cd();
488  zdceventntuple->Write("", TObject::kOverwrite);
489  edm::LogVerbatim("ZdcAnalysis") << "ZdcTestAnalysis: Ntuple event written for event: " << eventIndex;
490  zdcOutputEventFile->Close();
491  edm::LogVerbatim("ZdcAnalysis") << "ZdcTestAnalysis: Event file closed";
492  }
493 }
494 
497 
Log< level::Info, true > LogVerbatim
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
int getTrackID() const
Definition: CaloG4Hit.h:64
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
Float_t zdceventarray[16]
TFile * zdcOutputEventFile
double getEM() const
Definition: CaloG4Hit.h:55
double npart
Definition: HydjetWrapper.h:48
ntzdce_elements
std::string stepNtFileName
double getHadr() const
Definition: CaloG4Hit.h:58
TFile * zdcOutputStepFile
~ZdcTestAnalysis() override
math::XYZPoint getPosition() const
Definition: CaloG4Hit.h:52
T sqrt(T t)
Definition: SSEVec.h:23
TNtuple * zdceventntuple
TNtuple * zdcstepntuple
ntzdcs_elements
int getTimeSliceID() const
Definition: CaloG4Hit.h:68
ZdcTestAnalysis(const edm::ParameterSet &p)
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
static void unpackZdcIndex(const unsigned int &idx, int &subDet, int &layer, int &fiber, int &channel, int &z)
double getEnergyDeposit() const
Definition: CaloG4Hit.h:79
uint32_t getUnitID() const
Definition: CaloG4Hit.h:66
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
std::string eventNtFileName
Float_t zdcsteparray[18]
step
Definition: StallMonitor.cc:83
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
MPlex< T, D1, D2, N > atan2(const MPlex< T, D1, D2, N > &y, const MPlex< T, D1, D2, N > &x)
Definition: Matriplex.h:648