CMS 3D CMS Logo

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