00001 #include "emcCalibrationDataHelper.h"
00002 #include "emcTimeStamp.h"
00003 #include "emcDataManager.h"
00004 #include <vector>
00005 #include "EmcIndexer.h"
00006 #include "emcCalFEM.h"
00007 #include <cassert>
00008 #include <cstdlib>
00009 #include <sstream>
00010 #include "TFile.h"
00011 #include "TH1.h"
00012 #include "TH2.h"
00013
00014
00015
00016
00017
00018 int trim(const std::vector<float>& xx, const float& alpha,
00019 float& tmean, float& wmean,
00020 float& tvar, float& wvar,
00021 int& k,
00022 std::vector<float>& sx)
00023 {
00024
00025
00026
00027
00028
00029
00030
00031 std::vector<float> x;
00032
00033 for ( size_t i = 0; i < xx.size(); ++i )
00034 {
00035 if ( xx[i] > 0 )
00036 {
00037 x.push_back(xx[i]);
00038 }
00039 }
00040
00041 unsigned int n = x.size();
00042 if ( n < 2 )
00043 {
00044 return -1;
00045 }
00046
00047 if ( alpha < 0 || alpha >= 0.5 )
00048
00049
00050 {
00051 return -2;
00052 }
00053
00054
00055
00056
00057 sx = x;
00058 std::sort(sx.begin(), sx.end());
00059
00060
00061 k = static_cast<int>(floorf(alpha * n));
00062
00063 float sum = 0.0;
00064
00065 for ( size_t i = k; i < n - k; ++i )
00066 {
00067 sum += sx[i];
00068 }
00069
00070 tmean = sum / (n - 2 * k);
00071 wmean = (sum + k * sx[k] + k * sx[n - k - 1]) / n;
00072
00073 float t2 = 0.0;
00074 float w2 = 0.0;
00075
00076 for ( size_t i = k; i < n - k; ++i )
00077 {
00078 t2 += (sx[i] - tmean) * (sx[i] - tmean);
00079 w2 += (sx[i] - wmean) * (sx[i] - wmean);
00080 }
00081
00082 tvar = (
00083 t2 +
00084 k * (sx[k] - tmean) * (sx[k] - tmean) +
00085 k * (sx[n - k - 1] - tmean) * (sx[n - k - 1] - tmean)
00086 ) / (n * n);
00087
00088 wvar = (
00089 w2 +
00090 k * (sx[k] - wmean) * (sx[k] - wmean) +
00091 k * (sx[n - k - 1] - wmean) * (sx[n - k - 1] - wmean)
00092 ) / (n * n);
00093
00094 return 0;
00095 }
00096
00097
00098 void outliers(const std::vector<float>& v,
00099 float mean, float sigma,
00100 size_t& under, size_t& over)
00101 {
00102 under = over = 0;
00103
00104 for ( size_t i = 0; i < v.size(); ++i )
00105 {
00106 if ( v[i] < mean-sigma )
00107 {
00108 ++under;
00109 }
00110 if ( v[i] > mean+sigma )
00111 {
00112 ++over;
00113 }
00114 }
00115 }
00116
00117
00118 class Sector
00119 {
00120 public:
00121
00122 Sector(int number)
00123 {
00124 static const size_t ntowers = 2592;
00125 fNumber = number;
00126 fGt.resize(ntowers);
00127 fGt0.resize(ntowers);
00128 fAsym_gt_gt0.resize(ntowers);
00129 fRatio_gt_gt0.resize(ntowers);
00130
00131 fHgt = 0;
00132 fHgt0 = 0;
00133 fHratio = 0;
00134 fHasym = 0;
00135
00136 fHgt_yz = 0;
00137 fHgt0_yz = 0;
00138 fHratio_yz = 0;
00139 fHasym_yz = 0;
00140 }
00141
00142 void set(int ist, float gt, float gt0)
00143 {
00144 fGt[ist] = gt;
00145 fGt0[ist] = gt0;
00146 fRatio_gt_gt0[ist] = 0;
00147 if ( gt0 > 0 )
00148 {
00149 fRatio_gt_gt0[ist] = gt/gt0;
00150 }
00151 fAsym_gt_gt0[ist] = (gt-gt0)/(gt+gt0);
00152 }
00153
00154 float gt(int ist) { return fGt[ist]; }
00155 float gt0(int ist) { return fGt0[ist] ; }
00156 float ratio(int ist) { return fRatio_gt_gt0[ist]; }
00157 float asym(int ist) { return fAsym_gt_gt0[ist]; }
00158
00159 TH1* hgt() { histos(); return fHgt; }
00160 TH1* hgt0() { histos(); return fHgt0; }
00161 TH1* hratio() { histos(); return fHratio; }
00162 TH1* hasym() { histos(); return fHasym; }
00163
00164 TH2* hgt_yz() { histos(); return fHgt_yz; }
00165 TH2* hgt0_yz() { histos(); return fHgt0_yz; }
00166 TH2* hratio_yz() { histos(); return fHratio_yz; }
00167 TH2* hasym_yz() { histos(); return fHasym_yz; }
00168
00169 void print(std::ostream& out = std::cout) const
00170 {
00171 float tmean,wmean;
00172 float tvar,wvar;
00173 int k;
00174 std::vector<float> sorted;
00175 float percent = 0.0;
00176 trim(fGt,percent,tmean,wmean,tvar,wvar,k,sorted);
00177 out << "<gain(t)> = " << tmean << " +- " << sqrt(tvar) << std::endl;
00178 float tmean0,tvar0;
00179 trim(fGt0,percent,tmean0,wmean,tvar0,wvar,k,sorted);
00180 out << "<gain(t0)> = " << tmean0 << " +- " << sqrt(tvar0) << std::endl;
00181 out << "<gain(t)>/<gain(t0)> = " << tmean/tmean0 << " +- " << sqrt(tvar0*tvar0+tvar*tvar) << std::endl;
00182 trim(fRatio_gt_gt0,percent,tmean,wmean,tvar,wvar,k,sorted);
00183 out << "<gain(t)/gain(t0)> = " << tmean << " +- " << sqrt(tvar)
00184 << std::endl;
00185 }
00186
00187 void write(TFile& f)
00188 {
00189 histos();
00190 TDirectory* dir = gDirectory;
00191 f.cd();
00192 hgt()->Write();
00193 hgt0()->Write();
00194 hratio()->Write();
00195 hasym()->Write();
00196 hgt_yz()->Write();
00197 hgt0_yz()->Write();
00198 hratio_yz()->Write();
00199 hasym_yz()->Write();
00200 dir->cd();
00201 }
00202
00203 private:
00204
00205 void histos()
00206 {
00207 if ( !fHgt )
00208 {
00209 std::ostringstream name;
00210 name << EmcIndexer::EmcSectorId(fNumber) << "_gt";
00211 fHgt = new TH1F(name.str().c_str(),name.str().c_str(),
00212 1000,0,500);
00213 }
00214 else
00215 {
00216 fHgt->Reset();
00217 }
00218
00219 for ( size_t i = 0; i < fGt.size(); ++i )
00220 {
00221 fHgt->Fill(fGt[i]);
00222 }
00223
00224 if ( !fHgt0 )
00225 {
00226 std::ostringstream name;
00227 name << EmcIndexer::EmcSectorId(fNumber) << "_gt0";
00228 fHgt0 = new TH1F(name.str().c_str(),name.str().c_str(),
00229 1000,0,500);
00230 }
00231 else
00232 {
00233 fHgt0->Reset();
00234 }
00235
00236 for ( size_t i = 0; i < fGt0.size(); ++i )
00237 {
00238 fHgt0->Fill(fGt0[i]);
00239 }
00240
00241 if ( !fHratio )
00242 {
00243 std::ostringstream name;
00244 name << EmcIndexer::EmcSectorId(fNumber) << "_ratio";
00245 fHratio = new TH1F(name.str().c_str(),name.str().c_str(),
00246 1000,0,3);
00247 }
00248 else
00249 {
00250 fHratio->Reset();
00251 }
00252
00253 for ( size_t i = 0; i < fRatio_gt_gt0.size(); ++i )
00254 {
00255 fHratio->Fill(fRatio_gt_gt0[i]);
00256 }
00257
00258 if ( !fHasym )
00259 {
00260 std::ostringstream name;
00261 name << EmcIndexer::EmcSectorId(fNumber) << "_asym";
00262 fHasym = new TH1F(name.str().c_str(),name.str().c_str(),
00263 1000,-3,3);
00264 }
00265 else
00266 {
00267 fHasym->Reset();
00268 }
00269
00270 for ( size_t i = 0; i < fAsym_gt_gt0.size(); ++i )
00271 {
00272 fHasym->Fill(fAsym_gt_gt0[i]);
00273 }
00274
00275
00276
00277 assert(fGt.size()==2592);
00278 assert(fGt0.size()==2592);
00279 assert(fRatio_gt_gt0.size()==2592);
00280 assert(fAsym_gt_gt0.size()==2592);
00281
00282 if ( !fHgt_yz )
00283 {
00284 std::ostringstream name;
00285 name << EmcIndexer::EmcSectorId(fNumber) << "_gt_yz";
00286 fHgt_yz = new TH2F(name.str().c_str(),name.str().c_str(),
00287 72,-0.5,71.5,36,-0.5,35.5);
00288 }
00289 else
00290 {
00291 fHgt_yz->Reset();
00292 }
00293
00294 for ( size_t i = 0; i < fGt.size(); ++i )
00295 {
00296 int is,iz,iy;
00297 EmcIndexer::decodeTowerId(i,is,iz,iy);
00298 fHgt_yz->Fill(iz,iy,fGt[i]);
00299 }
00300
00301 if ( !fHgt0_yz )
00302 {
00303 std::ostringstream name;
00304 name << EmcIndexer::EmcSectorId(fNumber) << "_gt0_yz";
00305 fHgt0_yz = new TH2F(name.str().c_str(),name.str().c_str(),
00306 72,-0.5,71.5,36,-0.5,35.5);
00307 }
00308 else
00309 {
00310 fHgt0_yz->Reset();
00311 }
00312
00313 for ( size_t i = 0; i < fGt0.size(); ++i )
00314 {
00315 int is,iz,iy;
00316 EmcIndexer::decodeTowerId(i,is,iz,iy);
00317 fHgt0_yz->Fill(iz,iy,fGt0[i]);
00318 }
00319
00320 if ( !fHratio_yz )
00321 {
00322 std::ostringstream name;
00323 name << EmcIndexer::EmcSectorId(fNumber) << "_ratio_yz";
00324 fHratio_yz = new TH2F(name.str().c_str(),name.str().c_str(),
00325 72,-0.5,71.5,36,-0.5,35.5);
00326 }
00327 else
00328 {
00329 fHratio_yz->Reset();
00330 }
00331
00332 for ( size_t i = 0; i < fRatio_gt_gt0.size(); ++i )
00333 {
00334 int is,iz,iy;
00335 EmcIndexer::decodeTowerId(i,is,iz,iy);
00336 fHratio_yz->Fill(iz,iy,fRatio_gt_gt0[i]);
00337 }
00338
00339 if ( !fHasym_yz )
00340 {
00341 std::ostringstream name;
00342 name << EmcIndexer::EmcSectorId(fNumber) << "_asym_yz";
00343 fHasym_yz = new TH2F(name.str().c_str(),name.str().c_str(),
00344 72,-0.5,71.5,36,-0.5,35.5);
00345 }
00346 else
00347 {
00348 fHasym_yz->Reset();
00349 }
00350
00351 for ( size_t i = 0; i < fAsym_gt_gt0.size(); ++i )
00352 {
00353 int is,iz,iy;
00354 EmcIndexer::decodeTowerId(i,is,iz,iy);
00355 fHasym_yz->Fill(iz,iy,fAsym_gt_gt0[i]);
00356 }
00357 }
00358
00359 private:
00360
00361 int fNumber;
00362 std::vector<float> fGt;
00363 std::vector<float> fGt0;
00364 std::vector<float> fRatio_gt_gt0;
00365 std::vector<float> fAsym_gt_gt0;
00366 TH1* fHgt;
00367 TH1* fHgt0;
00368 TH1* fHratio;
00369 TH1* fHasym;
00370
00371 TH2* fHgt_yz;
00372 TH2* fHgt0_yz;
00373 TH2* fHratio_yz;
00374 TH2* fHasym_yz;
00375 };
00376
00377
00378 void norm(int runnumber, const char* outfile)
00379 {
00380 emcCalibrationDataHelper cdh_t(runnumber,
00381 false,
00382 emcManageable::kDB_Pg,
00383 "pbsc");
00384
00385 static const int runnumber_t0 = 92446;
00386
00387 emcCalibrationDataHelper cdh_t0(runnumber_t0,
00388 false,
00389 emcManageable::kDB_Pg,
00390 "pbsc");
00391
00392 PHTimeStamp ts = cdh_t.timeStamp();
00393
00394 for ( size_t is = 0; is < 6; ++is )
00395 {
00396 cdh_t0.getGainBaseLine(is);
00397 }
00398
00399 std::vector<Sector> sectors;
00400
00401 for ( size_t is = 0; is < 6; ++is )
00402 {
00403 sectors.push_back(Sector(is));
00404 cdh_t.getGainBaseLine(is);
00405 }
00406
00407 for ( size_t ifem = 0; ifem < 108; ++ifem )
00408 {
00409 const emcCalFEM* g_t = cdh_t.getCalibration(ifem, "Gains");
00410 const emcCalFEM* g_t0 = cdh_t0.getCalibration(ifem,"Gains");
00411
00412 for ( size_t channel = 0; channel < 144; ++channel )
00413 {
00414 float norm_t = g_t->getValue(channel,ts.getTics());
00415 float norm_t0 = g_t0->getValue(channel,
00416 g_t0->GetXmax());
00417 int towerid = EmcIndexer::PXSM144iCH_iPX(ifem,channel);
00418 int is,ist;
00419 EmcIndexer::iPXiSiST(towerid,is,ist);
00420 sectors[is].set(ist,norm_t,norm_t0);
00421 }
00422 }
00423
00424 for ( size_t is = 0; is < sectors.size(); ++is )
00425 {
00426 std::cout << "Sector " << EmcIndexer::EmcSectorId(is) << std::endl;
00427 sectors[is].print();
00428 }
00429
00430 TFile fout(outfile,"RECREATE");
00431
00432 for ( size_t is = 0; is < sectors.size(); ++is )
00433 {
00434 sectors[is].write(fout);
00435 }
00436 fout.Close();
00437
00438 }