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 
15 
18 
19 #include "TFile.h"
20 #include <cmath>
21 #include <iostream>
22 #include <iomanip>
23 
24 #define debugLog
25 
28 };
29 
32 };
33 
35 
36  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("CastorTestAnalysis");
37  verbosity = m_Anal.getParameter<int>("Verbosity");
38  doNTcastorstep = m_Anal.getParameter<int>("StepNtupleFlag");
39  doNTcastorevent = m_Anal.getParameter<int>("EventNtupleFlag");
40  stepNtFileName = m_Anal.getParameter<std::string>("StepNtupleFileName");
41  eventNtFileName = m_Anal.getParameter<std::string>("EventNtupleFileName");
42 
43  if (verbosity > 0) {
44  std::cout<<std::endl;
45  std::cout<<"============================================================================"<<std::endl;
46  std::cout << "CastorTestAnalysis:: Initialized as observer"<< std::endl;
47  if (doNTcastorstep > 0){
48  std::cout <<" Step Ntuple will be created"<< std::endl;
49  std::cout <<" Step Ntuple file: "<<stepNtFileName<<std::endl;
50  }
51  if (doNTcastorevent > 0){
52  std::cout <<" Event Ntuple will be created"<< std::endl;
53  std::cout <<" Step Ntuple file: "<<stepNtFileName<<std::endl;
54  }
55  std::cout<<"============================================================================"<<std::endl;
56  std::cout<<std::endl;
57  }
58  if (doNTcastorstep > 0)
59  castorstepntuple = new TNtuple("NTcastorstep","NTcastorstep","evt:trackid:charge:pdgcode:x:y:z:stepl:stepe:eta:phi:vpx:vpy:vpz");
60 
61  if (doNTcastorevent > 0)
62  castoreventntuple = new TNtuple("NTcastorevent","NTcastorevent","evt:ihit:detector:sector:module:enem:totalenergy:hitenergy:x:y:z");
63 }
64 
66  //destructor of CastorTestAnalysis
67 
68  Finish();
69  if (verbosity > 0) {
70  std::cout << std::endl << "End of CastorTestAnalysis"
71  << std::endl;
72  }
73 
74  std::cout<<"CastorTestAnalysis: End of process"<<std::endl;
75 
76 }
77 
78 //=================================================================== per EVENT
80 
81  std::cout << " Starting new job " << std::endl;
82 }
83 
84 //==================================================================== per RUN
86 
87  std::cout << std::endl << "CastorTestAnalysis: Starting Run"<< std::endl;
88  if (doNTcastorstep) {
89  std::cout << "CastorTestAnalysis: output step root file created"<< std::endl;
90  TString stepfilename = stepNtFileName;
91  castorOutputStepFile = new TFile(stepfilename,"RECREATE");
92 
93  }
94 
95  if (doNTcastorevent) {
96  std::cout << "CastorTestAnalysis: output event root file created"<< std::endl;
97  TString stepfilename = eventNtFileName;
98  castorOutputEventFile = new TFile(stepfilename,"RECREATE");
99  }
100 
101  eventIndex = 0;
102 }
103 
105  std::cout << "CastorTestAnalysis: Processing Event Number: "<<eventIndex<< std::endl;
106  eventIndex++;
107  stepIndex = 0;
108 }
109 
110 
111 
112 
113 void CastorTestAnalysis::update(const G4Step * aStep) {
114  stepIndex++;
115 
116 
117  if (doNTcastorstep) {
118 
119  G4StepPoint * preStepPoint = aStep->GetPreStepPoint();
120 // G4StepPoint * postStepPoint= aStep->GetPostStepPoint();
121  G4double stepL = aStep->GetStepLength();
122  G4double stepE = aStep->GetTotalEnergyDeposit();
123 
124  if (verbosity >= 2)
125  std::cout << "Step " << stepL << ", " << stepE << std::endl;
126 
127  G4Track * theTrack = aStep->GetTrack();
128  G4int theTrackID = theTrack->GetTrackID();
129  G4double theCharge = theTrack->GetDynamicParticle()->GetCharge();
130  // G4String particleType = theTrack->GetDefinition()->GetParticleName();
131  G4int pdgcode = theTrack->GetDefinition()->GetPDGEncoding();
132 
133  const G4ThreeVector& vert_mom = theTrack->GetVertexMomentumDirection();
134  G4double vpx = vert_mom.x();
135  G4double vpy = vert_mom.y();
136  G4double vpz = vert_mom.z();
137  double eta = 0.5 * log( (1.+vpz) / (1.-vpz) );
138  double phi = atan2(vpy,vpx);
139 
140  const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
141 
142  // Fill-in ntuple
143  // castorsteparray[ntcastors_evt] = (*evt)()->GetEventID();
145  castorsteparray[ntcastors_trackid] = (float)theTrackID;
148  castorsteparray[ntcastors_x] = hitPoint.x();
149  castorsteparray[ntcastors_y] = hitPoint.y();
150  castorsteparray[ntcastors_z] = hitPoint.z();
158 
159  /*
160  std::cout<<"TrackID: "<< theTrackID<<std::endl;
161  std::cout<<" StepN: "<< theTrack->GetCurrentStepNumber() <<std::endl;
162  std::cout<<" ParentID: "<< aStep->GetTrack()->GetParentID() <<std::endl;
163  std::cout<<" PDG: "<< pdgcode <<std::endl;
164  std::cout<<" X,Y,Z (mm): "<< theTrack->GetPosition().x() <<","<< theTrack->GetPosition().y() <<","<< theTrack->GetPosition().z() <<std::endl;
165  std::cout<<" KE (MeV): "<< theTrack->GetKineticEnergy() <<std::endl;
166  std::cout<<" Total EDep (MeV): "<< aStep->GetTotalEnergyDeposit() <<std::endl;
167  std::cout<<" StepLength (mm): "<< aStep->GetStepLength() <<std::endl;
168  std::cout<<" TrackLength (mm): "<< theTrack->GetTrackLength() <<std::endl;
169 
170  if ( theTrack->GetNextVolume() != 0 )
171  std::cout<<" NextVolume: "<< theTrack->GetNextVolume()->GetName() <<std::endl;
172  else
173  std::cout<<" NextVolume: OutOfWorld"<<std::endl;
174 
175  if(aStep->GetPostStepPoint()->GetProcessDefinedStep() != NULL)
176  std::cout<<" ProcessName: "<< aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() <<std::endl;
177  else
178  std::cout<<" ProcessName: UserLimit"<<std::endl;
179 
180 
181  std::cout<<std::endl;
182  */
183 
184 #ifdef DebugLog
185  if ( theTrack->GetNextVolume() != 0 )
186  LogDebug("ForwardSim") << " NextVolume: " << theTrack->GetNextVolume()->GetName() ;
187  else
188  LogDebug("ForwardSim") << " NextVolume: OutOfWorld" ;
189 #endif
190 
191 
192 //fill ntuple with step level information
194  }
195 }
196 
197 //================= End of EVENT ===============
199 
200  // Look for the Hit Collection
201  std::cout << std::endl << "CastorTest::update(EndOfEvent * evt) - event #" << (*evt)()->GetEventID() << std::endl;
202 
203  // access to the G4 hit collections
204  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
205  std::cout << "update(*evt) --> accessed all HC";
206 
207  int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("CastorFI");
208 
209  CaloG4HitCollection* theCAFI = (CaloG4HitCollection*) allHC->GetHC(CAFIid);
210 
212  // CastorNumberingScheme *theCastorNumScheme = new CastorNumberingScheme();
213 
214 /*
215  unsigned int volumeID=0;
216  int det, zside, sector, zmodule;
217  std::map<int,float,std::less<int> > themap;
218  double totalEnergy = 0;
219  double hitEnergy = 0;
220  double en_in_fi = 0.;
221  double en_in_pl = 0.;
222 */
223 // double en_in_bu = 0.;
224 // double en_in_tu = 0.;
225 
226  if (doNTcastorevent) {
227 
228  eventGlobalHit = 0 ;
229  // int eventGlobalHit = 0 ;
230 
231  // Check FI TBranch for Hits
232  if (theCAFI->entries() > 0) getCastorBranchData(theCAFI) ;
233 
234  // Find Primary info:
235  int trackID = 0;
236  int particleType = 0;
237  G4PrimaryParticle* thePrim=nullptr;
238  G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
239  std::cout << "Event has " << nvertex << " vertex" << std::endl;
240  if (nvertex==0) std::cout << "CASTORTest End Of Event ERROR: no vertex" << std::endl;
241 
242  for (int i = 0 ; i<nvertex; i++) {
243  G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
244  if (avertex == nullptr)
245  std::cout << "CASTORTest End Of Event ERR: pointer to vertex = 0" << std::endl;
246  std::cout << "Vertex number :" <<i << std::endl;
247  int npart = avertex->GetNumberOfParticle();
248  if (npart ==0)
249  std::cout << "CASTORTest End Of Event ERR: no primary!" << std::endl;
250  if (thePrim==nullptr) thePrim=avertex->GetPrimary(trackID);
251  }
252 
253  double px=0.,py=0.,pz=0., pInit=0;
254  double eta = 0., phi = 0.;
255 
256  if (thePrim != nullptr) {
257  px = thePrim->GetPx();
258  py = thePrim->GetPy();
259  pz = thePrim->GetPz();
260  pInit = sqrt(pow(px,2.)+pow(py,2.)+pow(pz,2.));
261  if (pInit==0) {
262  std::cout << "CASTORTest End Of Event ERR: primary has p=0 " << std::endl;
263  } else {
264  float costheta = pz/pInit;
265  float theta = acos(std::min(std::max(costheta,float(-1.)),float(1.)));
266  eta = -log(tan(theta/2));
267 
268  if (px != 0) phi = atan(py/px);
269  }
270  particleType = thePrim->GetPDGcode();
271  } else {
272  std::cout << "CASTORTest End Of Event ERR: could not find primary "
273  << std::endl;
274  }
275  LogDebug("ForwardSim") << "CastorTestAnalysis: Particle Type "
276  << particleType << " p/eta/phi " << pInit << ", "
277  << eta << ", " << phi;
278  }
279 
280  int iEvt = (*evt)()->GetEventID();
281  if (iEvt < 10)
282  std::cout << " CastorTest Event " << iEvt << std::endl;
283  else if ((iEvt < 100) && (iEvt%10 == 0))
284  std::cout << " CastorTest Event " << iEvt << std::endl;
285  else if ((iEvt < 1000) && (iEvt%100 == 0))
286  std::cout << " CastorTest Event " << iEvt << std::endl;
287  else if ((iEvt < 10000) && (iEvt%1000 == 0))
288  std::cout << " CastorTest Event " << iEvt << std::endl;
289 
290  std::cout << std::endl << "===>>> Done writing user histograms " << std::endl;
291 }
292 
294 
295 //===================================================================
297 
298  int nentries = hc->entries();
299 
300  if (nentries > 0) {
301 
302  unsigned int volumeID=0;
303  int det=0, zside, sector, zmodule;
304  std::map<int,float,std::less<int> > themap;
305  double totalEnergy = 0;
306  double hitEnergy = 0;
307  double en_in_sd = 0.;
308 
309  for (int ihit = 0; ihit < nentries; ihit++) {
310  CaloG4Hit* aHit = (*hc)[ihit];
311  totalEnergy += aHit->getEnergyDeposit();
312  }
313 
314  for (int ihit = 0; ihit < nentries; ihit++) {
315  CaloG4Hit* aHit = (*hc)[ihit];
316  volumeID = aHit->getUnitID();
317  hitEnergy = aHit->getEnergyDeposit();
318  en_in_sd += aHit->getEnergyDeposit();
319 // double enEm = aHit->getEM();
320 // double enHad = aHit->getHadr();
321 
322  themap[volumeID] += aHit->getEnergyDeposit();
323  // int det, zside, sector, zmodule;
324  theCastorNumScheme->unpackIndex(volumeID, zside, sector,zmodule);
325 
326  // det = 2 ; // det=2/3 for CAFI/CAPL
327 
329 // castoreventarray[ntcastore_ihit] = (float)ihit;
334  castoreventarray[ntcastore_enem] = en_in_sd;
335  castoreventarray[ntcastore_enhad] = totalEnergy;
340 // castoreventarray[ntcastore_x] = aHit->getEntry().x();
341 // castoreventarray[ntcastore_y] = aHit->getEntry().y();
342 // castoreventarray[ntcastore_z] = aHit->getEntry().z();
343 
345 
346  eventGlobalHit++ ;
347  }
348  } // nentries > 0
349 }
350 
351 //===================================================================
352 
354  if (doNTcastorstep) {
355  castorOutputStepFile->cd();
356  castorstepntuple->Write();
357  std::cout << "CastorTestAnalysis: Ntuple step written" <<std::endl;
358  castorOutputStepFile->Close();
359  std::cout << "CastorTestAnalysis: Step file closed" << std::endl;
360  }
361 
362  if (doNTcastorevent) {
363  castorOutputEventFile->cd();
364  castoreventntuple->Write("",TObject::kOverwrite);
365  std::cout << "CastorTestAnalysis: Ntuple event written" << std::endl;
366  castorOutputEventFile->Close();
367  std::cout << "CastorTestAnalysis: Event file closed" << std::endl;
368  }
369 
370 }
#define LogDebug(id)
T getParameter(std::string const &) const
ntcastors_elements
math::XYZPoint getPosition() const
Definition: CaloG4Hit.h:52
~CastorTestAnalysis() override
Float_t castoreventarray[11]
Geom::Theta< T > theta() const
double npart
Definition: HydjetWrapper.h:49
int zside(DetId const &)
ntcastore_elements
static void unpackIndex(const uint32_t &idx, int &z, int &sector, int &zmodule)
CastorNumberingScheme * theCastorNumScheme
T sqrt(T t)
Definition: SSEVec.h:18
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
T min(T a, T b)
Definition: MathUtil.h:58
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
CastorTestAnalysis(const edm::ParameterSet &p)
std::string stepNtFileName
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
susybsm::HSCParticleCollection hc
Definition: classes.h:25
uint32_t getUnitID() const
Definition: CaloG4Hit.h:65
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
void getCastorBranchData(const CaloG4HitCollection *hc)
std::string eventNtFileName
double getEnergyDeposit() const
Definition: CaloG4Hit.h:77