00001 #include "TPedestalExtractor.h"
00002
00003 #include <stdio.h>
00004 #include <stdexcept>
00005 #include "Riostream.h"
00006
00007 TPedestalExtractor::TPedestalExtractor( Int_t nRocs, Int_t nNxs, Int_t nChannels, Int_t nAdcBins, Double_t adcMin, Double_t adcMax ) {
00008 if( nRocs < 0 || nNxs < 0 || nChannels < 0 || nAdcBins < 0 || adcMin > adcMax ) {
00009 printf("In TPedestalExtractor::TPedestalExtractor: Invalid argument\n");
00010
00011 return;
00012 }
00013 fNRocs = nRocs;
00014 fNNxs = nNxs;
00015 fNChannels = nChannels;
00016 fHistograms = new THistogramType[IndexMax() + 1];
00017 fPedestals = new Double_t[IndexMax() + 1];
00018 fWidths = new Double_t[IndexMax() + 1];
00019 for( Int_t index = 0; index <= IndexMax(); index++ ) {
00020 Char_t name[25];
00021 Int_t iRoc;
00022 Int_t iNx;
00023 Int_t iChannel;
00024 IndexDec( index, iRoc, iNx, iChannel );
00025 sprintf( name, "pedRoc%dNx%dCh%d", iRoc, iNx, iChannel );
00026 fHistograms[index].SetName( name );
00027 fHistograms[index].SetTitle( name );
00028 fHistograms[index].SetBins( nAdcBins, adcMin, adcMax );
00029 fPedestals[index] = fkInvalidPedestalValue;
00030 fWidths[index] = fkInvalidWidthValue;
00031 }
00032 }
00033
00034 TPedestalExtractor::~TPedestalExtractor() {
00035 if( fPedestals ) delete [] fPedestals;
00036 if( fWidths ) delete [] fWidths;
00037 if( fHistograms ) delete [] fHistograms;
00038 }
00039
00040 void TPedestalExtractor::IndexDec( Int_t index, Int_t& iRoc, Int_t& iNx, Int_t& iChannel ) {
00041 if( index > IndexMax() || index < 0 ) {
00042 printf("In TPedestalExtractor::DecodeIndex: Invalid argument\n");
00043 return;
00044 }
00045 iRoc = index / ( fNNxs * fNChannels );
00046 iNx = ( index % ( fNNxs * fNChannels ) ) / fNChannels;
00047 iChannel = index % fNChannels;
00048 }
00049
00050 bool TPedestalExtractor::TestValid( Int_t iRoc, Int_t iNx, Int_t iChannel ) {
00051 if( iRoc < 0 || iRoc >= fNRocs ) {
00052 printf( "Error in TPedestalExtractor: Trying to access ROC %d, valid ROCs: 0..%d\n", iRoc, fNRocs - 1 );
00053
00054 return false;
00055 }
00056 if( iNx < 0 || iNx >= fNNxs ) {
00057 printf( "Error in TPedestalExtractor: Trying to access nX %d, valid nXs: 0..%d\n", iNx, fNNxs - 1 );
00058
00059 return false;
00060 }
00061 if( iChannel < 0 || iChannel >= fNChannels ) {
00062 printf( "Error in TPedestalExtractor: Trying to access Channel %d, valid Channels: 0..%d\n", iChannel, fNChannels - 1 );
00063
00064 return false;
00065 }
00066 else return true;
00067 }
00068
00069 void TPedestalExtractor::AddHit( Int_t iRoc, Int_t iNx, Int_t iChannel, Int_t adc) {
00070 if( TestValid( iRoc, iNx, iChannel ) )
00071 fHistograms[IndexEnc( iRoc, iNx, iChannel )].Fill(adc);
00072
00073 }
00074
00075 void TPedestalExtractor::Extract() {
00076 for( Int_t iHist = 0; iHist <= IndexMax(); iHist++ ) {
00077 Double_t pedestal;
00078 Double_t width;
00079 TH1* hist = &(fHistograms[iHist]);
00080 Extract( hist, pedestal, width );
00081 fPedestals[iHist] = pedestal;
00082 fWidths[iHist] = width;
00083 }
00084 }
00085
00086 void TPedestalExtractor::Extract( TH1* hist, Double_t& pedestal, Double_t& width ) {
00087 pedestal = fkInvalidPedestalValue;
00088 width = fkInvalidWidthValue;
00089 if( ! hist ) {
00090 printf("In TPedestalExtractor::Extract: Invalid argument\n");
00091 return;
00092
00093 }
00094 if( hist->GetEntries() < fkEntriesMin ) {
00095 return;
00096 }
00097 Int_t medianBin = 1;
00098 Double_t* integral = hist->GetIntegral();
00099 while( integral[medianBin + 1] <= 0.5 ) {
00100 medianBin++;
00101 }
00102 pedestal = hist->GetXaxis()->GetBinCenter( medianBin );
00103 }
00104
00105
00106 void TPedestalExtractor::Reset() {
00107 for( Int_t index = 0; index <= IndexMax(); index++ ) {
00108 fHistograms[index].Reset();
00109 }
00110 }
00111
00112 Double_t TPedestalExtractor::GetPedestal( Int_t iRoc, Int_t iNx, Int_t iChannel ) {
00113 if( TestValid( iRoc, iNx, iChannel ) )
00114 return fPedestals[IndexEnc( iRoc, iNx, iChannel )];
00115
00116 return -1000.;
00117 }
00118
00119 TH1* TPedestalExtractor::GetHistogram( Int_t iRoc, Int_t iNx, Int_t iChannel ) {
00120 if( TestValid( iRoc, iNx, iChannel ) )
00121 return new THistogramType( fHistograms[IndexEnc( iRoc, iNx, iChannel )] );
00122
00123 return 0;
00124 }
00125
00126 void TPedestalExtractor::Print()
00127 {
00128 for( Int_t index = 0; index <= IndexMax(); index++ ) {
00129 printf( "idx=%4d ped=%f\n", index, fPedestals[index] );
00130 }
00131 }
00132
00133