ATLAS Offline Software
TestVectorTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
11 #include "TestVectorTool.h"
12 #include <fstream>
13 
15 {
16  ATH_MSG_INFO("Initializing TestVectorTool tool");
17 
18  return StatusCode::SUCCESS;
19 }
20 
21 StatusCode TestVectorTool::prepareTV(const std::string inputFile, std::vector<uint64_t> &testVector) const
22 {
23  ATH_MSG_DEBUG("Preparing input test vector from " << inputFile);
24 
25  // Check if the input file ends with .txt or .bin
26  if (inputFile.find(".txt") == std::string::npos && inputFile.find(".bin") == std::string::npos)
27  {
28  ATH_MSG_ERROR("Input TV file must be either .txt or .bin");
29  return StatusCode::FAILURE;
30  }
31 
32  // clear the test vector before reading
33  testVector.clear();
34 
35  // if .txt
36  if (inputFile.find(".txt") != std::string::npos)
37  {
38  std::ifstream file(inputFile, std::ios::in);
39  if (!file.is_open())
40  {
41  ATH_MSG_ERROR("Cannot open file " << inputFile);
42  return StatusCode::FAILURE;
43  }
44 
45  uint64_t cache;
46  while (file >> std::hex >> cache)
47  {
48  testVector.push_back(cache);
49  }
50 
51  // close the file
52  file.close();
53  }
54  else
55  {
56  std::ifstream file(inputFile, std::ios::binary);
57  if (!file.is_open())
58  {
59  ATH_MSG_ERROR("Cannot open input TV file " << inputFile);
60  return StatusCode::FAILURE;
61  }
62 
63  uint64_t cache;
64  while (file.read(reinterpret_cast<char *>(&cache), sizeof(uint64_t)))
65  {
66  // Reverse the byte order
67  cache = __builtin_bswap64(cache);
68  testVector.push_back(cache);
69  }
70 
71  // close the file
72  file.close();
73  }
74 
75  return StatusCode::SUCCESS;
76 }
77 
78 StatusCode TestVectorTool::compare(const std::vector<uint64_t> &tv_1, const std::vector<uint64_t> &tv_2) const
79 {
80  ATH_MSG_DEBUG("Comparing two test vectors");
81 
82  std::vector<uint64_t>::size_type size = -1;
83 
84  if (tv_1.size() != tv_2.size())
85  {
86  ATH_MSG_WARNING("The two test vectors have different sizes: " << tv_1.size() << " and " << tv_2.size());
87  // Use the shorter one for comparison
88  size = tv_1.size() < tv_2.size() ? tv_1.size() : tv_2.size();
89  }
90  else
91  {
92  ATH_MSG_DEBUG("The two test vectors have the same size: " << tv_1.size());
93  size = tv_1.size();
94  }
95 
96  bool pass = true;
97  for (std::vector<uint64_t>::size_type i = 0; i < size; i++)
98  {
99  if (tv_1[i] != tv_2[i])
100  {
101  ATH_MSG_DEBUG("The two test vectors are different at index " << i);
102  pass = false;
103  }
104  }
105 
106  if (pass)
107  {
108  ATH_MSG_INFO("The two test vectors are the same");
109  }
110  else
111  {
112  ATH_MSG_WARNING("The two test vectors are different!");
113  }
114 
115  return StatusCode::SUCCESS;
116 }
117 
118 StatusCode TestVectorTool::compare(const EFTrackingFPGAIntegration::TVHolder &tvHolder, const std::vector<uint64_t> &tv_comp) const
119 {
120  ATH_MSG_INFO("Comparing the FPGA output to the reference vector for " << tvHolder.name);
121 
122  std::vector<uint64_t>::size_type size = -1;
123 
124  if (tvHolder.refTV.size() != tv_comp.size())
125  {
126  ATH_MSG_WARNING("The two test vectors have different sizes: " << tvHolder.refTV.size() << " and " << tv_comp.size());
127  // Use the shorter one for comparison
128  size = tvHolder.refTV.size() < tv_comp.size() ? tvHolder.refTV.size() : tv_comp.size();
129  }
130  else
131  {
132  ATH_MSG_DEBUG("The two test vectors have the same size: " << tvHolder.refTV.size());
133  size = tvHolder.refTV.size();
134  }
135 
136  bool pass = true;
137  for (std::vector<uint64_t>::size_type i = 0; i < size; i++)
138  {
139  if (tvHolder.refTV[i] != tv_comp[i])
140  {
141  ATH_MSG_DEBUG("The two test vectors are different at index " << i);
142  pass = false;
143  }
144  }
145 
146  if (pass)
147  {
148  ATH_MSG_INFO(tvHolder.name << " FPGA output matches the reference vector");
149  }
150  else
151  {
152  ATH_MSG_WARNING(tvHolder.name << " FPGA output does not match the reference vector");
153  }
154 
155  return StatusCode::SUCCESS;
156 }
EFTrackingFPGAIntegration::TVHolder::refTV
std::vector< uint64_t > refTV
Definition: TestVectorTool.h:32
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TestVectorTool::prepareTV
StatusCode prepareTV(const std::string inputFile, std::vector< uint64_t > &testVector) const
Prepare test vector in the form of std::vector<uint64_t>, can be either .txt or .bin.
Definition: TestVectorTool.cxx:21
TestVectorTool::initialize
StatusCode initialize() override
Definition: TestVectorTool.cxx:14
EFTrackingFPGAIntegration::TVHolder::name
std::string name
Definition: TestVectorTool.h:30
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
TestVectorTool::compare
StatusCode compare(const std::vector< uint64_t > &tv_1, const std::vector< uint64_t > &tv_2) const
Compare two TV in the form of std::vector<uint64_t>
Definition: TestVectorTool.cxx:78
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
file
TFile * file
Definition: tile_monitor.h:29
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
EFTrackingFPGAIntegration::TVHolder
Definition: TestVectorTool.h:27
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TestVectorTool.h
Tool for test vector.