-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathTrapSimulator.cxx
More file actions
2532 lines (2189 loc) · 98.7 KB
/
Copy pathTrapSimulator.cxx
File metadata and controls
2532 lines (2189 loc) · 98.7 KB
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
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
///////////////////////////////////////////////////////////////////////////////
// //
// TRD MCM (Multi Chip Module) simulator //
// which simulates the TRAP processing after the AD-conversion. //
// The relevant parameters (i.e. configuration settings of the TRAP) //
// are taken from TrapConfig. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "TRDBase/TRDSimParam.h"
#include "TRDBase/TRDCommonParam.h"
#include "TRDBase/TRDGeometry.h"
#include "TRDBase/FeeParam.h"
#include "TRDBase/Tracklet.h"
#include "TRDBase/CalOnlineGainTables.h"
#include "TRDSimulation/TrapConfigHandler.h"
#include "TRDSimulation/TrapConfig.h"
#include "TRDSimulation/TrapSimulator.h"
#include "fairlogger/Logger.h"
//to pull in the digitzer incomnig data.
#include "TRDBase/Digit.h"
#include "TRDSimulation/Digitizer.h"
#include <SimulationDataFormat/MCCompLabel.h>
#include <SimulationDataFormat/MCTruthContainer.h>
#include <DataFormatsTRD/RawData.h>
#include <DataFormatsTRD/Tracklet64.h>
#include <iostream>
#include <iomanip>
#include "TCanvas.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TGraph.h"
#include "TLine.h"
#include "TRandom.h"
#include "TMath.h"
#include <TTree.h>
#include <TFile.h>
#include "TRandom3.h"
#include <ostream>
#include <fstream>
using namespace o2::trd;
using namespace std;
using namespace o2::trd::constants;
#define DEBUGTRAP 1
bool TrapSimulator::mgApplyCut = true;
int TrapSimulator::mgAddBaseline = 0;
bool TrapSimulator::mgStoreClusters = true;
const int TrapSimulator::mgkFormatIndex = std::ios_base::xalloc();
const std::array<unsigned short, 4> TrapSimulator::mgkFPshifts{11, 14, 17, 21};
TrapSimulator::TrapSimulator()
: mInitialized(false), mDetector(-1), mRobPos(-1), mMcmPos(-1), mRow(-1), mNTimeBin(-1), mTrklBranchName("mcmtrklbranch"), mFeeParam(nullptr), mTrapConfig(nullptr)
{
//
// TrapSimulator default constructor
// By default, nothing is initialized.
// It is necessary to issue init before use.
mFitPtr[0] = 0;
mFitPtr[1] = 0;
mFitPtr[2] = 0;
mFitPtr[3] = 0;
mNHits = 0;
// mCalib.setCCDBForSimulation(297595);
}
void TrapSimulator::init(TrapConfig* trapconfig, int det, int robPos, int mcmPos)
{
//
// Initialize the class with new MCM position information
// memory is allocated in the first initialization
//
//
LOG(debug4) << " : trap sim is at : 0x" << hex << trapconfig;
if (!mInitialized) {
mFeeParam = FeeParam::instance();
mTrapConfig = trapconfig;
}
mDetector = det;
mRobPos = robPos;
mMcmPos = mcmPos;
mRow = mFeeParam->getPadRowFromMCM(mRobPos, mMcmPos);
if (!mInitialized) {
mNTimeBin = mTrapConfig->getTrapReg(TrapConfig::kC13CPUA, mDetector, mRobPos, mMcmPos);
mZSMap.resize(NADCMCM);
// tracklet calculation
//now a 25 slot array mFitReg.resize(NADCMCM); //TODO for now this is constant size in an array not a vector
// mTrackletArray-.resize(mgkMaxTracklets);
//now a 100 slot array as per run2 mHits.resize(50);
mMCMT.resize(mgkMaxTracklets);
mADCR.resize(mNTimeBin * NADCMCM);
mADCF.resize(mNTimeBin * NADCMCM);
}
mInitialized = true;
mNHits = 0;
reset();
}
void TrapSimulator::reset()
{
// Resets the data values and internal filter registers
// by re-initialising them
if (!checkInitialized())
return;
//clear the adc data
memset(&mADCR[0], 0, sizeof(mADCR[0]) * mADCR.size());
memset(&mADCF[0], 0, sizeof(mADCF[0]) * mADCF.size());
//clear the labels
mADCLabels[0].clear();
mADCLabels[1].clear();
mADCLabels[2].clear();
mTrackletLabels.clear(); // as the name implies clear the stored labels
mNHits = 0;
for (auto filterreg : mInternalFilterRegisters)
filterreg.ClearReg();
// Default unread, low active bit mask
memset(&mZSMap[0], 0, sizeof(mZSMap[0]) * NADCMCM);
memset(&mMCMT[0], 0, sizeof(mMCMT[0]) * mgkMaxTracklets);
//mDict1.clear();
//mDict2.clear();
//mDict3.clear();
filterPedestalInit();
filterGainInit();
filterTailInit();
}
// ----- I/O implementation -----
ostream& TrapSimulator::text(ostream& os)
{
// manipulator to activate output in text format (default)
os.iword(mgkFormatIndex) = 0;
return os;
}
ostream& TrapSimulator::cfdat(ostream& os)
{
// manipulator to activate output in CFDAT format
// to send to the FEE via SCSN
os.iword(mgkFormatIndex) = 1;
return os;
}
ostream& TrapSimulator::raw(ostream& os)
{
// manipulator to activate output as raw data dump
os.iword(mgkFormatIndex) = 2;
return os;
}
//std::ostream& operator<<(std::ostream& os, const TrapSimulator& mcm); // data output using ostream (e.g. cout << mcm;)
std::ostream& o2::trd::operator<<(std::ostream& os, const TrapSimulator& mcm)
{
// output implementation
// no output for non-initialized MCM
if (!mcm.checkInitialized())
return os;
// ----- human-readable output -----
if (os.iword(TrapSimulator::mgkFormatIndex) == 0) {
os << "TRAP " << mcm.getMcmPos() << " on ROB " << mcm.getRobPos() << " in detector " << mcm.getDetector() << std::endl;
os << "----- Unfiltered ADC data (10 bit) -----" << std::endl;
os << "ch ";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++)
os << std::setw(5) << iChannel;
os << std::endl;
for (int iTimeBin = 0; iTimeBin < mcm.getNumberOfTimeBins(); iTimeBin++) {
os << "tb " << std::setw(2) << iTimeBin << ":";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++) {
os << std::setw(5) << (mcm.getDataRaw(iChannel, iTimeBin) >> mcm.mgkAddDigits);
}
os << std::endl;
}
os << "----- Filtered ADC data (10+2 bit) -----" << std::endl;
os << "ch ";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++)
os << std::setw(4) << iChannel
<< ((~mcm.getZeroSupressionMap(iChannel) != 0) ? "!" : " ");
os << std::endl;
for (int iTimeBin = 0; iTimeBin < mcm.getNumberOfTimeBins(); iTimeBin++) {
os << "tb " << std::setw(2) << iTimeBin << ":";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++) {
os << std::setw(4) << (mcm.getDataFiltered(iChannel, iTimeBin))
<< (((mcm.getZeroSupressionMap(iChannel) & (1 << iTimeBin)) == 0) ? "!" : " ");
}
os << std::endl;
}
}
// ----- CFDAT output -----
else if (os.iword(TrapSimulator::mgkFormatIndex) == 1) {
int dest = 127;
int addrOffset = 0x2000;
int addrStep = 0x80;
for (int iTimeBin = 0; iTimeBin < mcm.getNumberOfTimeBins(); iTimeBin++) {
for (int iChannel = 0; iChannel < NADCMCM; iChannel++) {
os << std::setw(5) << 10
<< std::setw(5) << addrOffset + iChannel * addrStep + iTimeBin
<< std::setw(5) << (mcm.getDataFiltered(iChannel, iTimeBin))
<< std::setw(5) << dest << std::endl;
}
os << std::endl;
}
}
// ----- raw data ouptut -----
else if (os.iword(TrapSimulator::mgkFormatIndex) == 2) {
int bufSize = 300;
std::vector<uint32_t> buf;
buf.reserve(bufSize);
int bufLength = mcm.getRawStream(buf, 0);
for (int i = 0; i < bufLength; i++)
std::cout << "0x" << std::hex << buf[i] << std::dec << std::endl;
}
else {
os << "unknown format set" << std::endl;
}
return os;
}
void TrapSimulator::printFitRegXml(ostream& os) const
{
// print fit registres in XML format
bool tracklet = false;
for (int cpu = 0; cpu < 4; cpu++) {
if (mFitPtr[cpu] != 31)
tracklet = true;
}
const char* const aquote{R"(")"};
const char* const cmdid{R"(" cmdid="0">)"};
if (tracklet == true) {
os << "<nginject>" << std::endl;
os << "<ack roc=\"" << mDetector << cmdid << std::endl;
os << "<dmem-readout>" << std::endl;
os << "<d det=\"" << mDetector << "\">" << std::endl;
os << " <ro-board rob=\"" << mRobPos << "\">" << std::endl;
os << " <m mcm=\"" << mMcmPos << "\">" << std::endl;
for (int cpu = 0; cpu < 4; cpu++) {
os << " <c cpu=\"" << cpu << "\">" << std::endl;
if (mFitPtr[cpu] != 31) {
for (int adcch = mFitPtr[cpu]; adcch < mFitPtr[cpu] + 2; adcch++) {
if (adcch > 24) {
LOG(error) << "adcch going awol : " << adcch << " > 25";
}
os << " <ch chnr=\"" << adcch << "\">" << std::endl;
os << " <hits>" << mFitReg[adcch].mNhits << "</hits>" << std::endl;
os << " <q0>" << mFitReg[adcch].mQ0 << "</q0>" << std::endl;
os << " <q1>" << mFitReg[adcch].mQ1 << "</q1>" << std::endl;
os << " <sumx>" << mFitReg[adcch].mSumX << "</sumx>" << std::endl;
os << " <sumxsq>" << mFitReg[adcch].mSumX2 << "</sumxsq>" << std::endl;
os << " <sumy>" << mFitReg[adcch].mSumY << "</sumy>" << std::endl;
os << " <sumysq>" << mFitReg[adcch].mSumY2 << "</sumysq>" << std::endl;
os << " <sumxy>" << mFitReg[adcch].mSumXY << "</sumxy>" << std::endl;
os << " </ch>" << std::endl;
}
}
os << " </c>" << std::endl;
}
os << " </m>" << std::endl;
os << " </ro-board>" << std::endl;
os << "</d>" << std::endl;
os << "</dmem-readout>" << std::endl;
os << "</ack>" << std::endl;
os << "</nginject>" << std::endl;
}
}
void TrapSimulator::printTrackletsXml(ostream& os) const
{
// print tracklets in XML format
const char* const cmdid{R"(" cmdid="0">)"};
os << "<nginject>" << std::endl;
os << "<ack roc=\"" << mDetector << cmdid << std::endl;
os << "<dmem-readout>" << std::endl;
os << "<d det=\"" << mDetector << "\">" << std::endl;
os << " <ro-board rob=\"" << mRobPos << "\">" << std::endl;
os << " <m mcm=\"" << mMcmPos << "\">" << std::endl;
int pid, padrow, slope, offset;
for (int cpu = 0; cpu < 4; cpu++) {
if (mMCMT[cpu] == 0x10001000) {
pid = -1;
padrow = -1;
slope = -1;
offset = -1;
} else {
pid = (mMCMT[cpu] & 0xFF000000) >> 24;
padrow = (mMCMT[cpu] & 0xF00000) >> 20;
slope = (mMCMT[cpu] & 0xFE000) >> 13;
offset = (mMCMT[cpu] & 0x1FFF);
}
os << " <trk> <pid>" << pid << "</pid>"
<< " <padrow>" << padrow << "</padrow>"
<< " <slope>" << slope << "</slope>"
<< " <offset>" << offset << "</offset>"
<< "</trk>" << std::endl;
}
os << " </m>" << std::endl;
os << " </ro-board>" << std::endl;
os << "</d>" << std::endl;
os << "</dmem-readout>" << std::endl;
os << "</ack>" << std::endl;
os << "</nginject>" << std::endl;
}
void TrapSimulator::printAdcDatTxt(ostream& os) const
{
// print ADC data in text format (suitable as Modelsim stimuli)
os << "# MCM " << mMcmPos << " on ROB " << mRobPos << " in detector " << mDetector << std::endl;
for (int iTimeBin = 0; iTimeBin < mNTimeBin; iTimeBin++) {
for (int iChannel = 0; iChannel < NADCMCM; ++iChannel) {
os << std::setw(5) << (getDataRaw(iChannel, iTimeBin) >> mgkAddDigits);
}
os << std::endl;
}
}
void TrapSimulator::printAdcDatHuman(ostream& os) const
{
// print ADC data in human-readable format
os << "MCM " << mMcmPos << " on ROB " << mRobPos << " in detector " << mDetector << std::endl;
os << "----- Unfiltered ADC data (10 bit) -----" << std::endl;
os << "ch ";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++)
os << std::setw(5) << iChannel;
os << std::endl;
for (int iTimeBin = 0; iTimeBin < mNTimeBin; iTimeBin++) {
os << "tb " << std::setw(2) << iTimeBin << ":";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++) {
os << std::setw(5) << (getDataRaw(iChannel, iTimeBin) >> mgkAddDigits);
}
os << std::endl;
}
os << "----- Filtered ADC data (10+2 bit) -----" << std::endl;
os << "ch ";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++)
os << std::setw(4) << iChannel
<< ((~mZSMap[iChannel] != 0) ? "!" : " ");
os << std::endl;
for (int iTimeBin = 0; iTimeBin < mNTimeBin; iTimeBin++) {
os << "tb " << std::setw(2) << iTimeBin << ":";
for (int iChannel = 0; iChannel < NADCMCM; iChannel++) {
os << std::setw(4) << (getDataFiltered(iChannel, iTimeBin))
<< (((mZSMap[iChannel] & (1 << iTimeBin)) == 0) ? "!" : " ");
}
os << std::endl;
}
}
void TrapSimulator::printAdcDatXml(ostream& os) const
{
// print ADC data in XML format
const char* const cmdid{R"(" cmdid="0">)"};
os << "<nginject>" << std::endl;
os << "<ack roc=\"" << mDetector << cmdid << std::endl;
os << "<dmem-readout>" << std::endl;
os << "<d det=\"" << mDetector << "\">" << std::endl;
os << " <ro-board rob=\"" << mRobPos << "\">" << std::endl;
os << " <m mcm=\"" << mMcmPos << "\">" << std::endl;
for (int iChannel = 0; iChannel < NADCMCM; iChannel++) {
os << " <ch chnr=\"" << iChannel << "\">" << std::endl;
for (int iTimeBin = 0; iTimeBin < mNTimeBin; iTimeBin++) {
os << "<tb>" << mADCF[iChannel * mNTimeBin + iTimeBin] / 4 << "</tb>";
}
os << " </ch>" << std::endl;
}
os << " </m>" << std::endl;
os << " </ro-board>" << std::endl;
os << "</d>" << std::endl;
os << "</dmem-readout>" << std::endl;
os << "</ack>" << std::endl;
os << "</nginject>" << std::endl;
}
void TrapSimulator::printAdcDatDatx(ostream& os, bool broadcast, int timeBinOffset) const
{
// print ADC data in datx format (to send to FEE)
mTrapConfig->printDatx(os, 2602, 1, 0, 127); // command to enable the ADC clock - necessary to write ADC values to MCM
os << std::endl;
int addrOffset = 0x2000;
int addrStep = 0x80;
int addrOffsetEBSIA = 0x20;
for (int iTimeBin = 0; iTimeBin < mNTimeBin; iTimeBin++) {
for (int iChannel = 0; iChannel < NADCMCM; iChannel++) {
if ((iTimeBin < timeBinOffset) || (iTimeBin >= mNTimeBin + timeBinOffset)) {
if (broadcast == false)
mTrapConfig->printDatx(os, addrOffset + iChannel * addrStep + addrOffsetEBSIA + iTimeBin, 10, getRobPos(), getMcmPos());
else
mTrapConfig->printDatx(os, addrOffset + iChannel * addrStep + addrOffsetEBSIA + iTimeBin, 10, 0, 127);
} else {
if (broadcast == false)
mTrapConfig->printDatx(os, addrOffset + iChannel * addrStep + addrOffsetEBSIA + iTimeBin, (getDataFiltered(iChannel, iTimeBin - timeBinOffset) / 4), getRobPos(), getMcmPos());
else
mTrapConfig->printDatx(os, addrOffset + iChannel * addrStep + addrOffsetEBSIA + iTimeBin, (getDataFiltered(iChannel, iTimeBin - timeBinOffset) / 4), 0, 127);
}
}
os << std::endl;
}
}
void TrapSimulator::printPidLutHuman()
{
// print PID LUT in human readable format
unsigned int addrEnd = mgkDmemAddrLUTStart + mTrapConfig->getDmemUnsigned(mgkDmemAddrLUTLength, mDetector, mRobPos, mMcmPos) / 4; // /4 because each addr contains 4 values
unsigned int nBinsQ0 = mTrapConfig->getDmemUnsigned(mgkDmemAddrLUTnbins, mDetector, mRobPos, mMcmPos);
std::cout << "nBinsQ0: " << nBinsQ0 << std::endl;
std::cout << "LUT table length: " << mTrapConfig->getDmemUnsigned(mgkDmemAddrLUTLength, mDetector, mRobPos, mMcmPos) << std::endl;
if (nBinsQ0 > 0) {
for (unsigned int addr = mgkDmemAddrLUTStart; addr < addrEnd; addr++) {
unsigned int result;
result = mTrapConfig->getDmemUnsigned(addr, mDetector, mRobPos, mMcmPos);
std::cout << addr << " # x: " << ((addr - mgkDmemAddrLUTStart) % ((nBinsQ0) / 4)) * 4 << ", y: " << (addr - mgkDmemAddrLUTStart) / (nBinsQ0 / 4)
<< " # " << ((result >> 0) & 0xFF)
<< " | " << ((result >> 8) & 0xFF)
<< " | " << ((result >> 16) & 0xFF)
<< " | " << ((result >> 24) & 0xFF) << std::endl;
}
}
}
void TrapSimulator::setNTimebins(int ntimebins)
{
// Reallocate memory if a change in the number of timebins
// is needed (should not be the case for real data)
LOG(fatal) << "setNTimebins(" << ntimebins << ") not implemented as we can no longer change the size of the ADC array";
if (!checkInitialized())
return;
mNTimeBin = ntimebins;
// for( int iAdc = 0 ; iAdc < NADCMCM; iAdc++ ) {
// delete [] mADCR[iAdc];
// delete [] mADCF[iAdc];
// mADCR[iAdc] = new int[mNTimeBin];
// mADCF[iAdc] = new int[mNTimeBin];
// }
}
bool TrapSimulator::loadMCM(int det, int rob, int mcm)
{
// loads the ADC data as obtained from the digitsManager for the specified MCM.
// This method is meant for rare execution, e.g. in the visualization. When called
// frequently use SetData(...) instead.
LOG(fatal) << "no longer implemented, this is left incase script calls it that I have not found yet.";
return false;
}
void TrapSimulator::noiseTest(int nsamples, int mean, int sigma, int inputGain, int inputTail)
{
// This function can be used to test the filters.
// It feeds nsamples of ADC values with a gaussian distribution specified by mean and sigma.
// The filter chain implemented here consists of:
// Pedestal -> Gain -> Tail
// With inputGain and inputTail the input to the gain and tail filter, respectively,
// can be chosen where
// 0: noise input
// 1: pedestal output
// 2: gain output
// The input has to be chosen from a stage before.
// The filter behaviour is controlled by the TRAP parameters from TrapConfig in the
// same way as in normal simulation.
// The functions produces four histograms with the values at the different stages.
if (!checkInitialized())
return;
std::string nameInputGain;
std::string nameInputTail;
switch (inputGain) {
case 0:
nameInputGain = "Noise";
break;
case 1:
nameInputGain = "Pedestal";
break;
default:
LOG(error) << "Undefined input to tail cancellation filter";
return;
}
switch (inputTail) {
case 0:
nameInputTail = "Noise";
break;
case 1:
nameInputTail = "Pedestal";
break;
case 2:
nameInputTail = "Gain";
break;
default:
LOG(error) << "Undefined input to tail cancellation filter";
return;
}
TH1F* h = new TH1F("noise", "Gaussian Noise;sample;ADC count",
nsamples, 0, nsamples);
TH1F* hfp = new TH1F("ped", "Noise #rightarrow Pedestal filter;sample;ADC count", nsamples, 0, nsamples);
TH1F* hfg = new TH1F("gain",
(nameInputGain + "#rightarrow Gain;sample;ADC count").c_str(),
nsamples, 0, nsamples);
TH1F* hft = new TH1F("tail",
(nameInputTail + "#rightarrow Tail;sample;ADC count").c_str(),
nsamples, 0, nsamples);
h->SetStats(false);
hfp->SetStats(false);
hfg->SetStats(false);
hft->SetStats(false);
for (int i = 0; i < nsamples; i++) {
int value; // ADC count with noise (10 bit)
int valuep; // pedestal filter output (12 bit)
int valueg; // gain filter output (12 bit)
int valuet; // tail filter value (12 bit)
value = (int)gRandom->Gaus(mean, sigma); // generate noise with gaussian distribution
h->SetBinContent(i, value);
valuep = filterPedestalNextSample(1, 0, ((int)value) << 2);
if (inputGain == 0)
valueg = filterGainNextSample(1, ((int)value) << 2);
else
valueg = filterGainNextSample(1, valuep);
if (inputTail == 0)
valuet = filterTailNextSample(1, ((int)value) << 2);
else if (inputTail == 1)
valuet = filterTailNextSample(1, valuep);
else
valuet = filterTailNextSample(1, valueg);
hfp->SetBinContent(i, valuep >> 2);
hfg->SetBinContent(i, valueg >> 2);
hft->SetBinContent(i, valuet >> 2);
}
TCanvas* c = new TCanvas;
c->Divide(2, 2);
c->cd(1);
h->Draw();
c->cd(2);
hfp->Draw();
c->cd(3);
hfg->Draw();
c->cd(4);
hft->Draw();
}
bool TrapSimulator::checkInitialized() const
{
//
// Check whether object is initialized
//
// if (!mInitialized)
// LOG(debug4) << "TrapSimulator is not initialized but function other than Init() is called.";
return mInitialized;
}
void TrapSimulator::print(int choice) const
{
// Prints the data stored and/or calculated for this TRAP
// The output is controlled by option which can be any bitpattern defined in the header
// PRINTRAW - prints raw ADC data
// PRINTFILTERED - prints filtered data
// PRINTHITS - prints detected hits
// PRINTTRACKLETS - prints found tracklets
// The later stages are only meaningful after the corresponding calculations
// have been performed.
// Codacy wont let us use string choices, as it says we must use string::starts_with() instead of string::find()
if (!checkInitialized())
return;
LOG(info) << "MCM " << mMcmPos << " on ROB " << mRobPos << " in detector " << mDetector;
//std::string opt = option;
if ((choice & PRINTRAW) != 0 || (choice & PRINTFILTERED) != 0) {
std::cout << *this;
}
if ((choice & PRINTDETECTED) != 0) {
LOG(info) << "Found " << mNHits << " hits:";
for (int iHit = 0; iHit < mNHits; iHit++) {
LOG(info) << "Hit " << std::setw(3) << iHit << " in timebin " << std::setw(2) << mHits[iHit].mTimebin << ", ADC " << std::setw(2) << mHits[iHit].mChannel << " has charge " << std::setw(3) << mHits[iHit].mQtot << " and position " << mHits[iHit].mYpos;
}
}
if ((choice & PRINTFOUND) != 0) {
LOG(info) << "Found Tracklets:";
for (int iTrkl = 0; iTrkl < mTrackletArray.size(); iTrkl++) {
LOG(info) << "tracklet " << iTrkl << ": 0x" << hex << std::setw(16) << mTrackletArray[iTrkl].getTrackletWord();
}
}
}
void TrapSimulator::draw(int choice, int index)
{
// Plots the data stored in a 2-dim. timebin vs. ADC channel plot.
// The choice selects what data is plotted and is enumated in the header.
// PLOTRAW - plot raw data (default)
// PLOTFILTERED - plot filtered data (meaningless if R is specified)
// In addition to the ADC values:
// PLOTHITS - plot hits
// PLOTTRACKLETS - plot tracklets
if (!checkInitialized())
return;
TFile* rootfile = new TFile("trdtrackletplots.root", "UPDATE");
TCanvas* c1 = new TCanvas(Form("canvas_%i_%i:%i:%i_%i", index, mDetector, mRobPos, mMcmPos, (int)mTrackletArray.size()));
TH2F* hist = new TH2F(Form("mcmdata_%i", index),
Form("Data of MCM %i on ROB %i in detector %i ", mMcmPos, mRobPos, mDetector),
NADCMCM,
-0.5,
NADCMCM - 0.5,
getNumberOfTimeBins(),
-0.5,
getNumberOfTimeBins() - 0.5);
hist->GetXaxis()->SetTitle("ADC Channel");
hist->GetYaxis()->SetTitle("Timebin");
hist->SetStats(false);
TH2F* histfiltered = new TH2F(Form("mcmdataf_%i", index),
Form("Data of MCM %i on ROB %i in detector %i filtered", mMcmPos, mRobPos, mDetector),
NADCMCM,
-0.5,
NADCMCM - 0.5,
getNumberOfTimeBins(),
-0.5,
getNumberOfTimeBins() - 0.5);
histfiltered->GetXaxis()->SetTitle("ADC Channel");
histfiltered->GetYaxis()->SetTitle("Timebin");
if ((choice & PLOTRAW) != 0) {
for (int iTimeBin = 0; iTimeBin < mNTimeBin; iTimeBin++) {
for (int iAdc = 0; iAdc < NADCMCM; iAdc++) {
hist->SetBinContent(iAdc + 1, iTimeBin + 1, mADCR[iAdc * mNTimeBin + iTimeBin] >> mgkAddDigits);
}
}
hist->Draw("COLZ");
} else {
for (int iTimeBin = 0; iTimeBin < mNTimeBin; iTimeBin++) {
for (int iAdc = 0; iAdc < NADCMCM; iAdc++) {
histfiltered->SetBinContent(iAdc + 1, iTimeBin + 1, mADCF[iAdc * mNTimeBin + iTimeBin] >> mgkAddDigits);
}
}
histfiltered->Draw("COLZ");
}
if ((choice & PLOTHITS) != 0) {
TGraph* grHits = new TGraph();
for (int iHit = 0; iHit < mNHits; iHit++) {
grHits->SetPoint(iHit,
mHits[iHit].mChannel + 1 + mHits[iHit].mYpos / 256.,
mHits[iHit].mTimebin);
}
grHits->Draw("*");
}
if ((choice & PLOTTRACKLETS) != 0) {
TLine* trklLines = new TLine[4];
LOG(info) << "Tracklet start for index : " << index;
if (mTrackletArray.size() > 0)
LOG(info) << "Tracklet : for " << mTrackletArray[0].getDetector() << "::" << mTrackletArray[0].getROB() << " : " << mTrackletArray[0].getMCM();
else
LOG(info) << "Tracklet : for trackletarray size of zero ";
for (int iTrkl = 0; iTrkl < mTrackletArray.size(); iTrkl++) {
Tracklet trkl = mTrackletArray[iTrkl];
float padWidth = 0.635 + 0.03 * (mDetector % 6);
float offset = padWidth / 256. * ((((((mRobPos & 0x1) << 2) + (mMcmPos & 0x3)) * 18) << 8) - ((18 * 4 * 2 - 18 * 2 - 3) << 7)); // revert adding offset in FitTracklet
//TODO replace the 18, 4 3 and 7 with constants for readability
int ndrift = mTrapConfig->getDmemUnsigned(mgkDmemAddrNdrift, mDetector, mRobPos, mMcmPos) >> 5;
float slope = 0;
if (ndrift)
slope = trkl.getdY() * 140e-4 / ndrift;
int t0 = mTrapConfig->getTrapReg(TrapConfig::kTPFS, mDetector, mRobPos, mMcmPos);
int t1 = mTrapConfig->getTrapReg(TrapConfig::kTPFE, mDetector, mRobPos, mMcmPos);
trklLines[iTrkl].SetX1((offset - (trkl.getY() - slope * t0)) / padWidth); // ??? sign?
trklLines[iTrkl].SetY1(t0);
trklLines[iTrkl].SetX2((offset - (trkl.getY() - slope * t1)) / padWidth); // ??? sign?
trklLines[iTrkl].SetY2(t1);
trklLines[iTrkl].SetLineColor(2);
trklLines[iTrkl].SetLineWidth(2);
LOG(info) << "Tracklet " << iTrkl << ": y = " << trkl.getY() << ", dy = " << (((float)trkl.getdY()) * 140e-4) << " offset : " << offset << "for a det:rob:mcm combo of : " << mDetector << ":" << mRobPos << ":" << mMcmPos;
LOG(info) << "Tracklet " << iTrkl << ": x1,y1,x2,y2 :: " << trklLines[iTrkl].GetX1() << "," << trklLines[iTrkl].GetY1() << "," << trklLines[iTrkl].GetX2() << "," << trklLines[iTrkl].GetY2();
LOG(info) << "Tracklet " << iTrkl << ": t0 : " << t0 << ", t1 " << t1 << ", padwidth:" << padWidth << ", slope:" << slope << ", ndrift:" << ndrift << " which comes from : " << mTrapConfig->getDmemUnsigned(mgkDmemAddrNdrift, mDetector, mRobPos, mMcmPos) << " shifted 5 to the right ";
trklLines[iTrkl].Draw();
}
LOG(info) << "Tracklet end ...";
}
c1->Write();
rootfile->Close();
}
void TrapSimulator::setData(int adc, const ArrayADC& data, std::vector<o2::MCCompLabel>& labels)
{
//
// Store ADC data into array of raw data
//
if (!checkInitialized())
return;
if (adc < 0 || adc >= NADCMCM) {
// LOG(error) << "Error: ADC " << adc << " is out of range (0 .. " << NADCMCM - 1 << ")";
return;
}
// LOG(info) << "Set Data : Det:Rob:MCM::"<< getDetector() <<":" <<getRobPos()<<":"<<getMcmPos() << " t:"<< mNTimeBin;
for (int it = 0; it < mNTimeBin; it++) {
mADCR[adc * mNTimeBin + it] = ((int)(data[it]) << mgkAddDigits) + (mgAddBaseline << mgkAddDigits);
mADCF[adc * mNTimeBin + it] = ((int)(data[it]) << mgkAddDigits) + (mgAddBaseline << mgkAddDigits);
// mADCR[adc * mNTimeBin + it] = (unsigned int)(data[it]);
// mADCF[adc * mNTimeBin + it] = (unsigned int)(data[it]);
// LOG(info) << data[it] <<" at "<< adc << "*" << mNTimeBin <<"+"<<it <<"="<< adc*mNTimeBin+it << " with data :"<<mADCR[adc*mNTimeBin+it] << ":" << mADCF[adc*mNTimeBin+it];
}
mDataIsSet = true;
mADCFilled |= (1 << adc);
//for (auto& tmplabel : labels) mADCLabels[adc].push_back(tmplabel);
LOG(debug) << "setting data labels incoming of : " << labels.size() << " with adc of " << adc;
mADCLabels[adc] = labels;
LOG(debug) << "setting data labels incoming of : " << labels.size() << " with adc of " << adc << " now mADCLabels[adc] size is : " << mADCLabels[adc].size();
}
void TrapSimulator::setData(int adc, int it, int data)
{
//
// Store ADC data into array of raw data
// This time enter it element by element.
//
if (!checkInitialized())
return;
if (adc < 0 || adc >= NADCMCM) {
LOG(error) << "Error: ADC " << adc << " is out of range (0 .. " << NADCMCM - 1 << ")";
return;
}
mADCR[adc * mNTimeBin + it] = data << mgkAddDigits;
mADCF[adc * mNTimeBin + it] = data << mgkAddDigits;
mADCFilled |= (1 << adc);
}
void TrapSimulator::setBaselines()
{
//This function exists as in the old simulator, when data was fed into the trapsim, it was done via the whole adc block for the mcm.
//if the data was zero or out of spec, the baselines were added
//we now add it by singular ADC, so there are adc channels that never get touched.
//this fixes that.
LOG(info) << "ENTER: " << __FILE__ << ":" << __func__ << ":" << __LINE__ << " for " << mDetector << ":" << mRobPos << ":" << mMcmPos;
//loop over all adcs.
for (int adc = 0; adc < NADCMCM; adc++) {
LOG(info) << "Setting baselines for adc: " << adc << " of " << mDetector << ":" << mRobPos << ":" << mMcmPos << hex << mADCFilled << " if of : " << (mADCFilled & (1 << adc));
if ((mADCFilled & (1 << adc)) == 0) { // adc is empty by construction of mADCFilled.
LOG(info) << "past if Setting baselines for adc: " << adc << " of " << mDetector << ":" << mRobPos << ":" << mMcmPos;
for (int timebin = 0; timebin < mNTimeBin; timebin++) {
mADCR[adc * mNTimeBin + timebin] = mTrapConfig->getTrapReg(TrapConfig::kFPNP, mDetector, mRobPos, mMcmPos) + (mgAddBaseline << mgkAddDigits);
mADCF[adc * mNTimeBin + timebin] = mTrapConfig->getTrapReg(TrapConfig::kTPFP, mDetector, mRobPos, mMcmPos) + (mgAddBaseline << mgkAddDigits);
}
}
}
}
void TrapSimulator::setDataPedestal(int adc)
{
//
// Store ADC data into array of raw data
//
if (!checkInitialized())
return;
if (adc < 0 || adc >= NADCMCM) {
return;
}
for (int it = 0; it < mNTimeBin; it++) {
mADCR[adc * mNTimeBin + it] = mTrapConfig->getTrapReg(TrapConfig::kFPNP, mDetector, mRobPos, mMcmPos) + (mgAddBaseline << mgkAddDigits);
mADCF[adc * mNTimeBin + it] = mTrapConfig->getTrapReg(TrapConfig::kTPFP, mDetector, mRobPos, mMcmPos) + (mgAddBaseline << mgkAddDigits);
}
}
bool TrapSimulator::getHit(int index, int& channel, int& timebin, int& qtot, int& ypos, float& y) const
{
// retrieve the MC hit information (not available in TRAP hardware)
if (index < 0 || index >= mNHits)
return false;
channel = mHits[index].mChannel;
timebin = mHits[index].mTimebin;
qtot = mHits[index].mQtot;
ypos = mHits[index].mYpos;
y = (float)((((((mRobPos & 0x1) << 2) + (mMcmPos & 0x3)) * 18) << 8) - ((18 * 4 * 2 - 18 * 2 - 1) << 7) -
(channel << 8) - ypos) *
(0.635 + 0.03 * (mDetector % 6)) / 256.0;
return true;
}
//TrapSimualtor::Hit& TrapSimulator::getHit(int index, int& channel, int& timebin, int& qtot, int& ypos, float& y, std::vector<o2::MCCompLabel> &labels) const
//{
// LOG(fatal) << "for now its not implemented";
// return nullptr;
//}
int TrapSimulator::getCol(int adc)
{
//
// Return column id of the pad for the given ADC channel
//
if (!checkInitialized())
return -1;
int col = mFeeParam->getPadColFromADC(mRobPos, mMcmPos, adc);
if (col < 0 || col >= NCOLUMN)
return -1;
else
return col;
}
//TODO figure why I could not get span to work here
int TrapSimulator::packData(std::vector<uint32_t>& rawdata, uint32_t offset)
{
// return # of 32 bit words.
//
//given the, up to 3 tracklets, pack them according to the define data format.
//
// std::cout << "span size in packData is : " << rawdata.size() << std::endl;
//TODO this is left blank so that the dataformats etc. can come in a seperate PR
//to keep different work seperate.
uint32_t wordswritten = 0; // count the 32 bit words written;
// std::cout << &raw[offset] << std::endl;
// std::cout << raw.data() << std::endl;;
TrackletMCMHeader mcmhead;
int trackletcount = 0;
// TrackletMCMHeader mcmhead;
offset++;
std::array<TrackletMCMData, 3> tracklets{};
mcmhead.oneb = 1;
mcmhead.onea = 1;
mcmhead.padrow = ((mRobPos >> 1) << 2) | (mMcmPos >> 2);
int mcmcol = mMcmPos % NMCMROBINCOL + (mRobPos % 2) * NMCMROBINCOL;
int padcol = mcmcol * NCOLMCM + NCOLMCM + 1;
mcmhead.col = 1; //TODO check this, cant call FeeParam due to virtual function
LOG(debug) << "packing data with trackletarry64 size of : " << mTrackletArray64.size();
for (int i = 0; i < 3; i++) {
if (i < mTrackletArray64.size()) { // we have a tracklet
LOG(debug) << "we have a tracklet at i=" << i << " with trackletword 0x" << mTrackletArray64[i].getTrackletWord();
switch (i) {
case 0:
mcmhead.pid0 = ((mTrackletArray64[0].getQ2()) << 2) + ((mTrackletArray64[0].getQ1()) >> 5);
break; // all of Q2 and upper 2 bits of Q1.
case 1:
mcmhead.pid1 = ((mTrackletArray64[1].getQ2()) << 2) + ((mTrackletArray64[1].getQ1()) >> 5);
break; // all of Q2 and upper 2 bits of Q1.
case 2:
mcmhead.pid2 = ((mTrackletArray64[2].getQ2()) << 2) + ((mTrackletArray64[2].getQ1()) >> 5);
break; // all of Q2 and upper 2 bits of Q1.
}
tracklets[i].checkbit = 1;
uint32_t tmppid = mTrackletArray64[i].getPID() & 0x3fff;
LOG(debug) << "tracklet i " << i << " has pid of 0x" << std::hex << mTrackletArray64[i].getPID() << " and we are going to put in 0x" << std::hex << tmppid;
LOG(debug) << mTrackletArray64[i];
tracklets[i].pid = mTrackletArray64[i].getPID() & 0x3fff; // the bottom 12 bits of the pid
tracklets[i].slope = mTrackletArray64[i].getSlope();
tracklets[i].pos = mTrackletArray64[i].getPosition();
tracklets[i].checkbit = 0;
trackletcount++;
} else { // else we dont have a tracklet so mark it off in the header.
switch (i) {
case 1:
mcmhead.pid1 = 0xff;
LOG(debug) << "setting mcmhead pid1 to 0xff with tracklet array size of " << mTrackletArray64[i];
break; // set the pid to ff to signify not there
case 2:
mcmhead.pid2 = 0xff;
break; // set the pid to maximal to signify not there (6bits).
}
}
}
// raw.push_back((uint32_t)mcmhead)
LOG(debug) << "pushing back mcm head of 0x" << std::hex << mcmhead.word << " with trackletcount of : " << std::dec << trackletcount << ":-:" << wordswritten;
rawdata.push_back(mcmhead.word); //memcpy(&rawdata[wordswritten++], &mcmhead, sizeof(mcmhead));
wordswritten++;
for (int i = 0; i < trackletcount; i++) {
LOG(debug) << "pushing back mcmtrackletword of 0x" << std::hex << tracklets[i].word;
rawdata.push_back(tracklets[i].word); //memcpy(&rawdata[wordswritten++], &tracklets[i], sizeof(TrackletMCMData));
wordswritten++;
}
//display the headers written
if (debugheaders) {
LOG(info) << ">>>>> START DEBUG OUTPUT OF packData trackletcount:-:wordcount" << trackletcount << ":-:" << wordswritten;
o2::trd::printTrackletMCMHeader(mcmhead);
o2::trd::printTrackletMCMData(tracklets[0]);
if (trackletcount > 1)
o2::trd::printTrackletMCMData(tracklets[1]);
if (trackletcount > 2)
o2::trd::printTrackletMCMData(tracklets[2]);
LOG(info) << "<<<<< END DEBUG OUTPUT OF packData";
}
//must produce between 2 and 4 words ... 1 and 3 tracklets.
// assert(wordswritten<5);
// assert(wordswritten>1);
LOG(debug) << "now to leave pack data after passing asserts with wordswritten = " << wordswritten;
return wordswritten; // in units of 32 bits.
}
int TrapSimulator::getRawStream(std::vector<uint32_t>& buf, uint32_t offset, unsigned int iEv) const
{
//
// Produce raw data stream from this MCM and put in buf
// Returns number of words filled, or negative value
// with -1 * number of overflowed words
//
if (!checkInitialized())
return 0;
unsigned int x;
unsigned int mcmHeader = 0;
unsigned int adcMask = 0;
int numberWordsWritten = 0; // Number numberOverFlowWordsWritten written words
int numberOverFlowWordsWritten = 0; // Number numberOverFlowWordsWritten overflowed words
int rawVer = mFeeParam->getRAWversion();
std::vector<int> adc; //TODO come back an avoid a copy.
if (!checkInitialized())
return 0;
if (mTrapConfig->getTrapReg(TrapConfig::kEBSF, mDetector, mRobPos, mMcmPos) != 0) // store unfiltered data
adc = mADCR;