IB-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
IBROC.h
Go to the documentation of this file.
1 /*////////////////////////////////////////////////////////////////////////////
2  Copyright 2018 Istituto Nazionale di Fisica Nucleare
3 
4  Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
5  the European Commission - subsequent versions of the EUPL (the "Licence").
6  You may not use this work except in compliance with the Licence.
7 
8  You may obtain a copy of the Licence at:
9 
10  https://joinup.ec.europa.eu/software/page/eupl
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
14  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  Licence for the specific language governing permissions and limitations under
16  the Licence.
18 
19 
20 
21 #ifndef IBROC_H
22 #define IBROC_H
23 
24 
25 #include <stdio.h>
26 #include <fstream>
27 #include <string>
28 #include <algorithm>
29 #include <vector>
30 
31 #include "Math/Dense.h"
32 
33 #define CSV_SEPARATOR ';'
34 
35 namespace uLib {
36 
40 // ROC
41 
42 
43 struct ROCElement : public Vector3f {
44  ROCElement (){}
45  ROCElement(float X, float Awo, float Owa) : Vector3f(X,Awo,Owa) {}
46  inline float &X() { return this->operator ()(0); }
47  inline float &Awo() { return this->operator ()(1); }
48  inline float &Owa() { return this->operator ()(2); }
49 };
50 
51 
52 class IBROC : public std::vector<ROCElement>{
53  typedef std::vector<ROCElement> BaseClass;
54  public:
55 
56  IBROC() : BaseClass() , m_Samples(0,0) {}
57  IBROC(unsigned int i) : BaseClass(i) , m_Samples(0,0) {}
58 
59  virtual void Update() {}
60 
61  virtual void read_csv(const char *file_name) {
62  std::ifstream file;
63  file.open(file_name);
64  this->read_csv(file);
65  file.close();
66  }
67 
68  virtual void read_csv(std::ifstream &file) {
69  std::string line;
70  std::string col;
71 
72  this->clear(); // clear current data! //
73  std::getline(file, line);
74  std::istringstream csvStream(line);
75 
76  // header
77  int j=0;
78  while( j<3 && std::getline(csvStream, col, CSV_SEPARATOR ) ) // labels
79  m_labels[j++] = col;
80  j=0;
81  while( j<2 && std::getline(csvStream, col, CSV_SEPARATOR ) ) // #Samples
82  m_Samples[j++] = atof(col.c_str());
83 
84  while ( std::getline(file, line) ) {
85  std::istringstream csvStream(line);
86  ROCElement elemt;
87  int i=0;
88  while( i<3 && std::getline(csvStream, col, CSV_SEPARATOR ) )
89  elemt(i++) = atof(col.c_str());
90  this->push_back(elemt);
91  }
92 
93  this->Update();
94  }
95 
96  virtual void write_csv(const char *file_name) {
97  std::ofstream file;
98  file.open(file_name);
99  this->write_csv(file);
100  file.close();
101  }
102 
103  virtual void write_csv (std::ofstream &stream) {
104  // header
105  stream << "X" << CSV_SEPARATOR << "Awo" << CSV_SEPARATOR << "Owa"
106  << CSV_SEPARATOR << Samples()(0)
107  << CSV_SEPARATOR << Samples()(1)
108  << "\n";
109  // items
110  for (IBROC::iterator itr = this->begin(); itr < this->end(); itr++)
111  stream << itr->X() << CSV_SEPARATOR
112  << itr->Awo() << CSV_SEPARATOR
113  << itr->Owa() << "\n";
114  }
115 
116  Vector2f GetRange(float y = 90 ) {
117  IBROC::iterator itr1,itr2;
118  itr1 = this->begin();
119  while (itr1 != this->end() && itr1->Awo() > y ) { itr1++; }
120  // itr2 = itr1-1;
121  // while(itr2->Awo() == (itr2-1)->Awo()) itr2--;
122  // Vector3f diff = static_cast<Vector3f>(*itr1 - *itr2);
123  // float m = diff(1)/diff(0);
124  // float begin = itr1->X() + (y-itr1->Awo())/m;
125  float begin = itr1->X();
126 
127  itr1 = this->end()-1;
128  while (itr1 != this->begin() && itr1->Owa() > y ) { itr1--; }
129  // itr2 = itr1+1;
130  // while(itr2->Owa() == (itr2+1)->Owa()) itr2++;
131  // diff = static_cast<Vector3f>(*itr1 - *itr2);
132  // m = diff(2)/diff(0);
133  // float end = itr1->X() + (y - itr1->Owa())/m;
134  float end = itr1->X();
135 
136  return Vector2f(begin,end);
137  }
138 
139 
140  inline void shift_roc(float shift ) {
141  for(IBROC::iterator itr = this->begin(); itr<this->end(); itr++)
142  itr->X() += shift;
143  this->Update();
144  }
145 
146  inline void scale_roc(float scale ) {
147  for(IBROC::iterator itr = this->begin(); itr<this->end(); itr++)
148  itr->X() *= scale;
149  this->Update();
150  }
151 
152  inline float match_midpt( float th, float match_pt = 50) {
153  // first point match_pt [%]
154  Vector2f range = this->GetRange(match_pt);
155  float scale_factor = th / range.sum() * 2;
156  scale_roc(scale_factor);
157  return scale_factor;
158  }
159 
160  inline Vector2i & Samples() { return this->m_Samples; }
161 
162 public:
163  std::string m_labels[3];
164  Vector2i m_Samples; // FPF and FNF trials size //
165 };
166 
167 
168 
169 inline std::ofstream&
170 operator << (std::ofstream& stream, IBROC &roc) {
171  roc.write_csv(stream);
172  return stream;
173 }
174 
175 
176 
177 
178 } // uLib
179 
180 #endif // IBROC_H