data.cpp 16.5 KB
Newer Older
1
2
3
4
5
6
7
8
/**
 * internal data representation
 * @author Tobias Weber <tweber@ill.fr>
 * @date 1-June-2018
 * @license see 'LICENSE' file
 */

#include "data.h"
Tobias WEBER's avatar
Tobias WEBER committed
9
#include "globals.h"
10
11
#include "libs/instr.h"
#include "libs/mat.h"
Tobias WEBER's avatar
Tobias WEBER committed
12
#include "libs/algos.h"
13
14


Tobias WEBER's avatar
Tobias WEBER committed
15
16
17
using t_real = t_real_dat;


Tobias WEBER's avatar
Tobias WEBER committed
18

Tobias WEBER's avatar
Tobias WEBER committed
19
20
21
22
23
24
25
26
27
/**
 * convert an instrument data file to the internal data format
 */
std::tuple<bool, Dataset> Dataset::convert_instr_file(const char* pcFile)
{
	Dataset dataset;


	// load instrument data file
28
	std::unique_ptr<tl2::FileInstrBase<t_real>> pInstr(tl2::FileInstrBase<t_real>::LoadInstr(pcFile));
Tobias WEBER's avatar
Tobias WEBER committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	if(!pInstr)
		return std::make_tuple(false, dataset);
	const auto &colnames = pInstr->GetColNames();
	const auto &filedata = pInstr->GetData();

	if(!pInstr || !colnames.size())	// only valid files with a non-zero column count
		return std::make_tuple(false, dataset);


	// process polarisation data
	pInstr->SetPolNames("p1", "p2", "i11", "i10");
	pInstr->ParsePolData();


	// get scan axis indices
	std::vector<std::size_t> scan_idx;
	for(const auto& scanvar : pInstr->GetScannedVars())
	{
		std::size_t idx = 0;
		pInstr->GetCol(scanvar, &idx);
		if(idx < colnames.size())
			scan_idx.push_back(idx);
	}
	// try first axis if none found
	if(scan_idx.size() == 0) scan_idx.push_back(0);


	// get counter column index
	std::vector<std::size_t> ctr_idx;
	{
		std::size_t idx = 0;
		pInstr->GetCol(pInstr->GetCountVar(), &idx);
		if(idx < colnames.size())
			ctr_idx.push_back(idx);
	}
	// try second axis if none found
	if(ctr_idx.size() == 0) ctr_idx.push_back(1);


	// get monitor column index
	std::vector<std::size_t> mon_idx;
	{
		std::size_t idx = 0;
		pInstr->GetCol(pInstr->GetMonVar(), &idx);
		if(idx < colnames.size())
			mon_idx.push_back(idx);
	}


	std::size_t numpolstates = pInstr->NumPolChannels();
	if(numpolstates == 0) numpolstates = 1;



	// iterate through all (polarisation) subplots
	for(std::size_t polstate=0; polstate<numpolstates; ++polstate)
	{
		Data data;

		// get scan axes
		for(std::size_t idx : scan_idx)
		{
			std::vector<t_real> thedat;
92
			tl2::copy_interleave(filedata[idx].begin(), filedata[idx].end(), std::back_inserter(thedat), numpolstates, polstate);
Tobias WEBER's avatar
Tobias WEBER committed
93
94
95
96
97
98
99
100
			data.AddAxis(thedat, colnames[idx]);
		}


		// get counters
		for(std::size_t idx : ctr_idx)
		{
			std::vector<t_real> thedat, theerr;
101
			tl2::copy_interleave(filedata[idx].begin(), filedata[idx].end(), std::back_inserter(thedat), numpolstates, polstate);
Tobias WEBER's avatar
Tobias WEBER committed
102
			std::transform(thedat.begin(), thedat.end(), std::back_inserter(theerr),
Tobias WEBER's avatar
Tobias WEBER committed
103
104
				[](t_real y) -> t_real
				{
105
					if(tl2::float_equal<t_real>(y, 0))
Tobias WEBER's avatar
Tobias WEBER committed
106
107
108
109
110
111
112
113
114
115
116
117
						return 1;
					return std::sqrt(y);
				});

			data.AddCounter(thedat, theerr);
		}


		// get monitors
		for(std::size_t idx : mon_idx)
		{
			std::vector<t_real> thedat, theerr;
118
			tl2::copy_interleave(filedata[idx].begin(), filedata[idx].end(), std::back_inserter(thedat), numpolstates, polstate);
Tobias WEBER's avatar
Tobias WEBER committed
119
			std::transform(thedat.begin(), thedat.end(), std::back_inserter(theerr),
Tobias WEBER's avatar
Tobias WEBER committed
120
121
				[](t_real y) -> t_real
				{
122
					if(tl2::float_equal<t_real>(y, 0))
Tobias WEBER's avatar
Tobias WEBER committed
123
124
125
126
127
128
129
130
131
132
133
134
135
						return 1;
					return std::sqrt(y);
				});

			data.AddMonitor(thedat, theerr);
		}

		dataset.AddChannel(std::move(data));
	}


	return std::make_tuple(true, dataset);
}
Tobias WEBER's avatar
Tobias WEBER committed
136
137
138
139
140
141





