00001 #include "PlotTPI_PedestalAndNoise.h"
00002 #include "RegisterModule.inc"
00003 #include "TGlobalData.h"
00004 #include "TSetupData.h"
00005 #include "ModulesOptions.h"
00006 #include "definitions.h"
00007
00008 #include "SetupNavigator.h"
00009
00010 #include <iostream>
00011 #include <cmath>
00012 #include <sstream>
00013 #include <fstream>
00014 using std::cout;
00015 using std::endl;
00016
00017 #include <TSQLiteServer.h>
00018 #include <TSQLiteResult.h>
00019 #include <TSQLiteRow.h>
00020
00021
00022 PlotTPI_PedestalAndNoise::PlotTPI_PedestalAndNoise(modules::options* opts):
00023 BaseModule("PlotTPI_PedestalAndNoise",opts),
00024 fNSamples(opts->GetInt("n_samples")),
00025 fExportSQL(opts->GetBool("export_sql", false)) {
00026 }
00027
00028 PlotTPI_PedestalAndNoise::~PlotTPI_PedestalAndNoise(){
00029 }
00030
00031
00032
00033
00034 int PlotTPI_PedestalAndNoise::BeforeFirstEntry(TGlobalData* gData,const TSetupData *setup){
00035
00036 if(Debug()){
00037 cout<<"-----PlotTPI_PedestalAndNoise::BeforeFirstEntry(): I'm debugging!"<<endl;
00038 }
00039
00040 return 0;
00041 }
00042
00043
00044
00045 int PlotTPI_PedestalAndNoise::ProcessEntry(TGlobalData* gData,const TSetupData *setup){
00046
00047
00048 std::string bankname, detname;
00049 PulseIslandList thePulseIslands;
00050 StringPulseIslandMap::const_iterator it;
00051
00052
00053
00054 for(it = gData->fPulseIslandToChannelMap.begin(); it != gData->fPulseIslandToChannelMap.end(); ++it){
00055
00056
00057 bankname = it->first;
00058 detname = setup->GetDetectorName(bankname);
00059
00060
00061 thePulseIslands = it->second;
00062 if (thePulseIslands.size() == 0) continue;
00063
00064
00065 if (fPedestalVsNoiseHistograms.find(detname) == fPedestalVsNoiseHistograms.end()) {
00066 int n_bits = TSetupData::Instance()->GetNBits(bankname);
00067 int x_min = 0;
00068 int x_max = std::pow(2, n_bits);
00069 int n_bins_x = 100;
00070
00071 int y_min = 0;
00072 int y_max = 200;
00073 int n_bins_y = (y_max - y_min)*5;
00074
00075 std::string histname = "fPedestalVsNoiseHistogram_" + detname;
00076 std::stringstream histtitle;
00077 histtitle << "Plot of the Pedestal vs Noise (mean and RMS of first " << fNSamples << " samples) in " << detname;
00078
00079 TH2D* histogram = new TH2D(histname.c_str(), histtitle.str().c_str(), n_bins_x,x_min,x_max, n_bins_y,y_min,y_max);
00080 histogram->GetXaxis()->SetTitle("Pedestal [ADC]");
00081 histogram->GetYaxis()->SetTitle("Noise [ADC]");
00082 fPedestalVsNoiseHistograms[detname] = histogram;
00083 }
00084
00085
00086
00087 TH2D* pedestal_vs_noise_histogram = fPedestalVsNoiseHistograms[detname];
00088
00089
00090 for (PulseIslandList::iterator pulseIter = thePulseIslands.begin(); pulseIter != thePulseIslands.end(); ++pulseIter) {
00091
00092 const std::vector<int>& theSamples = (*pulseIter)->GetSamples();
00093 int limit=fNSamples;
00094 if((int)theSamples.size() < fNSamples) limit=theSamples.size();
00095
00096 double sum = 0;
00097 for (int iSample = 0; iSample < limit; ++iSample) {
00098 sum += theSamples.at(iSample);
00099 }
00100 double mean = sum / limit;
00101
00102 double sum_of_deviations_squared = 0;
00103 for (int iSample = 0; iSample < limit; ++iSample) {
00104 sum_of_deviations_squared += (theSamples.at(iSample) - mean)*(theSamples.at(iSample) - mean);
00105 }
00106 double stdev = std::sqrt(sum_of_deviations_squared / limit);
00107
00108 pedestal_vs_noise_histogram->Fill(mean, stdev);
00109 }
00110 }
00111
00112 return 0;
00113 }
00114
00115
00116
00117
00118 int PlotTPI_PedestalAndNoise::AfterLastEntry(TGlobalData* gData,const TSetupData *setup){
00119
00120
00121 if(Debug()){
00122 cout<<"-----PlotTPI_PedestalAndNoise::AfterLastEntry(): I'm debugging!"<<endl;
00123 }
00124
00125 if (fExportSQL) {
00126
00127
00128 for (std::map<std::string, TH2D*>::iterator histIter = fPedestalVsNoiseHistograms.begin(); histIter != fPedestalVsNoiseHistograms.end(); ++histIter) {
00129 std::string detname = histIter->first;
00130 IDs::channel channel(detname);
00131
00132 TH2D* pedestal_vs_noise_histogram = histIter->second;
00133
00134 double pedestal = pedestal_vs_noise_histogram->GetMean(1);
00135 double noise = pedestal_vs_noise_histogram->GetMean(2);
00136
00137 SetupNavigator::Instance()->SetPedestalAndNoise(channel, pedestal, noise);
00138 }
00139 }
00140
00141 return 0;
00142 }
00143
00144
00145
00146
00147
00148 ALCAP_REGISTER_MODULE(PlotTPI_PedestalAndNoise,n_samples,export_sql);