ATLAS Offline Software
Loading...
Searching...
No Matches
SelectedParticles.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <sstream>
8
9using namespace std;
10
11SelectedParticles::SelectedParticles(const vector<unsigned>& indices):m_bits(){
12 int nind=indices.size();
13 this->SetMaxBits(nind);
14 for(int i=0;i<nind;++i){
15 this->SetBit(indices[i],true);
16 }
17}
18
19void SelectedParticles::SetBit(unsigned i, bool good){
20 unsigned nwords=m_bits.size();
21 unsigned iword=i/32;
22
23 if(iword+1>nwords){
24 for(unsigned iw=nwords; iw<iword+1; ++iw){
25 unsigned temp=0;
26 m_bits.push_back(temp);
27 }
28 }
29
30 if(good==false) return;
31
32 unsigned bitnum = i % 32 ;
33 unsigned temp = 1 << bitnum;
34 m_bits[iword] |= temp;
35
36 return;
37}
38
39void SelectedParticles::SetMaxBits(unsigned maxbits)
40{
41 unsigned nwords=maxbits/32;
42 for(unsigned iw=0; iw<nwords+1; ++iw){
43 unsigned temp=0;
44 m_bits.push_back(temp);
45 }
46}
47
48
50{
51 stringstream os;
52 unsigned c;
53 // Invariant: displayed j 32-bit ints so far
54 unsigned nwords=m_bits.size();
55 vector<unsigned>::const_iterator itb=m_bits.begin();
56 int j=0;
57 for (; itb != m_bits.end(); ++itb){
58 // display mask will be a single 1 at the most significant bit
59 unsigned displayMask = 1u << 31;
60 // need to display the words in reverse order
61 int temp = nwords - j - 1;
62 ++j;
63 // Invariant: displayed c bits in the unsigned int
64 // displayMask has shifted its bits c places
65 for (c = 0; c != 32; c++ ) {
66 //if the current most significant bit in value temp is a 1
67 // display it if not display zero
68 os << (m_bits[temp] & displayMask ? '1' : '0');
69 // shift the displayMask one bit, so there is a new
70 // bit to check with value
71 displayMask >>= 1;
72
73 // group display by bytes
74 if (c % 8 == 7 )
75 os << ' ';
76 }
77 // a new line for each 32-bit line
78 os << std::endl;
79 }
80 os << std::endl;
81 return os.str();
82}
83
84bool SelectedParticles::isGood(unsigned ipart) const
85{
86
87 unsigned c;
88 unsigned nwords=m_bits.size();
89 vector<unsigned>::const_iterator itb=m_bits.begin();
90 unsigned j=ipart/32;
91 if(j>nwords-1) return false; // this should probably be an abort
92 for (; itb != m_bits.end(); ++itb){
93 // display mask will be a single 1 at the most significant bit
94 unsigned displayMask = 1;
95
96 // Invariant: displayed c bits in the unsigned int
97 // displayMask has shifted its bits c places
98 for (c = 0; c != 32; c++ ) {
99 //if the current most significant bit in value temp is a 1
100 // display it if not display zero
101 if (m_bits[j] & displayMask) {
102 if(c==ipart) return true;
103 }
104 // shift the displayMask one bit, so there is a new
105 // bit to check with value
106 displayMask <<= 1;
107 }
108 }
109 return false;
110}
111
113{
114 unsigned nwords=m_bits.size();
115 if(nwords==0) return 0;
116 vector<unsigned> goodparts;
117 goodParticles(goodparts);
118 return goodparts.size();
119}
120
121void SelectedParticles::goodParticles(std::vector<unsigned>& goodparts ) const
122{
123 unsigned nbits=m_bits.size()*32;
124 for(unsigned ibit=0; ibit<nbits; ++ibit) {
125 if(isGood(ibit)) goodparts.push_back(ibit);
126 }
127 return;
128}
129
void SetMaxBits(unsigned maxbits)
void SetBit(unsigned i, bool good=true)
std::vector< unsigned > m_bits
std::string displayBits() const
bool isGood(unsigned i) const
void goodParticles(std::vector< unsigned > &goodparts) const
unsigned numGood() const
STL namespace.