qse  0.5.4
qsenormalizer.cpp
Go to the documentation of this file.
1 #include "qsenormalizer.h"
2 #include "qsedataset.h"
3 #include "qsescan.h"
4 
5 #include <stdio.h>
6 
8 (QseDataSet* inData, QseDataSet* outData, const QString& name, QObject* parent)
9  : QseEXAFSOperator(inData, outData, name, parent),
10  m_ScanPattern(this, "scanpattern",NULL),
11  m_MinScanRowCount(this, "minscanrowcount", NULL),
12  m_MaxScanRowCount(this, "maxscanrowcount", NULL),
13  m_MinScanColumnCount(this, "minscancolumncount", NULL),
14  m_MaxScanColumnCount(this, "maxscancolumncount", NULL),
15  m_InputEnergyUnits(this, QStringList()<<"eV"<<"keV",
16  "inputenergyunits", NULL),
17  m_OutputEnergyUnits(this, QStringList()<<"eV"<<"keV",
18  "outputenergyunits", NULL)
19 {
20  m_InputEnergyUnits.setValue(1);
21  m_OutputEnergyUnits.setValue(1);
22 }
23 
25 {
26 // printf("QseNormalizer::exec()\n");
27  QString fnam = m_InputData -> fileName();
28 
29  m_OutputData -> clear();
30  m_OutputData -> setFileName(fnam+"_norm");
31  m_OutputData -> setHeader(m_InputData -> header());
32 
33  int ns = m_InputData -> scanCount();
34 
35 // printf("%d scans\n", ns);
36 
37  for (int s = 0; s < ns; s++) {
38  if (matchesScan(s)) {
39 // printf("scan %d matches\n", s);
40 
41  QseScan *osc = m_OutputData -> appendScan();
42 
43  normalizeScan(s, osc);
44  }
45  }
46 
47 // printf("%d output scans\n", m_OutputData -> scanCount());
48 
49  m_OutputData -> emitOperationCompleted();
50 }
51 
53 {
54  QseScan* s = m_InputData->scan(n);
55 
56  if (s) {
57  bool ok1, ok2;
58 
59  double minVal = m_MinScanRowCount.value().toDouble(&ok1);
60  double maxVal = m_MaxScanRowCount.value().toDouble(&ok2);
61 
62  if (ok1 || ok2) {
63  int nrows = s->maxRowCount();
64 
65  if (ok1 && (nrows < minVal)) {
66  return false;
67  }
68 
69  if (ok2 && (nrows > maxVal)) {
70  return false;
71  }
72  }
73 
74  minVal = m_MinScanColumnCount.value().toDouble(&ok1);
75  maxVal = m_MaxScanColumnCount.value().toDouble(&ok2);
76 
77  if (ok1 || ok2) {
78  int ncols = s->columnCount();
79 
80  if (ok1 && (ncols < minVal)) {
81  return false;
82  }
83 
84  if (ok2 && (ncols > maxVal)) {
85  return false;
86  }
87  }
88 
89  return m_ScanPattern.value().toRegExp().exactMatch(s->scanCommand());
90  } else {
91  return false;
92  }
93 }
94 
96 {
97  QseScan* s = m_InputData->scan(n);
98 
99  if (s) {
101 
102  if (m_ScanExceptions.contains(s)) {
103  r = m_ScanExceptions[s];
104  }
105 
106  bool ok1, ok2;
107 
108  double minVal = m_MinScanRowCount.value().toDouble(&ok1);
109  double maxVal = m_MaxScanRowCount.value().toDouble(&ok2);
110 
111  if (ok1 || ok2) {
112  int nrows = s->maxRowCount();
113 
114  if (ok1 && (nrows < minVal)) {
115  return false;
116  }
117 
118  if (ok2 && (nrows > maxVal)) {
119  return false;
120  }
121  }
122 
123  minVal = m_MinScanColumnCount.value().toDouble(&ok1);
124  maxVal = m_MaxScanColumnCount.value().toDouble(&ok2);
125 
126  if (ok1 || ok2) {
127  int ncols = s->columnCount();
128 
129  if (ok1 && (ncols < minVal)) {
130  return false;
131  }
132 
133  if (ok2 && (ncols > maxVal)) {
134  return false;
135  }
136  }
137 
138  if (r == UnspecifiedScanRole) {
139  return m_ScanPattern.value().toRegExp().exactMatch(s->scanCommand());
140  } else {
141  return r == ScanUsedRole;
142  }
143  } else {
144  return false;
145  }
146 }
147 
149 {
150  QseScan *sc = m_InputData->scan(n);
151 
152  if (sc) {
153  int nc = sc -> columnCount();
154  int energycol = -1;
155 
156  for (int c = 0; c < nc; c++) {
157  QseColumn *col = sc -> column(c);
158 
159  if (matchesEnergyColumn(col,c)) {
160  energycol = c;
161  }
162  }
163 
164  if (energycol < 0) {
165  emit message(QString("No energy column found for scan %1").arg(n));
166  } else {
167  osc -> mergeHeaders(sc->header());
168  osc -> setScanNumber(sc->scanNumber());
169  osc -> setName(sc->name()+"_norm");
170  osc -> setScanCommand(sc->scanCommand()+"_norm");
171  osc -> setScanDateTime(QDateTime::currentDateTime());
172 
173  QseColumn *energy = osc -> appendColumn("Energy", sc -> column(energycol));
174  QseColumn *normal = osc -> appendColumn("Norm");
175  QseColumn *detect = osc -> appendColumn("Detector");
176  QseColumn *seconds = osc -> appendColumn("Seconds");
177 
178  energy -> multiplication(energyScalingRatio());
179 
180  int nnorm = 0, ndet = 0, nsecs = 0;
181 
182  for (int c = 0; c < nc; c++) {
183  QseColumn *col = sc -> column(c);
184 
185  if (matchesNormalizationColumn(col,c)) {
186  nnorm += 1;
187  normal -> addition(col);
188  } else if (matchesDetectorColumn(col,c)) {
189  ndet += 1;
190  detect -> addition(col);
191  } else if (matchesSecondsColumn(col,c)) {
192  nsecs += 1;
193  seconds -> addition(col);
194  }
195  }
196 
197  if (nnorm > 0) {
198  QseColumn *ratio = osc -> appendColumn("Ratio", detect);
199 
200  ratio -> division(normal);
201 
202  emit message(QString("Scan %1 had %2 detector columns, was normalized by %3 ion chambers").arg(n).arg(ndet).arg(nnorm));
203  } else if (nsecs > 0) {
204  QseColumn *ratio = osc -> appendColumn("Ratio", detect);
205 
206  ratio -> division(seconds);
207  emit message(QString("Scan %1 had %2 detector columns, was normalized by counting time").arg(n).arg(ndet));
208  } else {
209  QseColumn *ratio = osc -> appendColumn("Ratio", detect);
210 
211  emit message(QString("Scan %1 had %2 detector columns, no normalization performed").arg(n).arg(ndet));
212  }
213 
214  for (int c=0; c<nc; c++) {
215  QseColumn *col = sc->column(c);
216 
217  if (matchesCopyNormalizedColumn(col, c)) {
218  QseColumn *copy = osc->appendColumn(col->name(), col);
219 
220  copy->division(normal);
221  } else if (matchesCopyRateColumn(col, c)) {
222  QseColumn *copy = osc->appendColumn(col->name(), col);
223 
224  copy->division(seconds);
225  } else if (matchesCopyTotalCountsColumn(col, c)) {
226  QseColumn *copy = osc->appendColumn(col->name(), col);
227 
228  copy->multiplication(seconds);
229  } else if (matchesCopyColumn(col, c)) {
230  QseColumn *copy = osc->appendColumn(col->name(), col);
231  }
232  }
233  }
234  }
235 }
236 
238 {
239  int inp = m_InputEnergyUnits.value().toInt();
240  int otp = m_OutputEnergyUnits.value().toInt();
241 
242  if (inp == otp) {
243  return 1.0;
244  } else if (inp == 0 && otp == 1) {
245  return 0.001;
246  } else if (inp == 1 && otp == 0) {
247  return 1000.0;
248  } else {
249  return 1;
250  }
251 }
252 
254 {
255  QseScan* scan = m_InputData -> scan(s);
256 
257  if (scan) {
258  if (role == UnspecifiedScanRole) {
259  m_ScanExceptions.remove(scan);
260  } else {
261  m_ScanExceptions.insert(scan,role);
262  }
263  }
264 }
265 
267 {
268  QseScan* scan = m_InputData -> scan(s);
269 
270  if (scan) {
271  QseColumn* col = scan -> column(c);
272 
273  if (col) {
274  if (role == UnspecifiedRole) {
275  m_Exceptions.remove(col);
276  } else {
277  m_Exceptions.insert(col,role);
278  }
279  }
280  }
281 }
282 
284 {
285  QseScan* scan = m_InputData -> scan(n);
286 
287  return m_ScanExceptions.contains(scan);
288 }
289 
291 {
292  QseScan* scan = m_InputData -> scan(s);
293 
294  if (scan) {
295  QseColumn* col = scan -> column(c);
296 
297  return m_Exceptions.contains(col);
298  } else {
299  return false;
300  }
301 }
302 
304 {
305  QseScan* scan = m_InputData -> scan(s);
306 
307  if (scan) {
308  QseColumn* col = scan -> column(c);
309 
310  if (m_Exceptions.contains(col)) {
311  return m_Exceptions[col];
312  }
313  }
314 
315  return UnspecifiedRole;
316 }
317 
319 {
320  QseScan* scan = m_InputData -> scan(s);
321 
322  if (scan) {
323  QseColumn* col = scan -> column(c);
324 
325  if (matchesEnergyPattern(col, c)) {
326  return EnergyRole;
327  } else if (matchesDetectorPattern(col, c)) {
328  return DetectorRole;
329  } else if (matchesNormalizationPattern(col, c)) {
330  return NormalizationRole;
331  } else if (matchesSecondsPattern(col, c)) {
332  return SecondsRole;
333  } else if (matchesCopyNormalizedPattern(col, c)) {
334  return CopyNormalizedRole;
335  } else if (matchesCopyRatePattern(col, c)) {
336  return CopyRateRole;
337  } else if (matchesCopyTotalCountsPattern(col, c)) {
338  return CopyTotalCountsRole;
339  } else if (matchesCopyPattern(col, c)) {
340  return CopyRole;
341  }
342  }
343 
344  return UnspecifiedRole;
345 }
346 
348 {
350 
351  if (role != UnspecifiedRole) {
352  return role;
353  } else {
354  return patternColumnRole(s, c);
355  }
356 }
QseScan * scan(int n) const
Definition: qsedataset.cpp:70
QVariant value() const
QStringList header() const
Definition: qsescan.cpp:167
QseRegExpParameter m_ScanPattern
Definition: qsenormalizer.h:47
bool matchesScanPattern(int n)
QseStringParameter m_MinScanColumnCount
Definition: qsenormalizer.h:51
bool matchesCopyTotalCountsColumn(const QseColumn *col, int colnum)
QseColumn * appendColumn(const QString &name, const QseColumn *proto=NULL)
Definition: qsescan.cpp:240
double energyScalingRatio()
void message(const QString &msg)
bool matchesCopyRateColumn(const QseColumn *col, int colnum)
bool matchesScan(int n)
void multiplication(const QseColumn *col)
Definition: qsecolumn.cpp:114
bool matchesDetectorPattern(const QseColumn *col, int colnum)
bool matchesEnergyColumn(const QseColumn *col, int colnum)
void division(const QseColumn *col)
Definition: qsecolumn.cpp:128
int scanNumber() const
Definition: qsescan.cpp:353
bool matchesCopyColumn(const QseColumn *col, int colnum)
void setName(const QString &name)
Definition: qseoperator.cpp:16
int columnCount() const
Definition: qsescan.cpp:45
QseNormalizer(QseDataSet *inData, QseDataSet *outData, const QString &name, QObject *parent=0)
bool matchesDetectorColumn(const QseColumn *col, int colnum)
QString scanCommand() const
Definition: qsescan.cpp:226
bool matchesCopyNormalizedColumn(const QseColumn *col, int colnum)
bool matchesCopyRatePattern(const QseColumn *col, int colnum)
bool matchesSecondsPattern(const QseColumn *col, int colnum)
QseColumnRole exceptionalColumnRole(int s, int c)
QseEnumParameter m_InputEnergyUnits
Definition: qsenormalizer.h:53
QseDataSet * m_InputData
QHash< const QseColumn *, QseColumnRole > m_Exceptions
bool matchesCopyPattern(const QseColumn *col, int colnum)
QseColumnRole patternColumnRole(int s, int c)
QHash< const QseScan *, QseScanRole > m_ScanExceptions
void setScanException(int s, QseScanRole role)
bool matchesCopyTotalCountsPattern(const QseColumn *col, int colnum)
QString name() const
Definition: qsecolumn.cpp:26
bool matchesScanException(int s)
QseStringParameter m_MaxScanRowCount
Definition: qsenormalizer.h:50
bool matchesCopyNormalizedPattern(const QseColumn *col, int colnum)
QseColumn * column(int n) const
Definition: qsescan.cpp:104
void setColumnException(int s, int c, QseColumnRole role)
bool matchesNormalizationPattern(const QseColumn *col, int colnum)
QseStringParameter m_MinScanRowCount
Definition: qsenormalizer.h:49
QseColumnRole columnRole(int s, int c)
int maxRowCount() const
Definition: qsescan.cpp:74
QString name() const
Definition: qsescan.cpp:31
QseStringParameter m_MaxScanColumnCount
Definition: qsenormalizer.h:52
bool matchesNormalizationColumn(const QseColumn *col, int colnum)
QseDataSet * m_OutputData
void normalizeScan(int n, QseScan *out)
bool matchesSecondsColumn(const QseColumn *col, int colnum)
bool matchesEnergyPattern(const QseColumn *col, int colnum)
QseEnumParameter m_OutputEnergyUnits
Definition: qsenormalizer.h:54
bool matchesColumnException(int s, int c)