// ----------------------------------------------------------------------------
142
// data operators
Tobias WEBER's avatar
Tobias WEBER committed
143
144
// ----------------------------------------------------------------------------

Tobias WEBER's avatar
Tobias WEBER committed
145
Data Data::add_pointwise(const Data& dat1, const Data& dat2)
Tobias WEBER's avatar
Tobias WEBER committed
146
{
Tobias WEBER's avatar
Tobias WEBER committed
147
148
	// check if x axes and dimensions are equal
	constexpr const t_real_dat eps = 0.01;
Tobias WEBER's avatar
Tobias WEBER committed
149

Tobias WEBER's avatar
Tobias WEBER committed
150
151
152
153
154
155
156
	bool compatible = true;
	compatible = compatible && (dat1.m_x_names == dat2.m_x_names);
	compatible = compatible && (dat1.m_x.size() == dat2.m_x.size());
	if(compatible)
	{
		for(std::size_t i=0; i<dat2.m_x.size(); ++i)
		{
157
			if(!tl2::vec_equal(dat1.m_x[i], dat2.m_x[i], eps))
Tobias WEBER's avatar
Tobias WEBER committed
158
159
160
161
162
163
			{
				compatible = false;
				break;
			}
		}
	}
Tobias WEBER's avatar
Tobias WEBER committed
164

Tobias WEBER's avatar
Tobias WEBER committed
165
166
167
168
169
170
171
172
	if(!compatible)
	{
		print_err("Cannot add incompatible data sets: x axes do not match.");
		return Data();
	}


	Data datret = dat1;
Tobias WEBER's avatar
Tobias WEBER committed
173
174
175
176
177
178
179
180
181

	// detectors
	for(std::size_t detidx=0; detidx<datret.m_counts.size(); ++detidx)
	{
		auto& det = datret.m_counts[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			auto& cnt = det[cntidx];
Tobias WEBER's avatar
Tobias WEBER committed
182
183
184
185
186
			cnt += dat2.m_counts[detidx][cntidx];

			datret.m_counts_err[detidx][cntidx] =
				std::sqrt(dat1.m_counts_err[detidx][cntidx]*dat1.m_counts_err[detidx][cntidx]
					+ dat2.m_counts_err[detidx][cntidx]*dat2.m_counts_err[detidx][cntidx]);
Tobias WEBER's avatar
Tobias WEBER committed
187
188
189
190
191
192
193
194
195
196
197
		}
	}

	// monitors
	for(std::size_t detidx=0; detidx<datret.m_monitors.size(); ++detidx)
	{
		auto& det = datret.m_monitors[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			auto& cnt = det[cntidx];
Tobias WEBER's avatar
Tobias WEBER committed
198
199
200
201
202
			cnt += dat2.m_monitors[detidx][cntidx];

			datret.m_monitors_err[detidx][cntidx] =
				std::sqrt(dat1.m_monitors_err[detidx][cntidx]*dat1.m_monitors_err[detidx][cntidx]
					+ dat2.m_monitors_err[detidx][cntidx]*dat2.m_monitors_err[detidx][cntidx]);
Tobias WEBER's avatar
Tobias WEBER committed
203
204
205
206
207
208
209
		}
	}

	return datret;
}


Tobias WEBER's avatar
Tobias WEBER committed
210
211
212
213
/**
 * append dat2 to the end of dat1
 */
