00001 #include "PlotPedestalAndNoise.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 PlotPedestalAndNoise::PlotPedestalAndNoise(modules::options* opts):
00023 BaseModule("PlotPedestalAndNoise",opts){
00024
00025
00026
00027
00028 fNSamples = opts->GetInt("n_samples", 5);
00029 fExportSQL = opts->GetBool("export_sql", false);
00030 }
00031
00032 PlotPedestalAndNoise::~PlotPedestalAndNoise(){
00033 }
00034
00035
00036
00037
00038 int PlotPedestalAndNoise::BeforeFirstEntry(TGlobalData* gData,const TSetupData *setup){
00039
00040 if(Debug()){
00041 cout<<"-----PlotPedestalAndNoise::BeforeFirstEntry(): I'm debugging!"<<endl;
00042 }
00043
00044 return 0;
00045 }
00046
00047
00048
00049 int PlotPedestalAndNoise::ProcessEntry(TGlobalData* gData,const TSetupData *setup){
00050
00051
00052 std::string bankname, detname;
00053 PulseIslandList thePulseIslands;
00054 StringPulseIslandMap::const_iterator it;
00055
00056
00057
00058 for(it = gData->fPulseIslandToChannelMap.begin(); it != gData->fPulseIslandToChannelMap.end(); ++it){
00059
00060
00061 bankname = it->first;
00062 detname = setup->GetDetectorName(bankname);
00063
00064
00065 thePulseIslands = it->second;
00066 if (thePulseIslands.size() == 0) continue;
00067
00068
00069 if (fPedestalVsNoiseHistograms.find(detname) == fPedestalVsNoiseHistograms.end()) {
00070 int n_bits = TSetupData::Instance()->GetNBits(bankname);
00071 int x_min = 0;
00072 int x_max = std::pow(2, n_bits);
00073 int n_bins_x = 100;
00074
00075 int y_min = 0;
00076 int y_max = 200;
00077 int n_bins_y = (y_max - y_min)*5;
00078
00079 std::string histname = "fPedestalVsNoiseHistogram_" + detname;
00080 std::stringstream histtitle;
00081 histtitle << "Plot of the Pedestal vs Noise (mean and RMS of first " << fNSamples << " samples) in " << detname;
00082
00083 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);
00084 histogram->GetXaxis()->SetTitle("Pedestal [ADC]");
00085 histogram->GetYaxis()->SetTitle("Noise [ADC]");
00086 fPedestalVsNoiseHistograms[detname] = histogram;
00087 }
00088
00089
00090
00091 TH2D* pedestal_vs_noise_histogram = fPedestalVsNoiseHistograms[detname];
00092
00093
00094 for (PulseIslandList::iterator pulseIter = thePulseIslands.begin(); pulseIter != thePulseIslands.end(); ++pulseIter) {
00095
00096 const std::vector<int>& theSamples = (*pulseIter)->GetSamples();
00097 int limit=fNSamples;
00098 if((int)theSamples.size() < fNSamples) limit=theSamples.size();
00099
00100 double sum = 0;
00101 for (int iSample = 0; iSample < limit; ++iSample) {
00102 sum += theSamples.at(iSample);
00103 }
00104 double mean = sum / limit;
00105
00106 double sum_of_deviations_squared = 0;
00107 for (int iSample = 0; iSample < limit; ++iSample) {
00108 sum_of_deviations_squared += (theSamples.at(iSample) - mean)*(theSamples.at(iSample) - mean);
00109 }
00110 double RMS = std::sqrt(sum_of_deviations_squared / limit);
00111
00112 pedestal_vs_noise_histogram->Fill(mean, RMS);
00113 }
00114 }
00115
00116 return 0;
00117 }
00118
00119
00120
00121
00122 int PlotPedestalAndNoise::AfterLastEntry(TGlobalData* gData,const TSetupData *setup){
00123
00124
00125 if(Debug()){
00126 cout<<"-----PlotPedestalAndNoise::AfterLastEntry(): I'm debugging!"<<endl;
00127 }
00128
00129 if (fExportSQL) {
00130
00131 TSQLiteServer* server = new TSQLiteServer("sqlite://pedestals-and-noises.sqlite");
00132
00133 std::stringstream query;
00134 std::string tablename = "pedestals_and_noises";
00135 if (server) {
00136
00137 query << "CREATE TABLE IF NOT EXISTS " << tablename << " (run INT, channel STRING, bank STRING, pedestal FLOAT, noise FLOAT)";
00138 server->Exec(query.str().c_str());
00139 query.str("");
00140
00141 int run_number = SetupNavigator::Instance()->GetRunNumber();
00142
00143
00144 for (std::map<std::string, TH2D*>::iterator histIter = fPedestalVsNoiseHistograms.begin(); histIter != fPedestalVsNoiseHistograms.end(); ++histIter) {
00145 std::string detname = histIter->first;
00146 std::string bankname = setup->GetBankName(detname);
00147 TH2D* pedestal_vs_noise_histogram = histIter->second;
00148
00149 double pedestal = pedestal_vs_noise_histogram->GetMean(1);
00150 double noise = pedestal_vs_noise_histogram->GetMean(2);
00151
00152
00153 query << "SELECT COUNT(*) FROM " << tablename << " WHERE run=" << run_number << " AND channel=\'" << detname << "\'";
00154 TSQLiteResult* result = (TSQLiteResult*) server->Query(query.str().c_str());
00155 TSQLiteRow* row = (TSQLiteRow*) result->Next();
00156 query.str("");
00157
00158 std::string n_table_entries = row->GetField(0);
00159
00160 if (n_table_entries == "0") {
00161
00162 query << "INSERT INTO " << tablename << " VALUES (" << run_number << ", \"" << detname << "\", \"" << bankname << "\", " << pedestal << ", " << noise << ");";
00163 server->Exec(query.str().c_str());
00164 query.str("");
00165 }
00166 else if (n_table_entries == "1") {
00167
00168 query << "UPDATE " << tablename << " SET run=" << run_number << ", bank=\'" << bankname << "\', pedestal=" << pedestal << ", noise=" << noise << " WHERE channel=\'" << detname << "\';";
00169 server->Exec(query.str().c_str());
00170 query.str("");
00171 }
00172
00173
00174 delete result;
00175 delete row;
00176 }
00177 }
00178 else {
00179 std::cout << "Error: Couldn't connect to SQLite database" << std::endl;
00180 }
00181 }
00182
00183 return 0;
00184 }
00185
00186
00187
00188
00189
00190 ALCAP_REGISTER_MODULE(PlotPedestalAndNoise,x_max);