CMS 3D CMS Logo

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