Data Data::append(const Data& dat1, const Data& dat2)
Tobias WEBER's avatar
Tobias WEBER committed
214
{
Tobias WEBER's avatar
Tobias WEBER committed
215
216
217
218
219
220
221
222
223
224
225
226
	if(dat1.m_counts.size() != dat2.m_counts.size())
	{
		print_err("Mismatch in number of detector counters.");
		return Data();
	}

	if(dat1.m_monitors.size() != dat2.m_monitors.size())
	{
		print_err("Mismatch in number of monitor counters.");
		return Data();
	}

Tobias WEBER's avatar
Tobias WEBER committed
227
228
229

	Data datret = dat1;

Tobias WEBER's avatar
Tobias WEBER committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
	// append x axes
	for(std::size_t xidx=0; xidx<dat1.m_x_names.size(); ++xidx)
	{
		// find matching axis
		auto iter = std::find(dat2.m_x_names.begin(), dat2.m_x_names.end(), dat1.m_x_names[xidx]);
		if(iter == dat2.m_x_names.end())
		{
			print_err("Column \"", dat1.m_x_names[xidx], "\" was not found in all data sets. Ignoring.");
			continue;
		}

		// insert data
		std::size_t xidx2 = iter - dat2.m_x_names.begin();
		datret.m_x[xidx].insert(datret.m_x[xidx].end(), dat2.m_x[xidx2].begin(), dat2.m_x[xidx2].end());
	}


	// append counters, monitors, and their errors
	for(std::size_t yidx=0; yidx<dat1.m_counts.size(); ++yidx)
		datret.m_counts[yidx].insert(datret.m_counts[yidx].end(), dat2.m_counts[yidx].begin(), dat2.m_counts[yidx].end());
	for(std::size_t yidx=0; yidx<dat1.m_counts_err.size(); ++yidx)
		datret.m_counts_err[yidx].insert(datret.m_counts_err[yidx].end(), dat2.m_counts_err[yidx].begin(), dat2.m_counts_err[yidx].end());
	for(std::size_t yidx=0; yidx<dat1.m_monitors.size(); ++yidx)
		datret.m_monitors[yidx].insert(datret.m_monitors[yidx].end(), dat2.m_monitors[yidx].begin(), dat2.m_monitors[yidx].end());
	for(std::size_t yidx=0; yidx<dat1.m_counts_err.size(); ++yidx)
		datret.m_monitors_err[yidx].insert(datret.m_monitors_err[yidx].end(), dat2.m_monitors_err[yidx].begin(), dat2.m_monitors_err[yidx].end());


	return datret;
}


const Data& operator +(const Data& dat)
{
	return dat;
}


Data operator -(const Data& dat)
{
	Data datret = dat;

Tobias WEBER's avatar
Tobias WEBER committed
272
273
274
275
276
277
278
279
	// detectors
	for(std::size_t detidx=0; detidx<datret.m_counts.size(); ++detidx)
	{
		auto& det = datret.m_counts[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			auto& cnt = det[cntidx];
Tobias WEBER's avatar
Tobias WEBER committed
280
			cnt = -cnt;
Tobias WEBER's avatar
Tobias WEBER committed
281
282
283
284
285
286
287
288
289
290
291
		}
	}

	// monitors
	for(std::size_t detidx=0; detidx<datret.m_monitors.size(); ++detidx)
	{
		auto& det = datret.m_monitors[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			auto& cnt = det[cntidx];
Tobias WEBER's avatar
Tobias WEBER committed
292
			cnt = -cnt;
Tobias WEBER's avatar
Tobias WEBER committed
293
294
295
296
297
298
299
		}
	}

	return datret;
}


Tobias WEBER's avatar
Tobias WEBER committed
300
301
302
303
304
305
Data operator +(const Data& dat1, const Data& dat2)
{
	return Data::add_pointwise(dat1, dat2);
}


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
Data operator +(const Data& dat, t_real_dat d)
{
	Data datret = dat;
	t_real_dat d_err = std::sqrt(d);
	t_real_dat d_mon = 0.;	// TODO
	t_real_dat d_mon_err = std::sqrt(d_mon);

	// detectors
	for(std::size_t detidx=0; detidx<datret.m_counts.size(); ++detidx)
	{
		auto& det = datret.m_counts[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			auto& cnt = det[cntidx];
			cnt += d;

			datret.m_counts_err[detidx][cntidx] =
				std::sqrt(dat.m_counts_err[detidx][cntidx]*dat.m_counts_err[detidx][cntidx]
					+ d*d_err);
		}
	}

	// monitors
	for(std::size_t detidx=0; detidx<datret.m_monitors.size(); ++detidx)
	{
		auto& det = datret.m_monitors[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			auto& cnt = det[cntidx];
			cnt += d_mon;

			datret.m_monitors_err[detidx][cntidx] =
				std::sqrt(dat.m_monitors_err[detidx][cntidx]*dat.m_monitors_err[detidx][cntidx]
					+ d_mon*d_mon_err);
		}
	}

	return datret;
}


