TAdvancedGraphicsDialog.cxx

Go to the documentation of this file.
00001 // @(#)root/fitpanel:$Id: TAdvancedGraphicsDialog.cxx 31212 2009-11-16 17:30:21Z moneta $
00002 // Author: David Gonzalez Maline 11/12/2008
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TAdvancedGraphicsDialog                                              //
00015 //                                                                      //
00016 // Allows to create advanced graphics from the last fit made in the     //
00017 // fitpanel. This includes the scan graphics, the contour and the       //
00018 // confidence levels.                                                   //
00019 //////////////////////////////////////////////////////////////////////////
00020 
00021 #include "TAdvancedGraphicsDialog.h"
00022 #include "TGraph.h"
00023 #include "TAxis.h"
00024 #include "TPad.h"
00025 #include "TColor.h"
00026 
00027 #include "Fit/BinData.h"
00028 #include "Math/IParamFunction.h"
00029 #include "TGraphErrors.h"
00030 #include "TGraph2DErrors.h"
00031 
00032 #include <sstream>
00033 
00034 #include <string>
00035 
00036 TAdvancedGraphicsDialog::TAdvancedGraphicsDialog(const TGWindow *p, const TGWindow *main):
00037    TGTransientFrame(p, main, 10, 10, kVerticalFrame), 
00038    fFitter((TBackCompFitter *) TVirtualFitter::GetFitter())
00039 {
00040    // Creates the Advanced Graphics Dialog.
00041 
00042 
00043    if (!p && !main) {
00044       MakeZombie();
00045       return;
00046    }
00047    SetCleanup(kDeepCleanup);
00048 
00049    fMainFrame = new TGVerticalFrame(this);
00050 
00051    fTab = new TGTab(fMainFrame, 10, 10);
00052    fMainFrame->AddFrame(fTab, new TGLayoutHints(kLHintsExpandY | kLHintsExpandX, 5,5,5,0));
00053    fTab->SetCleanup(kDeepCleanup);
00054    fTab->Associate(this);
00055 
00056    // Add the first method to the dialog (Contour)
00057    CreateContourFrame();
00058    fTab->AddTab("Contour", fContourFrame);
00059 
00060    // Add the second method to the dialog (Scan)
00061    CreateScanFrame();
00062    fTab->AddTab("Scan", fScanFrame);
00063 
00064    CreateConfFrame();
00065    fTab->AddTab("Conf Intervals", fConfFrame);
00066 
00067    TGCompositeFrame * frame = new TGHorizontalFrame(fMainFrame);
00068 
00069    fDraw = new TGTextButton(frame, "&Draw", kAGD_BDRAW);
00070    fDraw->Associate(this);
00071    frame->AddFrame(fDraw, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0));
00072 
00073    fClose = new TGTextButton(frame, "&Close", kAGD_BCLOSE);
00074    fClose->Associate(this);
00075    frame->AddFrame(fClose, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 5, 0, 0));
00076 
00077    UInt_t width = 0, height = 0;
00078    height = fClose->GetDefaultHeight();
00079    width  = TMath::Max(width, fClose->GetDefaultWidth());
00080    frame->Resize((width + 20) * 2, height);
00081 
00082    fMainFrame->AddFrame(frame, new TGLayoutHints(kLHintsBottom | kLHintsRight, 0, 0, 5, 0));
00083 
00084    this->AddFrame(fMainFrame, new TGLayoutHints(kLHintsNormal | kLHintsExpandX,0,0,5,5));
00085 
00086    ConnectSlots();
00087 
00088    SetWindowName("Advanced Drawing Tools");
00089 
00090    // map all widgets and calculate size of dialog
00091    MapSubwindows();
00092 
00093    width  = GetDefaultWidth();
00094    height = GetDefaultHeight();
00095 
00096    Resize(width, height);
00097    MapWindow();
00098 
00099    // position relative to the parent's window
00100    CenterOnParent();
00101 
00102    // make the message box non-resizable
00103    SetWMSize(width, height);
00104    SetWMSizeHints(width, height, width, height, 0, 0);
00105 
00106    SetMWMHints(kMWMDecorAll | kMWMDecorResizeH  | kMWMDecorMaximize |
00107                kMWMDecorMinimize | kMWMDecorMenu,
00108                kMWMFuncAll  | kMWMFuncResize    | kMWMFuncMaximize |
00109                kMWMFuncMinimize,
00110                kMWMInputModeless);
00111 
00112    // popup dialog and wait till user replies
00113    gClient->WaitFor(this);
00114 }
00115 
00116 //______________________________________________________________________________
00117 void TAdvancedGraphicsDialog::CreateContourFrame()
00118 {
00119    // Create the frame that contains all the necessary information for
00120    // the Contour method.
00121 
00122    fContourFrame = new TGVerticalFrame(fTab);
00123    TGHorizontalFrame* frame = new TGHorizontalFrame(fContourFrame);
00124 
00125    TGLabel* label = new TGLabel(frame, "Number of Points: ");
00126    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00127 
00128    fContourPoints = new TGNumberEntry(frame, 40, 
00129                                 5, kAGD_SCANMIN,
00130                                 TGNumberFormat::kNESInteger,
00131                                 TGNumberFormat::kNEAPositive,
00132                                 TGNumberFormat::kNELNoLimits);
00133    fContourPoints->Resize(130, 20);
00134    fContourPoints->GetNumberEntry()->SetToolTipText("Sets the number of points used for the contour");
00135    frame->AddFrame(fContourPoints, new TGLayoutHints(kLHintsNormal, 8, 0, 5, 0));
00136    fContourFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 0));
00137 
00138    frame = new TGHorizontalFrame(fContourFrame);
00139    label = new TGLabel(frame, "Parameter 1: ");
00140    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00141 
00142    fContourPar1 = new TGComboBox(frame, kAGD_CONTPAR1);
00143    AddParameters(fContourPar1);
00144    fContourPar1->Resize(130, 20);
00145    fContourPar1->Associate(this);
00146    TGListBox *lb = fContourPar1->GetListBox();
00147    lb->Resize(lb->GetWidth(), 200);
00148    frame->AddFrame(fContourPar1, new TGLayoutHints(kLHintsNormal, 37, 0, 5, 0));
00149    fContourFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 0));
00150 
00151    frame = new TGHorizontalFrame(fContourFrame);
00152 
00153    label = new TGLabel(frame, "Parameter 2: ");
00154    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00155 
00156    fContourPar2 = new TGComboBox(frame, kAGD_CONTPAR2);
00157    AddParameters(fContourPar2);
00158    fContourPar2->Select(kAGD_PARCOUNTER+1, kFALSE);
00159    fContourPar2->Resize(130, 20);
00160    fContourPar2->Associate(this);
00161    lb = fContourPar2->GetListBox();
00162    lb->Resize(lb->GetWidth(), 200);
00163    frame->AddFrame(fContourPar2, new TGLayoutHints(kLHintsNormal, 37, 0, 5, 0));
00164 
00165    fContourFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 0));
00166 
00167    frame = new TGHorizontalFrame(fContourFrame);
00168 
00169    label = new TGLabel(frame, "Confidence Level: ");
00170    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00171 
00172    fContourError = new TGNumberEntry(frame, 0.683, 5, kAGD_CONTERR,
00173                                      TGNumberFormat::kNESRealThree,
00174                                      TGNumberFormat::kNEANonNegative,
00175                                      TGNumberFormat::kNELNoLimits);
00176    fContourError->Resize(130, 20);
00177    fContourError->GetNumberEntry()->SetToolTipText("Sets the contour confidence level");
00178    frame->AddFrame(fContourError, new TGLayoutHints(kLHintsNormal, 5, 0, 5, 0));
00179 
00180    fContourFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 5));
00181 
00182    frame = new TGHorizontalFrame(fContourFrame);
00183    
00184    label = new TGLabel(frame, "Fill Colour: ");
00185    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00186 
00187    fContourColor = new TGColorSelect(frame, TColor::Number2Pixel(kYellow - 10), kAGD_CONTCOLOR);
00188    frame->AddFrame(fContourColor, new TGLayoutHints(kLHintsNormal, 5, 0, 5, 0));
00189 
00190    fContourOver = new TGCheckButton(frame, "Superimpose", kAGD_CONTOVER);
00191    fContourOver->SetToolTipText("If checked, the new contour will overlap the previous one");
00192    frame->AddFrame(fContourOver, new TGLayoutHints(kLHintsNormal, 5, 0, 5, 0));
00193 
00194    fContourFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 5));
00195 }
00196 
00197 //______________________________________________________________________________
00198 void TAdvancedGraphicsDialog::CreateScanFrame()
00199 {
00200    // Create the frame that contains all the necessary information for
00201    // the Scan method.
00202 
00203    fScanFrame = new TGVerticalFrame(fTab);
00204    TGHorizontalFrame* frame = new TGHorizontalFrame(fScanFrame);
00205 
00206    TGLabel* label = new TGLabel(frame, "Number of Points: ");
00207    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00208 
00209    fScanPoints = new TGNumberEntry(frame, 40, 
00210                                 5, kAGD_SCANMIN,
00211                                 TGNumberFormat::kNESInteger,
00212                                 TGNumberFormat::kNEAPositive,
00213                                 TGNumberFormat::kNELNoLimits);
00214    fScanPoints->Resize(140, 20);
00215    fScanPoints->GetNumberEntry()->SetToolTipText("Sets the number of points used in the scan");
00216    frame->AddFrame(fScanPoints, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 0));
00217    fScanFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 0));
00218 
00219    frame = new TGHorizontalFrame(fScanFrame);
00220 
00221    label = new TGLabel(frame, "Parameter: ");
00222    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00223 
00224    fScanPar = new TGComboBox(frame, kAGD_SCANPAR);
00225    AddParameters(fScanPar);
00226    fScanPar->Resize(140, 20);
00227    fScanPar->Associate(this);
00228    TGListBox *lb = fScanPar->GetListBox();
00229    lb->Resize(lb->GetWidth(), 200);
00230    frame->AddFrame(fScanPar, new TGLayoutHints(kLHintsNormal, 39, 0, 5, 0));
00231    fScanFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 0));
00232 
00233    frame = new TGHorizontalFrame(fScanFrame);
00234    label = new TGLabel(frame, "Min: ");
00235    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00236 
00237    double val = fFitter->GetParameter( fScanPar->GetSelected() - kAGD_PARCOUNTER );
00238    double err = fFitter->GetParError( fScanPar->GetSelected() - kAGD_PARCOUNTER ); 
00239    fScanMin = new TGNumberEntry(frame, val - 2.*err , 
00240                                 5, kAGD_SCANMIN,
00241                                 TGNumberFormat::kNESRealFour,
00242                                 TGNumberFormat::kNEAAnyNumber,
00243                                 TGNumberFormat::kNELNoLimits);
00244    fScanMin->Resize(70, 20);
00245    fScanMin->GetNumberEntry()->SetToolTipText("Sets the minimum parameter value");
00246    frame->AddFrame(fScanMin, new TGLayoutHints(kLHintsNormal, 2, 0, 5, 0));
00247 
00248    label = new TGLabel(frame, "Max: ");
00249    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 35, 5, 5, 0));
00250    fScanMax = new TGNumberEntry(frame, val + 2.*err,
00251                                 5, kAGD_SCANMAX,
00252                                 TGNumberFormat::kNESRealFour,
00253                                 TGNumberFormat::kNEAAnyNumber,
00254                                 TGNumberFormat::kNELNoLimits);
00255    fScanMax->Resize(70, 20);
00256    fScanMax->GetNumberEntry()->SetToolTipText("Sets the maximum parameter value");
00257    frame->AddFrame(fScanMax, new TGLayoutHints(kLHintsNormal, 2, 0, 5, 0));
00258    fScanFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 0));
00259    
00260 }
00261 
00262 //______________________________________________________________________________
00263 void TAdvancedGraphicsDialog::CreateConfFrame()
00264 {
00265    // Create the frame that contains all the necessary information for
00266    // the Confidence Level  method.
00267 
00268    fConfFrame = new TGVerticalFrame(fTab);
00269    TGHorizontalFrame* frame = new TGHorizontalFrame(fConfFrame);
00270 
00271    TGLabel* label = new TGLabel(frame, "Confidence Level: ");
00272    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00273 
00274    fConfLevel = new TGNumberEntry(frame, 0.95, 
00275                                   5, kAGD_SCANMIN,
00276                                   TGNumberFormat::kNESRealTwo,
00277                                   TGNumberFormat::kNEAPositive,
00278                                   TGNumberFormat::kNELLimitMinMax,
00279                                   0, 0.9999);
00280    fConfLevel->Resize(140, 20);
00281    fConfLevel->GetNumberEntry()->SetToolTipText("Sets the value of the confidence level");
00282    frame->AddFrame(fConfLevel, new TGLayoutHints(kLHintsNormal, 0, 0, 5, 0));
00283    fConfFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 0));
00284 
00285    frame = new TGHorizontalFrame(fConfFrame);
00286    
00287    label = new TGLabel(frame, "Fill Colour: ");
00288    frame->AddFrame(label, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0));
00289 
00290    fConfColor = new TGColorSelect(frame, TColor::Number2Pixel(kYellow - 10), kAGD_CONTCOLOR);
00291    frame->AddFrame(fConfColor, new TGLayoutHints(kLHintsNormal, 5, 0, 5, 0));
00292 
00293    fConfFrame->AddFrame(frame, new TGLayoutHints(kLHintsExpandX, 5, 5, 0, 5));
00294 }
00295 
00296 void TAdvancedGraphicsDialog::AddParameters(TGComboBox* comboBox) 
00297 {
00298    // Add all the parameters of the VirtualFitter into a comboBox
00299    // (helper method)
00300 
00301    for ( Int_t i = 0; i < fFitter->GetNumberTotalParameters(); ++i ) {
00302       comboBox->AddEntry(fFitter->GetParName(i), kAGD_PARCOUNTER + i);
00303    }
00304    comboBox->Select(kAGD_PARCOUNTER, kFALSE);
00305 }
00306 
00307 //______________________________________________________________________________
00308 void TAdvancedGraphicsDialog::ConnectSlots()
00309 {
00310    // Connect the slots (buttons mainly + specific methods)
00311 
00312    // Buttons
00313    fClose->Connect("Clicked()", "TAdvancedGraphicsDialog", this, "CloseWindow()");
00314    fDraw->Connect("Clicked()", "TAdvancedGraphicsDialog", this, "DoDraw()");
00315 
00316    // Slots for the Scan method
00317    fScanPar->Connect("Selected(Int_t)", "TAdvancedGraphicsDialog", this, "DoChangedScanPar(Int_t)");
00318 }
00319 
00320 //______________________________________________________________________________
00321 void TAdvancedGraphicsDialog::DoChangedScanPar(Int_t selected)
00322 {
00323    // Changes the Min and Max default values of the scan method,
00324    // depending on the selected parameter.
00325 
00326    double val = fFitter->GetParameter( selected - kAGD_PARCOUNTER );
00327    double err = fFitter->GetParError(  selected - kAGD_PARCOUNTER ); 
00328    fScanMin->SetNumber( val -2 * err );
00329    fScanMax->SetNumber( val +2 * err );
00330 }
00331 
00332 //______________________________________________________________________________
00333 void TAdvancedGraphicsDialog::DoDraw()
00334 {
00335    // Calls the correspoding method, depending on the selected tab.
00336 
00337    if ( fTab->GetCurrent() == 0 ) {
00338       DrawContour();
00339    } else if ( fTab->GetCurrent() == 1 ) {
00340       DrawScan();
00341    } else if ( fTab->GetCurrent() == 2 ) {
00342       DrawConfidenceLevels();
00343    }
00344 }
00345 
00346 //______________________________________________________________________________
00347 void TAdvancedGraphicsDialog::DrawContour()
00348 {
00349    // Generates all necessary data for the Contour method from its
00350    // tab. Then it call Virtual Fitter to perform it.
00351 
00352    static TGraph * graph = 0;
00353    std::string options;
00354    if ( ! (fContourOver->GetState() == kButtonDown) ) {
00355       if ( graph )
00356          delete graph;
00357       options = "ALF";
00358    } else
00359       options = "LF";
00360    graph = new TGraph( static_cast<int>(fContourPoints->GetNumber()) ); 
00361    Int_t par1 = fContourPar1->GetSelected() - kAGD_PARCOUNTER;
00362    Int_t par2 = fContourPar2->GetSelected() - kAGD_PARCOUNTER;
00363    if ( par1 == par2 ) {
00364       Error("TAdvancedGraphicsDialog::DrawContour", "Parameters cannot be the same");
00365       return;
00366    }
00367    // contour error is actually the desired confidence level
00368    Double_t cl = fContourError->GetNumber(); 
00369    fFitter->Contour( par1, par2, graph, cl);
00370    graph->SetFillColor( TColor::GetColor( fContourColor->GetColor() ) );
00371    graph->GetXaxis()->SetTitle( fFitter->GetParName(par1) );
00372    graph->GetYaxis()->SetTitle( fFitter->GetParName(par2) );
00373    graph->Draw( options.c_str() ); 
00374    gPad->Update();
00375 }
00376 
00377 //______________________________________________________________________________
00378 void TAdvancedGraphicsDialog::DrawScan()
00379 {
00380    // Generates all necessary data for the Scan method from its
00381    // tab. Then it call Virtual Fitter to perform it.
00382 
00383    static TGraph * graph = 0;
00384    if ( graph )
00385       delete graph;
00386    graph = new TGraph( static_cast<int>(fScanPoints->GetNumber()) );
00387    Int_t par = fScanPar->GetSelected() - kAGD_PARCOUNTER;
00388    fFitter->Scan( par, graph, 
00389                   fScanMin->GetNumber(),
00390                   fScanMax->GetNumber() ); 
00391    graph->SetLineColor(kBlue);
00392    graph->SetLineWidth(2);
00393    graph->GetXaxis()->SetTitle(fFitter->GetParName(par) );
00394    graph->GetYaxis()->SetTitle("FCN" );
00395    graph->Draw("APL");
00396    gPad->Update();
00397 }
00398 
00399 //______________________________________________________________________________
00400 void TAdvancedGraphicsDialog::DrawConfidenceLevels()
00401 {
00402    // Generates all necessary data for the Scan method from its
00403    // tab. Then it call Virtual Fitter to perform it.
00404 
00405    const ROOT::Fit::FitResult& result = fFitter->GetFitResult();
00406    const ROOT::Fit::FitResult::IModelFunction* function = result.FittedFunction();
00407    const ROOT::Fit::BinData* data = dynamic_cast<const ROOT::Fit::BinData*>(&(fFitter->GetFitData()));
00408    if ( !data ) 
00409    {
00410       Error("DrawConfidenceLevels","Unbinned data set cannot draw confidence levels."); 
00411       return;
00412    }
00413 
00414    if ( !function )
00415    {
00416       Error("DrawConfidenceLevels","Fit Function does not exist!");
00417       return;
00418    }
00419 
00420    std::vector<Double_t> ci(data->Size());
00421    result.GetConfidenceIntervals(*data, &ci[0], fConfLevel->GetNumber());
00422 
00423    if ( data->NDim() == 1 )
00424    {
00425       TGraphErrors* g = new TGraphErrors(ci.size());
00426       for (unsigned int i = 0; i < ci.size(); ++i)
00427       {
00428          const Double_t *x = data->Coords(i);
00429          const Double_t y = (*function)(x);
00430          g->SetPoint(i, *x, y);
00431          g->SetPointError(i, 0, ci[i]);
00432       }
00433       std::ostringstream os;
00434       os << "Confidence Intervals with " << fConfLevel->GetNumber()
00435          << " conf. band.";
00436       g->SetTitle(os.str().c_str());
00437       g->SetLineColor( TColor::GetColor( fConfColor->GetColor() ));
00438       g->SetFillColor( TColor::GetColor( fConfColor->GetColor() ));
00439       g->SetFillStyle(3001);
00440       g->Draw("C3same");
00441    } else if ( data->NDim() == 2 )
00442    {
00443       TGraph2DErrors* g = new TGraph2DErrors(ci.size());
00444       for (unsigned int i = 0; i < ci.size(); ++i)
00445       {
00446          const Double_t *x = data->Coords(i);
00447          const Double_t y = (*function)(x);
00448          g->SetPoint(i, x[0], x[1], y);
00449          g->SetPointError(i, 0, 0, ci[i]);
00450       }
00451       std::ostringstream os;
00452       os << "Confidence Intervals with " << fConfLevel->GetNumber()
00453          << " conf. band.";
00454       g->SetTitle(os.str().c_str());
00455       g->SetLineColor( TColor::GetColor( fConfColor->GetColor() ));
00456       g->SetFillColor( TColor::GetColor( fConfColor->GetColor() ));
00457       g->SetFillStyle(3001);
00458       g->Draw("C3same");
00459    }
00460    gPad->Update();
00461 }
00462 
00463 //______________________________________________________________________________
00464 TAdvancedGraphicsDialog::~TAdvancedGraphicsDialog()
00465 {
00466    // Cleanup dialog.
00467 
00468    Cleanup();
00469 }

Generated on Tue Jul 5 14:19:22 2011 for ROOT_528-00b_version by  doxygen 1.5.1