ATLAS Offline Software
CSVWrapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
5 #include <iostream>
6 #include <sstream>
7 #include <algorithm>
8 #include <iterator>
16  m_valid(false) , m_firstsearch(true) , m_table(), m_columnwidth(0)
17 {
18  std::istream_iterator<std::string> file_start{inputfile};
19  std::istream_iterator<std::string> file_end;
20 
21  std::vector<std::string> fileContent;
22  std::copy(file_start,file_end,back_inserter(fileContent)); // copy file content into vector
23 
24  fileContent.erase(begin(fileContent)); // first line is comment
25  // WARNING if the line contains spaces, it is
26  // treted as several lines!!!
27 
28  for (auto& line: fileContent){ // iterate over lines, unpack
29  std::vector<std::string> table_entry;
30 
31  std::string cell;
32  std::stringstream lineStream(line);
33 
34  while(std::getline(lineStream,cell, ','))
35  table_entry.push_back(cell);
36  m_table.push_back(std::move(table_entry));
37  }
38  m_columnwidth = m_table.at(10).size();
39  //Intialise prev results vector
40  m_previousresults.push_back(m_table);
41  m_valid = true;
42 }
48 std::shared_ptr<CSVWrapper::tableline> CSVWrapper::FindLine(
49  const std::vector<std::pair<int,std::string>>& argumentpairs){
50  std::vector<CSVWrapper::tableline> result = QueryTable(argumentpairs);
51  //Check for unique result, if not throw exception
52  if ( result.empty())
53  {
54  CSVWrapper::tableline returnval;
55  std::shared_ptr<CSVWrapper::tableline> return_ptr (
56  new CSVWrapper::tableline (std::move(returnval)));
57  return return_ptr;
58  }
59  if(result.size() != 1) {
60  std::cout << "Duplicate found!" << std::endl;
61  for (const auto & res: result) {
62  for (const auto & r: res) {
63  std::cout << r << ", ";
64  }
65  std::cout << std::endl;
66  }
67  throw "Non unique result to FindLine";
68  }
69 
70  CSVWrapper::tableline returnval = result.at(0);
71  std::shared_ptr<CSVWrapper::tableline> return_ptr (
72  new CSVWrapper::tableline (std::move(returnval)));
73  return return_ptr;
74 }
75 
81 std::vector<std::shared_ptr<CSVWrapper::tableline>> CSVWrapper::FindLines(
82  const std::vector<std::pair<int,std::string>>& argumentpairs){
83  std::vector<CSVWrapper::tableline> result = QueryTable(argumentpairs);
84 
85  std::vector<std::shared_ptr<CSVWrapper::tableline>> return_vec;
86  if ( result.empty())
87  {
88  CSVWrapper::tableline returnval;
89  std::shared_ptr<CSVWrapper::tableline> return_ptr (
90  new CSVWrapper::tableline (std::move(returnval)));
91  return_vec.push_back(std::move(return_ptr));
92  }
93  else {
94  for (CSVWrapper::tableline returnval: result) {
95  std::shared_ptr<CSVWrapper::tableline> return_ptr (
96  new CSVWrapper::tableline (std::move(returnval)));
97  return_vec.push_back(std::move(return_ptr));
98  }
99  }
100  return return_vec;
101 }
102 
104  if (element==-1)
105  return m_table.back();
106  return m_table.at(element);
107 }
108 
114  if(m_valid){
115  size_t rowidx(0) ;
116  for(const auto& row : m_table){
117  if(!sample || (m_table.size()-rowidx)<100){
118  for(size_t idx(0);idx<row.size();idx++ ){
119  std::cout << row.at(idx) << " , ";
120  }
121  std::cout << std::endl;
122  }
123  rowidx++;
124  }
125  }
126 }
127 //Check what values compared to the last FindLine search remain the same and are in the same order, halt if no match
128 //i.e match,match,not_match,match will return match,match,not_match,notmatch
129 std::vector<bool> CSVWrapper::ComparePreviousSearch(const std::vector<std::pair<int,std::string>>& argumentpairs){
130  std::vector<bool> matchvector(argumentpairs.size(), false);
131  if(m_firstsearch){
132  m_firstsearch = false;
133  }
134  else{
135  int idx = 0;
136  for(const auto& newsearch : argumentpairs){
137  matchvector.at(idx) = ((newsearch.first == m_prevsearch.at(idx).first) &&
138  (newsearch.second == m_prevsearch.at(idx).second));
139  if(!matchvector.at(idx)) break; //Escape if match fails
140  idx++;
141  }
142  }
143  m_prevsearch = argumentpairs;
144  return matchvector;
145 }
146 
147 std::vector<CSVWrapper::tableline> CSVWrapper::QueryTable(const std::vector<std::pair<int,std::string>>&
148  argumentpairs)
149 {
150  //Loop over every column argument in the find statement
151  size_t searchcolumnidx = 0;
152  auto matches = this->ComparePreviousSearch(argumentpairs);
153  //std::vector<CSVWrapper::tableline> result = m_table;
154  std::vector<CSVWrapper::tableline> result;
155  for(bool matched : matches ){
156  if(!matched){ //If not the same search as before (in this column)
157  //Complete one column search routine
158  result = m_previousresults.at(searchcolumnidx);
159  auto search = argumentpairs.at(searchcolumnidx);
160  auto findmismatch = [&] (CSVWrapper::tableline row){
161  return (row.at(search.first) != search.second);
162  };
163  result.erase(std::remove_if(result.begin(), result.end(), findmismatch)
164  , result.end() );
165  if(m_previousresults.size() <= searchcolumnidx + 1){//If unintialised
166  m_previousresults.push_back(result);
167  }
168  else{
169  m_previousresults.at(searchcolumnidx+1) = result;
170  }
171  }
172  searchcolumnidx++;
173  }
174  //Cover Case that exactly the same search as before
175  if(matches.back() == true){result = m_previousresults.at(searchcolumnidx);}
176  return result;
177 }
beamspotman.r
def r
Definition: beamspotman.py:672
CSVWrapper::m_columnwidth
size_t m_columnwidth
Definition: CSVWrapper.h:30
get_generator_info.result
result
Definition: get_generator_info.py:21
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
CSVWrapper::tableline
std::vector< std::string > tableline
Definition: CSVWrapper.h:10
PrintTrkAnaSummary.row
row
Appending html table to final .html summary file.
Definition: PrintTrkAnaSummary.py:356
CSVWrapper::QueryTable
std::vector< CSVWrapper::tableline > QueryTable(const std::vector< std::pair< int, std::string >> &argumentpairs)
Definition: CSVWrapper.cxx:147
CSVWrapper::PrintTable
void PrintTable(bool sample=false) const
Definition: CSVWrapper.cxx:113
CSVWrapper::FindLine
std::shared_ptr< tableline > FindLine(const std::vector< std::pair< int, std::string >> &argumentpairs)
Definition: CSVWrapper.cxx:48
ReadBchFromCrest.begin
begin
Definition: ReadBchFromCrest.py:80
search
void search(TDirectory *td, const std::string &s, std::string cwd, node *n)
recursive directory search for TH1 and TH2 and TProfiles
Definition: hcg.cxx:739
CSVWrapper::m_table
std::vector< tableline > m_table
Definition: CSVWrapper.h:29
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
CSVWrapper::m_previousresults
std::vector< std::vector< tableline > > m_previousresults
Definition: CSVWrapper.h:32
CSVWrapper.h
CSVWrapper::m_firstsearch
bool m_firstsearch
Definition: CSVWrapper.h:28
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:116
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:11
CSVWrapper::m_valid
bool m_valid
Definition: CSVWrapper.h:27
python.ElectronD3PDObject.matched
matched
Definition: ElectronD3PDObject.py:138
CSVWrapper::GetLine
tableline GetLine(const int element) const
Definition: CSVWrapper.cxx:103
CSVWrapper::FindLines
std::vector< std::shared_ptr< tableline > > FindLines(const std::vector< std::pair< int, std::string >> &argumentpairs)
Definition: CSVWrapper.cxx:81
CSVWrapper::m_prevsearch
std::vector< std::pair< int, std::string > > m_prevsearch
Definition: CSVWrapper.h:31
FullCPAlgorithmsTest_CA.inputfile
dictionary inputfile
Definition: FullCPAlgorithmsTest_CA.py:62
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
calibdata.copy
bool copy
Definition: calibdata.py:26
CSVWrapper::ComparePreviousSearch
std::vector< bool > ComparePreviousSearch(const std::vector< std::pair< int, std::string >> &argumentpairs)
Definition: CSVWrapper.cxx:129
CSVWrapper::CSVWrapper
CSVWrapper()