11 #include "GeometryManager.hxx" 15 #include <TGeoManager.h> 17 #include <TGeoVolume.h> 25 #include <VersioningUtils.hxx> 26 #include <ND280AnalysisUtils.hxx> 27 #include <Parameters.hxx> 40 GeometryManager::GeometryManager(){
44 _defaultFileName =
"$(PSYCHEUTILSROOT)/data/v5r1p3-baseline-2010-11_geom-aa7ba9d3-c9bce01d-1965fe02-bb7afdbf-6c35814b.root";
45 _useDefaultGeometry = ND::params().
GetParameterD(
"psycheND280Utils.Geometry.UseDefaultGeometry");
54 if (!fGeometryManager) {
55 std::cout<<
"Create a new GeometryManager object"<<std::endl;
59 return *fGeometryManager;
66 std::string file_name = file_name0;
67 std::string geomDir = geomDir0;
68 if (_useDefaultGeometry || file_name==
""){
69 file_name = fGeometryManager->_defaultFileName;
75 if (geomID>=0 && geomID<(Int_t)(_GeoManagers.size())){
76 _currentGeomID = geomID;
81 TFile* file = TFile::Open(file_name.c_str());
82 if(!file || !file->IsOpen())
86 file->cd(geomDir.c_str());
89 TIter next(file->GetDirectory(geomDir.c_str())->GetListOfKeys());
92 TKey* defaultKey = NULL;
95 while ((key = dynamic_cast<TKey*>(next()))) {
96 if (!defaultKey && std::string(key->GetName()).find(
"ND280Geometry")!=std::string::npos) {
117 if (_currentGeomID>=0 && _currentGeomID<(Int_t)(_GeoManagers.size()) &&
118 _GeoManagers[_currentGeomID] && !std::strcmp(_GeoManagers[_currentGeomID]->GetName(), key->GetName()))
return false;
121 if (gGeoManager) gGeoManager->UnlockGeometry();
123 TGeoManager *saveGeom = gGeoManager;
124 TGeoManager *geom =
dynamic_cast<TGeoManager*
>(file->GetDirectory(geomDir.c_str())->
Get(key->GetName()));
126 gGeoManager = saveGeom;
130 if(!gGeoManager->GetCache()->HasIdArray())
131 gGeoManager->GetCache()->BuildIdArray();
136 TGeoVolume* top = gGeoManager->GetTopVolume();
137 std::string topName(top->GetName());
138 if (topName !=
"t2k") {
139 std::cout<<
"Geometry top volume has changed from t2k " << topName<<std::endl;
140 top = gGeoManager->GetVolume(
"t2k");
144 std::cout<<
"Resetting top volume to " << top->GetName()<<std::endl;
145 gGeoManager->SetTopVolume(top);
149 fGeometryManager->_GeoManagers.push_back(gGeoManager);
151 gGeoManager->LockGeometry();
156 _currentGeomID=fGeometryManager->_GeoManagers.size()-1;
158 std::cout <<
"GeometryManager::LoadGeometry(). New Geometry with ID "<< _currentGeomID <<
" loaded. " << std::endl;
159 std::cout <<
"GeometryManager::LoadGeometry(). Geometry Name = " << fGeometryManager->GeoManager()->GetName() << std::endl;
161 if (ND::params().GetParameterI(
"psycheND280Utils.Geometry.DetectorDefinitionFromROOTGeometry"))
178 if (!versionUtils::prod6_corrections)
181 anaUtils::CopyArray(DetDef::tecalLmin_p6_data, DetDef::tecalLmin, 3);
183 anaUtils::CopyArray(DetDef::tecalLmax_p6_data, DetDef::tecalLmax, 3);
185 anaUtils::CopyArray(DetDef::tecalRmin_p6_data, DetDef::tecalRmin, 3);
187 anaUtils::CopyArray(DetDef::tecalRmax_p6_data, DetDef::tecalRmax, 3);
189 anaUtils::CopyArray(DetDef::tecalTLmin_p6_data, DetDef::tecalTLmin, 3);
191 anaUtils::CopyArray(DetDef::tecalTLmax_p6_data, DetDef::tecalTLmax, 3);
193 anaUtils::CopyArray(DetDef::tecalTRmin_p6_data, DetDef::tecalTRmin, 3);
195 anaUtils::CopyArray(DetDef::tecalTRmax_p6_data, DetDef::tecalTRmax, 3);
197 anaUtils::CopyArray(DetDef::tecalBLmin_p6_data, DetDef::tecalBLmin, 3);
199 anaUtils::CopyArray(DetDef::tecalBLmax_p6_data, DetDef::tecalBLmax, 3);
201 anaUtils::CopyArray(DetDef::tecalBRmin_p6_data, DetDef::tecalBRmin, 3);
203 anaUtils::CopyArray(DetDef::tecalBRmax_p6_data, DetDef::tecalBRmax, 3);
214 std::cout <<
"GeometryManager::InitializeDetectorDefinitionFromROOTGeometry(). Overwrite DetectorDefinition values with ROOT geometry !!!!" << std::endl;
216 FillVolumesWithGeometryLoop();
218 if (ND::params().GetParameterI(
"psycheND280Utils.Geometry.DetectorDefinitionFromROOTGeometry.FillFGDInfo"))
262 std::string full_path1 =
"/t2k_1/OA_0/Magnet_0/Basket_0/Tracker_0/FGD1_0/";
263 std::string full_path2 =
"/t2k_1/OA_0/Magnet_0/Basket_0/Tracker_0/FGD2_0/";
272 for (
int i=0; i<15; i++){
273 std::stringstream sstream;
279 DetDef::fgd1BarCenter[index++] = pos[2];
283 DetDef::fgd1BarCenter[index++] = pos[2];
288 for (
int i=0; i<7; i++){
289 std::stringstream sstream;
295 DetDef::fgd2BarCenter[index++] = pos[2];
299 DetDef::fgd2BarCenter[index++] = pos[2];
303 for (
int i=0; i<6; i++){
304 std::stringstream sstream;
308 DetDef::fgd2WaterCenter[i] = pos[2];
316 DetDef::fgdXYBarWidth = 2*fabs(size[2]);
320 DetDef::fgdXYGlueWidth = 2*fabs(size[2]);
324 DetDef::fgdXYMiddleGlueWidth = 2*fabs(size[2]);
328 DetDef::fgdWaterGlueWidth = 2*fabs(size[2]);
332 DetDef::fgdXYAirWidth = 2*fabs(size[2]);
336 DetDef::fgdWaterAirWidth = 2*fabs(size[2]);
340 DetDef::fgdXYSkinWidth = 2*fabs(size[2]);
344 DetDef::fgdWaterSkinWidth = 2*fabs(size[2]);
348 DetDef::fgdWaterPCWidth = 2*fabs(size[2]);
351 DetDef::fgdXYModuleWidth = DetDef::fgdXYSkinWidth + DetDef::fgdXYGlueWidth + DetDef::fgdXYBarWidth +
352 DetDef::fgdXYMiddleGlueWidth + DetDef::fgdXYBarWidth + DetDef::fgdXYGlueWidth + DetDef::fgdXYSkinWidth;
355 DetDef::fgdWaterModuleWidth = DetDef::fgdWaterSkinWidth + DetDef::fgdWaterGlueWidth + DetDef::fgdWaterPCWidth +
356 DetDef::fgdWaterGlueWidth + DetDef::fgdWaterSkinWidth;
360 DetDef::DumpFGDInfo();
367 TGeoManager* geom = fGeometryManager->GeoManager();
368 int ini_node = geom->GetCurrentNodeId();
370 geom->cd(name.c_str());
371 TGeoNode* node = geom->GetCurrentNode();
372 TGeoVolume* volume = node->GetVolume();
373 TGeoBBox *shape =
static_cast<TGeoBBox*
>(volume->GetShape());
375 double local[3] = {0,0,0};
376 geom->LocalToMaster(local,pos);
379 size0[0] = shape->GetDX();
380 size0[1] = shape->GetDY();
381 size0[2] = shape->GetDZ();
384 while (geom->GetCurrentNodeId() > 1){
386 node = geom->GetCurrentNode();
388 const Double_t *rot = node->GetMatrix()->GetRotationMatrix();
389 for (
int i=0;i<3;i++){
391 for (
int j=0;j<3;j++){
392 size[i] += rot[i*3+j]*size0[j];
396 for (
int i=0;i<3;i++)
403 geom->CdNode(ini_node);
409 const std::string& name1,
const std::string& name2)
const {
419 for (Int_t i=0;i<3;i++){
430 for (Int_t i=0;i<3;i++){
431 temp[0][i] = pos1[i]+size1[i];
432 temp[1][i] = pos1[i]-size1[i];
434 for (Int_t j=0;j<2;j++){
435 if (temp[j][i]<det_min[i]) det_min[i]=temp[j][i];
436 if (temp[j][i]>det_max[i]) det_max[i]=temp[j][i];
440 temp[2][i] = pos2[i]+size2[i];
441 temp[3][i] = pos2[i]-size2[i];
443 for (Int_t j=2;j<4;j++){
444 if (temp[j][i]<det_min[i]) det_min[i]=temp[j][i];
445 if (temp[j][i]>det_max[i]) det_max[i]=temp[j][i];
453 sprintf(out,
" %-13s %10.3f %10.3f %10.3f %10.3f %10.3f %10.3f", det_name.c_str(), det_min[0], det_max[0], det_min[1], det_max[1], det_min[2], det_max[2]);
454 std::cout << out << std::endl;
461 void GeometryManager::FillVolumesWithGeometryLoop()
const {
465 ResetVolumeDefinitions();
467 TGeoManager* geom = fGeometryManager->GeoManager();
478 gGeoManager->PopPath();
480 DetDef::DumpVolumes();
486 void GeometryManager::RecurseGeometry(std::string name)
const {
488 TGeoManager* geom = fGeometryManager->GeoManager();
490 TGeoNode* node = geom->GetCurrentNode();
491 name = name +
"/" + node->GetName();
493 if (!VisitNode(name, node)) {
499 for (
int i=0; i<node->GetNdaughters(); ++i) {
501 RecurseGeometry(name);
508 bool GeometryManager::VisitNode(
const std::string& name,
const TGeoNode* node)
const {
510 TGeoManager* geom = fGeometryManager->GeoManager();
511 TGeoVolume *volume = node->GetVolume();
512 TGeoBBox* shape =
dynamic_cast<TGeoBBox*
>(volume->GetShape());
514 Double_t high_local[3] = {shape->GetDX(), shape->GetDY(), shape->GetDZ()};
515 Double_t low_local[3] = {-high_local[0], -high_local[1], -high_local[2]};
517 Double_t high_tmp[3];
520 geom->LocalToMaster(low_local, low_tmp);
521 geom->LocalToMaster(high_local, high_tmp);
526 std::copy(low_tmp, low_tmp + 3, low);
527 std::copy(high_tmp, high_tmp + 3, high);
530 if (name.find(
"FGD") != std::string::npos
531 || name.find(
"TPC") != std::string::npos){
532 UpdateVolumeBound(DetDef::trackerAllmin, DetDef::trackerAllmax, low, high);
537 if (name.find(
"P0D_") != std::string::npos
538 && name.find(
"Bar_") != std::string::npos) {
539 UpdateVolumeBound(DetDef::p0dmin, DetDef::p0dmax, low, high);
545 if (name.find(
"TPC1") != std::string::npos
548 UpdateVolumeBound(DetDef::tpc1min, DetDef::tpc1max, low, high);
551 if (name.find(
"TPC2") != std::string::npos
554 UpdateVolumeBound(DetDef::tpc2min, DetDef::tpc2max, low, high);
557 if (name.find(
"TPC3") != std::string::npos
560 UpdateVolumeBound(DetDef::tpc3min, DetDef::tpc3max, low, high);
565 if (name.find(
"FGD1") != std::string::npos
566 && name.find(
"/Scint") != std::string::npos) {
567 UpdateVolumeBound(DetDef::fgd1min, DetDef::fgd1max, low, high);
572 if (name.find(
"FGD2") != std::string::npos
573 && name.find(
"/Scint") != std::string::npos) {
574 UpdateVolumeBound(DetDef::fgd2min, DetDef::fgd2max, low, high);
579 if (name.find(
"DsECal") != std::string::npos
580 && name.find(
"/Active") != std::string::npos) {
581 UpdateVolumeBound(DetDef::dsecalmin, DetDef::dsecalmax, low, high);
586 if (name.find(
"/LeftClam_") != std::string::npos
587 && name.find(
"/P0DECal_") != std::string::npos
588 && name.find(
"/Top_") != std::string::npos
589 && name.find(
"/Active") != std::string::npos) {
590 UpdateVolumeBound(DetDef::pecalTLmin, DetDef::pecalTLmax, low, high);
593 if (name.find(
"/LeftClam_") != std::string::npos
594 && name.find(
"/P0DECal_") != std::string::npos
595 && name.find(
"/Side_") != std::string::npos
596 && name.find(
"/Active") != std::string::npos) {
597 UpdateVolumeBound(DetDef::pecalLmin, DetDef::pecalLmax, low, high);
600 if (name.find(
"/LeftClam_") != std::string::npos
601 && name.find(
"/P0DECal_") != std::string::npos
602 && name.find(
"/Bottom_") != std::string::npos
603 && name.find(
"/Active_") != std::string::npos) {
604 UpdateVolumeBound(DetDef::pecalBLmin, DetDef::pecalBLmax, low, high);
607 if (name.find(
"/RightClam_") != std::string::npos
608 && name.find(
"/P0DECal_") != std::string::npos
609 && name.find(
"/Top_") != std::string::npos
610 && name.find(
"/Active") != std::string::npos) {
611 UpdateVolumeBound(DetDef::pecalTRmin, DetDef::pecalTRmax, low, high);
614 if (name.find(
"/RightClam_") != std::string::npos
615 && name.find(
"/P0DECal_") != std::string::npos
616 && name.find(
"/Side_") != std::string::npos
617 && name.find(
"/Active") != std::string::npos) {
618 UpdateVolumeBound(DetDef::pecalRmin, DetDef::pecalRmax, low, high);
621 if (name.find(
"/RightClam_") != std::string::npos
622 && name.find(
"/P0DECal_") != std::string::npos
623 && name.find(
"/Bottom_") != std::string::npos
624 && name.find(
"/Active_") != std::string::npos) {
625 UpdateVolumeBound(DetDef::pecalBRmin, DetDef::pecalBRmax, low, high);
630 if (name.find(
"/LeftClam_") != std::string::npos
631 && name.find(
"/BrlECal_") != std::string::npos
632 && name.find(
"/Top_") != std::string::npos
633 && name.find(
"/Active") != std::string::npos) {
634 UpdateVolumeBound(DetDef::tecalTLmin, DetDef::tecalTLmax, low, high);
637 if (name.find(
"/LeftClam_") != std::string::npos
638 && name.find(
"/BrlECal_") != std::string::npos
639 && name.find(
"/Side_") != std::string::npos
640 && name.find(
"/Active") != std::string::npos) {
641 UpdateVolumeBound(DetDef::tecalLmin, DetDef::tecalLmax, low, high);
644 if (name.find(
"/LeftClam_") != std::string::npos
645 && name.find(
"/BrlECal_") != std::string::npos
646 && name.find(
"/Bottom_") != std::string::npos
647 && name.find(
"/Active_") != std::string::npos) {
648 UpdateVolumeBound(DetDef::tecalBLmin, DetDef::tecalBLmax, low, high);
651 if (name.find(
"/RightClam_") != std::string::npos
652 && name.find(
"/BrlECal_") != std::string::npos
653 && name.find(
"/Top_") != std::string::npos
654 && name.find(
"/Active") != std::string::npos) {
655 UpdateVolumeBound(DetDef::tecalTRmin, DetDef::tecalTRmax, low, high);
658 if (name.find(
"/RightClam_") != std::string::npos
659 && name.find(
"/BrlECal_") != std::string::npos
660 && name.find(
"/Side_") != std::string::npos
661 && name.find(
"/Active") != std::string::npos) {
662 UpdateVolumeBound(DetDef::tecalRmin, DetDef::tecalRmax, low, high);
665 if (name.find(
"/RightClam_") != std::string::npos
666 && name.find(
"/BrlECal_") != std::string::npos
667 && name.find(
"/Bottom_") != std::string::npos
668 && name.find(
"/Active_") != std::string::npos) {
669 UpdateVolumeBound(DetDef::tecalBRmin, DetDef::tecalBRmax, low, high);
675 if (name.find(
"LeftClam_") != std::string::npos
676 && ( name.find(
"0_pv") != std::string::npos
677 || name.find(
"FluxReturn_pv_0_") != std::string::npos)) {
678 UpdateVolumeBound(DetDef::smrdBLmin, DetDef::smrdBLmax, low, high);
682 if (name.find(
"LeftClam_") != std::string::npos
683 && ( name.find(
"6_pv") != std::string::npos
684 || name.find(
"FluxReturn_pv_170_0") != std::string::npos)) {
685 UpdateVolumeBound(DetDef::smrdTLmin, DetDef::smrdTLmax, low, high);
689 if (name.find(
"RightClam_") != std::string::npos
690 && ( name.find(
"0_pv") != std::string::npos
691 || name.find(
"FluxReturn_pv_0_") != std::string::npos)) {
692 UpdateVolumeBound(DetDef::smrdTRmin, DetDef::smrdTRmax, low, high);
696 if (name.find(
"RightClam_") != std::string::npos
697 && ( name.find(
"6_pv") != std::string::npos
698 || name.find(
"FluxReturn_pv_170_0") != std::string::npos)) {
699 UpdateVolumeBound(DetDef::smrdBRmin, DetDef::smrdBRmax, low, high);
706 if (name.find(
"LeftClam_") != std::string::npos
707 && ( name.find(
"MRDSide:4") != std::string::npos
708 || name.find(
"FluxReturn_pv_67_0") != std::string::npos)) {
709 UpdateVolumeBound(DetDef::smrd15Lmin, DetDef::smrd15Lmax, low, high);
713 if (name.find(
"LeftClam_") != std::string::npos
714 && ( name.find(
"MRDSide:5") != std::string::npos
715 || name.find(
"FluxReturn_pv_67_5") != std::string::npos)) {
716 UpdateVolumeBound(DetDef::smrd6Lmin, DetDef::smrd6Lmax, low, high);
720 if (name.find(
"LeftClam_") != std::string::npos
721 && ( name.find(
"MRDSide:7") != std::string::npos
722 || name.find(
"FluxReturn_pv_67_6") != std::string::npos)) {
723 UpdateVolumeBound(DetDef::smrd78Lmin, DetDef::smrd78Lmax, low, high);
729 if (name.find(
"RightClam_") != std::string::npos
730 && ( name.find(
"MRDSide:4") != std::string::npos
731 || name.find(
"FluxReturn_pv_67_0") != std::string::npos)) {
732 UpdateVolumeBound(DetDef::smrd15Rmin, DetDef::smrd15Rmax, low, high);
736 if (name.find(
"RightClam_") != std::string::npos
737 && ( name.find(
"MRDSide:5") != std::string::npos
738 || name.find(
"FluxReturn_pv_67_5") != std::string::npos)) {
739 UpdateVolumeBound(DetDef::smrd6Rmin, DetDef::smrd6Rmax, low, high);
743 if (name.find(
"RightClam_") != std::string::npos
744 && ( name.find(
"MRDSide:7") != std::string::npos
745 || name.find(
"FluxReturn_pv_67_6") != std::string::npos)) {
746 UpdateVolumeBound(DetDef::smrd78Rmin, DetDef::smrd78Rmax, low, high);
756 void GeometryManager::UpdateVolumeBound(Float_t* detMin, Float_t* detMax,
const Float_t* low,
const Float_t* high)
const{
759 for (
int i=0; i<3; i++){
761 detMin[i] = std::min(detMin[i], low[i]);
762 detMin[i] = std::min(detMin[i], high[i]);
764 detMax[i] = std::max(detMax[i], low[i]);
765 detMax[i] = std::max(detMax[i], high[i]);
771 void GeometryManager::ResetVolumeDefinitions()
const {
774 Float_t min[3] = {1e10, 1e10, 1e10};
775 Float_t max[3] = {-1e10, -1e10, -1e10};
void InitializeGeometry(bool IsMC=true) const
void SetDetectorBoundaries(const std::string &det_name, Float_t *det_min, Float_t *det_max, const std::string &name1, const std::string &name2="") const
Overrride the values in DetectorDefinion with the ones extracted from the ROOT geometry.
This exception is thrown when fail to open a file with geometry.
static GeometryManager & Get()
Get a pointer to the singleton instance of the geometry information.
double GetParameterD(std::string)
Get parameter. Value is returned as double.
This exception is thrown when no geometry is found in a file.
void GetVolumeProperties(const std::string &name, Double_t *pos, Double_t *size) const
Get the volume position and size from the ROOT geometry.
void InitializeDetectorDefinitionFromROOTGeometry() const
Overrride the values in DetectorDefinion with the ones extracted from the ROOT geometry.
void FillFGDInfoFromROOTGeometry() const
Fill FGD info.
bool LoadGeometry(const std::string &file="", Int_t geomID=-1, const std::string &geomDir="")
Load the TGeoManager from the input root file. Returns true when the new geometry was loaded...