diff --git a/DDCore/include/DD4hep/DetectorSelector.h b/DDCore/include/DD4hep/DetectorSelector.h new file mode 100644 index 0000000000000000000000000000000000000000..e35b45bdec935cf59c5071388b470c819b18fc1e --- /dev/null +++ b/DDCore/include/DD4hep/DetectorSelector.h @@ -0,0 +1,84 @@ +// $Id$ +//========================================================================== +// AIDA Detector description implementation for LCD +//-------------------------------------------------------------------------- +// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) +// All rights reserved. +// +// For the licensing terms see $DD4hepINSTALL/LICENSE. +// For the list of contributors see $DD4hepINSTALL/doc/CREDITS. +// +// Author : M.Frank +// +//========================================================================== + +#ifndef DD4HEP_DETECTORSELECTOR_H +#define DD4HEP_DETECTORSELECTOR_H + +// Framework include files +#include "DD4hep/LCDD.h" + +/// Namespace for the AIDA detector description toolkit +namespace DD4hep { + + /// Namespace for the geometry part of the AIDA detector description toolkit + namespace Geometry { + + + /// View on LCDD to select detectors according to certain criteria + /** + * Usage: + * + * LCDD& lcd = ....; + * DetectorSelector selector(lcdd); + * DetectorSelector::Result r = selector.detectors("tracker"); + * + * or multiple types: + * r = selector.detectors("tracker", "calorimeter"); + * + * or selections using DetElement flags (DetElement::typeFlag) + * r = selector.detectors(0x15, 0xFF0); + * + * \author M.Frank + * \version 1.0 + */ + class DetectorSelector { + public: + /// Result set definition + typedef std::vector<DetElement> Result; + /// Reference to main detector description object + LCDD& lcdd; + + public: + /// Default constructor + DetectorSelector(LCDD& _lcdd) : lcdd(_lcdd) {} + /// Default destructor + ~DetectorSelector() {} + + /// Access a set of subdetectors according to the sensitive type. + /** + Please note: + - The sensitive type of a detector is set in the 'detector constructor'. + - Not sensitive detector structures have the name 'passive' + - Compounds (ie. nested detectors) are of type 'compound' + */ + const Result& detectors(const std::string& type); + + /// Access a set of subdetectors according to several sensitive types. + Result detectors(const std::string& type1, + const std::string& type2, + const std::string& type3="", + const std::string& type4="", + const std::string& type5="" ); + + /** return a vector with all detectors that have all the type properties in + * includeFlag set but none of the properties given in excludeFlag + */ + Result detectors(unsigned int includeFlag, + unsigned int excludeFlag=0 ) const ; + }; + + } /* End namespace Geometry */ +} /* End namespace DD4hep */ + +#endif /* DD4HEP_DETECTORSELECTOR_H */ diff --git a/DDCore/src/DetectorSelector.cpp b/DDCore/src/DetectorSelector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..103f8724ff79c1c6e18c802f8efa1b21871b8bd6 --- /dev/null +++ b/DDCore/src/DetectorSelector.cpp @@ -0,0 +1,70 @@ +// $Id$ +//========================================================================== +// AIDA Detector description implementation for LCD +//-------------------------------------------------------------------------- +// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) +// All rights reserved. +// +// For the licensing terms see $DD4hepINSTALL/LICENSE. +// For the list of contributors see $DD4hepINSTALL/doc/CREDITS. +// +// Author : M.Frank +// +//========================================================================== + +// Framework include files +#include "DD4hep/DetectorSelector.h" +#include "DD4hep/LCDD.h" + +using namespace std; +using namespace DD4hep::Geometry; + +/// Access a set of subdetectors according to the sensitive type. +const DetectorSelector::Result& +DetectorSelector::detectors(const string& type) +{ + return lcdd.detectors(type); +} + +/// Access a set of subdetectors according to several sensitive types. +DetectorSelector::Result +DetectorSelector::detectors(const string& type1, + const string& type2, + const string& type3, + const string& type4, + const string& type5) { + const string* types[] = { &type1, &type2, &type3, &type4, &type5 }; + Result result; + for(size_t i=0; i<sizeof(types)/sizeof(types[0]); ++i) { + try { + if ( !types[i]->empty() ) { + const vector<DetElement>& v = lcdd.detectors(*(types[i])); + result.insert(result.end(),v.begin(),v.end()); + } + } + catch(...) {} + } + return result; +} + +/** return a vector with all detectors that have all the type properties in + * includeFlag set but none of the properties given in excludeFlag + */ +DetectorSelector::Result +DetectorSelector::detectors(unsigned int includeFlag, unsigned int excludeFlag ) const { + Result result; + const LCDD::HandleMap& entries = lcdd.detectors(); + result.reserve( entries.size() ) ; + lcdd.detectors(""); // Just to ensure the geometry is closed.... + for(LCDD::HandleMap::const_iterator i=entries.begin(); i!=entries.end(); ++i) { + DetElement det((*i).second); + if ( det.parent().isValid() ) { // Exclude 'world' + //fixme: what to do with compounds - add their daughters ? + // ... + if( ( det.typeFlag() & includeFlag ) == includeFlag && + ( det.typeFlag() & excludeFlag ) == 0 ) + result.push_back( det ) ; + } + } + return result; +}