CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
G4StepStatistics.h
Go to the documentation of this file.
1 #ifndef HelpfulWatchers_G4StepStatistics_h
2 #define HelpfulWatchers_G4StepStatistics_h
3 // -*- C++ -*-
4 //
5 // Package: HelpfulWatchers
6 // Class : SimTracer
7 //
16 //
17 // Original Author:
18 // Created: Tue Nov 22 16:41:33 EST 2005
19 // $Id: G4StepStatistics.h,v 1.7 2010/01/12 07:14:48 hegner Exp $
20 //
21 
22 // system include files
23 #include <iostream>
24 
25 // user include files
29 #include "G4Step.hh"
30 #include "G4VProcess.hh"
31 #include "G4ParticleDefinition.hh"
32 #include <map>
35 
36 #include <TROOT.h>
37 #include <TTree.h>
38 #include <TFile.h>
39 #include <TVector.h>
40 #include <TString.h>
41 #include <TClonesArray.h>
42 //#include<TObjString.h>
43 
44 // forward declarations
45 class DDDWorld;
46 class BeginOfJob;
47 class BeginOfRun;
48 class BeginOfEvent;
49 class BeginOfTrack;
50 class G4Step;
51 
52 class EndOfRun;
53 class EndOfEvent;
54 class EndOfTrack;
55 
56 //Define a class StepID
57 class StepID {
58 
59  private:
60  //G4 Region
61  G4String theG4RegionName;
62  //G4 Physical Process
63  G4String theG4ProcessName;
64  //Particle PDG ID
66 
67  public:
68  //Constructor using G4Step
69  StepID(const G4Step* theG4Step)
70  :
71  theG4RegionName("UNDEFINED"),
72  theG4ProcessName("UNDEFINED"),
73  theParticlePDGID(theG4Step->GetTrack()->GetDefinition()->GetPDGEncoding())
74  {
75  std::cout<<"Start"<<std::endl;
76  if (theG4Step->GetPreStepPoint()->GetPhysicalVolume()) {
77  theG4RegionName = theG4Step->GetPreStepPoint()->GetPhysicalVolume()->GetLogicalVolume()->GetRegion()->GetName();
78  }
79  std::cout<<"Physical Volume"<<std::endl;
80  if (theG4Step->GetPreStepPoint()->GetProcessDefinedStep()){
81  theG4ProcessName = theG4Step->GetPreStepPoint()->GetProcessDefinedStep()->GetProcessName();
82  }
83  std::cout<<"Process Name"<<std::endl;
84  }
85 
86  //Getters
87  G4String GetRegionName () const { return theG4RegionName; }
88  G4String GetProcessName () const { return theG4ProcessName; }
89  G4int GetParticlePDGID () const { return theParticlePDGID; }
90 
91  //Comparison Operators (necessary in order to use StepID as a key in a map)
92  bool operator==(const StepID& id) const{
93  return ( strcmp(theG4RegionName,id.GetRegionName())==0 && strcmp(theG4ProcessName,id.GetProcessName())==0 && theParticlePDGID==id.GetParticlePDGID() ) ? true : false;
94  }
95 
96  bool operator<(const StepID& id) const
97  {
99  return (theParticlePDGID > id.GetParticlePDGID());
100  }
101  else if (strcmp(theG4RegionName,id.GetRegionName())!=0){
102  return strcmp(theG4RegionName,id.GetRegionName())>0 ? true : false;
103  }
104  else if (strcmp(theG4ProcessName,id.GetProcessName())!=0){
105  return strcmp(theG4ProcessName,id.GetProcessName())>0 ? true : false;
106  }
107  else {//The case in which they are all equal!
108  return false;
109  }
110  }
111 
112  bool operator>(const StepID& id) const
113  {
115  return (theParticlePDGID < id.GetParticlePDGID());
116  }
117  else if(strcmp(theG4RegionName,id.GetRegionName())!=0){
118  return strcmp(theG4RegionName,id.GetRegionName())<0 ? true : false;
119  }
120  else if (strcmp(theG4ProcessName,id.GetProcessName())!=0){
121  return strcmp(theG4ProcessName,id.GetProcessName())<0 ? true : false;
122  }
123  else {//The case in which they are all equal!
124  return false;
125  }
126  }
127 };
128 
129 #define OBSERVES(type) public Observer<const type*>
130 #define UPDATE(type) void update(const type*) { std::cout <<"++ signal " #type<<std::endl; }
131 class G4StepStatistics : public SimWatcher,
132 OBSERVES(DDDWorld),
133 OBSERVES(BeginOfJob),
134 OBSERVES(BeginOfRun),
135 OBSERVES(BeginOfEvent),
136 OBSERVES(BeginOfTrack),
137 OBSERVES(G4Step),
138 OBSERVES(EndOfRun),
139 OBSERVES(EndOfEvent),
140 OBSERVES(EndOfTrack)
141 {
142  public:
144  m_verbose(pSet.getUntrackedParameter<bool>("verbose",false)),
145  Event(0)
146  {
147  //Adding TFile Service output
148  G4StepTree = fs->make<TTree>("G4StepTree","G4Step Tree ");
149  G4StepTree->Branch("Event",&Event,"Event/I");
150  G4StepTree->Branch("PDGID",&PDGID,"PDGID[100000]/I");
151  Region = new TClonesArray("TObjString",100000);
152  G4StepTree->Branch("Region",&Region);
153  Process = new TClonesArray("TObjString",100000);
154  G4StepTree->Branch("Process",&Process);
155  G4StepTree->Branch("G4StepFreq",&G4StepFreq,"G4StepFreq[100000]/I");
156  }
160  // void update(const BeginOfRun* iRun) {
161  //std::cout <<"++ signal BeginOfRun " <<std::endl;
162  //}
165  void update(const G4Step* iStep) {
166  std::cout <<"++ signal G4Step " ;
167  //Dump the relevant information from the G4 step in the object mysteptest
168  StepID mysteptest(iStep);
169  //Add the StepID to the map, or increment if it already exists:
170  if ( G4StatsMap.find(mysteptest) == G4StatsMap.end() )
171  {
172  //Allocating new memory for a pointer to associate with the key mysteptest
173  //in our map. Initializing it to 1,will be incremented working on the value of the pointer.
174  unsigned int* MyValue = new unsigned int(1);
175  //Inserting the new key,value pair
176  G4StatsMap.insert(std::make_pair(mysteptest, MyValue));
177  }
178  else
179  {
180  //Incrementing the value of the pointer by 1
181  *G4StatsMap[mysteptest] = *G4StatsMap[mysteptest] + 1;
182  }
183 
184  //If the verbose flag is set, then dump the information
185  if (m_verbose) {
186  std::cout << " StepID RegionName: "<< mysteptest.GetRegionName();
187  std::cout << " StepID ProcessName: "<< mysteptest.GetProcessName();
188  std::cout << " StepID ParticlePDGID: "<< mysteptest.GetParticlePDGID();
189  }
190  std::cout<<std::endl;
191 }
192 //UPDATE(G4Step)
194  // void update(const EndOfRun* iRun) {
195  //std::cout <<"++ signal EndOfRun " <<std::endl;
196  //}
197 
198  //UPDATE(EndOfEvent)
199  void update(const EndOfEvent* iRun) {
200  std::cout <<"++ signal EndOfEvent " <<std::endl;
201  Event++;
202 
203  //Dumping the map in the log if verbose is chosen:
204  if(m_verbose) {
205  std::cout <<" G4StatsMap size is: "<<G4StatsMap.size()<<std::endl;
206  }
207  int index(0);
208  for (std::map<const StepID,unsigned int*>::const_iterator step = G4StatsMap.begin(); step != G4StatsMap.end(); ++step, ++index){
209  if(m_verbose) {
210  std::cout <<" G4StatsMap step is: "<<step->first.GetRegionName()<<" "<<step->first.GetProcessName()<<" "<<step->first.GetParticlePDGID();//<<" "<<step->first.GetTrackID() ;
211  std::cout <<" Number of such steps: "<< *step->second <<std::endl;
212  }
213  //Rolling the map into 5 "arrays", containing the StepID information and the G4Step statistics
214  PDGID[index]=step->first.GetParticlePDGID();
215  new ((*Region)[index]) TObjString (step->first.GetRegionName());
216  new ((*Process)[index]) TObjString (step->first.GetProcessName());
217  G4StepFreq[index]=*step->second;
218  }
219 
220  G4StepTree->Fill();
221 }
223 
224  private:
225 
226  bool m_verbose;
227 
228 //Adding the G4StatsMap to keep track of statistics in terms of step information...
229  std::map<const StepID,unsigned int*> G4StatsMap;
231  TTree* G4StepTree;
232  unsigned int Event;
233  Int_t PDGID[100000];
234  TClonesArray* Region;
235  TClonesArray* Process;
236  Int_t G4StepFreq[100000];
237 };
238 
239 #endif
void update(const G4Step *iStep)
std::map< const StepID, unsigned int * > G4StatsMap
Int_t G4StepFreq[100000]
bool operator==(const StepID &id) const
list step
Definition: launcher.py:15
G4StepStatistics(const edm::ParameterSet &pSet)
bool operator<(const StepID &id) const
G4String theG4RegionName
StepID(const G4Step *theG4Step)
G4String theG4ProcessName
dictionary map
Definition: Association.py:196
G4String GetProcessName() const
G4int theParticlePDGID
Int_t PDGID[100000]
bool operator>(const StepID &id) const
TClonesArray * Process
#define OBSERVES(type)
G4int GetParticlePDGID() const
string const
Definition: compareJSON.py:14
#define private
Definition: FWFileEntry.h:18
T * make() const
make new ROOT object
TClonesArray * Region
tuple cout
Definition: gather_cfg.py:121
#define UPDATE(type)
edm::Service< TFileService > fs
G4String GetRegionName() const