CMS 3D CMS Logo

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