ATLAS Offline Software
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
CSVWrapper Class Reference

#include <CSVWrapper.h>

Collaboration diagram for CSVWrapper:

Public Types

using tableline = std::vector< std::string >
 

Public Member Functions

 CSVWrapper ()
 
 CSVWrapper (std::ifstream &inputfile)
 
std::shared_ptr< tablelineFindLine (const std::vector< std::pair< int, std::string >> &argumentpairs)
 
std::vector< std::shared_ptr< tableline > > FindLines (const std::vector< std::pair< int, std::string >> &argumentpairs)
 
tableline GetLine (const int element) const
 
void PrintTable (bool sample=false) const
 

Private Member Functions

std::vector< CSVWrapper::tablelineQueryTable (const std::vector< std::pair< int, std::string >> &argumentpairs)
 
std::vector< bool > ComparePreviousSearch (const std::vector< std::pair< int, std::string >> &argumentpairs)
 

Private Attributes

bool m_valid
 
bool m_firstsearch
 
std::vector< tablelinem_table
 
size_t m_columnwidth
 
std::vector< std::pair< int, std::string > > m_prevsearch
 
std::vector< std::vector< tableline > > m_previousresults
 

Detailed Description

Class used to read in/out/print csv tables of mappings written by generateMapping.py in efexControl to use in place of currently missing database solutions

Definition at line 8 of file CSVWrapper.h.

Member Typedef Documentation

◆ tableline

using CSVWrapper::tableline = std::vector<std::string>

Definition at line 10 of file CSVWrapper.h.

Constructor & Destructor Documentation

◆ CSVWrapper() [1/2]

CSVWrapper::CSVWrapper ( )

◆ CSVWrapper() [2/2]

CSVWrapper::CSVWrapper ( std::ifstream &  inputfile)

Definition at line 12 of file CSVWrapper.cxx.

12  :
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 }

Member Function Documentation

◆ ComparePreviousSearch()

std::vector< bool > CSVWrapper::ComparePreviousSearch ( const std::vector< std::pair< int, std::string >> &  argumentpairs)
private

Definition at line 126 of file CSVWrapper.cxx.

126  {
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 }

◆ FindLine()

std::shared_ptr< CSVWrapper::tableline > CSVWrapper::FindLine ( const std::vector< std::pair< int, std::string >> &  argumentpairs)
Parameters
argumentpairsa vector of search inputs where the 1st value is the column index and the second
Returns
a shared pointer to a row that uniquely matches critereon

Definition at line 45 of file CSVWrapper.cxx.

46  {
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 }

◆ FindLines()

std::vector< std::shared_ptr< CSVWrapper::tableline > > CSVWrapper::FindLines ( const std::vector< std::pair< int, std::string >> &  argumentpairs)
Parameters
argumentpairsa vector of search inputs where the 1st value is the column index and the second
Returns
a list of shared pointers to all rows that match the critereon

Definition at line 78 of file CSVWrapper.cxx.

79  {
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 }

◆ GetLine()

CSVWrapper::tableline CSVWrapper::GetLine ( const int  element) const

Definition at line 100 of file CSVWrapper.cxx.

100  {
101  if (element==-1)
102  return m_table.back();
103  return m_table.at(element);
104 }

◆ PrintTable()

void CSVWrapper::PrintTable ( bool  sample = false) const
Parameters
samplebool defaulted to false that when true prints only the last 100 lines

Definition at line 110 of file CSVWrapper.cxx.

110  {
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 }

◆ QueryTable()

std::vector< CSVWrapper::tableline > CSVWrapper::QueryTable ( const std::vector< std::pair< int, std::string >> &  argumentpairs)
private

Definition at line 144 of file CSVWrapper.cxx.

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 }

Member Data Documentation

◆ m_columnwidth

size_t CSVWrapper::m_columnwidth
private

Definition at line 30 of file CSVWrapper.h.

◆ m_firstsearch

bool CSVWrapper::m_firstsearch
private

Definition at line 28 of file CSVWrapper.h.

◆ m_previousresults

std::vector<std::vector<tableline> > CSVWrapper::m_previousresults
private

Definition at line 32 of file CSVWrapper.h.

◆ m_prevsearch

std::vector<std::pair<int,std::string> > CSVWrapper::m_prevsearch
private

Definition at line 31 of file CSVWrapper.h.

◆ m_table

std::vector<tableline> CSVWrapper::m_table
private

Definition at line 29 of file CSVWrapper.h.

◆ m_valid

bool CSVWrapper::m_valid
private

Definition at line 27 of file CSVWrapper.h.


The documentation for this class was generated from the following files:
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
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
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::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:14
CSVWrapper::m_valid
bool m_valid
Definition: CSVWrapper.h:27
python.ElectronD3PDObject.matched
matched
Definition: ElectronD3PDObject.py:138
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