Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-02 03:46:11

0001 #include "SimG4Core/Notification/interface/BeginOfRun.h"
0002 #include "SimG4Core/Notification/interface/Observer.h"
0003 #include "SimG4Core/Watcher/interface/SimWatcher.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 
0006 #include "G4Run.hh"
0007 #include "G4VPhysicalVolume.hh"
0008 #include "G4LogicalVolume.hh"
0009 #include "G4NavigationHistory.hh"
0010 #include "G4TransportationManager.hh"
0011 
0012 #include <set>
0013 #include <map>
0014 #include <iostream>
0015 #include <string>
0016 
0017 class PrintSensitive : public SimWatcher, public Observer<const BeginOfRun *> {
0018 public:
0019   PrintSensitive(edm::ParameterSet const &p);
0020   ~PrintSensitive() override;
0021 
0022 private:
0023   void update(const BeginOfRun *run) override;
0024   int dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, bool printIt, int ns, std::ostream &out = G4cout);
0025   G4VPhysicalVolume *getTopPV();
0026 
0027 private:
0028   std::string name_;
0029   int nchar_;
0030   G4NavigationHistory fHistory;
0031 };
0032 
0033 PrintSensitive::PrintSensitive(const edm::ParameterSet &p) {
0034   name_ = p.getUntrackedParameter<std::string>("Name", "*");
0035   nchar_ = name_.find('*');
0036   name_.assign(name_, 0, nchar_);
0037   G4cout << "PrintSensitive:: Print position of all Sensitive Touchables: "
0038          << " for names (0-" << nchar_ << ") = " << name_ << G4endl;
0039 }
0040 
0041 PrintSensitive::~PrintSensitive() {}
0042 
0043 void PrintSensitive::update(const BeginOfRun *run) {
0044   G4VPhysicalVolume *theTopPV = getTopPV();
0045   int nsens = dumpTouch(theTopPV, 0, false, 0, G4cout);
0046   G4cout << "\nTotal number of sensitive detector volumes for " << name_ << " is " << nsens << G4endl;
0047 }
0048 
0049 int PrintSensitive::dumpTouch(G4VPhysicalVolume *pv, unsigned int leafDepth, bool printIt, int ns, std::ostream &out) {
0050   if (leafDepth == 0)
0051     fHistory.SetFirstEntry(pv);
0052   else
0053     fHistory.NewLevel(pv, kNormal, pv->GetCopyNo());
0054 
0055   int nsens(ns);
0056   G4ThreeVector globalpoint = fHistory.GetTopTransform().Inverse().TransformPoint(G4ThreeVector(0, 0, 0));
0057   G4LogicalVolume *lv = pv->GetLogicalVolume();
0058 
0059   std::string mother = "World";
0060   if (pv->GetMotherLogical())
0061     mother = pv->GetMotherLogical()->GetName();
0062   std::string lvname = lv->GetName();
0063   lvname.assign(lvname, 0, nchar_);
0064   if (lvname == name_)
0065     printIt = true;
0066 
0067   if (lv->GetSensitiveDetector() && printIt) {
0068     ++nsens;
0069     out << nsens << ":" << leafDepth << " ### VOLUME = " << lv->GetName() << " Copy No " << pv->GetCopyNo() << " in "
0070         << mother << " global position of centre " << globalpoint << " (r=" << globalpoint.perp()
0071         << ", phi=" << globalpoint.phi() / CLHEP::deg << ")\n";
0072   }
0073 
0074   int NoDaughters = lv->GetNoDaughters();
0075   while ((NoDaughters--) > 0) {
0076     G4VPhysicalVolume *pvD = lv->GetDaughter(NoDaughters);
0077     if (!pvD->IsReplicated())
0078       nsens = dumpTouch(pvD, leafDepth + 1, printIt, nsens, out);
0079   }
0080 
0081   if (leafDepth > 0)
0082     fHistory.BackLevel();
0083   return nsens;
0084 }
0085 
0086 G4VPhysicalVolume *PrintSensitive::getTopPV() {
0087   return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
0088 }
0089 
0090 #include "SimG4Core/Watcher/interface/SimWatcherFactory.h"
0091 #include "FWCore/PluginManager/interface/ModuleDef.h"
0092 
0093 DEFINE_SIMWATCHER(PrintSensitive);