Geometry_service.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file Geometry.cxx
3 /// \brief
4 /// \author messier@indiana.edu
5 ////////////////////////////////////////////////////////////////////////
6 
7 #include <cassert>
8 #include <iostream>
9 #include <algorithm>
10 
11 // Framework includes
12 #include "cetlib/search_path.h"
21 
22 // NOvA includes
23 #include "SummaryData/RunData.h"
24 #include "Geometry/Geometry.h"
25 #include "GeometryObjects/Geo.h"
26 
27 namespace geo
28 {
29  //......................................................................
31  : GeometryBase(params())
32  , fLoadedGDMLInfo("(none)")
33  {
34  // This line is not useless as it might first appear. The call to
35  // setDetectorID in the GeometryBase constructor does not call the override
36  // in this class and so communicate with DetectorService. Now that the
37  // derived class is more fully constructed call it again so it will.
39 
40  // tell the activity registry to check in after runs have been loaded
41  // into the event processing so that we can update configurations if necessary
42  reg.sPreBeginRun.watch(this, &Geometry::preBeginRun);
43  }
44 
45  //......................................................................
47  {
48  // Clear up temporary files at shutdown, if necessary
49  if(fGDMLFile.find("TEMP_GEO_GDML") != std::string::npos)
51  }
52 
53  //......................................................................
54  // 5.15.12 BJR: use the gdml file for both the fGDMLFile and fROOTFile
55  // variables as ROOT v5.30.06 is once again able to read in gdml files
56  // during batch operation, in this case think of fROOTFile meaning the
57  // file used to make the ROOT TGeoManager. I don't want to remove
58  // the separate variables in case ROOT breaks again
60  {
61  // check the current run number, do nothing if it is the same
62 
63  if(run.run() == fRunNumber) return;
64  fRunNumber = run.run();
65 
66  // check here to see if we need to load a new geometry.
67  // get the detector id from the run object
68  std::vector< art::Handle<sumdata::RunData> > rdcol;
69  run.getManyByType(rdcol);
70  if(!rdcol.empty() && !fParams.ForceUseFCLOnly()){
71  const std::string newGDMLInfo = rdcol[0]->DetGDMLInfo();
72 
73  if(newGDMLInfo == fLoadedGDMLInfo){
74  std::cout << "Geometry for run " << fRunNumber
75  << " unchanged from previous run." << std::endl;
76  // So there's nothing left to do
77  return;
78  }
79 
80  //if there is a gdml string in the file, use that
81  if(!newGDMLInfo.empty()){
82 
83  //if geometry for last run came from a temp file, remove it
84  if (fGDMLFile.find("TEMP_GEO_GDML") != std::string::npos){
85  this->RemoveTmpFile(fGDMLFile);
86  }
87 
88  //gdml info is nonzero, attempt to load geometry from here
89  if (this->MakeTmpFile(newGDMLInfo) == 0){
90  this->LoadGeometryFile(fGDMLFile, rdcol[0]->DetId());
91  fLoadedGDMLInfo = newGDMLInfo;
92  std::cout<<"Successfully loaded geometry from in file "<<fGDMLFile<<std::endl;
93  std::cout<<"The geometry stored in the file was originally named: "<<rdcol[0]->DetGeometryBaseName()<<std::endl;
94  fROOTFile = fGDMLFile; //hook left for root file
95  return;
96  }
97  else {
98  std::cout<<"WARNING: Could not load geometry information "
99  <<"from within the file. The temporary file was not "
100  <<"successfully created, something is wrong. Will "
101  <<"attempt to load from the gdml file directly."
102  <<std::endl;
103  }
104  }
105 
106  // get strings with the relative paths for the detector geometry files
107  std::string relpathgdml("Geometry/gdml/");
108  relpathgdml += rdcol[0]->DetGeometryBaseName();
109  relpathgdml += ".gdml";
110 
111  if(fGDMLFile.find(relpathgdml) != std::string::npos) return;
112 
113  // constructor decides if initialized value is a path or an environment variable
114  fGDMLFile = FindGDMLFile(relpathgdml);
115 
116  mf::LogWarning("LoadNewGeometry") << "loading new geometry files\n"
117  << fGDMLFile << "\n";
118 
120  fROOTFile = fGDMLFile; //hook left for root file
121  return;
122  }
123  else{
124  //if we made it this far without a load, check to see if the original
125  //gdml file specified in the fcl file is still stored in fGDMLFile
126  //if this is not the case, make it so and load the geometry
127  if (fGDMLFile.find(fGDMLFromFCL) == std::string::npos){
130  }
131  mf::LogWarning("LoadNewGeometry") << "cannot find sumdata::RunData object to grab detid\n"
132  << "or user has determined to only use the value "
133  << "from the fcl file\n"
134  << "using currently configured geometry:\n"
135  << "\t" << fGDMLFile << "\n";
136  fROOTFile = fGDMLFile; //hook left for root file
137  return;
138  }
139 
140  }// end of preBeginRun
141 
142  //......................................................................
143  bool Geometry::calculateMassesLong(const unsigned int number_of_points) const
144  {
146  CLHEP::HepRandomEngine& engine = rng->getEngine();
147  return GeometryBase::calculateMassesLong(number_of_points, engine);
148  }
149 
150 
151  //----------------------------------------------------------------------------
152  TVector3 Geometry::getDirectionInBeamCoordinates(TVector3 const& startPos,
153  TVector3 const& dir) const
154  {
155 
156  /// x0 in Detector coordinates. All dimensions are in cm
157  const TVector3 x0_det_cm = startPos;
158 
159  /// Small displacement in the Prong's direction
160  const TVector3 dr = dir * 0.001;
161 
162  /// x1 in Detector coordinates. All dimensions are in cm
163  const TVector3 x1_det_cm = x0_det_cm + dr;
164 
165  /// x0, x1 in Detector coordinates. All dimensions are CLHEP
166  double x0_det[3] = {x0_det_cm.X()*CLHEP::cm
167  ,x0_det_cm.Y()*CLHEP::cm
168  ,x0_det_cm.Z()*CLHEP::cm
169  };
170  double x1_det[3] = {x1_det_cm.X()*CLHEP::cm
171  ,x1_det_cm.Y()*CLHEP::cm
172  ,x1_det_cm.Z()*CLHEP::cm
173  };
174 
175  /// x0 and x1 in beam coordinates
176  double x0_beam[3] = {0.0, 0.0, 0.0};
177  double x1_beam[3] = {0.0, 0.0, 0.0};
178 
179  /// Calculate beam coordinates for x0 and x1
180  const geo::CoordinateTransformation* coordinate_transformation = getCoordinateTransformation();
181  coordinate_transformation->transformDetectorToBeamCoordinates(x0_det, x0_beam);
182  coordinate_transformation->transformDetectorToBeamCoordinates(x1_det, x1_beam);
183 
184  /// Transform into a vector class
185  const TVector3 x0_beam_vec(x0_beam);
186 
187  /// Direction in beam coordinates. Not normalized
188  TVector3 dir_beam(x1_beam);
189  dir_beam -= x0_beam_vec;
190 
191  /// return Unit vector
192  return dir_beam.Unit();
193  }
194 
195  //......................................................................
197  {
198  // Intercept all calls to setDetectorID. First set the parameter in
199  // GeometryBase as usual.
201 
202  // Make sure to also inform the detector service
204  ds->SetDetector(fDetId);
205  }
206 
208 } // end namespace geo
209 ////////////////////////////////////////////////////////////////////////
novadaq::cnv::DetId fDetId
id for the detector being used
Definition: GeometryBase.h:285
void SetDetector(novadaq::cnv::DetId det)
Call this if you have definitive special knowledge (ie you&#39;re the Geometry service) ...
Atom< bool > ForceUseFCLOnly
Definition: GeometryBase.h:71
std::string fROOTFile
root file holding the geometry
Definition: GeometryBase.h:282
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:93
bool calculateMassesLong(const unsigned int number_of_points, CLHEP::HepRandomEngine &engine) const
std::string fGDMLFile
gdml file holding the geometry
Definition: GeometryBase.h:281
bool transformDetectorToBeamCoordinates(double *input_detector_coords, double *output_beam_coords) const
RunNumber_t run() const
Definition: Run.h:47
CoordinateTransformation * getCoordinateTransformation() const
Definition: GeometryBase.h:245
void preBeginRun(art::Run const &run)
Definition: Run.h:31
static constexpr double cm
Definition: SystemOfUnits.h:99
base_engine_t & getEngine() const
unsigned int fRunNumber
Run number of configuration.
Definition: GeometryBase.h:332
bool LoadGeometryFile(std::string gdmlfile, novadaq::cnv::DetId det_id=novadaq::cnv::kUNKNOWN_DET)
In case of unknown ID, guesses from the filename.
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
TVector3 getDirectionInBeamCoordinates(TVector3 const &startPos, TVector3 const &dir) const
Get direction of the Prong in the beam coordinates.
static std::string FindGDMLFile(std::string fname)
Search for Geometry/gdml/fname in FW_SEARCH_PATH, return full path.
Collect Geo headers and supply basic geometry functions.
void getManyByType(std::vector< Handle< PROD >> &results) const
Definition: DataViewImpl.h:446
The geometry of one entire detector (near, far, ipnd)
Definition: Geometry.h:23
A very simple service to remember what detector we&#39;re working in.
Definition: run.py:1
OStream cout
Definition: OStream.cxx:6
Geometry(const Parameters &params, art::ActivityRegistry &reg)
virtual void setDetectorID(novadaq::cnv::DetId) override
Method to set DetectorID.
GlobalSignal< detail::SignalResponseType::FIFO, void(Run const &)> sPreBeginRun
bool calculateMassesLong(const unsigned int number_of_points) const
int MakeTmpFile(std::string gdmlInfo)
TDirectory * dir
Definition: macro.C:5
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
std::string fLoadedGDMLInfo
Definition: Geometry.h:43
The geometry of one entire detector (near, far, ipnd)
Definition: GeometryBase.h:49
std::string fGDMLFromFCL
keep track of original fcl parameter
Definition: GeometryBase.h:283
Helper for AttenCurve.
Definition: Path.h:10
Simple object representing a (plane, cell) pair.
Encapsulate the geometry of one entire detector (near, far, ndos)
virtual void setDetectorID(novadaq::cnv::DetId)
Method to set DetectorID.
void RemoveTmpFile(std::string fileName)
Method to remove temporary file that holds detector geometry file.
enum BeamMode string