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 {
33  theData = std::make_shared<MaterialBudgetData>();
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 if(theHistoList == "HGCal" ) {
56  std::cout << "TestGeometry: MaterialBudgetAction running in HGCal Mode" << std::endl;
57  }
58  else {
59  std::cout << "TestGeometry: MaterialBudgetAction running in General Mode" << std::endl;
60  }
61  //
62 
63  //---- Stop track when a process occurs
64  theProcessToStop = m_Anal.getParameter<std::string>("StopAfterProcess");
65  std::cout << "TestGeometry: stop at process " << theProcessToStop << std::endl;
66 
67  //---- Save histos to ROOT file
68  std::string saveToHistosFile = m_Anal.getParameter<std::string>("HistosFile");
69  if( saveToHistosFile != "None" ) {
70  saveToHistos = true;
71  std::cout << "TestGeometry: saving histograms to " << saveToHistosFile << std::endl;
72  theHistoMgr = std::make_shared<TestHistoMgr>();
73  // rr
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  // rr
89  } else {
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  std::cout << "TestGeometry: saving text info to " << saveToTxtFile << std::endl;
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  std::cout << "TestGeometry: all steps are computed " << allSteps << std::endl;
106  if( allSteps ) theData->SetAllStepsToTree();
107 
108  //---- Save tree to ROOT file
109  std::string saveToTreeFile = m_Anal.getParameter<std::string>("TreeFile");
110  // std::string saveToTreeFile = "";
111  if( saveToTreeFile != "None" ) {
112  saveToTree = true;
113  theTree = std::make_shared<MaterialBudgetTree>( theData, saveToTreeFile );
114  } else {
115  saveToTree = false;
116  }
117  std::cout << "TestGeometry: saving ROOT TREE to " << saveToTreeFile << std::endl;
118 
119  //---- Track the first decay products of the main particle
120  // if their kinetic energy is greater than Ekin
121  storeDecay = m_Anal.getUntrackedParameter<bool>("storeDecay",false);
122  Ekin = m_Anal.getUntrackedParameter<double>("EminDecayProd",1000.0); // MeV
123  std::cout << "TestGeometry: decay products steps are stored " << storeDecay;
124  if(storeDecay) std::cout << " if their kinetic energy is greater than " << Ekin << " MeV";
125  std::cout << std::endl;
126  firstParticle = false;
127  }
128 
129 
130 //-------------------------------------------------------------------------
132 {
133 }
134 
135 //-------------------------------------------------------------------------
137 {
138  //----- Check that selected volumes are indeed part of the geometry
139  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
140  std::vector<G4LogicalVolume*>::const_iterator lvcite;
141  std::vector<G4String>::const_iterator volcite;
142 
143  for( volcite = theVolumeList.begin(); volcite != theVolumeList.end(); volcite++ ){
144  bool volFound = false;
145  for( lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++ ) {
146  if( (*lvcite)->GetName() == *volcite ) {
147  volFound = true;
148  break;
149  }
150  }
151  if( !volFound ) {
152  std::cerr << " @@@@@@@ WARNING at MaterialBudgetAction: selected volume not found in geometry " << *volcite << std::endl;
153  }
154  }
155 
156 
157  //----- Check process selected is one of the available ones
158  bool procFound = false;
159  if( theProcessToStop == "None" ) {
160  procFound = true;
161  } else {
162  G4ParticleTable * partTable = G4ParticleTable::GetParticleTable();
163  int siz = partTable->size();
164  for (int ii= 0; ii < siz; ii++) {
165  G4ParticleDefinition * particle = partTable->GetParticle(ii);
166 
167  //--- All processes of this particle
168  G4ProcessManager * pmanager = particle->GetProcessManager();
169  G4ProcessVector * pvect = pmanager->GetProcessList();
170  int sizproc = pvect->size();
171  for (int jj = 0; jj < sizproc; jj++) {
172  if( (*pvect)[jj]->GetProcessName() == theProcessToStop ) {
173  procFound = true;
174  break;
175  }
176  }
177  }
178  }
179 
180  if( !procFound ) {
181  std::cerr << " @@@@@@@ WARNING at MaterialBudgetAction: selected process to stop tracking not found " << theProcessToStop << std::endl;
182  }
183 
184 }
185 
186 
187 //-------------------------------------------------------------------------
189 {
190  const G4Track * aTrack = (*trk)(); // recover G4 pointer if wanted
191 
192  // that was a temporary action while we're sorting out
193  // about # of secondaries (produced if CutsPerRegion=true)
194  //
195  std::cout << "Track ID " << aTrack->GetTrackID() << " Track parent ID " << aTrack->GetParentID()
196  << " PDG Id. = " << aTrack->GetDefinition()->GetPDGEncoding()
197  << " Ekin = " << aTrack->GetKineticEnergy() << " MeV" << std::endl;
198  if( aTrack->GetCreatorProcess() ) std::cout << " produced through " << aTrack->GetCreatorProcess()->GetProcessType() << std::endl;
199 
200  if(aTrack->GetTrackID() == 1) {
201  firstParticle = true;
202  } else {
203  firstParticle = false;
204  }
205 
206  if( storeDecay ) { // if record of the decay is requested
207  if( aTrack->GetCreatorProcess() ) {
208  if (
209  aTrack->GetParentID() == 1
210  &&
211  // aTrack->GetCreatorProcess()->GetProcessType() == 6
212  // &&
213  aTrack->GetKineticEnergy() > Ekin
214  ) {
215  // continue
216  } else {
217  G4Track * aTracknc = const_cast<G4Track*>(aTrack);
218  aTracknc->SetTrackStatus(fStopAndKill);
219  return;
220  }
221  } // particles produced from a decay (type=6) of the main particle (ID=1) with Kinetic Energy [MeV] > Ekin
222  } else { // kill all the other particles (take only the main one until it disappears) if decay not stored
223  if( aTrack->GetParentID() != 0) {
224  G4Track * aTracknc = const_cast<G4Track*>(aTrack);
225  aTracknc->SetTrackStatus(fStopAndKill);
226  return;
227  }
228  }
229 
230 
231  // if(firstParticle) {
232  //--------- start of track
233  //- std::cout << " Data Start Track " << std::endl;
234  theData->dataStartTrack( aTrack );
235  if (saveToTree) theTree->fillStartTrack();
236  if (saveToHistos) theHistos->fillStartTrack();
237  if (saveToTxt) theTxt->fillStartTrack();
238  // }
239 }
240 
241 
242 //-------------------------------------------------------------------------
243 void MaterialBudgetAction::update(const G4Step* aStep)
244 {
245  //----- Check it is inside one of the volumes selected
246  if( !theVolumeList.empty() ) {
247  if( !CheckTouchableInSelectedVolumes( aStep->GetTrack()->GetTouchable() ) ) return;
248  }
249 
250  //---------- each step
251  theData->dataPerStep( aStep );
252  //- std::cout << " aStep->GetPostStepPoint()->GetTouchable() " << aStep->GetPostStepPoint()->GetTouchable()->GetVolume() << " " << aStep->GetPreStepPoint()->GetTouchable()->GetVolume() << std::endl;
253  if (saveToTree) theTree->fillPerStep();
254  if (saveToHistos) theHistos->fillPerStep();
255  if (saveToTxt) theTxt->fillPerStep();
256 
257 
258  //----- Stop tracking after selected process
259  if( StopAfterProcess( aStep ) ) {
260  G4Track* track = aStep->GetTrack();
261  track->SetTrackStatus( fStopAndKill );
262  }
263 
264  return;
265 
266 }
267 
268 
269 //-------------------------------------------------------------------------
271 {
272  const G4TouchableHistory* theTouchable
273  = (const G4TouchableHistory*)(aStepPoint->GetTouchable());
274  G4int num_levels = theTouchable->GetHistoryDepth();
275 
276  if( theTouchable->GetVolume() ) {
277  return theTouchable->GetVolume(num_levels-1)->GetName();
278  } else {
279  return "OutOfWorld";
280  }
281 }
282 
283 
284 //-------------------------------------------------------------------------
286 {
287  const G4TouchableHistory* theTouchable
288  = (const G4TouchableHistory*)(aStepPoint->GetTouchable());
289  G4int num_levels = theTouchable->GetHistoryDepth();
290  // theTouchable->MoveUpHistory(num_levels-3);
291 
292  if( theTouchable->GetVolume() ) {
293  return theTouchable->GetVolume(num_levels-3)->GetName();
294  } else {
295  return "OutOfWorld";
296  }
297 }
298 
299 //-------------------------------------------------------------------------
301 {
302  const G4Track * aTrack = (*trk)(); // recover G4 pointer if wanted
303  // if( aTrack->GetParentID() != 0 ) return;
304 
305  //---------- end of track (OutOfWorld)
306  //- std::cout << " Data End Track " << std::endl;
307  theData->dataEndTrack( aTrack );
308 
309  //- std::cout << " Data End Event " << std::endl;
310  if (saveToTree) theTree->fillEndTrack();
311  if (saveToHistos) theHistos->fillEndTrack();
312  if (saveToTxt) theTxt->fillEndTrack();
313 }
314 
315 //-------------------------------------------------------------------------
317 {
318  return;
319 }
320 
321 //-------------------------------------------------------------------------
323 {
324  // std::vector<G4String>::const_iterator ite;
325  size_t volh = touch->GetHistoryDepth();
326 // for( ite = theVolumeList.begin(); ite != theVolumeList.end(); ite++ ){
327 // //- std::cout << " CheckTouchableInSelectedVolumes vol " << *ite << std::endl;
328  for( int ii = volh; ii >= 0; ii-- ){
329 // //- std::cout << ii << " CheckTouchableInSelectedVolumes parent " << touch->GetVolume(ii)->GetName() << std::endl;
330  if (
331  std::find(theVolumeList.begin(),
332  theVolumeList.end(),
333  touch->GetVolume(ii)->GetName()) != theVolumeList.end() )
334  return true;
335  }
336 // }
337 
338  return false;
339 }
340 
341 //-------------------------------------------------------------------------
342 bool MaterialBudgetAction::StopAfterProcess( const G4Step* aStep )
343 {
344  if( theProcessToStop.empty() ) return false;
345 
346  if(aStep->GetPostStepPoint()->GetProcessDefinedStep() == nullptr) return false;
347  if( aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() == theProcessToStop ) {
348  std::cout << " MaterialBudgetAction::StopAfterProcess " << aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() << std::endl;
349  return true;
350  } else {
351  return false;
352  }
353 }
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.