ATLAS Offline Software
Loading...
Searching...
No Matches
HitIdHelper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <cassert>
7#include <bit>
8#include <iostream>
9
10void HitIdHelper::InitializeField(const std::string& n, int vmn, int vmx)
11{
12 int field = vmx - vmn + 1;
13 int nb = std::bit_width(static_cast<unsigned>(field - 1));
14 IdField id={n,vmn,vmx,nb,m_currentStorage+1};
15 m_IDs.push_back(std::move(id));
17 assert (m_currentStorage<33);
18}
19
20void HitIdHelper::InitializeField(const std::string& n,int nb)
21{
22 int vmn=0;
23 int vmx = (1 << nb) - 1;
24 IdField id={n,vmn,vmx,nb,m_currentStorage+1};
25 m_IDs.push_back(std::move(id));
27 assert (m_currentStorage<33);
28}
29
30void HitIdHelper::SetFieldValue(const std::string& name, int n, HitID& targetID) const
31{
32 for (unsigned int i=0;i<m_IDs.size();i++)
33 {
34 if (m_IDs[i].fieldName==name)
35 {
36 if (n<m_IDs[i].vMin || n>m_IDs[i].vMax)
37 std::cout<<"HitIdHelper: field "<<name<<": supplied value "<<n<<
38 " is not within the allowed range ["<<m_IDs[i].vMin<<","<<m_IDs[i].vMax
39 <<"] : the result of this action is unpredictable "<<std::endl;
40 n-=m_IDs[i].vMin;
41 Store(targetID,n,m_IDs[i].firstBit,m_IDs[i].nBits);
42 break;
43 }
44 }
45}
46
47int HitIdHelper::GetFieldValue(const std::string& name, HitID targetID) const
48{
49 for (unsigned int i=0;i<m_IDs.size();i++)
50 {
51 if (m_IDs[i].fieldName==name)
52 {
53 int n=Retrieve(targetID,m_IDs[i].firstBit,m_IDs[i].nBits);
54 return n+m_IDs[i].vMin;
55 }
56 }
57 std::cout<<"HitIdHelper: field "<<name<<" not found: returning 0"<<
58 std::endl;
59 return 0;
60}
61
63{
64 for (unsigned int i=0;i<m_IDs.size();i++)
65 {
66 std::cout<<"-- Field "<<m_IDs[i].fieldName<<"\t["<<m_IDs[i].vMin
67 <<","<<m_IDs[i].vMax<<"]\tnBits "<<m_IDs[i].nBits
68 <<"\tfirst Bit "<<m_IDs[i].firstBit<<std::endl;
69 }
70}
71
73{
74 for (unsigned int i=0;i<8*sizeof(int);i++)
75 {
76 if ((id & (1u<<i))) std::cout<<"1";
77 else std::cout<<"0";
78 }
79 std::cout<<std::endl;
80}
81
82void HitIdHelper::SetBit(int& i, int bitpos)
83{
84 int l=1;
85 l<<=(bitpos-1);
86 i|=l;
87}
88void HitIdHelper::UnsetBit(int& i, int bitpos)
89{
90 int l=0;
91 SetBit(l,bitpos);
92 int k=~l;
93 i&=k;
94}
95
96void HitIdHelper::Store(int& id, int value, int firstbit, int field)
97{
98 int l=0;
99 for (int i=0;i<field;i++)
100 UnsetBit(id,i+firstbit);
101 l=value<<(firstbit-1);
102 id |= l;
103}
104
105int HitIdHelper::Retrieve(int id, int first, int field)
106{
107 int j=0;
108 for (int i=0;i<field;i++)
109 SetBit(j,(i+first));
110 int l=id&j;
111 return l>>(first-1);
112}
int HitID
int GetFieldValue(const std::string &name, HitID targetID) const
static void UnsetBit(int &i, int bitpos)
static void Print(int id)
static void Store(int &id, int value, int first, int field)
IdFields m_IDs
Definition HitIdHelper.h:34
static int Retrieve(int id, int first, int field)
int m_currentStorage
Definition HitIdHelper.h:35
void PrintFields()
static void SetBit(int &i, int bitpos)
void InitializeField(const std::string &n, int vmn, int vmx)
void SetFieldValue(const std::string &name, int n, HitID &targetID) const