diff --git a/DDCore/include/DD4hep/IDDescriptor.h b/DDCore/include/DD4hep/IDDescriptor.h index ea860384f23356711aafcfd7f3279448b415c56e..78ff5b3d879eb26db0a1a3e4da86bb5bc78b5b5a 100644 --- a/DDCore/include/DD4hep/IDDescriptor.h +++ b/DDCore/include/DD4hep/IDDescriptor.h @@ -12,6 +12,7 @@ // Framework include files #include "DD4hep/Handle.h" +#include "DD4hep/Primitives.h" #include "DD4hep/BitField64.h" // C++ include files @@ -37,28 +38,12 @@ namespace DD4hep { */ struct IDDescriptor : public Ref_t { public: - typedef unsigned long long int VolumeID; -#if 0 - //typedef std::pair<int,int> Field; - struct Field { - int first, second; - VolumeID mask; - VolumeID encode(int value) const { - VolumeID v = value; - return ( ( v << first ) | mask ) ; - //xx return mask|((v<<(64-second))>>first); - - } - int decode(VolumeID value) const { - return ( ~mask & value ) >> first ; - //xx return (~mask&value)>>(64-second-first); - } - }; -#endif typedef std::pair<std::string,int> VolID; typedef BitFieldValue* Field; typedef std::vector<std::pair<std::string,Field> > FieldMap; typedef std::vector<std::pair<size_t,std::string> > FieldIDs; + typedef std::pair<Field,VolumeID> VolIDField; + typedef std::vector<VolIDField> VolIDFields; /** @class IDDescriptor::Object IDDescriptor.h DDCore/IDDescriptor.h * @@ -70,11 +55,14 @@ namespace DD4hep { FieldMap fieldMap; FieldIDs fieldIDs; std::string description; - //unsigned maxBit; /// Standard constructor Object(const std::string& initString); /// Default destructor virtual ~Object(); + /// Access to the field container of the BitField64 + const std::vector<BitFieldValue*> fields() const { + return _fields; + } }; public: /// Default constructor @@ -99,6 +87,8 @@ namespace DD4hep { Field field(size_t identifier) const; /// Encoede a set of volume identifiers (corresponding to this description of course!) to a volumeID. VolumeID encode(const std::vector<VolID>& ids) const; + /// Decode volume IDs and return filled descriptor with all fields + void decodeFields(VolumeID vid, VolIDFields& fields); /// Acces string representation std::string toString() const; }; diff --git a/DDCore/include/DD4hep/Primitives.h b/DDCore/include/DD4hep/Primitives.h new file mode 100644 index 0000000000000000000000000000000000000000..b3434df17bd69830746fe17d547e08e4fe9998ff --- /dev/null +++ b/DDCore/include/DD4hep/Primitives.h @@ -0,0 +1,29 @@ +// $Id: Primitives.h 603 2013-06-13 21:15:14Z markus.frank $ +//==================================================================== +// AIDA Detector description implementation +//-------------------------------------------------------------------- +// +// Author : M.Frank +// +//==================================================================== +#ifndef DD4HEP_DD4HEP_PRIMITIVES_H +#define DD4HEP_DD4HEP_PRIMITIVES_H + +/* + * DD4hep namespace declaration + */ +namespace DD4hep { + + // Put here global basic type defintiions derived from primitive types of the DD4hep namespace + typedef unsigned long long int VolumeID; + + /* + * Geometry namespace declaration + */ + namespace Geometry { + + // Put here global basic type defintiions derived from primitive types of the Geometry namespace + + } // End namespace Geometry +} // End namespace DD4hep +#endif // DD4HEP_DD4HEP_PRIMITIVES_H diff --git a/DDCore/include/DD4hep/Printout.h b/DDCore/include/DD4hep/Printout.h index d143b3d76eed642548d9e0abdf4bc3a38d8ddd03..f686a7e50a2e6e640561a75ea392a7cf4b5c4f03 100644 --- a/DDCore/include/DD4hep/Printout.h +++ b/DDCore/include/DD4hep/Printout.h @@ -16,10 +16,14 @@ // C/C++ include files #include <cstdio> #include <cstdlib> +#include <cstdarg> #include <map> #include <string> #include <iostream> +/// Forward declarations +class TNamed; + /* * DD4hep namespace declaration */ @@ -79,6 +83,58 @@ namespace DD4hep { */ int printout(PrintLevel severity, const char* src, const std::string& fmt, ...); + /** Calls the display action + * @arg severity [int,read-only] Display severity flag (see enum) + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @arg args [ap_list,read-only] List with variable number of arguments to fill format string. + * @return Status code indicating success or failure + */ + int printout(PrintLevel severity, const char* src, const char* fmt, va_list& args); + + /** Calls the display action + * @arg severity [int,read-only] Display severity flag (see enum) + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @arg args [ap_list,read-only] List with variable number of arguments to fill format string. + * @return Status code indicating success or failure + */ + int printout(PrintLevel severity, const std::string& src, const char* fmt, va_list& args); + + /** Calls the display action + * @arg severity [int,read-only] Display severity flag (see enum) + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @arg args [ap_list,read-only] List with variable number of arguments to fill format string. + * @return Status code indicating success or failure + */ + int printout(PrintLevel severity, const std::string& src, const std::string& fmt, va_list& args); + + /** Calls the display action + * @arg severity [int,read-only] Display severity flag (see enum) + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @arg args [ap_list,read-only] List with variable number of arguments to fill format string. + * @return Status code indicating success or failure + */ + int printout(PrintLevel severity, const char* src, const std::string& fmt, va_list& args); + + /** Build formatted string + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @return Status code indicating success or failure + */ + std::string format(const std::string& src, const std::string& fmt, ...); + + /** Build formatted string + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @arg args [ap_list,read-only] List with variable number of arguments to fill format string. + * @return Status code indicating success or failure + */ + std::string format(const std::string& src, const std::string& fmt, va_list& args); + + /// Set new print level. Returns the old print level PrintLevel setPrintLevel(PrintLevel new_level); diff --git a/DDCore/include/DD4hep/VolumeManager.h b/DDCore/include/DD4hep/VolumeManager.h index f5ea1784317c5774f5bcd34f9ca34255cff136f4..908bc7dedca173811d457c73215edb763dc34ac9 100644 --- a/DDCore/include/DD4hep/VolumeManager.h +++ b/DDCore/include/DD4hep/VolumeManager.h @@ -61,8 +61,7 @@ namespace DD4hep { */ struct VolumeManager : public Ref_t { public: - typedef unsigned long long int VolumeID; - + typedef DD4hep::VolumeID VolumeID; // Creation flags enum PopulateFlags { NONE = 0, diff --git a/DDCore/src/IDDescriptor.cpp b/DDCore/src/IDDescriptor.cpp index 8ca79a8a7e54c49a9070e76e0d867264022c9f1c..efc675d75e99ec0655acab4d270fe49b790af708 100644 --- a/DDCore/src/IDDescriptor.cpp +++ b/DDCore/src/IDDescriptor.cpp @@ -17,51 +17,8 @@ using namespace DD4hep; using namespace DD4hep::Geometry; -namespace { - void split(const string& str, char delim, vector<string>& result) { - string t, s, d = str; - while( !d.empty() ) { - size_t idx = d.find(delim); - s = d.substr(0,idx); - result.push_back(s); - if ( idx == string::npos ) break; - d = string(d).substr(idx+1); - } - } - +namespace { void _construct(IDDescriptor::Object* o, const string& dsc) { -#if 0 - typedef vector<string> Elements; - Elements elements, f; - IDDescriptor::Field field; - int pos = 0; - - split(dsc,',',elements); - o->maxBit = 0; - o->fieldIDs.clear(); - o->fieldMap.clear(); - o->description = dsc ; - - for(Elements::const_iterator i=elements.begin();i!=elements.end();++i) { - const string& s = *i; - f.clear(); - split(s,':',f); - if ( !(f.size() == 2 || f.size() == 3) ) - throw runtime_error("Invalid field descriptor:"+dsc); - field.first = f.size() == 3 ? ::atoi(f[1].c_str()) : pos; - field.second = f.size() == 3 ? ::atoi(f[2].c_str()) : ::atoi(f[1].c_str()); - field.second = ::abs(field.second); - - field.mask = ~ ( ( ( 0x0001LL << (field.second) ) - 1 ) << field.first ) ; - //field.mask = ~((~0x0ull<<(64-field.second))>>(64-field.second)<<(64-field.first-field.second)); - - pos = field.first + ::abs(field.second); - if ( pos>o->maxBit ) o->maxBit = pos; - o->fieldIDs.push_back(make_pair(o->fieldMap.size(),f[0])); - o->fieldMap.push_back(make_pair(f[0],field)); - } -#endif - BitField64& bf = *o; o->fieldIDs.clear(); o->fieldMap.clear(); @@ -148,7 +105,7 @@ size_t IDDescriptor::fieldID(const string& field_name) const { } /// Encoede a set of volume identifiers (corresponding to this description of course!) to a volumeID. -IDDescriptor::VolumeID IDDescriptor::encode(const std::vector<VolID>& ids) const { +VolumeID IDDescriptor::encode(const std::vector<VolID>& ids) const { typedef std::vector<VolID> VolIds; VolumeID id = 0; for(VolIds::const_iterator i=ids.begin(); i!=ids.end(); ++i) { @@ -158,3 +115,14 @@ IDDescriptor::VolumeID IDDescriptor::encode(const std::vector<VolID>& ids) cons return id; } +/// Decode volume IDs and return filled descriptor with all fields +void IDDescriptor::decodeFields(VolumeID vid, VolIDFields& fields) { + fields.clear(); + if ( isValid() ) { + const vector<BitFieldValue*>& v = data<Object>()->fields(); + for(vector<BitFieldValue*>::const_iterator i=v.begin(); i != v.end(); ++i) + fields.push_back(VolIDField(*i,(*i)->value(vid))); + return; + } + throw runtime_error("Attempt to access an invalid IDDescriptor object."); +} diff --git a/DDCore/src/Printout.cpp b/DDCore/src/Printout.cpp index 0037fa1885bc8bbb8923ccbe5e8397a08186d1a5..61ea2e42ebd8469dd86158f23a1cc1efe3dbc52e 100644 --- a/DDCore/src/Printout.cpp +++ b/DDCore/src/Printout.cpp @@ -11,6 +11,7 @@ #include "DD4hep/Objects.h" #include "DD4hep/Printout.h" #include <cstdarg> +#include <stdexcept> static size_t _the_printer(void*, DD4hep::PrintLevel, const char* src, const char* text) { size_t len = ::fputs(src,stdout); @@ -31,15 +32,11 @@ static DD4hep::output_function_t print_func = _the_printer; * @return Status code indicating success or failure */ int DD4hep::printout(PrintLevel severity, const char* src, const char* fmt, ...) { - if ( severity >= print_lvl ) { // receives: - va_list args; // - the log level - va_start( args, fmt); // - a standard C formatted - char str[4096]; // string (like printf) - size_t len = vsnprintf(str,sizeof(str)-2,fmt,args); - va_end (args); - str[len] = '\n'; - str[len+1] = '\0'; - print_func(print_arg,severity,src,str); + if ( severity >= print_lvl ) { + va_list args; + va_start(args,fmt); + printout(severity,src,fmt,args); + va_end(args); } return 1; } @@ -51,20 +48,15 @@ int DD4hep::printout(PrintLevel severity, const char* src, const char* fmt, ...) * @return Status code indicating success or failure */ int DD4hep::printout(PrintLevel severity, const std::string& src, const char* fmt, ...) { - if ( severity >= print_lvl ) { // receives: - va_list args; // - the log level - va_start( args, fmt); // - a standard C formatted - char str[4096]; // string (like printf) - size_t len = vsnprintf(str,sizeof(str)-2,fmt,args); - va_end (args); - str[len] = '\n'; - str[len+1] = '\0'; - print_func(print_arg,severity,src.c_str(),str); + if ( severity >= print_lvl ) { + va_list args; + va_start(args,fmt); + printout(severity,src.c_str(),fmt,args); + va_end(args); } return 1; } - /** Calls the display action * @arg severity [int,read-only] Display severity flag * @arg src [string,read-only] Information source (component, etc.) @@ -72,15 +64,11 @@ int DD4hep::printout(PrintLevel severity, const std::string& src, const char* fm * @return Status code indicating success or failure */ int DD4hep::printout(PrintLevel severity, const char* src, const std::string& fmt, ...) { - if ( severity >= print_lvl ) { // receives: - va_list args; // - the log level - va_start( args, fmt); // - a standard C formatted - char str[4096]; // string (like printf) - size_t len = vsnprintf(str,sizeof(str)-2,fmt.c_str(),args); - va_end (args); - str[len] = '\n'; - str[len+1] = '\0'; - print_func(print_arg,severity,src,str); + if ( severity >= print_lvl ) { + va_list args; + va_start(args,fmt); + printout(severity,src,fmt.c_str(),args); + va_end(args); } return 1; } @@ -92,19 +80,88 @@ int DD4hep::printout(PrintLevel severity, const char* src, const std::string& fm * @return Status code indicating success or failure */ int DD4hep::printout(PrintLevel severity, const std::string& src, const std::string& fmt, ...) { - if ( severity >= print_lvl ) { // receives: - va_list args; // - the log level - va_start( args, fmt); // - a standard C formatted - char str[4096]; // string (like printf) - size_t len = vsnprintf(str,sizeof(str)-2,fmt.c_str(),args); - va_end (args); + if ( severity >= print_lvl ) { + va_list args; + va_start(args,fmt); + printout(severity,src.c_str(),fmt.c_str(),args); + va_end(args); + } + return 1; +} + +/** Calls the display action + * @arg severity [int,read-only] Display severity flag + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @return Status code indicating success or failure + */ +int DD4hep::printout(PrintLevel severity, const char* src, const char* fmt, va_list& args) { + if ( severity >= print_lvl ) { + char str[4096]; + size_t len = vsnprintf(str,sizeof(str)-2,fmt,args); str[len] = '\n'; str[len+1] = '\0'; - print_func(print_arg,severity,src.c_str(),str); + print_func(print_arg,severity,src,str); } return 1; } +/** Calls the display action + * @arg severity [int,read-only] Display severity flag + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @return Status code indicating success or failure + */ +int DD4hep::printout(PrintLevel severity, const std::string& src, const char* fmt, va_list& args) { + return printout(severity,src.c_str(),fmt,args); +} + +/** Calls the display action + * @arg severity [int,read-only] Display severity flag + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @return Status code indicating success or failure + */ +int DD4hep::printout(PrintLevel severity, const char* src, const std::string& fmt, va_list& args) { + return printout(severity,src,fmt.c_str(),args); +} + +/** Calls the display action + * @arg severity [int,read-only] Display severity flag + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @return Status code indicating success or failure + */ +int DD4hep::printout(PrintLevel severity, const std::string& src, const std::string& fmt, va_list& args) { + return printout(severity,src.c_str(),fmt.c_str(),args); +} + +/** Build exception string and throw std::runtime_error + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @return Status code indicating success or failure + */ +std::string DD4hep::format(const std::string& src, const std::string& fmt, ...) { + va_list args; + va_start(args,fmt); + std::string str = format(src,fmt,args); + va_end(args); + return str; +} + +/** Build exception string and throw std::runtime_error + * @arg src [string,read-only] Information source (component, etc.) + * @arg fmt [string,read-only] Format string for ellipsis args + * @arg args [ap_list,read-only] List with variable number of arguments to fill format string. + * @return Status code indicating success or failure + */ +std::string DD4hep::format(const std::string& src, const std::string& fmt, va_list& args) { + char str[4096]; + size_t len = ::snprintf(str,sizeof(str),"%s: ",src.c_str()); + ::vsnprintf(str+len,sizeof(str)-len,fmt.c_str(),args); + return std::string(str); +} + /// Set new print level. Returns the old print level DD4hep::PrintLevel DD4hep::setPrintLevel(PrintLevel new_level) { PrintLevel old = print_lvl; diff --git a/DDCore/src/VolumeManager.cpp b/DDCore/src/VolumeManager.cpp index db361fa8e399b6c24d41d3a5ff1df0bf627e48e1..6a25bd1ae20a05808dd18fd5c9ccecfa32e41e86 100644 --- a/DDCore/src/VolumeManager.cpp +++ b/DDCore/src/VolumeManager.cpp @@ -27,7 +27,6 @@ using namespace DD4hep::Geometry; namespace { struct Populator { - typedef VolumeManager::VolumeID VolumeID; typedef vector<const TGeoNode*> Chain; /// Reference to the LCDD instance @@ -135,15 +134,6 @@ namespace { } return make_pair(volume_id,mask); } - - void add_entry(DetElement parent, DetElement e,const TGeoNode* n, - const PlacedVolume::VolIDs& ids, const Chain& nodes) - { - Volume vol = PlacedVolume(n).volume(); - SensitiveDetector sd = vol.sensitiveDetector(); - add_entry(sd, parent,e,n,ids,nodes); - } - void add_entry(SensitiveDetector sd, DetElement parent, DetElement e,const TGeoNode* n, const PlacedVolume::VolIDs& ids, const Chain& nodes) @@ -178,6 +168,16 @@ namespace { m_entries.insert(code.first); } } + +#if 0 + void add_entry(DetElement parent, DetElement e,const TGeoNode* n, + const PlacedVolume::VolIDs& ids, const Chain& nodes) + { + Volume vol = PlacedVolume(n).volume(); + SensitiveDetector sd = vol.sensitiveDetector(); + add_entry(sd, parent,e,n,ids,nodes); + } + void find_entry(DetElement parent, DetElement e,const TGeoNode* n, const PlacedVolume::VolIDs& ids, const Chain& nodes) { @@ -231,7 +231,7 @@ namespace { SensitiveDetector sd = vol.sensitiveDetector(); print_node(sd, parent, e, n, ids, nodes); } - +#endif void print_node(SensitiveDetector sd, DetElement parent, DetElement e, const TGeoNode* n, const PlacedVolume::VolIDs& ids, const Chain& nodes) { @@ -241,7 +241,7 @@ namespace { PlacedVolume pv = Ref_t(n); bool sensitive = pv.volume().isSensitive(); pair<VolumeID,VolumeID> code = encoding(en, ids); - IDDescriptor::VolumeID volume_id = code.first; + VolumeID volume_id = code.first; //if ( !sensitive ) return; ++s_count;