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