ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataTree/interface/BitMask.h
Revision: 1.12
Committed: Tue Dec 9 17:46:59 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006b, Mit_006a
Changes since 1.11: +11 -7 lines
Log Message:
Added ObjType to retrieve type of object.

File Contents

# User Rev Content
1 loizides 1.3 //--------------------------------------------------------------------------------------------------
2 loizides 1.12 // $Id: BitMask.h,v 1.11 2008/12/03 17:37:46 loizides Exp $
3 loizides 1.3 //
4     // BitMask
5     //
6 loizides 1.10 // Helper class implementing a Nx8 bit bitmask where N is a template parameter.
7 loizides 1.3 //
8     // Authors: J.Bendavid, C.Loizides
9     //--------------------------------------------------------------------------------------------------
10    
11     #ifndef MITANA_DATATREE_BITMASK_H
12     #define MITANA_DATATREE_BITMASK_H
13    
14 loizides 1.10 #include "MitAna/DataTree/interface/DataBase.h"
15 loizides 1.12 #include "MitAna/DataTree/interface/ObjTypes.h"
16 loizides 1.3 #include <assert.h>
17    
18     namespace mithep
19     {
20     template<UInt_t N>
21 loizides 1.10 class BitMask : public DataBase
22 loizides 1.3 {
23     public:
24 loizides 1.12 BitMask() { Clear(); }
25     BitMask(const char *bits) { SetBits(bits); }
26     BitMask(const BitMask<N> &copy) : DataBase(copy)
27     { SetBits(copy.Bits()); }
28     BitMask(Long64_t bits) { SetBits(bits); }
29 loizides 1.11 ~BitMask() {}
30 loizides 1.3
31 loizides 1.12 const char *Bits() const { return fBitMask; }
32 loizides 1.10 void Clear(Option_t */*opt*/="") { memset(fBitMask,0,N); }
33 loizides 1.12 void ClearBit(UInt_t n) { SetBit(n,0); }
34 loizides 1.3 UInt_t NBitsSet(UInt_t first=0, UInt_t last=N*8) const;
35 loizides 1.12 EObjType ObjType() const { return mithep::kBitMask; }
36 loizides 1.9 void Print(Option_t *opt="") const;
37 loizides 1.3 void SetBit(UInt_t n, Bool_t b=1);
38 loizides 1.10 void SetBits(const Char_t *bits) { memcpy(fBitMask,bits,N); }
39 loizides 1.3 void SetBits(Long64_t bits);
40 loizides 1.9 UInt_t Size() const { return N*8; }
41     Bool_t TestBit(UInt_t n) const;
42 loizides 1.12
43 loizides 1.5 BitMask &operator&=(const BitMask<N> &rhs)
44     { for (UInt_t n=0; n<N; ++n) fBitMask[n]&=rhs.fBitMask[n]; return *this; }
45     BitMask &operator|=(const BitMask<N> &rhs)
46     { for (UInt_t n=0; n<N; ++n) fBitMask[n]|=rhs.fBitMask[n]; return *this; }
47 bendavid 1.7 BitMask &operator^=(const BitMask<N> &rhs)
48     { for (UInt_t n=0; n<N; ++n) fBitMask[n]^=rhs.fBitMask[n]; return *this; }
49 loizides 1.5 Bool_t operator!=(const BitMask<N> &other) const;
50     Bool_t operator==(const BitMask<N> &other) const;
51 loizides 1.9 BitMask operator& (const BitMask &other) const
52 bendavid 1.6 { return BitMask<N>(*this) &= other; }
53 loizides 1.9 BitMask operator| (const BitMask &other) const
54 bendavid 1.6 { return BitMask<N>(*this) |= other; }
55 loizides 1.9 BitMask operator^ (const BitMask &other) const
56 bendavid 1.7 { return BitMask<N>(*this) ^= other; }
57 loizides 1.9 BitMask operator~ () const;
58 bendavid 1.6
59 loizides 1.3 protected:
60     Char_t fBitMask[N]; //the actual bitmask
61    
62 loizides 1.10 ClassDefT(BitMask, 2) // Generic templated bitmask
63 loizides 1.3 };
64     }
65    
66     //--------------------------------------------------------------------------------------------------
67     template<UInt_t N>
68     inline void mithep::BitMask<N>::SetBit(UInt_t n, Bool_t b)
69     {
70     // Set nth bit to given value.
71    
72     if(n>=N*8) return;
73    
74     UInt_t loc = n/8;
75     UChar_t bit = n%8;
76     if (b)
77     fBitMask[loc] |= (1<<bit);
78     else
79     fBitMask[loc] &= (0xFF ^ (1<<bit));
80     }
81    
82     //--------------------------------------------------------------------------------------------------
83     template<UInt_t N>
84     UInt_t mithep::BitMask<N>::NBitsSet(UInt_t first, UInt_t last) const
85     {
86     // Count number of bits which are set.
87    
88     UInt_t numBits = 0;
89     for (UInt_t i=first; i<=last; i++)
90     numBits += TestBit(i);
91    
92     return numBits;
93     }
94    
95     //--------------------------------------------------------------------------------------------------
96     template<UInt_t N>
97 loizides 1.10 void mithep::BitMask<N>::Print(Option_t */*opt*/) const
98 loizides 1.9 {
99     // Print bitmask.
100    
101     for (UInt_t i=0;i<N;++i)
102     printf("%X",i);
103     printf("\n");
104     }
105    
106     //--------------------------------------------------------------------------------------------------
107     template<UInt_t N>
108 loizides 1.3 inline void mithep::BitMask<N>::SetBits(Long64_t bits)
109     {
110     // Set bits given by bits.
111    
112 loizides 1.4 assert(sizeof(Long64_t)>=N);
113 loizides 1.3 SetBits(reinterpret_cast<const char*>(&bits));
114     }
115    
116     //--------------------------------------------------------------------------------------------------
117     template<UInt_t N>
118     inline Bool_t mithep::BitMask<N>::TestBit(UInt_t n) const
119     {
120     // Return true if nth bit is set.
121    
122     if(n>=N*8) return 0;
123    
124     UInt_t loc = n/8;
125     UChar_t bit = n%8;
126     Char_t val = fBitMask[loc];
127     Bool_t result = (val & (1<<bit)) != 0;
128     return result;
129     }
130 loizides 1.5
131     //--------------------------------------------------------------------------------------------------
132     template<UInt_t N>
133     Bool_t mithep::BitMask<N>::operator==(const mithep::BitMask<N> &other) const
134     {
135     // Equal operator.
136    
137     for (UInt_t n=0; n<N; ++n) {
138     if (fBitMask[n]!=other.fBitMask[n])
139     return kFALSE;
140     }
141     return kTRUE;
142     }
143    
144     //--------------------------------------------------------------------------------------------------
145     template<UInt_t N>
146     Bool_t mithep::BitMask<N>::operator!=(const mithep::BitMask<N> &other) const
147     {
148     // Unequal operator.
149    
150     for (UInt_t n=0; n<N; ++n) {
151 bendavid 1.6 if (fBitMask[n]!=other.fBitMask[n])
152     return kTRUE;
153     }
154     return kFALSE;
155     }
156    
157     //--------------------------------------------------------------------------------------------------
158     template<UInt_t N>
159 loizides 1.9 mithep::BitMask<N> mithep::BitMask<N>::operator~() const
160 bendavid 1.6 {
161     // bitwise inversion operator
162     BitMask<N> bits;
163     for (UInt_t n=0; n<N; ++n) {
164     bits.fBitMask[n] = ~fBitMask[n];
165 loizides 1.5 }
166 bendavid 1.6
167     return bits;
168     }
169 loizides 1.3 #endif