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