forked from eutelescope/eutelescope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEUTelPedestalNoiseProcessor.cc
1916 lines (1561 loc) · 83.5 KB
/
EUTelPedestalNoiseProcessor.cc
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
// Author Antonio Bulgheroni, INFN <mailto:antonio.bulgheroni@gmail.com>
// Version $Id$
/*
* This source code is part of the Eutelescope package of Marlin.
* You are free to use this source files for your own development as
* long as it stays in a public research context. You are not
* allowed to use it for commercial purpose. You must put this
* header with author names in all development based on this file.
*
*/
// eutelescope includes ".h"
#include "EUTelExceptions.h"
#include "EUTelRunHeaderImpl.h"
#include "EUTelEventImpl.h"
#include "EUTelPedestalNoiseProcessor.h"
#include "EUTelHistogramManager.h"
#include "EUTELESCOPE.h"
// marlin includes ".h"
#include "marlin/Processor.h"
#include "marlin/Exceptions.h"
#include "marlin/Global.h"
#if defined(USE_AIDA) || defined(MARLIN_USE_AIDA)
// aida includes <.h>
#include <marlin/AIDAProcessor.h>
#include <AIDA/IHistogramFactory.h>
#include <AIDA/IHistogram1D.h>
#include <AIDA/IHistogram2D.h>
#include <AIDA/IProfile2D.h>
#include <AIDA/ITree.h>
#endif
// lcio includes <.h>
#include <lcio.h>
#include <UTIL/LCTOOLS.h>
#include <UTIL/CellIDEncoder.h>
#include <UTIL/CellIDDecoder.h>
#include <UTIL/LCTime.h>
#include <IMPL/LCEventImpl.h>
#include <IMPL/TrackerRawDataImpl.h>
#include <IMPL/TrackerDataImpl.h>
// system includes <>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <memory>
#include <cstdlib>
#include <limits>
#include <algorithm>
using namespace std;
using namespace lcio;
using namespace marlin;
using namespace eutelescope;
// definition of static members mainly used to name histograms
#if defined(USE_AIDA) || defined(MARLIN_USE_AIDA)
std::string EUTelPedestalNoiseProcessor::_pedeDistHistoName = "PedeDist";
std::string EUTelPedestalNoiseProcessor::_noiseDistHistoName = "NoiseDist";
std::string EUTelPedestalNoiseProcessor::_commonModeHistoName = "CommonMode";
std::string EUTelPedestalNoiseProcessor::_pedeMapHistoName = "PedeMap";
std::string EUTelPedestalNoiseProcessor::_noiseMapHistoName = "NoiseMap";
std::string EUTelPedestalNoiseProcessor::_statusMapHistoName = "StatusMap";
std::string EUTelPedestalNoiseProcessor::_tempProfile2DName = "TempProfile2D";
std::string EUTelPedestalNoiseProcessor::_fireFreqHistoName = "FiringFrequency";
std::string EUTelPedestalNoiseProcessor::_aPixelHistoName = "APixelHisto";
#endif
EUTelPedestalNoiseProcessor::EUTelPedestalNoiseProcessor () :Processor("EUTelPedestalNoiseProcessor") {
// modify processor description
_description =
"EUTelPedestalNoiseProcessor computes the pedestal and noise values of a pixel detector";
vector< string > rawDataCollectionNameVecExample;
rawDataCollectionNameVecExample.push_back( "rawdata" );
// first of all we need to register the input collection
registerInputCollections (LCIO::TRACKERRAWDATA, "RawDataCollectionNameVec",
"Input raw data collection",
_rawDataCollectionNameVec, rawDataCollectionNameVecExample );
// register compulsory parameters
registerProcessorParameter ("CalculationAlgorithm",
"Select the algorithm for pede/noise calculation",
_pedestalAlgo,
string (EUTELESCOPE::MEANRMS));
registerProcessorParameter("CommonModeAlgorithm",
"Select the algorithm for the common mode calculation. Possible values are:\n"
" FullFrame: all pixels in the frame are averaged\n"
" RowWise: pixels are averaged line by line",
_commonModeAlgo, string( "FullFrame" ));
registerProcessorParameter ("NoOfCMIteration",
"Number of common mode suppression iterations",
_noOfCMIterations, static_cast < int >(1));
registerProcessorParameter ("HitRejectionCut",
"Threshold for rejection of hit pixel (SNR units)",
_hitRejectionCut, static_cast < float >(4));
registerProcessorParameter ("MaxNoOfRejectedPixels",
"Maximum allowed number of rejected pixels per event",
_maxNoOfRejectedPixels,
static_cast < int >(1000));
registerProcessorParameter("MaxNoOfRejectedPixelPerRow",
"Maximum allowed number of rejected pixels per row (only with RowWise)",
_maxNoOfRejectedPixelPerRow,
static_cast < int > (25) );
registerProcessorParameter("MaxNoOfSkippedRow",
"Maximum allowed number of skipped rows (only with RowWise)",
_maxNoOfSkippedRow,
static_cast< int > ( 15 ) );
// new names for the pixel masking algorithms
// since v00-00-09, the user can select multiple bad pixel
// algorithms. The _badPixelAlgo has been changed into a
// _badPixelAlgoVec.
vector< string > badPixelAlgoVecExample;
badPixelAlgoVecExample.push_back( "NoiseDistribution" );
badPixelAlgoVecExample.push_back( "DeadPixel" );
registerProcessorParameter("BadPixelMaskingAlgorithm",
"Select the algorithm for bad pixel masking. Possible values are:\n"
" NoiseDistribution: removing pixels with noise above PixelMaskUpperNoiseCut in sigma unit\n"
" AbsoluteNoiseValue: removing pixels with noise above PixelMaskUpperAbsNoiseCut in ADC value\n"
" DeadPixel: removing pixels with noise below PixelMaskLowerAbsNoiseCut in ADC value\n"
" AbsolutePedeValue: removing pixels having pedestal too low or high using PixelMaskUpperAbsPedeCut and PixelMaskLowerAbsPedeCut",
_badPixelAlgoVec, badPixelAlgoVecExample);
registerProcessorParameter ("PixelMaskUpperNoiseCut",
"Upper threshold for bad pixel identification using NoiseDistribution",
_pixelMaskUpperNoiseCut, static_cast < float >(3.5));
registerProcessorParameter ("PixelMaskUpperAbsNoiseCut",
"Upper threshold for bad pixel identification using NoiseDistribution",
_pixelMaskUpperAbsNoiseCut, static_cast < float >(3.5));
registerProcessorParameter ("PixelMaskLowerAbsNoiseCut",
"Lower threshold for bad pixel identification using DeadPixel",
_pixelMaskLowerAbsNoiseCut, static_cast < float >(0.2));
registerProcessorParameter ("PixelMaskUpperAbsPedeCut",
"Upper threshold for bad pixel identification using AbsolutePedeValue",
_pixelMaskUpperAbsPedeCut, static_cast< float > ( 15 ) );
registerProcessorParameter ("PixelMaskLowerAbsPedeCut",
"Lower threshold for bad pixel identification using AbsolutePedeValue",
_pixelMaskLowerAbsPedeCut, static_cast< float > ( -15 ) );
registerProcessorParameter("PixelMaskMaxFiringFrequency",
"This is the maximum allowed firing % frequency, being 0.1% the Gaussian limit\n"
"Used only during the additional masking loop",
_maxFiringFreq, static_cast< float > ( 0.2 ) ) ;
registerOptionalParameter ("AdditionalMaskingLoop",
"Perform an additional loop for bad pixel masking",
_additionalMaskingLoop, static_cast<bool> ( true ) );
registerOptionalParameter ("HitRejectionPreLoop",
"Perform a fast first loop to improve the efficiency of hit rejection",
_preLoopSwitch, static_cast< bool > ( true ) ) ;
registerProcessorParameter ("FirstEvent",
"First event for pedestal calculation",
_firstEvent, static_cast < int > (0));
registerProcessorParameter ("LastEvent",
"Last event for pedestal calculation",
_lastEvent, static_cast < int > (-1));
registerProcessorParameter ("OutputPedeFile","The filename (w/o .slcio) to store the pedestal file",
_outputPedeFileName , string("outputpede"));
registerProcessorParameter ("ASCIIOutputSwitch","Set to true if the pedestal should also be saved as ASCII files",
_asciiOutputSwitch, static_cast< bool > ( true ) );
registerProcessorParameter("HistoInfoFileName", "This is the name of the histogram information file",
_histoInfoFileName, string( "histoinfo.xml" ) );
// now the optional parameters
registerOptionalParameter ("PedestalCollectionName",
"Pedestal collection name",
_pedestalCollectionName, string ("pedestalDB"));
registerOptionalParameter ("NoiseCollectionName",
"Noise collection name", _noiseCollectionName,
string ("noiseDB"));
registerOptionalParameter ("StatusCollectionName",
"Status collection name",
_statusCollectionName, string ("statusDB"));
_histogramSwitch = true;
}
void EUTelPedestalNoiseProcessor::init () {
// this method is called only once even when the rewind is active
// usually a good idea to
printParameters ();
// set to zero the run and event counters
_iRun = 0;
_iEvt = 0;
// set the pedestal flag to true and the loop counter to zero
_doPedestal = true;
// set the geometry ready switch to false
_isGeometryReady = false;
// set the loop counter
if ( _preLoopSwitch ) _iLoop = -1;
else _iLoop = 0;
if ( _pedestalAlgo == EUTELESCOPE::MEANRMS ) {
// reset the temporary arrays
_tempPede.clear ();
_tempNoise.clear ();
_tempEntries.clear ();
}
#ifndef MARLIN_USE_AIDA
_histogramSwitch = false;
if ( _pedestalAlgo == EUTELESCOPE::AIDAPROFILE ) {
streamlog_out ( ERROR0 ) << "The " << EUTELESCOPE::AIDAPROFILE
<< " algorithm cannot be applied since Marlin is not using AIDA" << endl
<< " Algorithm changed to " << EUTELESCOPE::MEANRMS << endl;
_pedestalAlgo = EUTELESCOPE::MEANRMS;
}
#endif
if ( _preLoopSwitch ) {
_maxValuePos.clear();
_maxValue.clear();
_minValuePos.clear();
_maxValue.clear();
}
// reset all the final arrays
_pedestal.clear();
_noise.clear();
_status.clear();
// set all the bad pixel masking switches
setBadPixelAlgoSwitches();
// reset the skip event list
_skippedEventList.clear();
_nextEventToSkip = _skippedEventList.begin();
}
void EUTelPedestalNoiseProcessor::processRunHeader (LCRunHeader * rdr) {
_detectorName = rdr->getDetectorName();
// to make things easier re-cast the input header to the EUTelRunHeaderImpl
auto_ptr<EUTelRunHeaderImpl> runHeader ( new EUTelRunHeaderImpl(rdr)) ;
runHeader->addProcessor( type() );
// increment the run counter
++_iRun;
// make some test on parameters
if ( ( _pedestalAlgo != EUTELESCOPE::MEANRMS ) &&
( _pedestalAlgo != EUTELESCOPE::AIDAPROFILE)
) {
throw InvalidParameterException(string("_pedestalAlgo cannot be " + _pedestalAlgo));
}
// the user can decide to limit the pedestal calculation on a
// sub range of events for many reasons. The most common one is that
// a run is started with the beam off and some hundreds of events
// are taken on purpose before switching the beam on. In this way
// the same file contains both pedestal and data, with pedestal
// events within a specific event window.
//
// From the Marlin steering file the best way the user has to select
// this range is using the _firstEvent and _lastEvent parameter of
// the processor it self.
// There is another variable that can influence this behavior and it
// is the global MaxRecordNumber. Being global, of course it is
// dominant with respect to the local _lastEvent setting. Once more,
// if the EORE is found before the _lastEvent than finalize method
// is called anyway.
//
int maxRecordNumber = Global::parameters->getIntVal("MaxRecordNumber");
streamlog_out ( DEBUG4 ) << "Event range for pedestal calculation is from " << _firstEvent << " to " << _lastEvent
<< "\nMaxRecordNumber from the global section is " << maxRecordNumber << endl;
// check if the user wants an additional loop to remove the bad pixels
int additionalLoop = 0;
if ( _additionalMaskingLoop ) additionalLoop = 1;
if ( _lastEvent == -1 ) {
// the user didn't select an upper limit for the event range, so
// we don't know on how many events the calculation should be done
//
// if the global MaxRecordNumber has been set, so warn the user
// that the procedure could be wrong due to a too low number of
// records.
if ( maxRecordNumber != 0 ) {
streamlog_out ( WARNING2 ) << "The MaxRecordNumber in the Global section of the steering file has been set to "
<< maxRecordNumber << ".\n"
<< "This means that in order to properly perform the pedestal calculation the maximum allowed number of events is "
<< maxRecordNumber / ( _noOfCMIterations + 1 + additionalLoop ) << ".\n"
<< "Let's hope it is correct and try to continue." << endl;
}
} else {
// ok we know on how many events the calculation should be done.
// we can compare this number with the maxRecordNumber if
// different from 0
if ( maxRecordNumber != 0 ) {
if ( (_lastEvent - _firstEvent) * ( _noOfCMIterations + 1 + additionalLoop ) > maxRecordNumber ) {
streamlog_out ( ERROR4 ) << "The pedestal calculation should be done on " << _lastEvent - _firstEvent
<< " times " << _noOfCMIterations + 1 << " iterations = "
<< (_lastEvent - _firstEvent) * ( _noOfCMIterations + 1 + additionalLoop ) << " records.\n"
<< "The global variable MarRecordNumber is limited to " << maxRecordNumber << endl;
throw InvalidParameterException("MaxRecordNumber");
}
}
}
if ( _iLoop == 0 ) {
// write the current header to the output condition file
LCWriter * lcWriter = LCFactory::getInstance()->createLCWriter();
try {
lcWriter->open(_outputPedeFileName, LCIO::WRITE_NEW);
} catch (IOException& e) {
cerr << e.what() << endl;
return;
}
LCRunHeaderImpl * lcHeader = new LCRunHeaderImpl;
EUTelRunHeaderImpl * newHeader = new EUTelRunHeaderImpl (lcHeader);
newHeader->lcRunHeader()->setRunNumber(runHeader->lcRunHeader()->getRunNumber());
newHeader->lcRunHeader()->setDetectorName(runHeader->lcRunHeader()->getDetectorName());
newHeader->setHeaderVersion(runHeader->getHeaderVersion());
newHeader->setDataType(runHeader->getDataType());
newHeader->setDateTime();
newHeader->setDAQHWName(runHeader->getDAQHWName());
newHeader->setDAQHWVersion(runHeader->getDAQHWVersion());
newHeader->setDAQSWName(runHeader->getDAQSWName());
newHeader->setDAQSWVersion(runHeader->getDAQSWVersion());
newHeader->setNoOfEvent(runHeader->getNoOfEvent());
newHeader->addProcessor(name());
lcWriter->writeRunHeader(lcHeader);
delete newHeader;
delete lcHeader;
lcWriter->close();
}
}
void EUTelPedestalNoiseProcessor::initializeGeometry( LCEvent * evt ) {
// starting from this version (v00-00-08-toto) the processor is
// accepting multiple input collections.
//
// The number of sensors will be the sum of the elements of all the
// input collections, while the detector boundaries are taken from
// each rawdata cellid.
_noOfDetector = 0;
_noOfDetectorVec.clear();
for ( size_t iCol = 0 ; iCol < _rawDataCollectionNameVec.size(); ++iCol ) {
try {
LCCollectionVec * collectionVec = dynamic_cast< LCCollectionVec * > ( evt->getCollection( _rawDataCollectionNameVec.at( iCol ) ) ) ;
_noOfDetector += collectionVec->size();
_noOfDetectorVec.push_back( collectionVec->size() );
// now to fill in the _minX, _maxX, _minY and _maxY vectors we
// have to go through all the collection elements.
CellIDDecoder< TrackerRawDataImpl > rawDataDecoder( collectionVec );
for ( size_t iDetector = 0; iDetector < collectionVec->size() ; ++iDetector ) {
TrackerRawDataImpl * rawData = dynamic_cast< TrackerRawDataImpl * > ( collectionVec->getElementAt( iDetector ) );
_minX.push_back( rawDataDecoder( rawData ) ["xMin"] ) ;
_maxX.push_back( rawDataDecoder( rawData ) ["xMax"] ) ;
_minY.push_back( rawDataDecoder( rawData ) ["yMin"] ) ;
_maxY.push_back( rawDataDecoder( rawData ) ["yMax"] ) ;
_orderedSensorIDVec.push_back( rawDataDecoder( rawData ) ["sensorID"] );
}
} catch (DataNotAvailableException& e) {
streamlog_out ( WARNING2 ) << "No input collection " << _rawDataCollectionNameVec.at( iCol ) << " is not available in the current event" << endl
<< "If this collection is not present in the input file, please remove it from the steering file" << endl
<< "Geometry initialization impossible, skipping the event" << endl;
_isGeometryReady = false;
throw SkipEventException(this);
}
}
_isGeometryReady = true;
}
void EUTelPedestalNoiseProcessor::processEvent (LCEvent * evt) {
EUTelEventImpl * eutelEvent = static_cast<EUTelEventImpl*> (evt);
EventType type = eutelEvent->getEventType();
if ( ! _isGeometryReady ) {
initializeGeometry(evt);
}
if ( type == kUNKNOWN ) {
streamlog_out ( WARNING2 ) << "Event number " << evt->getEventNumber() << " in run " << evt->getRunNumber()
<< " is of unknown type. Continue considering it as a normal Data Event." << endl;
}
if ( _iLoop == -1 ) preLoop( evt );
else if ( _iLoop == 0 ) firstLoop(evt);
else if ( _additionalMaskingLoop ) {
if ( _iLoop == _noOfCMIterations + 1 ) {
additionalMaskingLoop(evt);
} else {
otherLoop(evt);
}
} else {
otherLoop(evt);
}
}
void EUTelPedestalNoiseProcessor::check (LCEvent * /* evt */ ) {
// nothing to check here - could be used to fill check plots in reconstruction processor
}
void EUTelPedestalNoiseProcessor::end() {
int additionalLoop = 0;
if ( _additionalMaskingLoop ) additionalLoop = 1;
if ( _iLoop == _noOfCMIterations + 1 + additionalLoop ) {
streamlog_out ( MESSAGE4 ) << "Successfully finished" << endl;
} else {
streamlog_out ( ERROR4 ) << "Not all the iterations have been done because of a too MaxRecordNumber.\n"
<< "Try to increase it in the global section of the steering file." << endl;
exit(-1);
}
}
void EUTelPedestalNoiseProcessor::fillHistos() {
#if defined(USE_AIDA) || defined(MARLIN_USE_AIDA)
streamlog_out ( MESSAGE2 ) << "Filling final histograms " << endl;
string tempHistoName;
for (size_t iDetector = 0; iDetector < _noOfDetector; iDetector++) {
int iPixel = 0;
for (int yPixel = _minY[iDetector]; yPixel <= _maxY[iDetector]; yPixel++) {
for (int xPixel = _minX[iDetector]; xPixel <= _maxX[iDetector]; xPixel++) {
if ( _histogramSwitch ) {
tempHistoName = _statusMapHistoName + "_d" + to_string( _orderedSensorIDVec.at( iDetector ) ) + "_l" + to_string( _iLoop );
if ( AIDA::IHistogram2D * histo = dynamic_cast<AIDA::IHistogram2D*>(_aidaHistoMap[tempHistoName]) )
histo->fill(static_cast<double>(xPixel), static_cast<double>(yPixel), static_cast<double> (_status[iDetector][iPixel]));
else {
streamlog_out ( ERROR1 ) << "Not able to retrieve histogram pointer for " << tempHistoName
<< ".\nDisabling histogramming from now on " << endl;
_histogramSwitch = false;
}
}
if ( _status[iDetector][iPixel] == EUTELESCOPE::GOODPIXEL) {
if ( _histogramSwitch ) {
tempHistoName = _pedeDistHistoName + "_d" + to_string( _orderedSensorIDVec.at( iDetector ) ) + "_l" + to_string( _iLoop );
if ( AIDA::IHistogram1D * histo = dynamic_cast<AIDA::IHistogram1D*> (_aidaHistoMap[tempHistoName]) )
histo->fill(_pedestal[iDetector][iPixel]);
else {
streamlog_out ( ERROR1 ) << "Not able to retrieve histogram pointer for " << tempHistoName
<< ".\nDisabling histogramming from now on " << endl;
_histogramSwitch = false;
}
}
if ( streamlog::out.write< streamlog::DEBUG0 > () ) {
if ( (xPixel == 10) && (yPixel == 10 )) {
streamlog::out() << "Detector " << _orderedSensorIDVec.at( iDetector ) << " pedestal " << (_pedestal[iDetector][iPixel]) << endl;
}
}
if ( _histogramSwitch ) {
tempHistoName = _noiseDistHistoName + "_d" + to_string( _orderedSensorIDVec.at( iDetector ) ) + "_l" + to_string( _iLoop );
if ( AIDA::IHistogram1D * histo = dynamic_cast<AIDA::IHistogram1D*> (_aidaHistoMap[tempHistoName]) )
histo->fill(_noise[iDetector][iPixel]);
else {
streamlog_out ( ERROR1 ) << "Not able to retrieve histogram pointer for " << tempHistoName
<< ".\nDisabling histogramming from now on " << endl;
_histogramSwitch = false;
}
}
if ( _histogramSwitch ) {
tempHistoName = _pedeMapHistoName + "_d" + to_string( _orderedSensorIDVec.at( iDetector ) ) + "_l" + to_string( _iLoop );
if ( AIDA::IHistogram2D * histo = dynamic_cast<AIDA::IHistogram2D*>(_aidaHistoMap[tempHistoName]) )
histo-> fill(static_cast<double>(xPixel), static_cast<double>(yPixel), _pedestal[iDetector][iPixel]);
else {
streamlog_out ( ERROR1 ) << "Not able to retrieve histogram pointer for " << tempHistoName
<< ".\nDisabling histogramming from now on " << endl;
_histogramSwitch = false;
}
}
if ( _histogramSwitch ) {
tempHistoName = _noiseMapHistoName + "_d" + to_string( _orderedSensorIDVec.at( iDetector ) ) + "_l" + to_string( _iLoop );
if ( AIDA::IHistogram2D* histo = dynamic_cast<AIDA::IHistogram2D*>(_aidaHistoMap[tempHistoName]) )
histo->fill(static_cast<double>(xPixel), static_cast<double>(yPixel), _noise[iDetector][iPixel]);
else {
streamlog_out ( ERROR1 ) << "Not able to retrieve histogram pointer for " << tempHistoName
<< ".\nDisabling histogramming from now on " << endl;
_histogramSwitch = false;
}
}
}
++iPixel;
}
}
}
#else
if ( _iEvt == 0 ) streamlog_out ( MESSAGE4 ) << "No histogram produced because Marlin doesn't use AIDA" << endl;
#endif
}
void EUTelPedestalNoiseProcessor::maskBadPixel() {
// a global counter of bad pixels
vector<int > badPixelCounterVec( _noOfDetector, 0 );
if ( ( !_additionalMaskingLoop ) ||
( _iLoop < _noOfCMIterations + 1 )) {
// prepare vectors of thresholds
//
// thresholdNoiseDistVec is the vector of threshold for the
// NOISEDISTRIBUTION algorithm
vector<double > thresholdNoiseDistVec;
// thresholdAbsNoiseVec is the vector of threshold for the
// ABSOLUTENOISEVALUE algorithm
vector<double > thresholdAbsNoiseVec( _noOfDetector, _pixelMaskUpperAbsNoiseCut );
// thresholdDeadPixelVec is the vector of threshold for the
// DEADPIXEL algorithm
vector<double > thresholdDeadPixelVec( _noOfDetector, _pixelMaskLowerAbsNoiseCut );
// thresholdLowerAbsPedeVec is the vector of threshold for the
// ABSOLUTEPEDEVALUE algorithm
vector<double > thresholdLowerAbsPedeVec( _noOfDetector, _pixelMaskLowerAbsPedeCut );
vector<double > thresholdUpperAbsPedeVec( _noOfDetector, _pixelMaskUpperAbsPedeCut );
if ( _badPixelNoiseDistributionSwitch ) {
// to do it we need to know the mean value and the RMS of the noise
// vector.
for (size_t iDetector = 0; iDetector < _noOfDetector; iDetector++) {
double sumw = 0;
double sumw2 = 0;
double num = 0;
// begin a first loop on all pixel to calculate the masking threshold
for ( size_t iPixel = 0; iPixel < _status[iDetector].size(); iPixel++) {
if ( _status[iDetector][iPixel] == EUTELESCOPE::GOODPIXEL ) {
sumw += _noise[iDetector][iPixel];
sumw2 += pow(_noise[iDetector][iPixel],2);
++num;
}
}
double meanw = sumw / num;
double meanw2 = sumw2 / num;
double rms = sqrt( meanw2 - pow(meanw,2));
thresholdNoiseDistVec.push_back( meanw + (rms * _pixelMaskUpperNoiseCut) );
streamlog_out ( DEBUG4 ) << "Mean noise value is " << meanw << " ADC\n"
"RMS of noise is " << rms << " ADC\n"
"Masking threshold is set to " << thresholdNoiseDistVec[iDetector] << endl;
// now reloop on pixels to mask the bad out
for ( size_t iPixel = 0; iPixel < _status[iDetector].size(); ++iPixel ) {
if ( ( _noise[iDetector][iPixel] > thresholdNoiseDistVec[iDetector] ) &&
( _status[iDetector][iPixel] == EUTELESCOPE::GOODPIXEL ) ) {
// first of all make the pixel bad
_status[iDetector][iPixel] = EUTELESCOPE::BADPIXEL;
streamlog_out ( DEBUG0 ) << "Masking pixel number " << iPixel
<< " on detector " << _orderedSensorIDVec.at( iDetector )
<< " (" << _noise[iDetector][iPixel]
<< " > " << thresholdNoiseDistVec[iDetector] << ")" << endl;
badPixelCounterVec[iDetector]++;
}
}
}
}
if ( _badPixelAbsNoiseSwitch ) {
for ( size_t iDetector = 0 ; iDetector < _noOfDetector; iDetector++ ) {
for ( size_t iPixel = 0; iPixel < _status[iDetector].size(); ++iPixel ) {
if ( ( _noise[iDetector][iPixel] > thresholdAbsNoiseVec[iDetector] ) &&
( _status[iDetector][iPixel] == EUTELESCOPE::GOODPIXEL ) ) {
_status[iDetector][iPixel] = EUTELESCOPE::BADPIXEL;
streamlog_out ( DEBUG0 ) << "Masking pixel number " << iPixel
<< " on detector " << _orderedSensorIDVec.at( iDetector )
<< " (" << _noise[iDetector][iPixel]
<< " > " << thresholdAbsNoiseVec[iDetector] << ")" << endl;
badPixelCounterVec[iDetector]++;
}
}
}
}
if ( _badPixelDeadPixelSwitch ) {
for ( size_t iDetector = 0 ; iDetector < _noOfDetector; ++iDetector ) {
for ( size_t iPixel = 0; iPixel < _status[iDetector].size(); ++iPixel ) {
if ( ( _noise[iDetector][iPixel] < thresholdDeadPixelVec[iDetector] ) &&
( _status[iDetector][iPixel] == EUTELESCOPE::GOODPIXEL ) ) {
_status[iDetector][iPixel] = EUTELESCOPE::BADPIXEL;
streamlog_out ( DEBUG0 ) << "Masking pixel number " << iPixel
<< " on detector " << _orderedSensorIDVec.at( iDetector )
<< " (" << _noise[iDetector][iPixel]
<< " > " << thresholdDeadPixelVec[iDetector] << ")" << endl;
badPixelCounterVec[iDetector]++;
}
}
}
}
if ( _badPixelAbsPedeSwitch ) {
for ( size_t iDetector = 0; iDetector < _noOfDetector ; ++iDetector ) {
for ( size_t iPixel = 0; iPixel < _status[iDetector].size(); ++iPixel ) {
if ( ( ( _pedestal[iDetector][iPixel] > thresholdUpperAbsPedeVec[iDetector] ) ||
( _pedestal[iDetector][iPixel] < thresholdLowerAbsPedeVec[iDetector] ) ) &&
_status[iDetector][iPixel] == EUTELESCOPE::GOODPIXEL ) {
_status[iDetector][iPixel] = EUTELESCOPE::BADPIXEL;
streamlog_out( DEBUG0 ) << "Masking pixel number " << iPixel
<< " on detector " << _orderedSensorIDVec.at( iDetector ) << endl;
badPixelCounterVec[iDetector]++;
}
}
}
}
streamlog_out ( MESSAGE4 ) << "Masking summary after loop " << _iLoop << ": " << endl;
int totalBad = 0;
int total = 0;
for ( size_t iDetector = 0; iDetector < _noOfDetector; ++iDetector ) {
streamlog_out ( MESSAGE4 ) << "Detector ID " << setw(4) << _orderedSensorIDVec[ iDetector ] << " has "
<< setw(8) << badPixelCounterVec[iDetector] << " bad pixels (" << 100 * (1.0 * badPixelCounterVec[iDetector] )/ _status[iDetector].size()
<< "%). " << endl;
totalBad += badPixelCounterVec[iDetector];
total += _status[iDetector].size();
}
streamlog_out ( MESSAGE4 ) << "Total masked pixels = " << totalBad << " (" << 100 * (1.0 * totalBad) / total << "%). " << endl;
}
if ( ( _additionalMaskingLoop ) &&
( _iLoop == _noOfCMIterations + 1 )) {
// now masking relying on the additional loop
for ( size_t iDetector = 0 ; iDetector < _noOfDetector; iDetector++ ) {
for (unsigned int iPixel = 0; iPixel < _status[iDetector].size(); iPixel++) {
#if defined(USE_AIDA) || defined(MARLIN_USE_AIDA)
if ( _histogramSwitch ) {
string tempHistoName;
tempHistoName = _fireFreqHistoName + "_d" + to_string( _orderedSensorIDVec.at( iDetector ) ) + "_l" + to_string( _iLoop );
if ( AIDA::IHistogram1D * histo = dynamic_cast<AIDA::IHistogram1D*> ( _aidaHistoMap[ tempHistoName ] ))
histo->fill( (static_cast<double> ( _hitCounter[ iDetector ][ iPixel ] )) / _iEvt * 100. );
}
#endif
if ( static_cast< double > ( _hitCounter[ iDetector ][ iPixel ] ) / _iEvt * 100. > _maxFiringFreq ) {
_status[ iDetector ][ iPixel ] = EUTELESCOPE::BADPIXEL;
badPixelCounterVec[iDetector]++;
}
}
} // end loop on detector
streamlog_out ( MESSAGE4 ) << "Masking summary after loop " << _iLoop << ": " << endl;
int totalBad = 0;
int total = 0;
for ( size_t iDetector = 0; iDetector < _noOfDetector; ++iDetector ) {
streamlog_out ( MESSAGE4 ) << "Detector ID " << setw(4) << _orderedSensorIDVec[ iDetector ] << " has "
<< setw(8) << badPixelCounterVec[iDetector] << " bad pixels (" << 100 * (1.0 * badPixelCounterVec[iDetector] )/ _status[iDetector].size()
<< "%). " << endl;
totalBad += badPixelCounterVec[iDetector];
total += _status[iDetector].size();
}
streamlog_out ( MESSAGE4 ) << "Total masked pixels = " << totalBad << " (" << 100 * (1.0 * totalBad) / total << "%). " << endl;
}
}
void EUTelPedestalNoiseProcessor::preLoop( LCEvent * event ) {
// first re-cast to event to EUTelEventImpl for better access
EUTelEventImpl * evt = static_cast<EUTelEventImpl*> (event);
// In case this is the last event, or it is the kEORE, then rewind
// the data and return immediately
if ( evt->getEventType() == kEORE ) {
streamlog_out ( DEBUG4 ) << "EORE found: calling simpleRewind()." << endl;
_iLoop = 0;
simpleRewind();
return;
}
if ( ( _lastEvent != -1 ) && ( _iEvt >= _lastEvent ) ) {
streamlog_out ( DEBUG4 ) << "Looping limited by _lastEvent: calling simpleRewind()." << endl;
_preLoopSwitch = false;
_iLoop = 0;
simpleRewind();
return;
}
// in case this event is before the first to be considered, just
// skip it
if ( _iEvt < _firstEvent ) {
++_iEvt;
throw SkipEventException(this);
}
// make some initialization (only in the first event
if ( isFirstEvent() ) {
for ( size_t iCol = 0; iCol < _rawDataCollectionNameVec.size(); ++iCol ) {
try {
LCCollectionVec *collectionVec = dynamic_cast < LCCollectionVec * >(evt->getCollection( _rawDataCollectionNameVec.at( iCol ) ));
for ( size_t iDetector = 0; iDetector < collectionVec->size(); iDetector++) {
TrackerRawData * trackerRawData = dynamic_cast< TrackerRawData * > ( collectionVec->getElementAt( iDetector ) );
ShortVec adcValues = trackerRawData->getADCValues ();
// we have to initialize all the vectors only if this is the
// first collection
ShortVec maxValue ( adcValues.size(), numeric_limits< short >::min() );
ShortVec maxValuePos( adcValues.size(), -1 );
ShortVec minValue ( adcValues.size(), numeric_limits< short >::max() );
ShortVec minValuePos( adcValues.size(), -1 );
_maxValue.push_back ( maxValue );
_maxValuePos.push_back( maxValuePos );
_minValue.push_back ( minValue );
_minValuePos.push_back( minValuePos );
}
} catch (DataNotAvailableException& e) {
streamlog_out ( WARNING2 ) << "No input collection " << _rawDataCollectionNameVec.at( iCol ) << " is not available in the current event" << endl;
}
}
_isFirstEvent = false;
}
// here is the real begin
for ( size_t iCol = 0 ; iCol < _rawDataCollectionNameVec.size(); ++iCol ) {
try {
LCCollectionVec *collectionVec = dynamic_cast < LCCollectionVec * >(evt->getCollection( _rawDataCollectionNameVec.at( iCol ) ));
for ( size_t iDetector = 0 ; iDetector < collectionVec->size() ; iDetector++) {
size_t detectorOffset = ( iCol == 0 ) ? 0 : _noOfDetectorVec.at( iCol - 1 );
TrackerRawData *trackerRawData = dynamic_cast < TrackerRawData * >(collectionVec->getElementAt( iDetector ) );
ShortVec adcValues = trackerRawData->getADCValues ();
for ( size_t iPixel = 0; iPixel < adcValues.size(); ++iPixel ) {
short currentVal = adcValues[ iPixel ];
if ( currentVal > _maxValue[ iDetector + detectorOffset] [ iPixel ] ) {
_maxValue [ iDetector + detectorOffset ] [ iPixel ] = currentVal;
_maxValuePos[ iDetector + detectorOffset ] [ iPixel ] = _iEvt;
}
if ( currentVal < _minValue[ iDetector + detectorOffset ] [ iPixel ] ) {
_minValue [ iDetector + detectorOffset ] [ iPixel ] = currentVal;
_minValuePos[ iDetector + detectorOffset ] [ iPixel ] = _iEvt;
}
}
}
} catch (DataNotAvailableException& e) {
streamlog_out ( WARNING2 ) << "No input collection " << _rawDataCollectionNameVec.at( iCol ) << " is not available in the current event ("
<< event->getEventNumber() << ")" << endl;
}
}
++_iEvt;
}
void EUTelPedestalNoiseProcessor::firstLoop(LCEvent * event) {
EUTelEventImpl * evt = static_cast<EUTelEventImpl*> (event);
// do some checks in order to see if we have to continue or to stop
// with the processor.
//
// 1. we have to go immediately to the finalize if this is a EORE event
// 2. we have to go to the finalize if the user select an event
// range for pedestal calculation and the current event number is
// out of range
// 3. we have to skip this event if _iEvt is < than the first event
// selected for pedestal calculation
if ( evt->getEventType() == kEORE ) {
streamlog_out ( DEBUG4 ) << "EORE found: calling finalizeProcessor()." << endl;
finalizeProcessor( false );
}
if ( ( _lastEvent != -1 ) && ( _iEvt >= _lastEvent ) ) {
streamlog_out ( DEBUG4 ) << "Looping limited by _lastEvent: calling finalizeProcessor()." << endl;
finalizeProcessor( false );
}
if ( _iEvt < _firstEvent ) {
++_iEvt;
throw SkipEventException(this);
}
if ( isFirstEvent() ) {
for ( size_t iCol = 0; iCol < _rawDataCollectionNameVec.size() ; ++iCol ) {
try {
LCCollectionVec * collectionVec = dynamic_cast < LCCollectionVec * >(evt->getCollection (_rawDataCollectionNameVec.at( iCol ) ));
for ( size_t iDetector = 0 ; iDetector < collectionVec->size() ; ++iDetector ) {
// _tempPedestal, _tempNoise, _tempEntries are vector of vector.
// they have been already cleared in the init() method we are
// already looping on detectors, so we just need to push back a
// vector empty for each cycle
//
// _tempPedestal should be initialized with the adcValues, while
// _tempNoise and _tempEntries must be initialized to zero. Since
// adcValues is a vector of shorts, we need to copy each
// elements into _tempPedestal with a suitable re-casting
// get the TrackerRawData object from the collection for this detector
TrackerRawData *trackerRawData = dynamic_cast < TrackerRawData * >(collectionVec->getElementAt (iDetector));
ShortVec adcValues = trackerRawData->getADCValues ();
if ( _pedestalAlgo == EUTELESCOPE::MEANRMS ) {
// in the case of MEANRMS we have to deal with the standard
// vectors
ShortVec::iterator iter = adcValues.begin();
FloatVec tempDoubleVec;
while ( iter != adcValues.end() ) {
tempDoubleVec.push_back( static_cast< double > (*iter));
++iter;
}
_tempPede.push_back(tempDoubleVec);
// initialize _tempNoise and _tempEntries with all zeros and
// ones
_tempNoise.push_back(FloatVec(adcValues.size(), 0.));
_tempEntries.push_back(IntVec(adcValues.size(), 1));
} else if ( _pedestalAlgo == EUTELESCOPE::AIDAPROFILE ) {
#if defined(USE_AIDA) || defined(MARLIN_USE_AIDA)
// in the case of AIDAPROFILE we don't need any vectors since
// everything is done by the IProfile2D automatically
int iPixel = 0;
size_t detectorOffset = ( iCol == 0 ) ? 0 : _noOfDetectorVec.at( iCol - 1 );
stringstream ss;
ss << _tempProfile2DName << "_d" << _orderedSensorIDVec.at( iDetector + detectorOffset );
for (int yPixel = _minY[ iDetector + detectorOffset ]; yPixel <= _maxY[ iDetector + detectorOffset ]; yPixel++) {
for (int xPixel = _minX[ iDetector + detectorOffset ]; xPixel <= _maxX[ iDetector + detectorOffset ]; xPixel++) {
double temp = static_cast<double> (adcValues[iPixel]);
if ( AIDA::IProfile2D * profile = dynamic_cast<AIDA::IProfile2D*> (_aidaHistoMap[ss.str()]) ) {
profile ->fill(static_cast<double> (xPixel), static_cast<double> (yPixel), temp);
} else {
streamlog_out ( ERROR4 ) << "Irreversible error: " << ss.str() << " is not available. Sorry for quitting." << endl;
exit(-1);
}
++iPixel;
}
}
#endif
}
// the status vector can be initialize as well with all
// GOODPIXEL
_status.push_back(ShortVec(adcValues.size(), EUTELESCOPE::GOODPIXEL));
// if the user wants to add an additional loop on events to
// mask pixels singing too loud, so the corresponding counter
// vector should be reset
if ( _additionalMaskingLoop ) _hitCounter.push_back( ShortVec( adcValues.size(), 0) );
} // end of detector loop
} catch (DataNotAvailableException& e) {
streamlog_out ( WARNING2 ) << "No input collection " << _rawDataCollectionNameVec.at( iCol ) << " is not available in the current event" << endl;
}
}
bookHistos();
_isFirstEvent = false;
} else {
// this is when it is not the first event
for ( size_t iCol = 0 ; iCol < _rawDataCollectionNameVec.size() ; ++iCol ) {
try {
LCCollectionVec *collectionVec = dynamic_cast < LCCollectionVec * >(evt->getCollection (_rawDataCollectionNameVec.at( iCol ) ));
// after the firstEvent all temp vectors and the status one have
// the correct number of entries for both indexes
// loop on the detectors
for ( size_t iDetector = 0; iDetector < collectionVec->size() ; iDetector++) {
// get the TrackerRawData object from the collection for this plane
TrackerRawData *trackerRawData = dynamic_cast < TrackerRawData * >(collectionVec->getElementAt (iDetector));
ShortVec adcValues = trackerRawData->getADCValues ();
size_t detectorOffset = ( iCol == 0 ) ? 0 : _noOfDetectorVec.at( iCol - 1 );
if ( _pedestalAlgo == EUTELESCOPE::MEANRMS ) {
// start looping on all pixels
int iPixel = 0;
for (int yPixel = _minY[ iDetector + detectorOffset ]; yPixel <= _maxY[ iDetector + detectorOffset ]; yPixel++) {
for (int xPixel = _minX[ iDetector + detectorOffset ]; xPixel <= _maxX[ iDetector + detectorOffset ]; xPixel++) {
short currentVal = adcValues[iPixel];
bool use = true;
if ( _preLoopSwitch && ( ( _iEvt == _maxValuePos[ iDetector + detectorOffset ] [ iPixel ] ) ||
( _iEvt == _minValuePos[ iDetector + detectorOffset ] [ iPixel ] ) ) ) {
use = false;
}
if ( use ) {
_tempEntries[iDetector + detectorOffset][iPixel] = _tempEntries[iDetector + detectorOffset][iPixel] + 1;
_tempPede [iDetector + detectorOffset][iPixel] = ((_tempEntries[iDetector + detectorOffset][iPixel] - 1)
* _tempPede[iDetector + detectorOffset][iPixel]
+ currentVal) / _tempEntries[iDetector+detectorOffset][iPixel];
_tempNoise [iDetector + detectorOffset][iPixel] = sqrt(((_tempEntries[iDetector+detectorOffset][iPixel] - 1)
* pow(_tempNoise[iDetector+detectorOffset][iPixel],2)
+ pow( currentVal - _tempPede[iDetector+detectorOffset][iPixel], 2)) /
_tempEntries[iDetector+detectorOffset][iPixel]);
}
++iPixel;
} // end loop on xPixel
} // end loop on yPixel
} else if ( _pedestalAlgo == EUTELESCOPE::AIDAPROFILE ) {
#if defined(USE_AIDA) || defined(MARLIN_USE_AIDA)