CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MaterialBudgetAction.cc
Go to the documentation of this file.
1 
4 
7 
12 
13 #include "G4Step.hh"
14 #include "G4Track.hh"
15 #include "G4UImanager.hh"
16 #include "G4UIterminal.hh"
17 #include "G4UItcsh.hh"
18 #include "G4LogicalVolumeStore.hh"
19 #include "G4TouchableHistory.hh"
20 #include "G4VPhysicalVolume.hh"
21 #include "G4VProcess.hh"
22 #include "G4ParticleTable.hh"
23 #include "G4ParticleDefinition.hh"
24 #include "G4ProcessManager.hh"
25 #include "G4TransportationManager.hh"
26 
27 #include <iostream>
28 
29 //-------------------------------------------------------------------------
31  : theHistoMgr(0)
32 {
34 
35  edm::ParameterSet m_Anal = iPSet.getParameter<edm::ParameterSet>("MaterialBudgetAction");
36 
37  //---- Accumulate material budget only inside selected volumes
38  std::string theHistoList = m_Anal.getParameter<std::string>("HistogramList");
39  std::vector<std::string> volList = m_Anal.getParameter< std::vector<std::string> >("SelectedVolumes");
40  std::vector<std::string>::const_iterator ite;
41  std::cout << "TestGeometry: List of the selected volumes: " << std::endl;
42  for( ite = volList.begin(); ite != volList.end(); ite++ ){
43  if( (*ite) != "None" ) {
44  theVolumeList.push_back( *ite );
45  std::cout << (*ite) << std::endl;
46  }
47  }
48  // log
49  if(theHistoList == "Tracker" ) {
50  std::cout << "TestGeometry: MaterialBudgetAction running in Tracker Mode" << std::endl;
51  }
52  else if(theHistoList == "ECAL" ) {
53  std::cout << "TestGeometry: MaterialBudgetAction running in Ecal Mode" << std::endl;
54  }
55  else {
56  std::cout << "TestGeometry: MaterialBudgetAction running in General Mode" << std::endl;
57  }
58  //
59 
60  //---- Stop track when a process occurs
61  theProcessToStop = m_Anal.getParameter<std::string>("StopAfterProcess");
62  std::cout << "TestGeometry: stop at process " << theProcessToStop << std::endl;
63 
64  //---- Save histos to ROOT file
65  std::string saveToHistosFile = m_Anal.getParameter<std::string>("HistosFile");
66  if( saveToHistosFile != "None" ) {
67  saveToHistos = true;
68  std::cout << "TestGeometry: saving histograms to " << saveToHistosFile << std::endl;
69  theHistoMgr = new TestHistoMgr();
70 
71  // rr
72  if(theHistoList == "Tracker" ) {
73  theHistos = new MaterialBudgetTrackerHistos( theData, theHistoMgr, saveToHistosFile );
74  }
75  else if (theHistoList == "ECAL") {
76  theHistos = new MaterialBudgetEcalHistos( theData, theHistoMgr, saveToHistosFile );
77  }
78  else {
79  theHistos = new MaterialBudgetHistos( theData, theHistoMgr, saveToHistosFile );
80  }
81  // rr
82  } else {
83  saveToHistos = false;
84  }
85 
86  //---- Save material budget info to TEXT file
87  std::string saveToTxtFile = m_Anal.getParameter<std::string>("TextFile");
88  if( saveToTxtFile != "None" ) {
89  saveToTxt = true;
90  std::cout << "TestGeometry: saving text info to " << saveToTxtFile << std::endl;
91  theTxt = new MaterialBudgetTxt( theData, saveToTxtFile );
92  } else {
93  saveToTxt = false;
94  }
95 
96  //---- Compute all the steps even if not stored on file
97  bool allSteps = m_Anal.getParameter<bool>("AllStepsToTree");
98  std::cout << "TestGeometry: all steps are computed " << allSteps << std::endl;
99  if( allSteps ) theData->SetAllStepsToTree();
100 
101  //---- Save tree to ROOT file
102  std::string saveToTreeFile = m_Anal.getParameter<std::string>("TreeFile");
103  // std::string saveToTreeFile = "";
104  if( saveToTreeFile != "None" ) {
105  saveToTree = true;
106  theTree = new MaterialBudgetTree( theData, saveToTreeFile );
107  } else {
108  saveToTree = false;
109  }
110  std::cout << "TestGeometry: saving ROOT TREE to " << saveToTreeFile << std::endl;
111 
112  //---- Track the first decay products of the main particle
113  // if their kinetic energy is greater than Ekin
114  storeDecay = m_Anal.getUntrackedParameter<bool>("storeDecay",false);
115  Ekin = m_Anal.getUntrackedParameter<double>("EminDecayProd",1000.0); // MeV
116  std::cout << "TestGeometry: decay products steps are stored " << storeDecay;
117  if(storeDecay) std::cout << " if their kinetic energy is greater than " << Ekin << " MeV";
118  std::cout << std::endl;
119  firstParticle = false;
120  }
121 
122 
123 //-------------------------------------------------------------------------
125 {
126  if (saveToTxt) delete theTxt;
127  if (saveToTree) delete theTree;
128  if (saveToHistos) delete theHistos;
129  if (theHistoMgr) delete theHistoMgr;
130  delete theData;
131 }
132 
133 //-------------------------------------------------------------------------
135 {
136  //----- Check that selected volumes are indeed part of the geometry
137  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
138  std::vector<G4LogicalVolume*>::const_iterator lvcite;
139  std::vector<G4String>::const_iterator volcite;
140 
141  for( volcite = theVolumeList.begin(); volcite != theVolumeList.end(); volcite++ ){
142  bool volFound = false;
143  for( lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++ ) {
144  if( (*lvcite)->GetName() == *volcite ) {
145  volFound = true;
146  break;
147  }
148  }
149  if( !volFound ) {
150  std::cerr << " @@@@@@@ WARNING at MaterialBudgetAction: selected volume not found in geometry " << *volcite << std::endl;
151  }
152  }
153 
154 
155  //----- Check process selected is one of the available ones
156  bool procFound = false;
157  if( theProcessToStop == "None" ) {
158  procFound = true;
159  } else {
160  G4ParticleTable * partTable = G4ParticleTable::GetParticleTable();
161  int siz = partTable->size();
162  for (int ii= 0; ii < siz; ii++) {
163  G4ParticleDefinition * particle = partTable->GetParticle(ii);
164  std::string particleName = particle->GetParticleName();
165 
166  //--- All processes of this particle
167  G4ProcessManager * pmanager = particle->GetProcessManager();
168  G4ProcessVector * pvect = pmanager->GetProcessList();
169  int sizproc = pvect->size();
170  for (int jj = 0; jj < sizproc; jj++) {
171  if( (*pvect)[jj]->GetProcessName() == theProcessToStop ) {
172  procFound = true;
173  break;
174  }
175  }
176  }
177  }
178 
179  if( !procFound ) {
180  std::cerr << " @@@@@@@ WARNING at MaterialBudgetAction: selected process to stop tracking not found " << theProcessToStop << std::endl;
181  }
182 
183 }
184 
185 
186 //-------------------------------------------------------------------------
188 {
189  const G4Track * aTrack = (*trk)(); // recover G4 pointer if wanted
190 
191  // that was a temporary action while we're sorting out
192  // about # of secondaries (produced if CutsPerRegion=true)
193  //
194  std::cout << "Track ID " << aTrack->GetTrackID() << " Track parent ID " << aTrack->GetParentID()
195  << " PDG Id. = " << aTrack->GetDefinition()->GetPDGEncoding()
196  << " Ekin = " << aTrack->GetKineticEnergy() << " MeV" << std::endl;
197  if( aTrack->GetCreatorProcess() ) std::cout << " produced through " << aTrack->GetCreatorProcess()->GetProcessType() << std::endl;
198 
199  if(aTrack->GetTrackID() == 1) {
200  firstParticle = true;
201  } else {
202  firstParticle = false;
203  }
204 
205  if( storeDecay ) { // if record of the decay is requested
206  if( aTrack->GetCreatorProcess() ) {
207  if (
208  aTrack->GetParentID() == 1
209  &&
210  // aTrack->GetCreatorProcess()->GetProcessType() == 6
211  // &&
212  aTrack->GetKineticEnergy() > Ekin
213  ) {
214  // continue
215  } else {
216  G4Track * aTracknc = const_cast<G4Track*>(aTrack);
217  aTracknc->SetTrackStatus(fStopAndKill);
218  return;
219  }
220  } // particles produced from a decay (type=6) of the main particle (ID=1) with Kinetic Energy [MeV] > Ekin
221  } else { // kill all the other particles (take only the main one until it disappears) if decay not stored
222  if( aTrack->GetParentID() != 0) {
223  G4Track * aTracknc = const_cast<G4Track*>(aTrack);
224  aTracknc->SetTrackStatus(fStopAndKill);
225  return;
226  }
227  }
228 
229 
230  if(firstParticle) {
231  //--------- start of track
232  //- std::cout << " Data Start Track " << std::endl;
233  theData->dataStartTrack( aTrack );
237  }
238 }
239 
240 
241 //-------------------------------------------------------------------------
242 void MaterialBudgetAction::update(const G4Step* aStep)
243 {
244  //----- Check it is inside one of the volumes selected
245  if( theVolumeList.size() != 0 ) {
246  if( !CheckTouchableInSelectedVolumes( aStep->GetTrack()->GetTouchable() ) ) return;
247  }
248 
249  //---------- each step
250  theData->dataPerStep( aStep );
251  //- std::cout << " aStep->GetPostStepPoint()->GetTouchable() " << aStep->GetPostStepPoint()->GetTouchable()->GetVolume() << " " << aStep->GetPreStepPoint()->GetTouchable()->GetVolume() << std::endl;
254  if (saveToTxt) theTxt->fillPerStep();
255 
256 
257  //----- Stop tracking after selected process
258  if( StopAfterProcess( aStep ) ) {
259  G4Track* track = aStep->GetTrack();
260  track->SetTrackStatus( fStopAndKill );
261  }
262 
263  return;
264 
265 }
266 
267 
268 //-------------------------------------------------------------------------
270 {
271  G4TouchableHistory* theTouchable
272  = (G4TouchableHistory*)(aStepPoint->GetTouchable());
273  G4int num_levels = theTouchable->GetHistoryDepth();
274 
275  if( theTouchable->GetVolume() ) {
276  return theTouchable->GetVolume(num_levels-1)->GetName();
277  } else {
278  return "OutOfWorld";
279  }
280 }
281 
282 
283 //-------------------------------------------------------------------------
285 {
286  G4TouchableHistory* theTouchable
287  = (G4TouchableHistory*)(aStepPoint->GetTouchable());
288  G4int num_levels = theTouchable->GetHistoryDepth();
289  // theTouchable->MoveUpHistory(num_levels-3);
290 
291  if( theTouchable->GetVolume() ) {
292  return theTouchable->GetVolume(num_levels-3)->GetName();
293  } else {
294  return "OutOfWorld";
295  }
296 }
297 
298 //-------------------------------------------------------------------------
300 {
301  const G4Track * aTrack = (*trk)(); // recover G4 pointer if wanted
302  // if( aTrack->GetParentID() != 0 ) return;
303 
304  //---------- end of track (OutOfWorld)
305  //- std::cout << " Data End Track " << std::endl;
306  theData->dataEndTrack( aTrack );
307 
308  //- std::cout << " Data End Event " << std::endl;
311  if (saveToTxt) theTxt->fillEndTrack();
312 }
313 
314 //-------------------------------------------------------------------------
316 {
317  std::vector<G4String>::const_iterator ite;
318  size_t volh = touch->GetHistoryDepth();
319  for( ite = theVolumeList.begin(); ite != theVolumeList.end(); ite++ ){
320  //- std::cout << " CheckTouchableInSelectedVolumes vol " << *ite << std::endl;
321  for( int ii = volh; ii >= 0; ii-- ){
322  //- std::cout << ii << " CheckTouchableInSelectedVolumes parent " << touch->GetVolume(ii)->GetName() << std::endl;
323  if( touch->GetVolume(ii)->GetName() == *ite ) return true;
324  }
325  }
326 
327  return false;
328 }
329 
330 //-------------------------------------------------------------------------
331 bool MaterialBudgetAction::StopAfterProcess( const G4Step* aStep )
332 {
333  if( theProcessToStop == "" ) return false;
334 
335  if(aStep->GetPostStepPoint()->GetProcessDefinedStep() == NULL) return false;
336  if( aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() == theProcessToStop ) {
337  std::cout << " MaterialBudgetAction::StopAfterProcess " << aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() << std::endl;
338  return true;
339  } else {
340  return false;
341  }
342 }
bool CheckTouchableInSelectedVolumes(const G4VTouchable *touch)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
virtual void fillPerStep()
MaterialBudgetAction(const edm::ParameterSet &)
void update(const BeginOfRun *)
This routine will be called when the appropriate signal arrives.
#define NULL
Definition: scimark2.h:8
virtual void fillEndTrack()
int ii
Definition: cuy.py:588
virtual void fillEndTrack()
MaterialBudgetTxt * theTxt
virtual void fillStartTrack()
virtual void fillStartTrack()=0
void dataEndTrack(const G4Track *aTrack)
MaterialBudgetFormat * theHistos
void dataPerStep(const G4Step *aStep)
virtual void fillEndTrack()=0
MaterialBudgetData * theData
bool StopAfterProcess(const G4Step *aStep)
virtual void fillPerStep()
void dataStartTrack(const G4Track *aTrack)
std::string getPartName(G4StepPoint *aStepPoint)
tuple cout
Definition: gather_cfg.py:145
std::vector< G4String > theVolumeList
std::string getSubDetectorName(G4StepPoint *aStepPoint)
virtual void fillStartTrack()
virtual void fillPerStep()=0
MaterialBudgetTree * theTree