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