CMS 3D CMS Logo

PrintMTDSens.cc
Go to the documentation of this file.
1 //#define EDM_ML_DEBUG
2 
8 
14 
15 #include "G4Run.hh"
16 #include "G4VPhysicalVolume.hh"
17 #include "G4LogicalVolume.hh"
18 #include "G4NavigationHistory.hh"
19 #include "G4TransportationManager.hh"
20 #include "G4Box.hh"
21 
22 #include <set>
23 #include <map>
24 #include <iostream>
25 #include <string>
26 #include <vector>
27 
28 class PrintMTDSens : public SimWatcher, public Observer<const BeginOfRun *> {
29 public:
31  ~PrintMTDSens() override;
32 
33 private:
34  void update(const BeginOfRun *run) override;
35  int dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, int ns);
36  G4VPhysicalVolume *getTopPV();
37  bool getBaseNumber();
38 
39 private:
40  std::vector<std::string> names_;
41  std::vector<size_t> nsens_;
42  G4NavigationHistory fHistory;
43 
47 };
48 
49 PrintMTDSens::PrintMTDSens(const edm::ParameterSet &p) : thisN_(), btlNS_(), etlNS_() {
50  names_ = p.getUntrackedParameter<std::vector<std::string>>("Name");
51  nsens_.resize(names_.size());
52  for (size_t index = 0; index < nsens_.size(); index++) {
53  nsens_[index] = 0;
54  }
55  G4cout << "PrintMTDSens:: Print position of MTD Sensitive Touchables: " << G4endl;
56  for (const auto &thisName : names_) {
57  G4cout << " for name " << thisName << "\n";
58  }
59  G4cout << " Total of " << names_.size() << " sensitive volume types" << G4endl;
60 }
61 
63 
65  G4VPhysicalVolume *theTopPV = getTopPV();
66  int ntotal = dumpTouch(theTopPV, 0, 0);
67  G4cout << "\nTotal number of sensitive detector volumes for MTD is " << ntotal << G4endl;
68  for (size_t index = 0; index < nsens_.size(); index++) {
69  G4cout << "Sensitive volume " << names_[index] << " # copies " << nsens_[index] << G4endl;
70  }
71 }
72 
74 
75 int PrintMTDSens::dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, int ns) {
76  if (leafDepth == 0)
77  fHistory.SetFirstEntry(pv);
78  else
79  fHistory.NewLevel(pv, kNormal, pv->GetCopyNo());
80 
81  int ntotal(ns);
82  G4ThreeVector globalpoint = fHistory.GetTopTransform().Inverse().TransformPoint(G4ThreeVector(0, 0, 0));
83  G4LogicalVolume *lv = pv->GetLogicalVolume();
84 
85  std::string mother = "World";
86  bool printIt(false);
87  if (pv->GetMotherLogical()) {
88  mother = pv->GetMotherLogical()->GetName();
89  }
90  size_t index(0);
91  for (const auto &thisName : names_) {
92  G4String g4name(thisName);
93  if (G4StrUtil::contains(lv->GetName(), g4name)) {
94  printIt = true;
95  break;
96  }
97  index++;
98  }
99 
100  if (lv->GetSensitiveDetector() && printIt) {
101  std::stringstream sunitt;
102  ++ntotal;
103  ++nsens_[index];
104  bool isBarrel = getBaseNumber();
105  if (isBarrel) {
107  sunitt << theId.rawId();
108 #ifdef EDM_ML_DEBUG
109  G4cout << theId << G4endl;
110 #endif
111  } else {
113  sunitt << theId.rawId();
114 #ifdef EDM_ML_DEBUG
115  G4cout << theId << G4endl;
116 #endif
117  }
118 
119  auto fround = [&](double in) {
120  std::stringstream ss;
121  ss << std::fixed << std::setw(14) << roundIfNear0(in);
122  return ss.str();
123  };
124 
125  G4Box *thisSens = static_cast<G4Box *>(lv->GetSolid());
126  G4ThreeVector cn1Global = fHistory.GetTopTransform().Inverse().TransformPoint(
127  G4ThreeVector(thisSens->GetXHalfLength(), thisSens->GetYHalfLength(), thisSens->GetZHalfLength()));
128 
129  sunitt << fround(globalpoint.x()) << fround(globalpoint.y()) << fround(globalpoint.z()) << fround(cn1Global.x())
130  << fround(cn1Global.y()) << fround(cn1Global.z());
131  edm::LogVerbatim("MTDG4sensUnitTest") << sunitt.str();
132  }
133 
134  int NoDaughters = lv->GetNoDaughters();
135  while ((NoDaughters--) > 0) {
136  G4VPhysicalVolume *pvD = lv->GetDaughter(NoDaughters);
137  if (!pvD->IsReplicated())
138  ntotal = dumpTouch(pvD, leafDepth + 1, ntotal);
139  }
140 
141  if (leafDepth > 0)
142  fHistory.BackLevel();
143  return ntotal;
144 }
145 
146 G4VPhysicalVolume *PrintMTDSens::getTopPV() {
147  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
148 }
149 
151  bool isBTL(false);
152  thisN_.reset();
153  thisN_.setSize(fHistory.GetMaxDepth());
154  int theSize = fHistory.GetDepth() + 1;
155  if (thisN_.getCapacity() < theSize)
156  thisN_.setSize(theSize);
157  //Get name and copy numbers
158  if (theSize > 1) {
159 #ifdef EDM_ML_DEBUG
160  G4cout << "Building MTD basenumber:" << G4endl;
161 #endif
162  for (int ii = theSize; ii-- > 0;) {
163  thisN_.addLevel(fHistory.GetVolume(ii)->GetName(), fHistory.GetReplicaNo(ii));
164 #ifdef EDM_ML_DEBUG
165  G4cout << "PrintMTDSens::getBaseNumber(): Adding level " << theSize - 1 - ii << ": "
166  << fHistory.GetVolume(ii)->GetName() << "[" << fHistory.GetReplicaNo(ii) << "]" << G4endl;
167 #endif
168  if (!isBTL) {
169  isBTL = G4StrUtil::contains(fHistory.GetVolume(ii)->GetName(), "BarrelTimingLayer");
170  }
171  }
172  }
173  return isBTL;
174 }
175 
178 
Log< level::Info, true > LogVerbatim
int dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, int ns)
Definition: PrintMTDSens.cc:75
#define DEFINE_SIMWATCHER(type)
bool contains(EventRange const &lh, EventID const &rh)
Definition: EventRange.cc:37
ETLNumberingScheme etlNS_
Definition: PrintMTDSens.cc:46
std::vector< size_t > nsens_
Definition: PrintMTDSens.cc:41
G4NavigationHistory fHistory
Definition: PrintMTDSens.cc:42
void addLevel(const std::string_view name, const int copyNumber)
~PrintMTDSens() override
Definition: PrintMTDSens.cc:62
PrintMTDSens(edm::ParameterSet const &p)
Definition: PrintMTDSens.cc:49
MTDBaseNumber thisN_
Definition: PrintMTDSens.cc:44
std::vector< std::string > names_
Definition: PrintMTDSens.cc:40
G4VPhysicalVolume * getTopPV()
BTLNumberingScheme btlNS_
Definition: PrintMTDSens.cc:45
constexpr valType roundIfNear0(valType value, double tolerance=1.e-7)
Definition: Rounding.h:11
ii
Definition: cuy.py:589
bool getBaseNumber()
uint32_t getUnitID(const MTDBaseNumber &baseNumber) const override
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
Detector identifier class for the Endcap Timing Layer.
Definition: ETLDetId.h:15
uint32_t getUnitID(const MTDBaseNumber &baseNumber) const override
Detector identifier class for the Barrel Timing Layer. The crystal count must start from 0...
Definition: BTLDetId.h:19
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
Definition: PrintMTDSens.cc:64
void setSize(const int size)
__host__ __device__ void printIt(C *m, const char *prefix="")
Definition: FitUtils.h:53