Data operator +(t_real_dat d, const Data& dat)
{
	return dat + d;
}


Data operator -(const Data& dat, t_real_dat d)
{
	return dat + (-d);
}


Tobias WEBER's avatar
Tobias WEBER committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
Data operator -(const Data& dat1, const Data& dat2)
{
	return dat1 + (-dat2);
}


Data operator *(const Data& dat1, t_real_dat d)
{
	Data datret = dat1;

	// detectors
	for(std::size_t detidx=0; detidx<datret.m_counts.size(); ++detidx)
	{
		auto& det = datret.m_counts[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			det[cntidx] *= d;
			datret.m_counts_err[detidx][cntidx] = d*dat1.m_counts_err[detidx][cntidx];
		}
	}

	// monitors
	for(std::size_t detidx=0; detidx<datret.m_monitors.size(); ++detidx)
	{
		auto& det = datret.m_monitors[detidx];

		for(std::size_t cntidx=0; cntidx<det.size(); ++cntidx)
		{
			det[cntidx] *= d;
			datret.m_monitors_err[detidx][cntidx] = d*dat1.m_monitors_err[detidx][cntidx];
		}
	}

	return datret;
}


Data operator *(t_real_dat d, const Data& dat1)
{
	return dat1 * d;
}


Data operator /(const Data& dat1, t_real d)
{
	return dat1 * t_real(1)/d;
}

Tobias WEBER's avatar
Tobias WEBER committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463


/**
 * normalise to monitor counter
 */
Data Data::norm(std::size_t monidx) const
{
	if(GetNumCounters() != GetNumMonitors())
	{
		print_err("Number of monitors has to be equal to the number of detector counters.");
		return *this;
	}
	if(monidx >= GetNumMonitors())
	{
		print_err("Invalid monitor selected.");
		return *this;
	}


	Data datret = *this;

	// normalise all counters
	for(std::size_t detidx=0; detidx<GetNumCounters(); ++detidx)
	{
		const auto& det = GetCounter(detidx);
		const auto& deterr = GetCounterErrors(detidx);
		const auto& mon = GetMonitor(monidx);
		const auto& monerr = GetMonitorErrors(monidx);

		if(det.size()!=deterr.size() || det.size()!=mon.size() || det.size()!=monerr.size())
		{
			print_err("Data, monitor and error columns have to be of equal size."
				" [det=", det.size(), " deterr=", deterr.size(), " mon=", mon.size(), ", monerr=", monerr.size(), "]");
			return *this;
		}

		// newcnts = cnts/mon
		for(std::size_t pt=0; pt<det.size(); ++pt)
		{
			datret.m_counts[detidx][pt] = det[pt] / mon[pt];
			datret.m_counts_err[detidx][pt] = std::sqrt(std::pow(deterr[pt]/mon[pt], 2) + std::pow(-monerr[pt]*det[pt]/(mon[pt]*mon[pt]), 2));
		}
	}


	// normalise monitor with itself
	for(std::size_t pt=0; pt<GetMonitor(monidx).size(); ++pt)
	{
		datret.m_monitors[monidx][pt] = 1.;
		datret.m_monitors_err[monidx][pt] = 0.;
	}

	return datret;
}
Tobias WEBER's avatar
Tobias WEBER committed
464
// ----------------------------------------------------------------------------
465
466
467
468
469
470
471
472




// ----------------------------------------------------------------------------
// dataset operators
// ----------------------------------------------------------------------------

473
Dataset Dataset::add_pointwise(const Dataset& dat1, const Dataset& dat2)
474
475
476
477
478
{
	Dataset dataset;

	for(std::size_t ch=0; ch<std::min(dat1.GetNumChannels(), dat2.GetNumChannels()); ++ch)
	{
479
		Data dat = Data::add_pointwise(dat1.GetChannel(ch), dat2.GetChannel(ch));
480
481
482
483
484
485
486
		dataset.AddChannel(std::move(dat));
	}

	return dataset;
}


487
Dataset Dataset::append(const Dataset& dat1, const Dataset& dat2)
488
489
490
491
492
{
	Dataset dataset;

	for(std::size_t ch=0; ch<std::min(dat1.GetNumChannels(), dat2.GetNumChannels()); ++ch)
	{
493
		Data dat = Data::append(dat1.GetChannel(ch), dat2.GetChannel(ch));
494
495
496
497
498
499
500
		dataset.AddChannel(std::move(dat));
	}

	return dataset;
}


501
502
503
504
505
506
507
508
509
510
511
512
Dataset operator +(const Dataset& dat1, const Dataset& dat2)
{
	return Dataset::add_pointwise(dat1, dat2);
}


Dataset operator -(const Dataset& dat1, const Dataset& dat2)
{
	return dat1 + (-dat2);
}


513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
Dataset operator +(const Dataset& dat, t_real_dat d)
{
	Dataset dataset;

	for(std::size_t ch=0; ch<dat.GetNumChannels(); ++ch)
	{
		Data data = dat.GetChannel(ch) + d;
		dataset.AddChannel(std::move(data));
	}

	return dataset;
}


Dataset operator +(t_real_dat d, const Dataset& dat)
{
	return dat + d;
}


Dataset operator -(const Dataset& dat, t_real_dat d)
{
	return dat + (-d);
}


539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
Dataset operator *(const Dataset& dat1, t_real_dat d)
{
	Dataset dataset;

	for(std::size_t ch=0; ch<dat1.GetNumChannels(); ++ch)
	{
		Data dat = dat1.GetChannel(ch) * d;
		dataset.AddChannel(std::move(dat));
	}

	return dataset;
}


Dataset operator *(t_real_dat d, const Dataset& dat1)
{
	return operator *(dat1, d);
}


Dataset operator /(const Dataset& dat1, t_real_dat d)
{
	Dataset dataset;

	for(std::size_t ch=0; ch<dat1.GetNumChannels(); ++ch)
	{
		Data dat = dat1.GetChannel(ch) / d;
		dataset.AddChannel(std::move(dat));
	}

	return dataset;
}


const Dataset& operator +(const Dataset& dat)
{
	return dat;
}


Dataset operator -(const Dataset& dat1)
{
	Dataset dataset;

	for(std::size_t ch=0; ch<dat1.GetNumChannels(); ++ch)
	{
		Data dat = -dat1.GetChannel(ch);
		dataset.AddChannel(std::move(dat));
	}

	return dataset;
}

Tobias WEBER's avatar
Tobias WEBER committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605


/**
 * normalise to monitor counter
 */
Dataset Dataset::norm(std::size_t mon) const
{
	Dataset dataset;

	for(std::size_t ch=0; ch<GetNumChannels(); ++ch)
		dataset.AddChannel(GetChannel(ch).norm());

	return dataset;
}
606
607
608



Tobias WEBER's avatar
Tobias WEBER committed
609
610
611
612
613
614
void Dataset::clear()
{
	m_data.clear();
}


615
616
617
618
619
620
621
622
/**
 * export data to gnuplot
 */
bool Dataset::SaveGpl(const std::string& file) const
{
	std::ofstream ofstr(file);
	if(!ofstr)
		return false;
Tobias WEBER's avatar
Tobias WEBER committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641

	std::size_t iPrec = 8;
	ofstr.precision(iPrec);


	// colors
	ofstr << "col_0 = \"#ff0000\"\n";
	ofstr << "col_1 = \"#0000ff\"\n";
	ofstr << "col_2 = \"#009900\"\n";
	ofstr << "col_3 = \"#000000\"\n\n\n";

	const std::string cols[] = { "col_0", "col_1", "col_2", "col_3" };
	const std::size_t iNumCols = sizeof(cols) / sizeof(*cols);
	
	const int pts[] = {7, 5, 9, 11, 13};
	const std::size_t iNumPts = sizeof(pts) / sizeof(*pts);


	std::string xlabel = "x";
642
643
644
645
646

	std::size_t N = GetNumChannels();
	for(std::size_t ch=0; ch<N; ++ch)
	{
		const Data& dat = GetChannel(ch);
Tobias WEBER's avatar
Tobias WEBER committed
647
648
649
		const std::size_t Nx = dat.GetNumAxes();
		const std::size_t Nc = dat.GetNumCounters();
		const std::size_t Nm = dat.GetNumMonitors();
650
651
652
653
654
655
656
657

		// channel empty ?
		if(Nx == 0) continue;


		// has to be the same for all columns
		std::size_t rows = dat.GetCounter(0).size();

Tobias WEBER's avatar
Tobias WEBER committed
658
659
660
661
		// take overall x label from first channel
		if(xlabel == "" && Nx >= 1)
			xlabel = dat.GetAxisName(0);

662
663
		ofstr << "$dat_" << ch << " << ENDDATA\n";

Tobias WEBER's avatar
Tobias WEBER committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
		// iterate all x axis names
		ofstr << "# ";
		for(std::size_t iX = 0; iX < Nx; ++iX)
			ofstr << std::setw(iX==0 ? iPrec*1.5 - 2 : iPrec*1.5) << std::left << dat.GetAxisName(iX) << " ";

		// iterate all counter names
		for(std::size_t iC = 0; iC < Nc; ++iC)
			ofstr << std::setw(iPrec*1.5) << std::left << "counter " << " "
				<< std::setw(iPrec*1.5) << std::left << "error" << " ";

		// iterate all monitors names
		for(std::size_t iM = 0; iM < Nm; ++iM)
			ofstr << std::setw(iPrec*1.5) << std::left << "monitor " << " "
				<< std::setw(iPrec*1.5) << std::left << "error" << " ";

		ofstr << "\n";


		// iterate data rows
683
684
685
686
687
688
		for(std::size_t iRow = 0; iRow < rows; ++iRow)
		{
			// iterate all x axes
			for(std::size_t iX = 0; iX < Nx; ++iX)
			{
				const auto& vec = dat.GetAxis(iX);
Tobias WEBER's avatar
Tobias WEBER committed
689
				ofstr << std::setw(iPrec*1.5) << std::left << vec[iRow] << " ";
690
691
692
693
694
695
696
697
698
			}


			// iterate all counters
			for(std::size_t iC = 0; iC < Nc; ++iC)
			{
				const auto& vec = dat.GetCounter(iC);
				const auto& vecErr = dat.GetCounterErrors(iC);

Tobias WEBER's avatar
Tobias WEBER committed
699
700
				ofstr << std::setw(iPrec*1.5) << std::left << vec[iRow] << " " 
					<< std::setw(iPrec*1.5) << std::left << vecErr[iRow] << " ";
701
702
703
704
705
706
707
708
709
			}


			// iterate all monitors
			for(std::size_t iM = 0; iM < Nm; ++iM)
			{
				const auto& vec = dat.GetMonitor(iM);
				const auto& vecErr = dat.GetMonitorErrors(iM);

Tobias WEBER's avatar
Tobias WEBER committed
710
711
				ofstr << std::setw(iPrec*1.5) << std::left << vec[iRow] << " " 
					<< std::setw(iPrec*1.5) << std::left << vecErr[iRow];
712
713
714
715
716
717
			}


			ofstr << "\n";
		}

Tobias WEBER's avatar
Tobias WEBER committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
		ofstr << "ENDDATA\n\n";
	}


	ofstr << "\nset xlabel \"" << xlabel << "\"\n";
	ofstr << "set ylabel \"Counts\"\n\n";

	ofstr << "\nplot \\\n";
	for(std::size_t ch=0; ch<N; ++ch)
	{
		const Data& dat = GetChannel(ch);
		const std::size_t Nx = dat.GetNumAxes();
		const std::size_t idx_x = 1;
		const std::size_t idx_y = Nx+1;
		const std::size_t idx_yerr = idx_y+1;

		ofstr << "\t$dat_" << ch << " u ($" << idx_x << "):($" << idx_y << "):($" << idx_yerr << ")"
			<< " w yerrorbars pt " << pts[ch % iNumPts] 
			<< " ps 1 lw 1.5 lc rgb " << cols[ch % iNumCols]
			<< " title \"Channel " << (ch+1) << "\""
			<< (ch == N-1 ? "\n" : ", \\\n");
739
740
741
742
	}

	return true;
}
743
// ----------------------------------------------------------------------------