CMS 3D CMS Logo

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