ATLAS Offline Software
Loading...
Searching...
No Matches
CSVWrapper Class Reference

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. More...

#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 15 of file CSVWrapper.cxx.

15 :
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}
std::vector< tableline > m_table
Definition CSVWrapper.h:29
bool m_valid
Definition CSVWrapper.h:27
size_t m_columnwidth
Definition CSVWrapper.h:30
std::vector< std::vector< tableline > > m_previousresults
Definition CSVWrapper.h:32
bool m_firstsearch
Definition CSVWrapper.h:28

Member Function Documentation

◆ ComparePreviousSearch()

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

Definition at line 129 of file CSVWrapper.cxx.

129 {
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}
std::vector< std::pair< int, std::string > > m_prevsearch
Definition CSVWrapper.h:31

◆ 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 48 of file CSVWrapper.cxx.

49 {
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}
std::pair< std::vector< unsigned int >, bool > res
std::vector< CSVWrapper::tableline > QueryTable(const std::vector< std::pair< int, std::string > > &argumentpairs)
std::vector< std::string > tableline
Definition CSVWrapper.h:10
int r
Definition globals.cxx:22

◆ 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 81 of file CSVWrapper.cxx.

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

◆ GetLine()

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

Definition at line 103 of file CSVWrapper.cxx.

103 {
104 if (element==-1)
105 return m_table.back();
106 return m_table.at(element);
107}

◆ 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 113 of file CSVWrapper.cxx.

113 {
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}
row
Appending html table to final .html summary file.

◆ QueryTable()

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

Definition at line 147 of file CSVWrapper.cxx.

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}
std::vector< bool > ComparePreviousSearch(const std::vector< std::pair< int, std::string > > &argumentpairs)
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
DataModel_detail::iterator< DVL > remove_if(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, Predicate pred)
Specialization of remove_if for DataVector/List.

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: