forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventListTest.h
2875 lines (2467 loc) · 102 KB
/
EventListTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidAPI/FrameworkManager.h"
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/Histogram1D.h"
#include "MantidKernel/CPUTimer.h"
#include "MantidKernel/Timer.h"
#include "MantidKernel/Unit.h"
#include <cxxtest/TestSuite.h>
#include <boost/scoped_ptr.hpp>
#include <cmath>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::HistogramData;
using namespace Mantid::DataObjects;
using Mantid::Types::Core::DateAndTime;
using Mantid::Types::Event::TofEvent;
using std::runtime_error;
using std::size_t;
using std::vector;
class EventListTest : public CxxTest::TestSuite {
private:
EventList el;
int NUMEVENTS;
int MAX_TOF;
int NUMBINS;
int BIN_DELTA;
int MAX_PULSE_TIME;
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static EventListTest *createSuite() { return new EventListTest(); }
static void destroySuite(EventListTest *suite) { delete suite; }
EventListTest() {
BIN_DELTA = 10000;
NUMBINS = 160;
MAX_TOF = 10000000;
MAX_PULSE_TIME = 10000000;
NUMEVENTS = 100;
}
void setUp() override {
// Make a little event list with 3 events
vector<TofEvent> mylist;
mylist.emplace_back(TofEvent(100, 200));
mylist.emplace_back(TofEvent(3.5, 400));
mylist.emplace_back(TofEvent(50, 60));
el = EventList(mylist);
}
void test_copyDataFrom() {
Histogram1D histogram{Histogram::XMode::Points, Histogram::YMode::Counts};
histogram.setHistogram(Points(1), Counts(1));
EventList eventList;
eventList.setHistogram(BinEdges{0.0, 2.0});
eventList += TofEvent(1.0, 2);
std::unique_ptr<const ISpectrum> specHist = std::make_unique<Histogram1D>(histogram);
std::unique_ptr<const ISpectrum> specEvent = std::make_unique<EventList>(eventList);
std::unique_ptr<ISpectrum> target = std::make_unique<EventList>();
TS_ASSERT_THROWS_EQUALS(target->copyDataFrom(*specHist), const std::runtime_error &e, std::string(e.what()),
"Incompatible types in ISpectrum::copyDataFrom");
TS_ASSERT_THROWS_NOTHING(target->copyDataFrom(*specEvent));
TS_ASSERT(target->binEdges());
TS_ASSERT_EQUALS(&target->binEdges()[0], &eventList.binEdges()[0]);
TS_ASSERT_EQUALS(target->counts()[0], 1.0);
}
void test_copyDataFrom_does_not_copy_indices() {
EventList eventList;
eventList.setHistogram(BinEdges{0.0, 2.0});
eventList += TofEvent(1.0, 2);
std::unique_ptr<const ISpectrum> specEvent = std::make_unique<EventList>(eventList);
std::unique_ptr<ISpectrum> target = std::make_unique<EventList>();
target->setSpectrumNo(37);
target->setDetectorID(42);
TS_ASSERT_THROWS_NOTHING(target->copyDataFrom(*specEvent));
TS_ASSERT(target->binEdges());
TS_ASSERT_EQUALS(&target->binEdges()[0], &eventList.binEdges()[0]);
TS_ASSERT_EQUALS(target->counts()[0], 1.0);
TS_ASSERT_EQUALS(target->getSpectrumNo(), 37);
TS_ASSERT_EQUALS(target->getDetectorIDs(), std::set<detid_t>{42});
}
void test_copyDataFrom_event_data_details() {
EventList eventList;
eventList.setHistogram(BinEdges{0.0, 2.0});
eventList += TofEvent(1.0, 2);
EventList target;
target.copyDataFrom(eventList);
TS_ASSERT_EQUALS(target.getEventType(), EventType::TOF)
TS_ASSERT_EQUALS(target.getSortType(), eventList.getSortType());
TS_ASSERT_EQUALS(target.getEvents(), eventList.getEvents());
TS_ASSERT_THROWS(target.getWeightedEvents(), const std::runtime_error &);
TS_ASSERT_THROWS(target.getWeightedEventsNoTime(), const std::runtime_error &);
eventList.switchTo(EventType::WEIGHTED);
target.copyDataFrom(eventList);
TS_ASSERT_EQUALS(target.getEventType(), EventType::WEIGHTED)
TS_ASSERT_EQUALS(target.getSortType(), eventList.getSortType());
TS_ASSERT_THROWS(target.getEvents(), const std::runtime_error &);
TS_ASSERT_EQUALS(target.getWeightedEvents(), eventList.getWeightedEvents());
TS_ASSERT_THROWS(target.getWeightedEventsNoTime(), const std::runtime_error &);
eventList.switchTo(EventType::WEIGHTED_NOTIME);
target.copyDataFrom(eventList);
TS_ASSERT_EQUALS(target.getEventType(), EventType::WEIGHTED_NOTIME)
TS_ASSERT_EQUALS(target.getSortType(), eventList.getSortType());
TS_ASSERT_THROWS(target.getEvents(), const std::runtime_error &);
TS_ASSERT_THROWS(target.getWeightedEvents(), const std::runtime_error &);
TS_ASSERT_EQUALS(target.getWeightedEventsNoTime(), eventList.getWeightedEventsNoTime());
}
//==================================================================================
//--- Basics ----
//==================================================================================
void test_Init() {
vector<TofEvent> rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 3);
TS_ASSERT_EQUALS(rel[0].tof(), 100);
TS_ASSERT_EQUALS(rel[0].pulseTime(), 200);
TS_ASSERT_EQUALS(rel[2].tof(), 50);
}
void test_AssignmentOperator() {
// Modify EventList such that is does not contain default values.
el.setSpectrumNo(42);
MantidVec x{0.1, 0.2, 0.3};
el.setX(make_cow<HistogramX>(x));
el.setPointVariances(2);
EventList other;
other = el;
TS_ASSERT_EQUALS(other, el);
// operator== does not compare everything, so we do some extra comparisons
TS_ASSERT_EQUALS(other.getSpectrumNo(), el.getSpectrumNo());
TS_ASSERT_EQUALS(other.getDetectorIDs(), el.getDetectorIDs());
TS_ASSERT_EQUALS(other.readX(), el.readX());
TS_ASSERT_EQUALS(other.sharedDx(), el.sharedDx());
}
//==================================================================================
//--- Plus Operators ----
//==================================================================================
void test_PlusOperator() {
vector<TofEvent> mylist{{45, 67}, {89, 12}, {34, 56}};
el += mylist;
vector<TofEvent> rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 6);
TS_ASSERT_EQUALS(rel[3].tof(), 45);
TS_ASSERT_EQUALS(rel[5].tof(), 34);
el += TofEvent(999, 888);
rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 7);
TS_ASSERT_EQUALS(rel[6].tof(), 999);
EventList el2;
el2 += TofEvent(1, 2);
el2 += TofEvent(3, 4);
el += el2;
rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 9);
el += el;
rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 18);
el.addEventQuickly(TofEvent(333, 444));
rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 19);
}
template <class T> void do_test_memory_handling(EventList &el2, std::vector<T> &events) {
std::vector<T> mylist{{45}, {89}, {34}};
el2 += mylist;
TS_ASSERT_EQUALS(events.size(), 3);
TS_ASSERT_EQUALS(events.capacity(), 3);
mylist.emplace_back(TofEvent(88, 88));
el2 += mylist;
TS_ASSERT_EQUALS(events.size(), 7);
TS_ASSERT_EQUALS(events.capacity(), 7);
el2.clear();
TS_ASSERT_EQUALS(events.size(), 0);
TS_ASSERT_EQUALS(events.capacity(), 0);
}
void test_Clear_AndOthers_FreesUpMemory() {
// We want to make sure that clearing really releases the vector memory.
EventList el2;
el2 = EventList();
do_test_memory_handling(el2, el2.getEvents());
el2 = EventList();
el2.switchTo(WEIGHTED);
do_test_memory_handling(el2, el2.getWeightedEvents());
el2 = EventList();
el2.switchTo(WEIGHTED_NOTIME);
do_test_memory_handling(el2, el2.getWeightedEventsNoTime());
}
//
// template<class T>
// void do_test_clearUnused(EventList & el2, typename std::vector<T> &
// events)
// {
// typename std::vector<T> mylist;
// mylist.emplace_back(T(45));
// mylist.emplace_back(T(89));
// mylist.emplace_back(T(34));
// el2 += mylist;
// TS_ASSERT_EQUALS(events.size(), 3);
// TS_ASSERT_EQUALS(events.capacity(), 3);
// mylist.emplace_back(TofEvent(88,88));
// el2 += mylist;
// TS_ASSERT_EQUALS(events.size(), 7);
// TS_ASSERT_EQUALS(events.capacity(), 7);
// el2.clear();
// TS_ASSERT_EQUALS(events.size(), 0);
// TS_ASSERT_EQUALS(events.capacity(), 0);
// }
//
// void test_clearUnused()
// {
// }
void test_PlusOperator2() {
vector<TofEvent> rel;
el += el;
rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 6);
TS_ASSERT_EQUALS(rel[3].tof(), 100);
TS_ASSERT_EQUALS(rel[5].tof(), 50);
}
void test_DetectorIDs() {
EventList el1;
el1.addDetectorID(14);
TS_ASSERT_EQUALS(el1.getDetectorIDs().size(), 1);
el1.addDetectorID(21);
TS_ASSERT_EQUALS(el1.getDetectorIDs().size(), 2);
el1.addDetectorID(21);
TS_ASSERT_EQUALS(el1.getDetectorIDs().size(), 2);
EventList el2;
el2.addDetectorID(7);
el2.addDetectorID(14);
el2.addDetectorID(28);
TS_ASSERT_EQUALS(el2.getDetectorIDs().size(), 3);
// One detID was repeated, so it doesn't appear twice
el2 += el1;
TS_ASSERT_EQUALS(el2.getDetectorIDs().size(), 4);
// Find the right stuff
for (int i = 7; i < 35; i += 7)
TS_ASSERT(el2.hasDetectorID(i));
TS_ASSERT(!el2.hasDetectorID(0));
}
//==================================================================================
//--- Switching to Weighted Events ----
//==================================================================================
//----------------------------------
void test_switchToWeightedEvents() {
// Start with a bit of fake data
this->fake_data();
TS_ASSERT_EQUALS(el.getEvents().size(), NUMEVENTS);
TS_ASSERT_EQUALS(el.getNumberEvents(), NUMEVENTS);
TS_ASSERT_THROWS(el.getWeightedEvents(), const std::runtime_error &);
TS_ASSERT_THROWS(el.getWeightedEventsNoTime(), const std::runtime_error &);
el.switchTo(WEIGHTED);
TS_ASSERT_THROWS(el.getEvents(), const std::runtime_error &);
TS_ASSERT_THROWS(el.getWeightedEventsNoTime(), const std::runtime_error &);
TS_ASSERT_EQUALS(el.getWeightedEvents().size(), NUMEVENTS);
TS_ASSERT_EQUALS(el.getNumberEvents(), NUMEVENTS);
TS_ASSERT_EQUALS(el.getEvent(0).weight(), 1.0);
TS_ASSERT_EQUALS(el.getEvent(0).error(), 1.0);
}
//----------------------------------
void test_switchToWeightedEventsNoTime() {
// Start with a bit of fake data
this->fake_data();
el.switchTo(WEIGHTED_NOTIME);
TS_ASSERT_THROWS(el.getEvents(), const std::runtime_error &);
TS_ASSERT_THROWS(el.getWeightedEvents(), const std::runtime_error &);
TS_ASSERT_EQUALS(el.getWeightedEventsNoTime().size(), NUMEVENTS);
TS_ASSERT_EQUALS(el.getNumberEvents(), NUMEVENTS);
TS_ASSERT_EQUALS(el.getWeightedEventsNoTime()[0].weight(), 1.0);
TS_ASSERT_EQUALS(el.getWeightedEventsNoTime()[0].error(), 1.0);
}
//----------------------------------
void test_switch_on_the_fly_when_adding_single_event() {
fake_data();
TS_ASSERT_EQUALS(el.getEventType(), TOF);
// Add a weighted event = everything switches
WeightedEvent we(123, 456, 2.0, 3.0 * 3.0);
el += we;
TS_ASSERT_EQUALS(el.getEventType(), WEIGHTED);
TS_ASSERT_EQUALS(el.getEvent(0).weight(), 1.0);
TS_ASSERT_EQUALS(el.getEvent(0).error(), 1.0);
// New one is at the end
TS_ASSERT_EQUALS(el.getWeightedEvents()[NUMEVENTS], we);
// But you can still add a plain one
TofEvent e(789, 654);
el += e;
TS_ASSERT_EQUALS(el.getWeightedEvents()[NUMEVENTS + 1], e);
TS_ASSERT_EQUALS(el.getEvent(NUMEVENTS + 1).weight(), 1.0);
}
//----------------------------------
/** Nine possibilies of adding event lists together
* (3 lhs x 3 rhs types).
*/
void test_switch_on_the_fly_when_appending_lists_all_nine_possibilities() {
EventList lhs, rhs;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Copy and switch
lhs = el;
lhs.switchTo(static_cast<EventType>(i));
// Copy and switch
rhs = el;
rhs.switchTo(static_cast<EventType>(j));
// Use the += operator to append
TS_ASSERT_THROWS_NOTHING(lhs += rhs;);
// The Ending type is whatever is HIGHER in the hierarchy
// TOF->WEIGHTED->WEIGHTED_NOTIME
int expected = i;
if (j > i)
expected = j;
TS_ASSERT_EQUALS(static_cast<int>(lhs.getEventType()), expected);
// The final list has 6 events
TS_ASSERT_EQUALS(lhs.getNumberEvents(), 6);
// And each element's TOF is what we expect.
TS_ASSERT_DELTA(lhs.getEvent(0).tof(), 100, 1e-5);
TS_ASSERT_DELTA(lhs.getEvent(1).tof(), 3.5, 1e-5);
TS_ASSERT_DELTA(lhs.getEvent(2).tof(), 50, 1e-5);
TS_ASSERT_DELTA(lhs.getEvent(3).tof(), 100, 1e-5);
TS_ASSERT_DELTA(lhs.getEvent(4).tof(), 3.5, 1e-5);
TS_ASSERT_DELTA(lhs.getEvent(5).tof(), 50, 1e-5);
}
}
}
//==================================================================================
//--- Minus Operation ----
//==================================================================================
/// Make a big bin holding all events
cow_ptr<HistogramX> one_big_bin() { return make_cow<HistogramX>(std::initializer_list<double>{0, 1e10}); }
void test_MinusOperator_all_9_possibilites() {
EventList lhs, rhs;
for (size_t i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
this->fake_uniform_data();
// Copy and switch
lhs = el;
lhs.switchTo(static_cast<EventType>(i));
// Copy and switch
rhs = el;
rhs.switchTo(static_cast<EventType>(j));
// Do the minus!
std::ostringstream mess;
mess << "Minus operation of types " << i << " -= " << j << ".";
TSM_ASSERT_THROWS_NOTHING(mess.str(), lhs -= rhs);
TSM_ASSERT_EQUALS(mess.str(), lhs.getNumberEvents(), 2 * el.getNumberEvents());
// Put a single big bin with all events
lhs.setX(one_big_bin());
// But the total neutrons is 0.0! They've been cancelled out :)
boost::scoped_ptr<MantidVec> Y(lhs.makeDataY());
boost::scoped_ptr<MantidVec> E(lhs.makeDataE());
TS_ASSERT_DELTA((*Y)[0], 0.0, 1e-6);
TS_ASSERT_DELTA((*E)[0], sqrt((double)lhs.getNumberEvents()), 1e-6);
}
}
}
/** Perform THIS -= THIS, e.g. clear the event list */
void test_MinusOperator_inPlace_3cases() {
EventList lhs, rhs;
for (size_t i = 0; i < 3; i++) {
this->fake_uniform_data();
// Copy and switch
lhs = el;
lhs.switchTo(static_cast<EventType>(i));
// Do the minus!
std::ostringstream mess;
mess << "Minus operation of type " << i << ".";
TSM_ASSERT_THROWS_NOTHING(mess.str(), lhs -= lhs);
TSM_ASSERT_EQUALS(mess.str(), lhs.getNumberEvents(), 0);
// Put a single big bin with all events
lhs.setX(one_big_bin());
// But the total neutrons is 0.0! They've been cancelled out :)
boost::scoped_ptr<MantidVec> Y(lhs.makeDataY());
boost::scoped_ptr<MantidVec> E(lhs.makeDataE());
TS_ASSERT_DELTA((*Y)[0], 0.0, 1e-6);
TS_ASSERT_DELTA((*E)[0], sqrt((double)lhs.getNumberEvents()), 1e-6);
}
}
//==================================================================================
//--- Multiplying ----
//==================================================================================
void test_multiply_scalar_simple() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
this->fake_uniform_data();
el.switchTo(static_cast<EventType>(this_type));
// Perform the multiply; no error on the scalar
TS_ASSERT_THROWS_NOTHING(el.multiply(2.0, 0.0));
TS_ASSERT_DELTA(el.getEvent(0).weight(), 2.0, 1e-5);
TS_ASSERT_DELTA(el.getEvent(0).error(), 2.0, 1e-5);
this->fake_uniform_data();
// Multiply by zero with error
TS_ASSERT_THROWS_NOTHING(el.multiply(0.0, 1.0));
TS_ASSERT_DELTA(el.getEvent(0).weight(), 0.0, 1e-5);
// Error is preserved!
TS_ASSERT_DELTA(el.getEvent(0).error(), 1.0, 1e-5);
}
}
void test_multiply_by_one_doesnt_give_weights() {
// No weights
this->fake_uniform_data();
// Perform the multiply by one without error.
el.multiply(1.0, 0.0);
TS_ASSERT_EQUALS(el.getEventType(), TOF);
}
void test_divide_by_one_doesnt_give_weights() {
// No weights
this->fake_uniform_data();
// Perform the multiply by one without error.
el.divide(1.0, 0.0);
TS_ASSERT_EQUALS(el.getEventType(), TOF);
}
//-----------------------------------------------------------------------------------------------
void test_multiply_scalar() {
// Weight 2, error (2.5)
this->fake_uniform_data_weights();
// Perform the multiply
el.multiply(2.0, 0.5);
TS_ASSERT_DELTA(el.getEvent(0).weight(), 4.0, 1e-5);
// Error^2 = 2.5*2.5 * 2.0*2.0 + 2.0*2.0*0.5*0.5
TS_ASSERT_DELTA(el.getEvent(0).errorSquared(), (2.5 * 2.5 * 2.0 * 2.0 + 2.0 * 2.0 * 0.5 * 0.5), 1e-5);
// Go through each possible EventType as the input
for (int this_type = 1; this_type < 3; this_type++) {
// Try it with no scalar error
this->fake_uniform_data_weights();
el.switchTo(static_cast<EventType>(this_type));
el.multiply(2.0);
TS_ASSERT_DELTA(el.getEvent(0).weight(), 4.0, 1e-5);
TS_ASSERT_DELTA(el.getEvent(0).error(), 1.25 * 4.0, 1e-5);
// *= operator
this->fake_uniform_data_weights();
el.switchTo(static_cast<EventType>(this_type));
el *= 2.0;
TS_ASSERT_DELTA(el.getEvent(0).weight(), 4.0, 1e-5);
TS_ASSERT_DELTA(el.getEvent(0).error(), 1.25 * 4.0, 1e-5);
}
}
//-----------------------------------------------------------------------------------------------
void test_multiply_histogram() {
// Make the histogram we are multiplying.
MantidVec X, Y, E;
// one tenth of the # of bins
double step = BIN_DELTA * 10;
X = this->makeX(step, NUMBINS / 10 + 1);
for (std::size_t i = 0; i < X.size() - 1; i++) {
Y.emplace_back(static_cast<double>(i + 1));
E.emplace_back(sqrt(static_cast<double>(i + 1)));
}
// Go through each possible EventType as the input
for (int this_type = 1; this_type < 3; this_type++) {
// Make the data and multiply: 2.0+-2.5
this->fake_uniform_data_weights();
el.switchTo(static_cast<EventType>(this_type));
// Perform the histogram multiplication
el.multiply(X, Y, E);
// The event list is of the right length and type
TS_ASSERT_EQUALS(el.getNumberEvents(), 2000);
TS_ASSERT_EQUALS(el.getEventType(), static_cast<EventType>(this_type));
for (std::size_t i = 0; i < el.getNumberEvents(); i++) {
double tof = el.getEvent(i).tof();
if (tof >= step && tof < BIN_DELTA * NUMBINS) {
double value = std::floor(tof / step);
double errorsquared = value;
// Check the formulae for value and error
TS_ASSERT_DELTA(el.getEvent(i).weight(), 2.0 * value, 1e-6);
TS_ASSERT_DELTA(el.getEvent(i).errorSquared(), 2.5 * 2.5 * value * value + 2.0 * 2.0 * errorsquared, 1e-6);
}
}
}
}
//-----------------------------------------------------------------------------------------------
void test_divide_scalar_simple() {
this->fake_uniform_data();
el.divide(2.0, 0.0);
TS_ASSERT_DELTA(el.getEvent(0).weight(), 0.5, 1e-5);
TS_ASSERT_DELTA(el.getEvent(0).error(), 0.5, 1e-5);
this->fake_uniform_data();
el.divide(2.0);
TS_ASSERT_DELTA(el.getEvent(0).weight(), 0.5, 1e-5);
TS_ASSERT_DELTA(el.getEvent(0).error(), 0.5, 1e-5);
}
void test_divide_scalar() {
// Weight 2, error 2.5
this->fake_uniform_data_weights();
el.divide(2.0, 0.5);
TS_ASSERT_DELTA(el.getEvent(0).weight(), 1.0, 1e-5);
// Relative errors sum, so (sqrt(2.5)/2)^2+0.25^2 = 1.625; error is
// sqrt(1.625 * 1.0)
TS_ASSERT_DELTA(el.getEvent(0).error(), sqrt(1.625), 1e-5);
// Try it with no scalar error
this->fake_uniform_data_weights();
el.divide(2.0);
TS_ASSERT_DELTA(el.getEvent(0).weight(), 1.0, 1e-5);
// Same relative error of 1.25
TS_ASSERT_DELTA(el.getEvent(0).error(), 1.25, 1e-5);
// *= operator
this->fake_uniform_data_weights();
el /= 2.0;
TS_ASSERT_DELTA(el.getEvent(0).weight(), 1.0, 1e-5);
TS_ASSERT_DELTA(el.getEvent(0).error(), 1.25, 1e-5);
}
void test_divide_by_zero() {
// Perform the multiply
TS_ASSERT_THROWS(el.divide(0.0, 0.5), const std::invalid_argument &);
TS_ASSERT_THROWS(el.divide(0.0), const std::invalid_argument &);
TS_ASSERT_THROWS(el /= 0, const std::invalid_argument &);
}
//-----------------------------------------------------------------------------------------------
void test_divide_histogram() {
// Make the histogram by which we'll divide
MantidVec X, Y, E;
// one tenth of the # of bins
double step = BIN_DELTA * 10;
for (double tof = step; tof < BIN_DELTA * (NUMBINS + 1); tof += step) {
X.emplace_back(tof);
}
for (std::size_t i = 0; i < X.size() - 1; i++) {
// Have one zero bin in there
if (i == 6)
Y.emplace_back(0.0);
else
Y.emplace_back(2.0);
E.emplace_back(0.5);
}
// Go through each possible EventType as the input
for (int this_type = 1; this_type < 3; this_type++) {
// Make the data and multiply: 2.0+-2.5
this->fake_uniform_data_weights();
el.switchTo(static_cast<EventType>(this_type));
// Now we divide
TS_ASSERT_THROWS_NOTHING(el.divide(X, Y, E));
// The event list is of the right length and type
TS_ASSERT_EQUALS(el.getNumberEvents(), 2000);
TS_ASSERT_EQUALS(el.getEventType(), static_cast<EventType>(this_type));
for (std::size_t i = 0; i < el.getNumberEvents(); i++) {
double tof = el.getEvent(i).tof();
if (tof >= step && tof < BIN_DELTA * NUMBINS) {
int bini = static_cast<int>(tof / step);
if (bini == 7) {
// That was zeros
TS_ASSERT(std::isnan(el.getEvent(i).weight()));
TS_ASSERT(std::isnan(el.getEvent(i).errorSquared()));
} else {
// Same weight error as dividing by a scalar with error before,
// since we divided by 2+-0.5 again
TS_ASSERT_DELTA(el.getEvent(i).weight(), 1.0, 1e-5);
TS_ASSERT_DELTA(el.getEvent(i).error(), sqrt(1.625), 1e-5);
}
}
}
}
}
void test_divide_by_a_scalar_without_error___then_histogram() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
// Make the data
this->fake_uniform_data();
el.switchTo(static_cast<EventType>(this_type));
// Divide by 2, no error = result should be 1 +- 0.707
TS_ASSERT_THROWS_NOTHING(el.divide(2.0, 0));
// Make the histogram we are multiplying.
MantidVec Y, E;
MantidVec X = this->makeX(BIN_DELTA, 10);
el.generateHistogram(X, Y, E);
for (std::size_t i = 0; i < Y.size(); i++) {
TSM_ASSERT_DELTA(this_type, Y[i], 1.0, 1e-5);
TS_ASSERT_DELTA(E[i], 0.5 * M_SQRT2, 1e-5);
}
}
}
void test_divide_by_a_scalar_with_error___then_histogram() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
// Make the data
this->fake_uniform_data();
el.switchTo(static_cast<EventType>(this_type));
// Divide by two with error sqrt(2) = result has less error than if you
// had started from a histogram.
TS_ASSERT_THROWS_NOTHING(el.divide(2.0, M_SQRT2));
// Make the histogram we are multiplying.
MantidVec Y, E;
MantidVec X = this->makeX(BIN_DELTA, 10);
el.generateHistogram(X, Y, E);
for (std::size_t i = 0; i < Y.size(); i++) {
TS_ASSERT_DELTA(Y[i], 1.0, 1e-5);
TS_ASSERT_DELTA(E[i], sqrt(0.75), 1e-5);
}
}
}
void test_multiply_by_a_scalar_without_error___then_histogram() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
// Make the data
this->fake_uniform_data();
el.switchTo(static_cast<EventType>(this_type));
// multiply
TS_ASSERT_THROWS_NOTHING(el.multiply(2.0, 0.0));
// Make the histogram we are multiplying.
MantidVec Y, E;
MantidVec X = this->makeX(BIN_DELTA);
el.generateHistogram(X, Y, E);
for (std::size_t i = 0; i < Y.size(); i++) {
TS_ASSERT_DELTA(Y[i], 4.0, 1e-5);
TS_ASSERT_DELTA(E[i], 4.0 * M_SQRT1_2, 1e-5);
}
}
}
void test_multiply_by_a_scalar_with_error___then_histogram() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
// Make the data
this->fake_uniform_data();
el.switchTo(static_cast<EventType>(this_type));
// Multiply with an error
TS_ASSERT_THROWS_NOTHING(el.multiply(2.0, M_SQRT2));
MantidVec Y, E;
MantidVec X = this->makeX(BIN_DELTA);
el.generateHistogram(X, Y, E);
for (std::size_t i = 0; i < Y.size(); i++) {
TSM_ASSERT_DELTA(this_type, Y[i], 4.0, 1e-5);
TS_ASSERT_DELTA(E[i], sqrt(12.0), 1e-5);
}
}
}
//==================================================================================
//--- Sorting Tests ---
//==================================================================================
void test_SortTOF_simple() {
el.sortTof();
vector<TofEvent> rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 3);
TS_ASSERT_EQUALS(rel[0].tof(), 3.5);
TS_ASSERT_EQUALS(rel[1].tof(), 50);
TS_ASSERT_EQUALS(rel[2].tof(), 100);
}
/// Test for all event types
void test_SortTOF_all_types() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
this->fake_data();
el.switchTo(static_cast<EventType>(this_type));
el.sort(TOF_SORT);
for (size_t i = 1; i < 100; i++) {
TSM_ASSERT_LESS_THAN_EQUALS(this_type, el.getEvent(i - 1).tof(), el.getEvent(i).tof());
}
}
}
void test_SortPulseTime_simple() {
el.sortPulseTime();
vector<TofEvent> rel = el.getEvents();
TS_ASSERT_EQUALS(rel.size(), 3);
TS_ASSERT_EQUALS(rel[0].pulseTime(), 60);
TS_ASSERT_EQUALS(rel[1].pulseTime(), 200);
TS_ASSERT_EQUALS(rel[2].pulseTime(), 400);
}
void test_SortPulseTime_allTypes() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
this->fake_data();
el.switchTo(static_cast<EventType>(this_type));
el.sort(PULSETIME_SORT);
for (size_t i = 1; i < 100; i++) {
TSM_ASSERT_LESS_THAN_EQUALS(this_type, el.getEvent(i - 1).pulseTime(), el.getEvent(i).pulseTime());
}
}
}
void test_SortPulseTime_weights() {
this->fake_data();
el.switchTo(WEIGHTED);
el.sort(PULSETIME_SORT);
vector<WeightedEvent> rwel = el.getWeightedEvents();
for (size_t i = 1; i < 100; i++) {
TS_ASSERT_LESS_THAN_EQUALS(rwel[i - 1].pulseTime(), rwel[i].pulseTime());
}
}
//-----------------------------------------------------------------------------------------------
void test_reverse_allTypes() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
this->fake_data();
el.switchTo(static_cast<EventType>(this_type));
el.sortTof();
double oldFirst = el.getEvent(0).tof();
double oldLast = el.getEvent(el.getNumberEvents() - 1).tof();
size_t oldNum = el.getNumberEvents();
el.reverse();
double newFirst = el.getEvent(0).tof();
double newLast = el.getEvent(el.getNumberEvents() - 1).tof();
size_t newNum = el.getNumberEvents();
TS_ASSERT_EQUALS(oldNum, newNum);
TS_ASSERT_EQUALS(oldFirst, newLast);
TS_ASSERT_EQUALS(oldLast, newFirst);
}
}
//==================================================================================
//--- Comparison Operators
//==================================================================================
void test_EqualityOperator() {
EventList el1, el2;
el1.addEventQuickly(TofEvent(1.5, 5));
TS_ASSERT(!(el1 == el2));
TS_ASSERT((el1 != el2));
el2.addEventQuickly(TofEvent(1.5, 5));
TS_ASSERT((el1 == el2));
TS_ASSERT(!(el1 != el2));
TS_ASSERT(el1.equals(el2, 0., 0., 0));
}
//==================================================================================
//--- Histogramming Tests ---
//==================================================================================
void test_setX() {
// Generate the histrogram bins
MantidVec shared_x;
double tof; // in ns
for (tof = 0; tof < BIN_DELTA * (NUMBINS + 1); tof += BIN_DELTA) {
// bins of 10 microsec
shared_x.emplace_back(tof);
}
el.setX(make_cow<HistogramX>(shared_x));
// Do we have the same data in X?
const EventList el2(el);
TS_ASSERT(el2.readX() == shared_x);
}
void test_dataX() {
el = EventList();
MantidVec inVec(10, 1.0);
el.dataX() = inVec;
const MantidVec &vec = el.dataX();
TS_ASSERT_EQUALS(vec, inVec);
}
void test_setX_empty_constructor() {
el = EventList();
// Generate the histrogram bins
MantidVec shared_x;
double tof; // in ns
for (tof = 0; tof < 16e3 * 1e3; tof += 1e4) {
// bins of 10 microsec
shared_x.emplace_back(tof);
}
el.setX(make_cow<HistogramX>(shared_x));
// Do we have the same data in X?
const EventList el2(el);
TS_ASSERT(el2.readX() == shared_x);
}
void test_empty_histogram() {
// Make sure there's no data
el.clear();
const EventList el2(el);
// Getting data before setting X returns empty vector
boost::scoped_ptr<MantidVec> Y2(el2.makeDataY());
TS_ASSERT_EQUALS(Y2->size(), 0);
// Now do set up an X axis.
this->test_setX();
const EventList el3(el);
MantidVec X = el3.readX();
boost::scoped_ptr<MantidVec> Y3(el3.makeDataY());
// Histogram is 0, since I cleared all the events
for (std::size_t i = 0; i < X.size() - 1; i++) {
TS_ASSERT_EQUALS((*Y3)[i], 0);
}
}
void test_no_histogram_x() {
// Make sure there's no data and no X
el.clear();
// Now give it some fake data, with NUMEVENTS events in it.
this->fake_data();
const EventList el4(el);
boost::scoped_ptr<MantidVec> Y(el4.makeDataY());
TS_ASSERT_EQUALS(Y->size(), 0);
}
void test_histogram_all_types() {
// Go through each possible EventType as the input
for (int this_type = 0; this_type < 3; this_type++) {
this->fake_uniform_data();
el.switchTo(static_cast<EventType>(this_type));
this->test_setX(); // Set it up
const EventList el3(el); // need to copy to a const method in order to
// access the data directly.
MantidVec X = el3.readX();
boost::scoped_ptr<MantidVec> Y(el3.makeDataY());
boost::scoped_ptr<MantidVec> E(el3.makeDataE());
TS_ASSERT_EQUALS(Y->size(), X.size() - 1);
// The data was created so that there should be exactly 2 events per bin
// The last bin entry will be 0 since we use it as the top boundary of
// i-1.
for (std::size_t i = 0; i < Y->size(); i++) {
TS_ASSERT_EQUALS((*Y)[i], 2.0);
TS_ASSERT_DELTA((*E)[i], M_SQRT2, 1e-5);
}
}
}
void test_histogram_tof_event_by_pulse_time() {
// Generate TOF events with Pulse times uniformly distributed.
EventList eList = this->fake_uniform_pulse_data();
// Generate the histrogram bins
MantidVec shared_x;
for (int pulse_time = 0; pulse_time < BIN_DELTA * (NUMBINS + 1); pulse_time += BIN_DELTA) {
shared_x.emplace_back(pulse_time);
}
eList.setX(make_cow<HistogramX>(shared_x));
// Do we have the same data in X?
TS_ASSERT(eList.readX() == shared_x);
MantidVec X = eList.readX();
MantidVec Y;
MantidVec E;
eList.generateHistogramPulseTime(X, Y, E);
for (std::size_t i = 0; i < Y.size(); i++) {
TS_ASSERT_EQUALS(Y[i], 2.0);
TS_ASSERT_DELTA(E[i], M_SQRT2, 1e-5);
}
// check uniform counts histogram.
size_t hist1 = Y.size();
MantidVec Y1(hist1, 0);
eList.generateCountsHistogramPulseTime(X[0], X[hist1], Y1);
for (std::size_t i = 0; i < Y.size(); i++) {
TS_ASSERT_EQUALS(Y[i], Y1[i]);
}
}
void test_histogram_weighed_event_by_pulse_time_throws() {
EventList eList = this->fake_uniform_pulse_data(WEIGHTED);
// Generate the histrogram bins
MantidVec shared_x;
for (int pulse_time = 0; pulse_time < BIN_DELTA * (NUMBINS + 1); pulse_time += BIN_DELTA) {
shared_x.emplace_back(pulse_time);
}
eList.setX(make_cow<HistogramX>(shared_x));
// Do we have the same data in X?
TS_ASSERT(eList.readX() == shared_x);
MantidVec X = eList.readX();
MantidVec Y;
MantidVec E;
TSM_ASSERT_THROWS("We don't support WeightedEvents with this feature at present.",
eList.generateHistogramPulseTime(X, Y, E), const std::runtime_error &);
}
void test_histogram_by_time_at_sample_pulse_only() {
// Generate TOF events with Pulse times uniformly distributed.
EventList eList = this->fake_uniform_pulse_data();
// Generate the histrogram bins
MantidVec shared_x;
for (int time_at_sample = 0; time_at_sample < BIN_DELTA * (NUMBINS + 1); time_at_sample += BIN_DELTA) {
shared_x.emplace_back(time_at_sample);
}
eList.setX(make_cow<HistogramX>(shared_x));
// Do we have the same data in X?
TS_ASSERT(eList.readX() == shared_x);
MantidVec X = eList.readX();
MantidVec Y;
MantidVec E;
const double tofFactor = 0;
const double tofOffset = 0;
// Should be doing the same job as generatehistogrampulsetime with tofFactor
// and tofOffset = 0.
eList.generateHistogramTimeAtSample(X, Y, E, tofFactor, tofOffset);
for (std::size_t i = 0; i < Y.size(); i++) {
TS_ASSERT_EQUALS(Y[i], 2.0);
TS_ASSERT_DELTA(E[i], M_SQRT2, 1e-5);
}
}
void test_get_min_pulse_time() {