00001
00002 #include "emcGainEvolution.h"
00003 #include "emcCalibrationDataHelper.h"
00004 #include "emcGainBaseLineCalculator.h"
00005 #include <iostream>
00006 #include <iomanip>
00007 #include "EmcIndexer.h"
00008 #include <cassert>
00009 #include <sstream>
00010 #include <cmath>
00011 #include "TGraphErrors.h"
00012 #include "TFile.h"
00013 #include "emcTimeStamp.h"
00014 #include "emcDataManager.h"
00015 #include "TGraphErrors.h"
00016
00017 namespace
00018 {
00019 std::string range(int run)
00020 {
00021 std::ostringstream rv;
00022 int r = static_cast<int>(run/1000);
00023 r *= 1000;
00024 rv << std::setw(10) << std::setfill('0')
00025 << r << "_"
00026 << std::setw(10) << std::setfill('0')
00027 << r+1000;
00028 return rv.str();
00029 }
00030 }
00031
00032
00033 emcGainEvolution::BaseLine
00034 emcGainEvolution::RunCheck::get(const std::string& details) const
00035 {
00036 std::map<std::string,BaseLine>::const_iterator it;
00037 it = fValues.find(details);
00038 if ( it != fValues.end() )
00039 {
00040 return it->second;
00041 }
00042 else
00043 {
00044 std::cerr << "RunCheck::get : details=" << details
00045 << " does not exist"
00046 << std::endl;
00047 return BaseLine();
00048 }
00049 }
00050
00051
00052 emcGainEvolution::emcGainEvolution(const char* outputfile)
00053 : fOutputFile(outputfile),
00054 fTimeOffset(5),
00055 fVerbose(0),
00056 fHistogrammingType(emcGainEvolution::kNone)
00057 {
00058 TFile f(outputfile,"RECREATE");
00059 if (!f.IsOpen())
00060 {
00061 throw;
00062 }
00063 f.Close();
00064 }
00065
00066
00067 emcGainEvolution::~emcGainEvolution()
00068 {
00069 std::map<std::string,TGraphErrors*>::iterator gip;
00070
00071 for ( gip = fGraphs.begin(); gip != fGraphs.end(); ++gip )
00072 {
00073 delete gip->second;
00074 gip->second=0;
00075 }
00076 }
00077
00078
00079 void
00080 emcGainEvolution::createGraph(const std::string& graphname,
00081 const std::vector<double>& x,
00082 const std::vector<double>& y,
00083 const std::vector<double>& xerr,
00084 const std::vector<double>& yerr)
00085 {
00086 std::map<std::string,TGraphErrors*>::iterator it =
00087 fGraphs.find(graphname);
00088
00089 if ( it != fGraphs.end() )
00090 {
00091 if ( verbose() )
00092 {
00093 std::cout << "emcGainEvolution::makeGraphs : graph "
00094 << graphname << " already exists. Erasing it."
00095 << std::endl;
00096 }
00097 delete it->second;
00098 }
00099
00100 fGraphs[graphname]
00101 = new TGraphErrors(x.size(),&x[0],&y[0],&xerr[0],&yerr[0]);
00102 fGraphs[graphname]->SetName(graphname.c_str());
00103 fGraphs[graphname]->SetTitle(graphname.c_str());
00104 }
00105
00106
00107 std::string
00108 emcGainEvolution::graphName(const std::string& process,
00109 const std::string& suffix)
00110 {
00111 std::string name = "g";
00112 name += process;
00113 for ( size_t i = 0; i < name.size(); ++i )
00114 {
00115 if (name[i]==':')
00116 {
00117 name[i]='_';
00118 }
00119 if (name[i]=='-')
00120 {
00121 name.erase(i,1);
00122 }
00123 }
00124 name += "_";
00125 name += suffix;
00126 return name;
00127 }
00128
00129
00130 void
00131 emcGainEvolution::setHistogrammingType(emcGainEvolution::EHistogrammingType htype)
00132 {
00133 if ( htype != emcGainEvolution::kNone )
00134 {
00135 emcGainBaseLineCalculator::histogramming(true);
00136 fHistogrammingType = htype;
00137 }
00138 else
00139 {
00140 emcGainBaseLineCalculator::histogramming(false);
00141 }
00142 }
00143
00144
00145 void
00146 emcGainEvolution::makeGraphs(const std::string& process)
00147 {
00148 makeGraph_absolute(process);
00149 makeGraph_wrt_production(process);
00150 makeGraph_distriShape(process);
00151 }
00152
00153
00154 void
00155 emcGainEvolution::makeGraph_distriShape(const std::string& process)
00156 {
00157
00158
00159 std::vector<double> x;
00160 std::vector<double> yrms;
00161 std::vector<double> yskewness;
00162 std::vector<double> ykurtosis;
00163
00164 for ( size_t i = 0; i < fRuns.size(); ++i )
00165 {
00166 int run = fRuns[i];
00167 std::map<int,RunCheck>::const_iterator it = fRunChecks.find(run);
00168 assert( it != fRunChecks.end() );
00169 BaseLine bl = it->second.get(process);
00170 x.push_back(run);
00171 yrms.push_back(bl.error());
00172 yskewness.push_back(bl.skewness());
00173 ykurtosis.push_back(bl.kurtosis());
00174 }
00175
00176 std::vector<double> zero(x.size());
00177
00178 createGraph(graphName(process,"rms"),x,yrms,zero,zero);
00179 createGraph(graphName(process,"skewness"),x,yskewness,zero,zero);
00180 createGraph(graphName(process,"kurtosis"),x,ykurtosis,zero,zero);
00181 }
00182
00183
00184 void
00185 emcGainEvolution::makeGraph_absolute(const std::string& process)
00186 {
00187
00188
00189
00190 std::string graphname = graphName(process,"abs");
00191
00192 std::vector<double> x;
00193 std::vector<double> xerr;
00194 std::vector<double> y;
00195 std::vector<double> yerr;
00196
00197 for ( size_t i = 0; i < fRuns.size(); ++i )
00198 {
00199 int run = fRuns[i];
00200 std::map<int,RunCheck>::const_iterator it = fRunChecks.find(run);
00201 assert( it != fRunChecks.end() );
00202 BaseLine bl = it->second.get(process);
00203 x.push_back(run);
00204 xerr.push_back(0.5);
00205 y.push_back(bl.value());
00206 yerr.push_back(bl.error());
00207 }
00208
00209 createGraph(graphname,x,y,xerr,yerr);
00210 }
00211
00212
00213 void
00214 emcGainEvolution::makeGraph_wrt_production(const std::string& process)
00215 {
00216
00217
00218
00219
00220 std::string graphname = graphName(process,"rprod");
00221
00222 std::vector<double> x;
00223 std::vector<double> xerr;
00224 std::vector<double> y;
00225 std::vector<double> yerr;
00226
00227 for ( size_t i = 0; i < fRuns.size(); ++i )
00228 {
00229 int run = fRuns[i];
00230 std::map<int,RunCheck>::const_iterator it = fRunChecks.find(run);
00231 assert( it != fRunChecks.end() );
00232 BaseLine bl = it->second.get(process);
00233 x.push_back(run);
00234 xerr.push_back(0.5);
00235 y.push_back(bl.diff());
00236 yerr.push_back(bl.diffError());
00237 }
00238
00239 createGraph(graphname,x,y,xerr,yerr);
00240 }
00241
00242
00243
00244
00245 void
00246 emcGainEvolution::print(std::ostream& os) const
00247 {
00248 std::set<std::string>::const_iterator it;
00249
00250 for ( size_t i = 0; i < fRuns.size(); ++i )
00251 {
00252 os << "RUN " << fRuns[i] << std::endl;
00253 std::map<int,RunCheck>::const_iterator mit = fRunChecks.find(fRuns[i]);
00254 assert(mit!=fRunChecks.end());
00255 for ( it = fProcesses.begin(); it != fProcesses.end(); ++it )
00256 {
00257 const std::string process = (*it);
00258 os << process << " -> ";
00259 BaseLine bl = mit->second.get(process);
00260 bl.print(os);
00261 }
00262 }
00263 }
00264
00265
00266 void
00267 emcGainEvolution::run()
00268 {
00269 emcTimeStamp ets;
00270 ets.SetSource(emcManageable::kDB_Pg);
00271 emcDataManager* dm = emcDataManager::GetInstance();
00272 PHTimeStamp ts;
00273
00274 for ( size_t i = 0; i < fRuns.size(); ++i )
00275 {
00276 int run = fRuns[i];
00277
00278 if ( verbose() )
00279 {
00280 std::cout << "Working on run " << run << std::endl;
00281 }
00282
00283 emcCalibrationDataHelper* cdh;
00284
00285 bool ok = dm->Read(ets,run);
00286 assert(ok==true);
00287 ts = ets.getTimeStamp().getTics();
00288 ts += fTimeOffset;
00289
00290 cdh = new emcCalibrationDataHelper(run,ts,
00291 false,
00292 emcManageable::kDB_Pg,
00293 "pbsc");
00294
00295 for ( size_t is = 0; is < fSectors.size(); ++is )
00296 {
00297 int isector = EmcIndexer::EmcSectorNumber(fSectors[is].c_str());
00298 assert(isector>=0 && isector<6);
00299
00300 const bool reallySilent = true;
00301
00302 float refvalue =
00303 cdh->getGainBaseLine(isector,"value",
00304 "0:x:ZS:AVOFRATIO:164777",reallySilent);
00305 float referror =
00306 cdh->getGainBaseLine(isector,"error",
00307 "0:x:ZS:AVOFRATIO:164777",reallySilent);
00308
00309 for ( size_t imd = 0; imd < fDetails.size(); ++imd )
00310 {
00311 const char* details[] = { "xmax","xmin","x" };
00312
00313
00314 for ( size_t ip = 0; ip < fPercent.size(); ++ip )
00315 {
00316 for ( size_t id = 0; id < 3; ++id )
00317 {
00318 float percent = fPercent[ip];
00319 std::ostringstream sdetails;
00320 sdetails << (static_cast<int>(floorf(percent*100)))
00321 << ":" << details[id]
00322 << ":" << fDetails[imd];
00323
00324
00325 float value =
00326 cdh->getGainBaseLine(isector,"value",
00327 sdetails.str().c_str(),
00328 reallySilent);
00329 float error =
00330 cdh->getGainBaseLine(isector,"error",
00331 sdetails.str().c_str(),
00332 reallySilent);
00333 float skewness =
00334 cdh->getGainBaseLine(isector,"skewness",
00335 sdetails.str().c_str(),
00336 reallySilent);
00337 float kurtosis =
00338 cdh->getGainBaseLine(isector,"kurtosis",
00339 sdetails.str().c_str(),
00340 reallySilent);
00341
00342 if ( verbose() > 1 )
00343 {
00344 std::cout << " " << fSectors[is]
00345 << " " << sdetails.str() << std::endl;
00346 }
00347
00348 std::ostringstream sprocess;
00349 sprocess << fSectors[is] << ":" << sdetails.str();
00350
00351 fProcesses.insert(sprocess.str());
00352
00353 float diff = value-refvalue;
00354 float diffError =
00355 sqrt(error*error+referror*referror)/diff;
00356
00357 if ( refvalue != 0)
00358 {
00359 diff = diff*100/refvalue;
00360 }
00361 else
00362 {
00363 diff = 0.0;
00364 }
00365
00366 diffError *= diff;
00367
00368 fRunChecks[run].set(sprocess.str(),
00369 BaseLine(value,error,
00370 diff,diffError,
00371 skewness,kurtosis));
00372 }
00373 }
00374 }
00375 }
00376 delete cdh;
00377 save(run);
00378 }
00379
00380 writeGraphs();
00381 }
00382
00383
00384 void
00385 emcGainEvolution::save(int run)
00386 {
00387 if ( fHistogrammingType == kNone )
00388 {
00389 return;
00390 }
00391
00392 std::ostringstream filename;
00393 TFile* f = 0;
00394
00395 std::string::size_type pos = fOutputFile.find_last_of('.');
00396 std::string mode = "UPDATE";
00397 bool make_subdirectories = true;
00398
00399 if ( fHistogrammingType == kOneFilePerRun )
00400 {
00401 filename << fOutputFile.substr(0,pos) << "." << run << ".root";
00402 mode = "RECREATE";
00403 make_subdirectories = false;
00404 }
00405 else if ( fHistogrammingType == kOneFilePer1KRange )
00406 {
00407 filename << fOutputFile.substr(0,pos) << "." << range(run) << ".root";
00408 mode = "UPDATE";
00409 }
00410 else if ( fHistogrammingType == kOneFileForAll )
00411 {
00412 filename << fOutputFile;
00413 mode = "UPDATE";
00414 }
00415 else
00416 {
00417 throw;
00418 }
00419
00420 std::cout << "<I> emcGainEvolution::save : saving results for run "
00421 << run << " into file "
00422 << filename.str() << std::endl;
00423
00424 f = new TFile(filename.str().c_str(),mode.c_str());
00425 emcGainBaseLineCalculator::write(make_subdirectories);
00426 f->Write();
00427 f->Close();
00428 delete f;
00429
00430
00431 emcGainBaseLineCalculator::deleteHistos();
00432 }
00433
00434
00435 void
00436 emcGainEvolution::writeGraphs()
00437 {
00438 std::cout << "<I> emcGainEvolution::writeGraphs to " << fOutputFile
00439 << std::endl;
00440
00441 std::set<std::string>::const_iterator it;
00442
00443 for ( it = fProcesses.begin(); it != fProcesses.end(); ++it )
00444 {
00445 std::string process = (*it);
00446
00447 makeGraphs(process);
00448 }
00449
00450 TFile f(fOutputFile.c_str(),"UPDATE");
00451
00452 std::map<std::string,TGraphErrors*>::const_iterator gip;
00453
00454 for ( gip = fGraphs.begin(); gip != fGraphs.end(); ++gip )
00455 {
00456 gip->second->Write();
00457 }
00458
00459 f.Write();
00460 f.Close();
00461
00462 std::cout << "done." << std::endl;
00463 }
00464