HardwareElements.h
Go to the documentation of this file.
1 // HarwareElements.h
2 //
3 // Jonathan Davies j.p.davies@sussex.ac.uk
4 // August 2016
5 //
6 // Description: Define some data structures (classes) that will be used to define pieces of Hardware in the detector
7 
8 #ifndef HARDWAREELEMENTS_H
9 #define HARDWAREELEMENTS_H
10 
11 #ifndef __GCCXML__
12 #include <memory>
13 #include <map>
14 #include <vector>
15 #include <set>
16 #include <iostream>
17 #include <iosfwd> // std::ostream
18 #endif //__GCCXML__
19 
20 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h"//jpd -- needed for raw::ChannelID_t
21 //......................................................
22 namespace Hardware{
23  typedef unsigned int ID;
24 
25  //jpd -- A Hardware::HardwareID is a class that has a type ("APA", "TPC", "Board"), an ID (i.e. this is "APA" #1)
26 
27  class HardwareID{
28  public:
29  HardwareID(): fID(0), fType("Unknown") {}
30  HardwareID(ID id, std::string this_type) : fID(id), fType(this_type) {}
31  ID const& getID() const{ return fID;}
32  std::string const& getType() const{ return fType; }
33 
34 #ifndef __GCCXML__
35  friend bool operator<( const HardwareID& lhs, const HardwareID& rhs ) {
36  if( lhs.getTypeOrder() == rhs.getTypeOrder())
37  return lhs.getID() < rhs.getID();
38  else return lhs.getTypeOrder() < rhs.getTypeOrder();
39  }
40 
41  friend std::ostream & operator << (std::ostream &os, HardwareID const &rhs){
42  os << rhs.getType() << ": " << rhs.getID();
43  return os;
44  }
45 
46  protected:
47  //jpd -- Helper for operator< (less than). The ordering is APA->manyTPC->manyBoard->manyASIC
48  int getTypeOrder() const{
49  if(fType == "APA") return 0;
50  if(fType == "TPC") return 1;
51  if(fType == "Board") return 2;
52  if(fType == "ASIC") return 3;
53  else return -1;
54  }
55 
56 
57 #endif //__GCCXML__
58 
59 
60  private:
61  ID fID;
63  };
64 
65  static const HardwareID UnknownID = HardwareID(0, "UnknownID");
66 
67  //jpd -- I don't think ROOT's dictionaries need to know about these derived classes for now
68 #ifndef __GCCXML__
69 //jpd -- A Hardware::Element is the same as a Hardware::HardwareID but also contains a list of channels
70 
71  class Element : public HardwareID
72  {
73  public:
74  Element(ID id, std::string this_type) : HardwareID(id, this_type) {}
75  std::vector<raw::ChannelID_t> const& getChannels() const{ return fChannelIDs;}
76  std::set<raw::ChannelID_t> const& getChannelsSet() const{ return fChannelIDsSet;}
77  size_t getNChannels() const{ return fChannelIDs.size();}
78  size_t getNChannelsSet() const{ return fChannelIDsSet.size();}
79 
80  std::vector<HardwareID> const& getHardwareIDs() const{ return fHardwareIDs;}
81  std::set<HardwareID> const& getHardwareIDsSet() const{ return fHardwareIDsSet;}
82  size_t getNHardwareIDs() const{ return fHardwareIDs.size();}
83  size_t getNHardwareIDsSet() const{ return fHardwareIDsSet.size();}
84 
86  //jpd -- Only add channel to the vector if it is not already in the set
87  //addChannelToSet returns true if channel was no already in the set and false otherwise
88  if(addChannelToSet(channel)) fChannelIDs.push_back(channel);
89  }
90 
92  //jpd -- Only add hardwareID to the vector if it is not already in the set
93  //addHardwareIDToSet returns true if hardwareID was no already in the set and false otherwise
94  if(addHardwareIDToSet(id)) fHardwareIDs.push_back(id);
95  }
96 
97  friend std::ostream & operator << (std::ostream &os, Element const &rhs){
98  HardwareID const& base = rhs;
99  os << base << " has "<< rhs.getNChannels() << " channels";
100 
101  std::set<raw::ChannelID_t> channels = rhs.getChannelsSet();
102  unsigned int max_num_channels = 16;
103  unsigned int this_channel_num = 0;
104  for(auto channel : channels){
105  if(this_channel_num==0) os << ":";
106  if(this_channel_num++ >= max_num_channels) {
107  os << " ...";
108  break;
109  }
110  os << " " << channel;
111  }
112 
113  os << "\n";
114  os << "Contains: " << rhs.getNHardwareIDs() << " pieces of hardware\n";
115  for(auto hardwareid : rhs.getHardwareIDsSet()){
116  os << hardwareid << "\n";
117  }
118 
119  return os;
120  }
121 
122  private:
123  std::vector<raw::ChannelID_t> fChannelIDs;
124  std::set<raw::ChannelID_t> fChannelIDsSet;
125  std::set<HardwareID> fHardwareIDsSet;
126  std::vector<HardwareID> fHardwareIDs;
127 
128 
129  //jpd -- Returns true if channel was not already in set and was inserted
130  // false if channel was already in set / there was a problem
131  bool addChannelToSet(raw::ChannelID_t this_channel){
132  auto result = fChannelIDsSet.insert(this_channel);
133  if(result.first != fChannelIDsSet.end()) return result.second;
134  else return false;
135  }
136 
137  //jpd -- Returns true if channel was not already in set and was inserted
138  // false if channel was already in set / there was a problem
139  bool addHardwareIDToSet(HardwareID this_hardwareid){
140  auto result = fHardwareIDsSet.insert(this_hardwareid);
141  if(result.first != fHardwareIDsSet.end()) return result.second;
142  else return false;
143  }
144  };
145 
146 
147  //jpd -- These derived classes are specialisations of a Hardware::Element
148  class ASIC : public Element{ public: ASIC (ID id) : Element(id, "ASIC" ) {} };
149  class Board : public Element{ public: Board (ID id) : Element(id, "Board" ) {} };
150  class TPC : public Element{ public: TPC (ID id) : Element(id, "TPC" ) {} };
151  class APA : public Element{ public: APA (ID id) : Element(id, "APA" ) {} };
152  class APAGroup : public Element{ public: APAGroup(ID id) : Element(id, "APAGroup" ) {} };
153  class Cryostat : public Element{ public: Cryostat(ID id) : Element(id, "Cryostat" ) {} };
154 
155  //jpd -- This just defines a bunch of shorthands for maps of Hardware::ID to fancy pointer to specialised Hardware::Element
156  //jpd -- Used std::shared_ptr so we don't have to do the memory management
157  using ASICMap = std::map<ID, std::shared_ptr<ASIC >>;
158  using BoardMap = std::map<ID, std::shared_ptr<Board>>;
159  using TPCMap = std::map<ID, std::shared_ptr<TPC >>;
160  using APAMap = std::map<ID, std::shared_ptr<APA >>;
161 
162 #endif //#ifndef __GCCXML__
163 }
164 
165 #endif //#ifndef HARDWAREELEMENTS_H
static QCString result
size_t getNHardwareIDs() const
std::string string
Definition: nybbler.cc:12
int getTypeOrder() const
unsigned int ID
bool addHardwareIDToSet(HardwareID this_hardwareid)
std::set< raw::ChannelID_t > fChannelIDsSet
std::vector< raw::ChannelID_t > fChannelIDs
friend std::ostream & operator<<(std::ostream &os, HardwareID const &rhs)
uint8_t channel
Definition: CRTFragment.hh:201
size_t getNChannels() const
static const HardwareID UnknownID
ID const & getID() const
HardwareID(ID id, std::string this_type)
void addChannel(raw::ChannelID_t channel)
size_t getNChannelsSet() const
Element(ID id, std::string this_type)
friend bool operator<(const HardwareID &lhs, const HardwareID &rhs)
std::map< ID, std::shared_ptr< TPC >> TPCMap
std::map< ID, std::shared_ptr< ASIC >> ASICMap
std::string const & getType() const
std::vector< raw::ChannelID_t > const & getChannels() const
std::vector< HardwareID > const & getHardwareIDs() const
std::set< HardwareID > const & getHardwareIDsSet() const
std::vector< HardwareID > fHardwareIDs
size_t getNHardwareIDsSet() const
std::map< ID, std::shared_ptr< APA >> APAMap
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
std::set< raw::ChannelID_t > const & getChannelsSet() const
std::map< ID, std::shared_ptr< Board >> BoardMap
std::set< HardwareID > fHardwareIDsSet
void addHardwareID(HardwareID id)
bool addChannelToSet(raw::ChannelID_t this_channel)