CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ZdcTestAnalysis.cc
Go to the documentation of this file.
1 
5 
8 
9 #include "TFile.h"
10 #include <cmath>
11 #include <iostream>
12 #include <iomanip>
13 
33 };
34 
52 };
53 
55  //constructor
56  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("ZdcTestAnalysis");
57  verbosity = m_Anal.getParameter<int>("Verbosity");
58  doNTzdcstep = m_Anal.getParameter<int>("StepNtupleFlag");
59  doNTzdcevent = m_Anal.getParameter<int>("EventNtupleFlag");
60  stepNtFileName = m_Anal.getParameter<std::string>("StepNtupleFileName");
61  eventNtFileName = m_Anal.getParameter<std::string>("EventNtupleFileName");
62 
63  if (verbosity > 0)
64  std::cout << std::endl;
65  std::cout << "============================================================================" << std::endl;
66  std::cout << "ZdcTestAnalysis:: Initialized as observer" << std::endl;
67  if (doNTzdcstep > 0) {
68  std::cout << " Step Ntuple will be created" << std::endl;
69  std::cout << " Step Ntuple file: " << stepNtFileName << std::endl;
70  }
71  if (doNTzdcevent > 0) {
72  std::cout << " Event Ntuple will be created" << std::endl;
73  std::cout << " Step Ntuple file: " << stepNtFileName << std::endl;
74  }
75  std::cout << "============================================================================" << std::endl;
76  std::cout << std::endl;
77 
78  if (doNTzdcstep > 0)
80  new TNtuple("NTzdcstep",
81  "NTzdcstep",
82  "evt:trackid:charge:pdgcode:x:y:z:stepl:stepe:eta:phi:vpx:vpy:vpz:idx:idl:pvtype:ncherphot");
83 
84  if (doNTzdcevent > 0)
86  new TNtuple("NTzdcevent",
87  "NTzdcevent",
88  "evt:ihit:fiberid:zside:subdet:layer:fiber:channel:enem:enhad:hitenergy:x:y:z:time:etot");
89 
90  theZdcNumScheme = nullptr;
91  //theZdcSD = new ZdcSD("ZDCHITSB", new ZdcNumberingScheme());
92 }
93 
95  // destructor
96  finish();
97  delete theZdcNumScheme;
98 }
99 
101  //job
102  std::cout << "beggining of job" << std::endl;
103  ;
104 }
105 
106 //==================================================================== per RUN
108  //run
109 
110  std::cout << std::endl << "ZdcTestAnalysis: Begining of Run" << std::endl;
111  if (doNTzdcstep) {
112  std::cout << "ZDCTestAnalysis: output step file created" << std::endl;
113  TString stepfilename = stepNtFileName;
114  zdcOutputStepFile = new TFile(stepfilename, "RECREATE");
115  }
116 
117  if (doNTzdcevent) {
118  std::cout << "ZDCTestAnalysis: output event file created" << std::endl;
119  TString stepfilename = eventNtFileName;
120  zdcOutputEventFile = new TFile(stepfilename, "RECREATE");
121  }
122 
123  eventIndex = 0;
124 }
125 
127  //event
128  std::cout << "ZdcTest: Processing Event Number: " << eventIndex << std::endl;
129  eventIndex++;
130  stepIndex = 0;
131 }
132 
133 void ZdcTestAnalysis::update(const G4Step* aStep) {
134  //step;
135  stepIndex++;
136 
137  if (doNTzdcstep) {
138  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
139  // G4StepPoint * postStepPoint= aStep->GetPostStepPoint();
140  G4double stepL = aStep->GetStepLength();
141  G4double stepE = aStep->GetTotalEnergyDeposit();
142 
143  if (verbosity >= 2)
144  std::cout << "Step " << stepL << "," << stepE << std::endl;
145 
146  G4Track* theTrack = aStep->GetTrack();
147  G4int theTrackID = theTrack->GetTrackID();
148  G4double theCharge = theTrack->GetDynamicParticle()->GetCharge();
149  G4String particleType = theTrack->GetDefinition()->GetParticleName();
150  G4int pdgcode = theTrack->GetDefinition()->GetPDGEncoding();
151 
152  const G4ThreeVector& vert_mom = theTrack->GetVertexMomentumDirection();
153  G4double vpx = vert_mom.x();
154  G4double vpy = vert_mom.y();
155  G4double vpz = vert_mom.z();
156  double eta = 0.5 * log((1. + vpz) / (1. - vpz));
157  double phi = atan2(vpy, vpx);
158 
159  const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
160  G4ThreeVector localPoint = theTrack->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
161 
162  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
163  int idx = touch->GetReplicaNumber(0);
164  int idLayer = -1;
165  int thePVtype = -1;
166 
167  int historyDepth = touch->GetHistoryDepth();
168 
169  if (historyDepth > 0) {
170  std::vector<int> theReplicaNumbers(historyDepth);
171  std::vector<G4VPhysicalVolume*> thePhysicalVolumes(historyDepth);
172  std::vector<G4String> thePVnames(historyDepth);
173  std::vector<G4LogicalVolume*> theLogicalVolumes(historyDepth);
174  std::vector<G4String> theLVnames(historyDepth);
175  std::vector<G4Material*> theMaterials(historyDepth);
176  std::vector<G4String> theMaterialNames(historyDepth);
177 
178  for (int jj = 0; jj < historyDepth; jj++) {
179  theReplicaNumbers[jj] = touch->GetReplicaNumber(jj);
180  thePhysicalVolumes[jj] = touch->GetVolume(jj);
181  thePVnames[jj] = thePhysicalVolumes[jj]->GetName();
182  theLogicalVolumes[jj] = thePhysicalVolumes[jj]->GetLogicalVolume();
183  theLVnames[jj] = theLogicalVolumes[jj]->GetName();
184  theMaterials[jj] = theLogicalVolumes[jj]->GetMaterial();
185  theMaterialNames[jj] = theMaterials[jj]->GetName();
186  if (verbosity >= 2)
187  std::cout << " GHD " << jj << ": " << theReplicaNumbers[jj] << "," << thePVnames[jj] << "," << theLVnames[jj]
188  << "," << theMaterialNames[jj] << std::endl;
189  }
190 
191  idLayer = theReplicaNumbers[1];
192  if (thePVnames[0] == "ZDC_EMLayer")
193  thePVtype = 1;
194  else if (thePVnames[0] == "ZDC_EMAbsorber")
195  thePVtype = 2;
196  else if (thePVnames[0] == "ZDC_EMFiber")
197  thePVtype = 3;
198  else if (thePVnames[0] == "ZDC_HadLayer")
199  thePVtype = 7;
200  else if (thePVnames[0] == "ZDC_HadAbsorber")
201  thePVtype = 8;
202  else if (thePVnames[0] == "ZDC_HadFiber")
203  thePVtype = 9;
204  else if (thePVnames[0] == "ZDC_PhobosLayer")
205  thePVtype = 11;
206  else if (thePVnames[0] == "ZDC_PhobosAbsorber")
207  thePVtype = 12;
208  else if (thePVnames[0] == "ZDC_PhobosFiber")
209  thePVtype = 13;
210  else {
211  thePVtype = 0;
212  if (verbosity >= 2)
213  std::cout << " pvtype=0 hd=" << historyDepth << " " << theReplicaNumbers[0] << "," << thePVnames[0] << ","
214  << theLVnames[0] << "," << theMaterialNames[0] << std::endl;
215  }
216  } else if (historyDepth == 0) {
217  int theReplicaNumber = touch->GetReplicaNumber(0);
218  G4VPhysicalVolume* thePhysicalVolume = touch->GetVolume(0);
219  const G4String& thePVname = thePhysicalVolume->GetName();
220  G4LogicalVolume* theLogicalVolume = thePhysicalVolume->GetLogicalVolume();
221  const G4String& theLVname = theLogicalVolume->GetName();
222  G4Material* theMaterial = theLogicalVolume->GetMaterial();
223  const G4String& theMaterialName = theMaterial->GetName();
224  if (verbosity >= 2)
225  std::cout << " hd=0 " << theReplicaNumber << "," << thePVname << "," << theLVname << "," << theMaterialName
226  << std::endl;
227  } else {
228  std::cout << " hd<0: hd=" << historyDepth << std::endl;
229  }
230 
231  double NCherPhot = -1;
233  zdcsteparray[ntzdcs_trackid] = (float)theTrackID;
235  zdcsteparray[ntzdcs_pdgcode] = (float)pdgcode;
236  zdcsteparray[ntzdcs_x] = hitPoint.x();
237  zdcsteparray[ntzdcs_y] = hitPoint.y();
238  zdcsteparray[ntzdcs_z] = hitPoint.z();
239  zdcsteparray[ntzdcs_stepl] = stepL;
240  zdcsteparray[ntzdcs_stepe] = stepE;
243  zdcsteparray[ntzdcs_vpx] = vpx;
244  zdcsteparray[ntzdcs_vpy] = vpy;
245  zdcsteparray[ntzdcs_vpz] = vpz;
246  zdcsteparray[ntzdcs_idx] = (float)idx;
247  zdcsteparray[ntzdcs_idl] = (float)idLayer;
248  zdcsteparray[ntzdcs_pvtype] = thePVtype;
249  zdcsteparray[ntzdcs_ncherphot] = NCherPhot;
251  }
252 }
253 
255  //end of event
256 
257  // Look for the Hit Collection
258  std::cout << std::endl
259  << "ZdcTest::upDate(const EndOfEvent * evt) - event #" << (*evt)()->GetEventID() << std::endl
260  << " # of aSteps followed in event = " << stepIndex << std::endl;
261 
262  // access to the G4 hit collections
263  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
264  std::cout << " accessed all HC";
265 
266  int theZDCHCid = G4SDManager::GetSDMpointer()->GetCollectionID("ZDCHITS");
267  std::cout << " - theZDCHCid = " << theZDCHCid;
268 
269  CaloG4HitCollection* theZDCHC = (CaloG4HitCollection*)allHC->GetHC(theZDCHCid);
270  std::cout << " - theZDCHC = " << theZDCHC << std::endl;
271 
272  if (!theZdcNumScheme) {
274  }
275 
276  float ETot = 0., SEnergy = 0.;
277  int maxTime = 0;
278  int fiberID = 0;
279  unsigned int unsignedfiberID = 0;
280  std::map<int, float, std::less<int> > energyInFibers;
281  std::map<int, float, std::less<int> > primaries;
282  float totalEnergy = 0;
283  int nentries = theZDCHC->entries();
284  std::cout << " theZDCHC has " << nentries << " entries" << std::endl;
285 
286  if (doNTzdcevent) {
287  if (nentries > 0) {
288  for (int ihit = 0; ihit < nentries; ihit++) {
289  CaloG4Hit* caloHit = (*theZDCHC)[ihit];
290  totalEnergy += caloHit->getEnergyDeposit();
291  }
292 
293  for (int ihit = 0; ihit < nentries; ihit++) {
294  CaloG4Hit* aHit = (*theZDCHC)[ihit];
295  fiberID = aHit->getUnitID();
296  unsignedfiberID = aHit->getUnitID();
297  double enEm = aHit->getEM();
298  double enHad = aHit->getHadr();
299  math::XYZPoint hitPoint = aHit->getPosition();
300  double hitEnergy = aHit->getEnergyDeposit();
301  if (verbosity >= 1)
302  std::cout << " entry #" << ihit << ": fiberID=0x" << std::hex << fiberID << std::dec << "; enEm=" << enEm
303  << "; enHad=" << enHad << "; hitEnergy=" << hitEnergy << "z=" << hitPoint.z() << std::endl;
304  energyInFibers[fiberID] += enEm + enHad;
305  primaries[aHit->getTrackID()] += enEm + enHad;
306  float time = aHit->getTimeSliceID();
307  if (time > maxTime)
308  maxTime = (int)time;
309 
310  int thesubdet, thelayer, thefiber, thechannel, thez;
311  theZdcNumScheme->unpackZdcIndex(fiberID, thesubdet, thelayer, thefiber, thechannel, thez);
312  int unsignedsubdet, unsignedlayer, unsignedfiber, unsignedchannel, unsignedz;
314  unsignedfiberID, unsignedsubdet, unsignedlayer, unsignedfiber, unsignedchannel, unsignedz);
315 
316  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
317  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
318  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
319  // unsigned int packidx1 = packZdcIndex(thesubdet, thelayer, thefiber, thechannel, thez);
320 
323  zdceventarray[ntzdce_fiberid] = (float)fiberID;
325  zdceventarray[ntzdce_subdet] = (float)thesubdet;
326  zdceventarray[ntzdce_layer] = (float)thelayer;
327  zdceventarray[ntzdce_fiber] = (float)thefiber;
328  zdceventarray[ntzdce_channel] = (float)thechannel;
329  zdceventarray[ntzdce_enem] = enEm;
330  zdceventarray[ntzdce_enhad] = enHad;
331  zdceventarray[ntzdce_hitenergy] = hitEnergy;
332  zdceventarray[ntzdce_x] = hitPoint.x();
333  zdceventarray[ntzdce_y] = hitPoint.y();
334  zdceventarray[ntzdce_z] = hitPoint.z();
336  zdceventarray[ntzdce_etot] = totalEnergy;
338  }
339 
340  for (std::map<int, float, std::less<int> >::iterator is = energyInFibers.begin(); is != energyInFibers.end();
341  is++) {
342  ETot = (*is).second;
343  SEnergy += ETot;
344  }
345 
346  // Find Primary info:
347  int trackID = 0;
348  G4PrimaryParticle* thePrim = nullptr;
349  G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
350  std::cout << "Event has " << nvertex << " vertex" << std::endl;
351  if (nvertex == 0)
352  std::cout << "ZdcTest End Of Event ERROR: no vertex" << std::endl;
353 
354  for (int i = 0; i < nvertex; i++) {
355  G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
356  if (avertex == nullptr) {
357  std::cout << "ZdcTest End Of Event ERR: pointer to vertex = 0" << std::endl;
358  } else {
359  std::cout << "Vertex number :" << i << std::endl;
360  int npart = avertex->GetNumberOfParticle();
361  if (npart == 0)
362  std::cout << "ZdcTest End Of Event ERR: no primary!" << std::endl;
363  if (thePrim == nullptr)
364  thePrim = avertex->GetPrimary(trackID);
365  }
366  }
367 
368  double px = 0., py = 0., pz = 0.;
369  double pInit = 0.;
370 
371  if (thePrim != nullptr) {
372  px = thePrim->GetPx();
373  py = thePrim->GetPy();
374  pz = thePrim->GetPz();
375  pInit = sqrt(pow(px, 2.) + pow(py, 2.) + pow(pz, 2.));
376  if (pInit == 0) {
377  std::cout << "ZdcTest End Of Event ERR: primary has p=0 " << std::endl;
378  }
379  } else {
380  std::cout << "ZdcTest End Of Event ERR: could not find primary " << std::endl;
381  }
382 
383  } // nentries > 0
384 
385  } // createNTzdcevent
386 
387  int iEvt = (*evt)()->GetEventID();
388  if (iEvt < 10)
389  std::cout << " ZdcTest Event " << iEvt << std::endl;
390  else if ((iEvt < 100) && (iEvt % 10 == 0))
391  std::cout << " ZdcTest Event " << iEvt << std::endl;
392  else if ((iEvt < 1000) && (iEvt % 100 == 0))
393  std::cout << " ZdcTest Event " << iEvt << std::endl;
394  else if ((iEvt < 10000) && (iEvt % 1000 == 0))
395  std::cout << " ZdcTest Event " << iEvt << std::endl;
396 }
397 
399 
401  if (doNTzdcstep) {
402  zdcOutputStepFile->cd();
403  zdcstepntuple->Write();
404  std::cout << "ZdcTestAnalysis: Ntuple step written for event: " << eventIndex << std::endl;
405  zdcOutputStepFile->Close();
406  std::cout << "ZdcTestAnalysis: Step file closed" << std::endl;
407  }
408 
409  if (doNTzdcevent) {
410  zdcOutputEventFile->cd();
411  zdceventntuple->Write("", TObject::kOverwrite);
412  std::cout << "ZdcTestAnalysis: Ntuple event written for event: " << eventIndex << std::endl;
413  zdcOutputEventFile->Close();
414  std::cout << "ZdcTestAnalysis: Event file closed" << std::endl;
415  }
416 }
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
T getParameter(std::string const &) const
math::XYZPoint getPosition() const
Definition: CaloG4Hit.h:52
Float_t zdceventarray[16]
TFile * zdcOutputEventFile
double npart
Definition: HydjetWrapper.h:46
ntzdce_elements
std::string stepNtFileName
TFile * zdcOutputStepFile
~ZdcTestAnalysis() override
T sqrt(T t)
Definition: SSEVec.h:19
TNtuple * zdceventntuple
ZdcNumberingScheme * theZdcNumScheme
TNtuple * zdcstepntuple
ntzdcs_elements
int getTrackID() const
Definition: CaloG4Hit.h:64
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)
int getTimeSliceID() const
Definition: CaloG4Hit.h:67
double getEM() const
Definition: CaloG4Hit.h:55
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
std::string eventNtFileName
Float_t zdcsteparray[18]
uint32_t getUnitID() const
Definition: CaloG4Hit.h:65
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
double getHadr() const
Definition: CaloG4Hit.h:58
double getEnergyDeposit() const
Definition: CaloG4Hit.h:77