StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DSMLayer.hh
1 //
2 // Pibero Djawotho <pibero@comp.tamu.edu>
3 // Texas A&M University Cyclotron Institute
4 // 1 Jan 2009
5 //
6 
7 #ifndef DSM_LAYER_HH
8 #define DSM_LAYER_HH
9 
10 #include <byteswap.h>
11 #include <vector>
12 #include <functional>
13 #include <algorithm>
14 
15 using namespace std;
16 
17 #include "DSM.hh"
18 
19 inline long long swapLL(long long x)
20 {
21  return ((x & 0xffff000000000000ll) >> 48 |
22  (x & 0x0000ffff00000000ll) >> 16 |
23  (x & 0x00000000ffff0000ll) << 16 |
24  (x & 0x000000000000ffffll) << 48);
25 }
26 
27 inline void copy_and_swap16(void* dest, const void* src)
28 {
29  long long* x = (long long*)src;
30  long long* y = (long long*)dest;
31 
32  //*y++ = bswap_64(*x++);
33  //*y++ = bswap_64(*x++);
34 
35  *y = bswap_64(*x); ++x; ++y;
36  *y = bswap_64(*x); ++x; ++y;
37 }
38 
39 inline void copy_and_swap8(void* dest, const void* src)
40 {
41  long long* x = (long long*)src;
42  long long* y = (long long*)dest;
43 
44  *y++ = swapLL(*x++);
45  *y++ = swapLL(*x++);
46 }
47 
48 template<class T> struct DSMLayer : public vector<DSM> {
49  DSMLayer(int n) : vector<DSM>(n) {}
50  virtual ~DSMLayer() {}
51  virtual void setRegister(int i, int value);
52  virtual int getRegister(int i) const;
53  virtual bool read(const T& event) = 0;
54  virtual void write(DSMLayer& layer) = 0;
55  virtual void run() = 0;
56  virtual void save(int nchannels, short* buffer);
57  virtual void dump() { for_each(begin(), end(), mem_fun_ref(&DSM::dump)); }
58 };
59 
60 template<class T> inline void DSMLayer<T>::setRegister(int i, int value)
61 {
62  for (vector<DSM>::iterator dsm = begin(); dsm != end(); ++dsm)
63  dsm->registers[i] = value;
64 }
65 
66 template<class T> inline int DSMLayer<T>::getRegister(int i) const
67 {
68  return front().registers[i];
69 }
70 
71 template<class T> inline void DSMLayer<T>::save(int nchannels, short* buffer)
72 {
73  for (size_t dsm = 0; dsm < size(); ++dsm) {
74  short* channels = (*this)[dsm].channels;
75  copy(&channels[0], &channels[nchannels], &buffer[dsm*nchannels]);
76  }
77 }
78 
79 #endif // DSM_LAYER_HH