00001 #include "Validation/RecoJets/interface/FitHist.h"
00002 #include "Validation/RecoJets/interface/CompMethods.h"
00003 #include "Validation/RecoJets/interface/FitHist_fwd.h"
00004 #include "Validation/RecoJets/interface/RootPostScript.h"
00005
00006 using namespace std;
00007
00008 void
00009 FitHist::configBlockFit(ConfigFile& cfg)
00010 {
00011
00012
00013
00014
00015
00016 try{
00017
00018
00019
00020 readVector( cfg.read<std::string>( "titleIndex"), axesIndex_);
00021 readLabels( cfg.read<std::string>( "xAxesFit" ), xAxesFit_ );
00022 readLabels( cfg.read<std::string>( "yAxesFit" ), yAxesFit_ );
00023
00024
00025
00026
00027 readVector( cfg.read<std::string>( "targetLabel" ), targetHistList_);
00028 fitFuncName_ = cfg.read<std::string>( "fitFunctionName" );
00029 fitFuncTitle_= cfg.read<std::string>( "fitFunctionTitle");
00030 fitFuncType_ = cfg.read<int> ( "fitFunctionType" );
00031 fitFuncLowerBound_= cfg.read<double>( "fitLowerBound" );
00032 fitFuncUpperBound_= cfg.read<double>( "fitUpperBound" );
00033 evalType_ = cfg.read<int>( "evalType" );
00034 }
00035 catch(...){
00036 std::cerr << "ERROR during reading of config file" << std::endl;
00037 std::cerr << " misspelled variables in cfg ?" << std::endl;
00038 std::cerr << " [--called in configBlockFit]" << std::endl;
00039 std::exit(1);
00040 }
00041 }
00042
00043 bool
00044 FitHist::checkTargetHistList()
00045 {
00046
00047
00048
00049
00050
00051 bool statusOk=true;
00052 for(unsigned int idx=0; idx<targetHistList_.size(); ++idx){
00053 if( statusOk ) statusOk=isInFitTargetList(targetHistList_[idx]);
00054 }
00055 return statusOk;
00056 }
00057
00058 bool
00059 FitHist::isInFitTargetList(std::string& label)
00060 {
00061
00062
00063
00064 TString target(label);
00065 if( !(target.CompareTo(FitTarget::Cal ) ||
00066 target.CompareTo(FitTarget::Res ) ||
00067 target.CompareTo(FitTarget::Sigma ) ||
00068 target.CompareTo(FitTarget::Mean ) )){
00069 std::cerr << "ERROR while filling target histogram" << std::endl;
00070 std::cerr << " can't find prefix: " << target << std::endl;
00071 return false;
00072 }
00073 return true;
00074 }
00075
00076 TH1F&
00077 FitHist::findFitHistogram(const TObjArray& hist, TString& zip, TString& name, int& bin)
00078 {
00079
00080
00081
00082
00083
00084
00085 TString refName( name );
00086 refName+="_";
00087 refName+=bin;
00088 refName.Remove(0, refName.First('_')+1);
00089
00090
00091
00092
00093 for(int idx=0; idx<hist.GetEntriesFast(); ++idx){
00094 TH1F& cmp = *((TH1F*)(hist)[idx]);
00095 TString buffer( cmp.GetName() );
00096 if( buffer.BeginsWith( zip ) ){
00097 TString cmpName( cmp.GetName() );
00098 cmpName.Remove(0, cmpName.First('/')+1);
00099 if( cmpName.BeginsWith(FitTarget::Fit) && cmpName.Contains(refName) ){
00100 return cmp;
00101 }
00102 }
00103 }
00104 std::cout << "WARNING: could not find required histogram fit_"
00105 << refName << "_x" << std::endl
00106 << " return reference to Null" << std::endl;
00107 return *(TH1F*)0;
00108 }
00109
00110 TH1F&
00111 FitHist::findTargetHistogram(const TObjArray& hist, TString& zip, TString& name, TString& target)
00112 {
00113
00114
00115
00116
00117
00118
00119 TString refName( name );
00120 refName.Remove(0, refName.First('_')+1);
00121 refName.Remove(refName.Last('_'), refName.Length());
00122
00123
00124
00125
00126 for(int idx=0; idx<hist.GetEntriesFast(); ++idx){
00127 TH1F& cmp = *((TH1F*)(hist)[idx]);
00128 TString buffer( cmp.GetName() );
00129 if( buffer.BeginsWith( zip ) ){
00130
00131 TString cmpName( cmp.GetName() );
00132 cmpName.Remove(0, cmpName.First('/')+1);
00133 if( cmpName.BeginsWith(target) && cmpName.Contains(refName) ){
00134 return cmp;
00135 }
00136 }
00137 }
00138 std::cout << "WARNING: could not find required histogram "
00139 << target << refName << std::endl
00140 << " return reference to Null" << std::endl;
00141 return *(TH1F*)0;
00142 }
00143
00144 double
00145 FitHist::normalize(TString& buffer, double val)
00146 {
00147 return ( (!buffer.CompareTo(FitTarget::Res ) && val>0.) ) ? 1./val : 1.;
00148 }
00149
00150 void
00151 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, Quantile& func)
00152 {
00153 if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00154 htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00155 }
00156 if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00157 double norm=normalize(buffer, func.value( hfit ));
00158 htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError ( bin, norm*func.spreadError( hfit ) );
00159 }
00160 }
00161
00162 void
00163 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, MaximalValue& func)
00164 {
00165 if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00166 htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00167 }
00168 if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00169 double norm=normalize(buffer, func.value( hfit ));
00170 htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError ( bin, norm*func.spreadError( hfit ) );
00171 }
00172 }
00173
00174 void
00175 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, HistogramMean& func)
00176 {
00177 if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00178 htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00179 }
00180 if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00181 double norm=normalize(buffer, func.value( hfit ));
00182 htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError ( bin, norm*func.spreadError( hfit ) );
00183 }
00184 }
00185
00186 void
00187 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, StabilizedGauss& func)
00188 {
00189 if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00190 htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00191 }
00192 if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00193 double norm=normalize(buffer, func.value( hfit ));
00194 htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError ( bin, norm*func.spreadError( hfit ) );
00195 }
00196 }
00197
00198 void
00199 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin)
00200 {
00201 if( !hfit.GetEntries()>0 ) return;
00202
00203
00204
00205
00206 TString buffer(htarget.GetName());
00207 buffer.Remove(0, buffer.First('/')+1);
00208 buffer.Remove(buffer.First('_')+1, buffer.Length());
00209
00210 switch(evalType_){
00211 case kStabilizedGauss:
00212 {
00213 StabilizedGauss func(fitFuncName_.c_str());
00214 fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00215 break;
00216 }
00217 case kHistogramMean:
00218 {
00219 HistogramMean func;
00220 fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00221 break;
00222 }
00223 case kMaximalValue:
00224 {
00225 MaximalValue func(0.9, 0.05);
00226 fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00227 break;
00228 }
00229 case kQuantile:
00230 {
00231 Quantile func(0.5, 0.05);
00232 fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00233 break;
00234 }
00235 default:
00236
00237 break;
00238 }
00239 }
00240
00241 void
00242 FitHist::setFitHistogramAxes(TH1F& hist, int idx)
00243 {
00244
00245
00246
00247
00248
00249
00250
00251 if(idx<(int)axesIndex_.size()){
00252 int jdx = axesIndex_[idx];
00253 if( jdx<(int)xAxesFit_.size() && jdx<(int)yAxesFit_.size() ){
00254 setAxesStyle( hist, xAxesFit_[jdx].c_str(), yAxesFit_[jdx].c_str() );
00255 }
00256 else if( jdx<(int)xAxesFit_.size() ){
00257 setAxesStyle( hist, xAxesFit_[jdx].c_str(), "events" );
00258 }
00259 }
00260 else{
00261 setAxesStyle( hist, hist.GetName(), "events" );
00262 }
00263 }
00264
00265 void
00266 FitHist::addBinLabelToFitHist(const TObjArray& hist, int& bin, TString& name, TString& zip)
00267 {
00268
00269
00270
00271
00272 TPaveText* text = new TPaveText(0.25, 0.85, 0.95, 0.95, "NDC");
00273 text->SetBorderSize( 0 );
00274 text->SetFillStyle( 4000 );
00275 text->SetTextAlign( 12 );
00276 text->SetTextSize ( 0.06 );
00277 text->SetTextColor( 1 );
00278 text->SetTextFont ( 62 );
00279
00280 char labelname[100];
00281 TString buffer(targetHistList_[0]); buffer+="_";
00282 TH1F& target = findTargetHistogram(hist, zip, name, buffer);
00283 double lowerEdge = target.GetBinLowEdge(bin+1);
00284 double upperEdge = target.GetBinLowEdge(bin+1)+target.GetBinWidth(bin+1);
00285 sprintf(labelname, "[%3.0f GeV; %3.0f GeV]", lowerEdge, upperEdge );
00286 text->AddText( labelname );
00287 text->Draw();
00288 }
00289
00290 void
00291 FitHist::addParLabelToFitHist(const TH1F& hist)
00292 {
00293 if( hist.GetFunction(fitFuncName_.c_str()) ){
00294 TPaveText* pars = new TPaveText(0.40, 0.55, 0.95, 0.70, "NDC");
00295 pars->SetBorderSize( 0 );
00296 pars->SetFillStyle( 4000 );
00297 pars->SetTextAlign( 12 );
00298 pars->SetTextSize ( 0.06 );
00299 pars->SetTextColor( 1 );
00300 pars->SetTextFont ( 62 );
00301
00302 char parstring[100];
00303 if(fitFuncType_==0){
00304 sprintf(parstring, "#mu=%3.2f ; #sigma=%3.2f",
00305 hist.GetFunction(fitFuncName_.c_str())->GetParameter( 1 ),
00306 hist.GetFunction(fitFuncName_.c_str())->GetParameter( 2 ) );
00307 }
00308 pars->AddText( parstring );
00309 pars->Draw();
00310 }
00311 }
00312
00313 void
00314 FitHist::fitAndDrawPs()
00315 {
00316
00317
00318
00319 TCanvas *canv = new TCanvas("canv", "fit histograms", 600, 600);
00320 setCanvasStyle( *canv );
00321 canv->SetGridx( 1 ); canv->SetLogx ( 0 );
00322 canv->SetGridy( 1 ); canv->SetLogy ( 0 );
00323
00324
00325
00326
00327
00328
00329
00330 std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00331 for(int idx=0; hist!=sampleList_.end(); ++hist, ++idx){
00332
00333
00334
00335 TString output( writeTo_.c_str() );
00336 output += "/inspectFit_";
00337 if(outputLabelList_.size()>=sampleList_.size())
00338 output += outputLabelList_[idx];
00339 else
00340 output == idx;
00341 output += ".ps";
00342 TPostScript psFile( output, 111 );
00343
00344
00345
00346
00347
00348
00349 int label=0;
00350 for(int jdx=0; jdx<(*hist).GetEntriesFast(); ++jdx){
00351 TH1F& hfit = *((TH1F*)(*hist)[jdx]);
00352
00353 TString cmp( hfit.GetName() );
00354 cmp.Remove(0, cmp.First('/')+1);
00355
00356 TString zip( hfit.GetName() );
00357 zip.Remove(zip.First('/'), zip.Length());
00358 if ( cmp.BeginsWith(FitTarget::Fit) ){
00359 psFile.NewPage();
00360 if(verbose_){
00361 std::cout << std::endl << "about to fit histogram: "
00362 << hfit.GetName() << " as " << cmp << std::endl;
00363 }
00364
00365
00366
00367
00368
00369 TString buffer( cmp );
00370 int bin = buffer.Remove(0, buffer.Last('_')+1).Atoi();
00371
00372 hfit.SetLineWidth( 5 );
00373 hfit.SetLineColor( 4 );
00374 hfit.SetLineStyle( 1 );
00375 hfit.SetMarkerStyle( 20 );
00376 hfit.SetMarkerSize( 2.0 );
00377 hfit.SetMarkerColor( 4 );
00378 hfit.SetMaximum(1.5*hfit.GetMaximum());
00379 setFitHistogramAxes( hfit, label++ );
00380
00381
00382
00383 StabilizedGauss func(fitFuncName_.c_str(), fitFuncType_, fitFuncLowerBound_, fitFuncUpperBound_);
00384 func.fit(hfit);
00385
00386 hfit.Draw("esame");
00387
00388
00389
00390
00391 addParLabelToFitHist( hfit );
00392 addBinLabelToFitHist(*hist, bin, cmp, zip);
00393
00394
00395
00396
00397 TLegend* leg = new TLegend(0.25,0.70,0.95,0.87);
00398 setLegendStyle( *leg );
00399 leg->AddEntry( &hfit, legend(idx).c_str(), "PL" );
00400 leg->AddEntry( hfit.GetFunction(fitFuncName_.c_str()), fitFuncTitle_.c_str(), "L" );
00401 leg->Draw( "same" );
00402 canv->RedrawAxis( );
00403 canv->Update( );
00404 if(jdx<((*hist).GetEntriesFast()-1)) delete leg;
00405 }
00406 }
00407 psFile.Close();
00408 }
00409 canv->Close();
00410 delete canv;
00411 }
00412
00413 void
00414 FitHist::fitAndDrawEps()
00415 {
00416
00417
00418
00419 TCanvas *canv = new TCanvas("canv", "fit histograms", 600, 600);
00420 setCanvasStyle( *canv );
00421 canv->SetGridx( 1 ); canv->SetLogx ( 0 );
00422 canv->SetGridy( 1 ); canv->SetLogy ( 0 );
00423
00424
00425
00426
00427
00428
00429
00430 std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00431 for(int idx=0; hist!=sampleList_.end(); ++hist, ++idx){
00432
00433
00434
00435
00436
00437
00438 int label=0;
00439 for(int jdx=0; jdx<(*hist).GetEntriesFast(); ++jdx){
00440 TH1F& hfit = *((TH1F*)(*hist)[jdx]);
00441
00442 TString cmp( hfit.GetName() );
00443 cmp.Remove(0, cmp.First('/')+1);
00444
00445 TString zip( hfit.GetName() );
00446 zip.Remove(zip.First('/'), zip.Length());
00447 if ( cmp.BeginsWith(FitTarget::Fit) ){
00448
00449
00450
00451 TString output( writeTo_.c_str() );
00452 output += "/";
00453 output += histList_[ jdx ];
00454 output += "_";
00455 if(outputLabelList_.size()>=sampleList_.size())
00456 output += outputLabelList_[idx];
00457 else
00458 output == idx;
00459 output += ".eps";
00460 TPostScript psFile( output, 113 );
00461 psFile.NewPage();
00462
00463
00464
00465
00466
00467
00468 TString buffer( cmp );
00469 int bin = buffer.Remove(0, buffer.Last('_')+1).Atoi();
00470
00471 hfit.SetLineWidth( 5 );
00472 hfit.SetLineColor( 4 );
00473 hfit.SetLineStyle( 1 );
00474 hfit.SetMarkerStyle( 20 );
00475 hfit.SetMarkerSize( 2.0 );
00476 hfit.SetMarkerColor( 4 );
00477 setFitHistogramAxes( hfit, label++ );
00478
00479
00480
00481 StabilizedGauss func(fitFuncName_.c_str(), fitFuncType_, fitFuncLowerBound_, fitFuncUpperBound_);
00482 func.fit(hfit);
00483
00484 hfit.Draw("esame");
00485
00486
00487
00488
00489 addParLabelToFitHist( hfit );
00490 addBinLabelToFitHist( *hist, bin, cmp, zip );
00491
00492
00493
00494
00495 TLegend* leg = new TLegend(0.25,0.70,0.95,0.87);
00496 setLegendStyle( *leg );
00497 leg->AddEntry( &hfit, legend(idx).c_str(), "PL" );
00498 leg->AddEntry( hfit.GetFunction(fitFuncName_.c_str()), fitFuncTitle_.c_str(), "L" );
00499 leg->Draw( "same" );
00500 canv->RedrawAxis( );
00501 canv->Update( );
00502 psFile.Close();
00503 delete leg;
00504 }
00505 }
00506 }
00507 canv->Close();
00508 delete canv;
00509 }
00510
00511 void
00512 FitHist::fillTargetHistograms()
00513 {
00514
00515
00516
00517 for(unsigned int idx=0; idx<targetHistList_.size(); ++idx){
00518 fillTargetHistogram(targetHistList_[idx]);
00519 }
00520 }
00521
00522 void
00523 FitHist::fillTargetHistogram(std::string& target)
00524 {
00525
00526
00527
00528
00529
00530 std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00531 for(; hist!=sampleList_.end(); ++hist){
00532
00533
00534
00535
00536
00537 TString buffer( target );
00538 if( isInFitTargetList(target) ){
00539 for(int jdx=0; jdx<(*hist).GetEntriesFast(); ++jdx){
00540 TH1F& htarget = *((TH1F*)(*hist)[jdx]);
00541
00542 TString cmp( htarget.GetName() );
00543 cmp.Remove(0, cmp.First('/')+1);
00544
00545 TString zip( htarget.GetName() );
00546 zip.Remove(zip.First('/'), zip.Length());
00547 if( cmp.BeginsWith( buffer ) ){
00548
00549
00550
00551 for(int kdx=0; kdx<htarget.GetNbinsX(); ++kdx){
00552 TH1F& hfit = findFitHistogram(*hist, zip, cmp, kdx);
00553 fillTargetHistogramBin(htarget, hfit, (kdx+1));
00554 }
00555 }
00556 }
00557 }
00558 }
00559 }
00560
00561 void
00562 FitHist::writeFitOutput()
00563 {
00564
00565
00566
00567
00568 if( isOutputRequested() ){
00569
00570
00571
00572 TString name( output_ );
00573 name.Remove(name.Last('.'), name.Length());
00574 name+=".hist";
00575 ofstream histFile(name, std::ios::out);
00576
00577
00578
00579
00580
00581 TFile file( output_.c_str(), "update" );
00582 if( !file.GetDirectory(rootOutDir_.c_str()) )
00583
00584 file.mkdir( rootOutDir_.c_str(), rootOutDir_.c_str() );
00585 else
00586
00587 (file.GetDirectory(rootOutDir_.c_str()))->Delete("*;*");
00588 file.cd(rootOutDir_.c_str());
00589
00590
00591 for(unsigned int jdx=0; jdx<targetHistList_.size(); ++jdx){
00592 TString buffer( targetHistList_[jdx] );
00593
00594
00595
00596 std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00597 for( ;hist!=sampleList_.end(); ++hist){
00598 for(int idx=0; idx<(int)histList_.size(); ++idx){
00599 TString cmp( ((TH1F*)(*hist)[idx])->GetName() );
00600 if( cmp.BeginsWith( buffer ) ){
00601 histFile << ((TH1F*)(*hist)[idx])->GetName() << "\n";
00602 ((TH1F*)(*hist)[idx])->Write();
00603 }
00604 }
00605 }
00606 }
00607 file.Close();
00608 }
00609 }