CMS 3D CMS Logo

DDG4ProductionCuts.cc
Go to the documentation of this file.
4 
7 
8 #include "G4ProductionCuts.hh"
9 #include "G4RegionStore.hh"
10 #include "G4Region.hh"
11 #include "G4LogicalVolume.hh"
12 
13 #include <algorithm>
14 
15 namespace {
21  bool dd_is_greater(const std::pair<G4LogicalVolume*, DDLogicalPart>& p1,
22  const std::pair<G4LogicalVolume*, DDLogicalPart>& p2) {
23  bool result = false;
24  if (p1.second.name().ns() > p2.second.name().ns()) {
25  result = true;
26  }
27  if (p1.second.name().ns() == p2.second.name().ns()) {
28  if (p1.second.name().name() > p2.second.name().name()) {
29  result = true;
30  }
31  if (p1.second.name().name() == p2.second.name().name()) {
32  if (p1.first->GetName() > p2.first->GetName()) {
33  result = true;
34  }
35  }
36  }
37  return result;
38  }
39 
40  bool sortByName(const std::pair<G4LogicalVolume*, const cms::DDSpecPar*>& p1,
41  const std::pair<G4LogicalVolume*, const cms::DDSpecPar*>& p2) {
42  bool result = false;
43  if (p1.first->GetName() > p2.first->GetName()) {
44  result = true;
45  }
46  return result;
47  }
48 } // namespace
49 
51  : map_(map), keywordRegion_("CMSCutsRegion"), verbosity_(verb), protonCut_(pcut) {
52  initialize();
53 }
54 
56  const dd4hep::sim::Geant4GeometryMaps::VolumeMap* map,
57  int verb,
58  bool pcut)
59  : dd4hepMap_(map), specPars_(specPars), keywordRegion_("CMSCutsRegion"), verbosity_(verb), protonCut_(pcut) {
61 }
62 
64 
67  // sort all root volumes - to get the same sequence at every run of the application.
68  // (otherwise, the sequence will depend on the pointer (memory address) of the
69  // involved objects, because 'new' does no guarantee that you allways get a
70  // higher (or lower) address when allocating an object of the same type ...
71  sort(vec_.begin(), vec_.end(), &dd_is_greater);
72  if (verbosity_ > 0) {
73  edm::LogVerbatim("Geometry") << " DDG4ProductionCuts : got " << vec_.size() << " region roots.\n"
74  << " DDG4ProductionCuts : List of all roots:";
75  for (auto const& vv : vec_)
76  edm::LogVerbatim("Geometry") << " " << vv.first->GetName() << " : " << vv.second.name();
77  }
78 
79  // Now generate all the regions
80  std::string curName = "";
81  std::string regionName = "";
82  G4Region* region = nullptr;
83  G4RegionStore* store = G4RegionStore::GetInstance();
84  for (auto const& vv : vec_) {
85  unsigned int num = map_->toString(keywordRegion_, vv.second, regionName);
86  edm::LogVerbatim("Geometry") << " num " << num << " regionName: " << regionName << ", the store of size "
87  << store->size();
88  if (num != 1) {
89  throw cms::Exception("SimG4CoreGeometry", " DDG4ProductionCuts::initialize: Problem with Region tags.");
90  }
91  if (regionName != curName) {
92  edm::LogVerbatim("Geometry") << "DDG4ProductionCuts : regionName " << regionName << ", the store of size "
93  << store->size();
94  region = store->FindOrCreateRegion(regionName);
95  edm::LogVerbatim("Geometry") << "DDG4ProductionCuts : region " << region->GetName();
96  if (!region) {
97  throw cms::Exception("SimG4CoreGeometry", " DDG4ProductionCuts::initialize: Problem with Region tags.");
98  }
99  curName = regionName;
100  edm::LogVerbatim("Geometry") << "DDG4ProductionCuts : new G4Region " << vv.first->GetName();
101  setProdCuts(vv.second, region);
102  }
103 
104  region->AddRootLogicalVolume(vv.first);
105 
106  if (verbosity_ > 0)
107  edm::LogVerbatim("Geometry") << " added " << vv.first->GetName() << " to region " << region->GetName();
108  }
109 }
110 
114 
115  for (auto const& it : *dd4hepMap_) {
116  for (auto const& fit : specs) {
117  for (auto const& pit : fit->paths) {
119  dd4hepVec_.emplace_back(std::make_pair<G4LogicalVolume*, const cms::DDSpecPar*>(&*it.second, &*fit));
120  }
121  }
122  }
123  }
124  // sort all root volumes - to get the same sequence at every run of the application.
125  sort(begin(dd4hepVec_), end(dd4hepVec_), &sortByName);
126 
127  // Now generate all the regions
128  for (auto const& it : dd4hepVec_) {
129  auto regName = it.second->strValue(keywordRegion_);
130  G4Region* region = G4RegionStore::GetInstance()->FindOrCreateRegion({regName.data(), regName.size()});
131  region->AddRootLogicalVolume(it.first);
132  edm::LogVerbatim("Geometry") << it.first->GetName() << ": " << it.second->strValue(keywordRegion_);
133  edm::LogVerbatim("Geometry") << " MakeRegions: added " << it.first->GetName() << " to region " << region->GetName();
134  edm::LogVerbatim("Geometry").log([&](auto& log) {
135  for (auto const& sit : it.second->spars) {
136  log << sit.first << " = " << sit.second[0] << "\n";
137  }
138  });
139  setProdCuts(it.second, region);
140  }
141 
142  if (verbosity_ > 0) {
143  LogDebug("Geometry") << " DDG4ProductionCuts (New) : starting\n"
144  << " DDG4ProductionCuts : Got " << dd4hepVec_.size() << " region roots.\n"
145  << " DDG4ProductionCuts : List of all roots:";
146  for (size_t jj = 0; jj < dd4hepVec_.size(); ++jj)
147  LogDebug("Geometry") << " DDG4ProductionCuts : root=" << dd4hepVec_[jj].first << " , " << dd4hepVec_[jj].second;
148  }
149 }
150 
152  //
153  // search for production cuts
154  // you must have four of them: e+ e- gamma proton
155  //
156  double gammacut = 0.0;
157  double electroncut = 0.0;
158  double positroncut = 0.0;
159  double protoncut = 0.0;
160  int temp = map_->toDouble("ProdCutsForGamma", lpart, gammacut);
161  if (temp != 1) {
162  throw cms::Exception(
163  "SimG4CorePhysics",
164  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - no/more than one ProdCutsForGamma.");
165  }
166  temp = map_->toDouble("ProdCutsForElectrons", lpart, electroncut);
167  if (temp != 1) {
168  throw cms::Exception(
169  "SimG4CorePhysics",
170  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - no/more than one ProdCutsForElectrons.");
171  }
172  temp = map_->toDouble("ProdCutsForPositrons", lpart, positroncut);
173  if (temp != 1) {
174  throw cms::Exception(
175  "SimG4CorePhysics",
176  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - no/more than one ProdCutsForPositrons.");
177  }
178  temp = map_->toDouble("ProdCutsForProtons", lpart, protoncut);
179  if (temp == 0) {
180  // There is no ProdCutsForProtons set in XML,
181  // check if it's a legacy geometry scenario without it
182  if (protonCut_) {
183  protoncut = electroncut;
184  } else {
185  protoncut = 0.;
186  }
187  } else if (temp != 1) {
188  throw cms::Exception(
189  "SimG4CorePhysics",
190  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - more than one ProdCutsForProtons.");
191  }
192 
193  //
194  // Create and fill production cuts
195  //
196  G4ProductionCuts* prodCuts = region->GetProductionCuts();
197  if (!prodCuts) {
198  prodCuts = new G4ProductionCuts();
199  region->SetProductionCuts(prodCuts);
200  }
201  prodCuts->SetProductionCut(gammacut, idxG4GammaCut);
202  prodCuts->SetProductionCut(electroncut, idxG4ElectronCut);
203  prodCuts->SetProductionCut(positroncut, idxG4PositronCut);
204  prodCuts->SetProductionCut(protoncut, idxG4ProtonCut);
205  if (verbosity_ > 0) {
206  edm::LogVerbatim("Geometry") << "DDG4ProductionCuts : Setting cuts for " << region->GetName()
207  << "\n Electrons: " << electroncut << "\n Positrons: " << positroncut
208  << "\n Gamma : " << gammacut << "\n Proton : " << protoncut;
209  }
210 }
211 
213  //
214  // Create and fill production cuts
215  //
216  G4ProductionCuts* prodCuts = region->GetProductionCuts();
217  if (!prodCuts) {
218  // FIXME: Here we use a dd4hep string to double evaluator
219  // Beware of the units!!!
220 
221  //
222  // search for production cuts
223  // you must have four of them: e+ e- gamma proton
224  //
225  double gammacut = 0.0;
226  double electroncut = 0.0;
227  double positroncut = 0.0;
228  double protoncut = 0.0;
229 
230  auto gammacutStr = spec->strValue("ProdCutsForGamma");
231  if (gammacutStr.empty()) {
232  throw cms::Exception(
233  "SimG4CorePhysics",
234  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - no/more than one ProdCutsForGamma.");
235  }
236  gammacut = dd4hep::_toDouble({gammacutStr.data(), gammacutStr.size()});
237 
238  auto electroncutStr = spec->strValue("ProdCutsForElectrons");
239  if (electroncutStr.empty()) {
240  throw cms::Exception(
241  "SimG4CorePhysics",
242  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - no/more than one ProdCutsForElectrons.");
243  }
244  electroncut = dd4hep::_toDouble({electroncutStr.data(), electroncutStr.size()});
245 
246  auto positroncutStr = spec->strValue("ProdCutsForPositrons");
247  if (positroncutStr.empty()) {
248  throw cms::Exception(
249  "SimG4CorePhysics",
250  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - no/more than one ProdCutsForPositrons.");
251  }
252  positroncut = dd4hep::_toDouble({positroncutStr.data(), positroncutStr.size()});
253 
254  if (!spec->hasValue("ProdCutsForProtons")) {
255  // There is no ProdCutsForProtons set in XML,
256  // check if it's a legacy geometry scenario without it
257  if (protonCut_) {
258  protoncut = electroncut;
259  } else {
260  protoncut = 0.;
261  }
262  } else {
263  auto protoncutStr = spec->strValue("ProdCutsForProtons");
264  if (protoncutStr.empty()) {
265  throw cms::Exception(
266  "SimG4CorePhysics",
267  " DDG4ProductionCuts::setProdCuts: Problem with Region tags - more than one ProdCutsForProtons.");
268  }
269  protoncut = dd4hep::_toDouble({protoncutStr.data(), protoncutStr.size()});
270  }
271 
272  prodCuts = new G4ProductionCuts();
273  region->SetProductionCuts(prodCuts);
274 
275  prodCuts->SetProductionCut(gammacut, idxG4GammaCut);
276  prodCuts->SetProductionCut(electroncut, idxG4ElectronCut);
277  prodCuts->SetProductionCut(positroncut, idxG4PositronCut);
278  prodCuts->SetProductionCut(protoncut, idxG4ProtonCut);
279  if (verbosity_ > 0) {
280  edm::LogVerbatim("Geometry") << "DDG4ProductionCuts : Setting cuts for " << region->GetName()
281  << "\n Electrons: " << electroncut << "\n Positrons: " << positroncut
282  << "\n Gamma : " << gammacut << "\n Proton : " << protoncut;
283  }
284  } else {
285  if (verbosity_ > 0) {
286  edm::LogVerbatim("Geometry")
287  << "DDG4ProductionCuts : Cuts are already set for " << region->GetName()
288  << "\n Electrons: " << region->GetProductionCuts()->GetProductionCut(idxG4ElectronCut)
289  << "\n Positrons: " << region->GetProductionCuts()->GetProductionCut(idxG4PositronCut)
290  << "\n Gamma : " << region->GetProductionCuts()->GetProductionCut(idxG4GammaCut)
291  << "\n Proton : " << region->GetProductionCuts()->GetProductionCut(idxG4ProtonCut);
292  }
293  }
294 }
DDMapper::toDouble
unsigned int toDouble(const std::string &name, const KeyType &key, double &value, unsigned int pos=0) const
returns the number specific parameters named 'name' and the corrsponding double
Definition: DDMapper.h:111
cms::dd::realTopName
std::string_view realTopName(std::string_view)
DDG4ProductionCuts::verbosity_
const int verbosity_
Definition: DDG4ProductionCuts.h:48
MessageLogger.h
DDMapper< G4LogicalVolume *, DDLogicalPart >
DDG4ProductionCuts::~DDG4ProductionCuts
~DDG4ProductionCuts()
Definition: DDG4ProductionCuts.cc:63
DDG4ProductionCuts::DDG4ProductionCuts
DDG4ProductionCuts(const G4LogicalVolumeToDDLogicalPartMap *, int, bool)
Definition: DDG4ProductionCuts.cc:50
HistogramManager_cfi.specs
specs
Definition: HistogramManager_cfi.py:80
cms::DDSpecParRegistry
Definition: DDSpecParRegistry.h:32
DDG4ProductionCuts::dd4hepMap_
const dd4hep::sim::Geant4GeometryMaps::VolumeMap * dd4hepMap_
Definition: DDG4ProductionCuts.h:41
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
DDG4ProductionCuts::protonCut_
const bool protonCut_
Definition: DDG4ProductionCuts.h:49
end
#define end
Definition: vmac.h:39
DDG4ProductionCuts::map_
const G4LogicalVolumeToDDLogicalPartMap * map_
Definition: DDG4ProductionCuts.h:32
DDG4ProductionCuts.h
p2
double p2[4]
Definition: TauolaWrapper.h:90
cms::dd::noNamespace
std::string_view noNamespace(std::string_view)
Definition: Filter.cc:63
cms::DDSpecParRefs
std::vector< const DDSpecPar * > DDSpecParRefs
Definition: DDSpecParRegistry.h:30
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
DDG4ProductionCuts::dd4hepVec_
std::vector< std::pair< G4LogicalVolume *, const cms::DDSpecPar * > > dd4hepVec_
Definition: DDG4ProductionCuts.h:42
cms::DDSpecPar::strValue
std::string_view strValue(const std::string &) const
Definition: DDSpecparRegistry.cc:10
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:670
DDG4ProductionCuts::keywordRegion_
const std::string keywordRegion_
Definition: DDG4ProductionCuts.h:47
DDLogicalPart
A DDLogicalPart aggregates information concerning material, solid and sensitveness ....
Definition: DDLogicalPart.h:93
edm::LogVerbatim::log
LogVerbatim & log(F &&iF)
Definition: MessageLogger.h:327
edm::LogVerbatim
Definition: MessageLogger.h:297
p1
double p1[4]
Definition: TauolaWrapper.h:89
DDMapper::toString
unsigned int toString(const std::string &name, const KeyType &key, std::string &value, unsigned int pos=0) const
same as toDouble but for std::string-valued values of named parameters
Definition: DDMapper.h:172
DDLogicalPart.h
DDG4ProductionCuts::specPars_
const cms::DDSpecParRegistry * specPars_
Definition: DDG4ProductionCuts.h:43
cms::dd::compareEqual
bool compareEqual(std::string_view, std::string_view)
EgammaValidation_cff.num
num
Definition: EgammaValidation_cff.py:34
cms::DDSpecPar
Definition: DDSpecParRegistry.h:14
DDG4ProductionCuts::vec_
G4LogicalVolumeToDDLogicalPartMap::Vector vec_
Definition: DDG4ProductionCuts.h:33
cms::DDSpecParRegistry::filter
void filter(DDSpecParRefs &, const std::string &, const std::string &="") const
Definition: DDSpecparRegistry.cc:98
Exception
Definition: hltDiff.cc:246
findQualityFiles.jj
string jj
Definition: findQualityFiles.py:188
HLT_2018_cff.region
region
Definition: HLT_2018_cff.py:81479
DDG4ProductionCuts::initialize
void initialize()
Definition: DDG4ProductionCuts.cc:65
DDMapper::all
Vector all(const std::string &name, const std::string &value) const
get all std::mapped instances which have a specific 'name' with value 'value'
Definition: DDMapper.h:199
cms::DDSpecPar::hasValue
bool hasValue(const std::string &key) const
Definition: DDSpecparRegistry.cc:17
Exception.h
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
mps_fire.result
result
Definition: mps_fire.py:303
genParticles_cff.map
map
Definition: genParticles_cff.py:11
DDG4ProductionCuts::setProdCuts
void setProdCuts(const DDLogicalPart, G4Region *)
Definition: DDG4ProductionCuts.cc:151
DDG4ProductionCuts::dd4hepInitialize
void dd4hepInitialize()
Definition: DDG4ProductionCuts.cc:111
begin
#define begin
Definition: vmac.h:32
fit
Definition: CombinedChiSquaredLikelihood.h:6
Filter.h