// @(#)root/fitpanel:$Id$
// Author: Ilka Antcheva, Lorenzo Moneta, David Gonzalez Maline 10/08/2006

/*************************************************************************
 * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TFitEditor
#define ROOT_TFitEditor

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TFitEditor                                                           //
//                                                                      //
// Allows to explore and compare various fits.                          //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TGFrame
#include "TGFrame.h"
#endif
#ifndef ROOT_TGButton
#include "TGButton.h"
#endif

#include "Foption.h"
#include "Math/MinimizerOptions.h"
#include "Fit/DataRange.h"



#include <vector>
#include <map>
#include <utility>

//--- Object types
enum EObjectType {
   kObjectHisto,
   kObjectGraph,
   kObjectGraph2D,
   kObjectHStack,
   kObjectTree,
   kObjectMultiGraph
};


class TGTab;
class TVirtualPad;
class TCanvas;
class TGLabel;
class TGComboBox;
class TGTextEntry;
class TGNumberEntry;
class TGDoubleHSlider;
class TGNumberEntry;
class TGNumberEntryField;
class TGStatusBar;
class TAxis;
class TF1;
class TF1NormSum;
class TF1Convolution;


class TFitEditor : public TGMainFrame {

protected:
   TGTab               *fTab;              // tab widget holding the editor
   TGCompositeFrame    *fTabContainer;     // main tab container
   TGCompositeFrame    *fGeneral;          // general tab
   TGCompositeFrame    *fMinimization;     // minimization tab
   TGTextButton        *fUpdateButton;     // updates data from gROOT and gDirectory
   TGTextButton        *fFitButton;        // performs fitting
   TGTextButton        *fResetButton;      // resets fit parameters
   TGTextButton        *fCloseButton;      // close the fit panel
   TGLabel             *fSelLabel;         // contains selected fit function
   TGComboBox          *fDataSet;          // contains list of data set to be fitted
   TGComboBox          *fTypeFit;          // contains the types of functions to be selected
   TGComboBox          *fFuncList;         // contains function list
   TGTextEntry         *fEnteredFunc;      // contains user function file name
   TGTextButton        *fUserButton;       // opens a dialog for user-defined fit method
   TGRadioButton       *fNone;             // set no operation mode
   TGRadioButton       *fAdd;              // set addition mode
   TGRadioButton       *fNormAdd;          // set normalized addition mode
   TGRadioButton       *fConv;             // set convolution mode
   TGLayoutHints       *fLayoutNone;       // layout hints of fNone radio button
   TGLayoutHints       *fLayoutAdd;        // layout hints of fAdd radio button
   TGLayoutHints       *fLayoutNormAdd;    // layout hints of fNOrmAdd radio button
   TGLayoutHints       *fLayoutConv;       // layout hints of fConv radio button
   TGTextButton        *fSetParam;         // open set parameters dialog
   TGCheckButton       *fIntegral;         // switch on/off option 'integral'
   TGCheckButton       *fBestErrors;       // switch on/off option 'improve errors'
   TGCheckButton       *fUseRange;         // switch on/off option 'use function range'
   TGCheckButton       *fAdd2FuncList;     // switch on/off option 'add to list'
   TGCheckButton       *fUseGradient ;     // switch on/off option 'use gradient'
   TGCheckButton       *fAllWeights1;      // switch on/off option 'all weights=1'
   TGCheckButton       *fImproveResults;   // switch on/off option 'improve fit results'
   TGCheckButton       *fEmptyBinsWghts1;  // switch on/off option 'include empry bins'
   TGComboBox          *fMethodList;       // contains method list
   TGCheckButton       *fLinearFit;        // switch on/off linear fit option
   TGCheckButton       *fNoChi2;           // switch on/off option 'No Chi-square'
   TGCheckButton       *fNoStoreDrawing;   // switch on/off 'no store/drwing' option
   TGCheckButton       *fNoDrawing;        // switch on/off 'no drawing' option
   TGCheckButton       *fDrawSame;         // switch on/off fit function drawing
   TGTextButton        *fDrawAdvanced;     // opens a dialog for advanced draw options
   TGDoubleHSlider     *fSliderX;          // slider to set fit range along x-axis
   TGNumberEntry       *fSliderXMax;       // entry to set the maximum in the range
   TGNumberEntry       *fSliderXMin;       // entry to set the minumum in the range
   TGDoubleHSlider     *fSliderY;          // slider to set fit range along y-axis
   TGNumberEntry       *fSliderYMax;       // entry to set the maximum in the range
   TGNumberEntry       *fSliderYMin;       // entry to set the minumum in the range
   TGDoubleHSlider     *fSliderZ;          // slider to set fit range along z-axis
   TGHorizontalFrame   *fSliderXParent;    // parent of fSliderX
   TGHorizontalFrame   *fSliderYParent;    // parent of fSliderY
   TGHorizontalFrame   *fSliderZParent;    // parent of fSliderZ
   TGCheckButton       *fEnableRobust;     // switch on/off robust option
   TGNumberEntry       *fRobustValue;      // contains robust value for linear fit
   TGRadioButton       *fOptDefault;       // set default printing mode
   TGRadioButton       *fOptVerbose;       // set printing mode to 'Verbose'
   TGRadioButton       *fOptQuiet;         // set printing mode to 'Quiet'
   TVirtualPad         *fParentPad;        // pad containing the object
   TObject             *fFitObject;        // selected object to fit
   EObjectType          fType;             // object type info
   Int_t                fDim;              // object dimension
   TAxis               *fXaxis;            // x-axis
   TAxis               *fYaxis;            // y-axis
   TAxis               *fZaxis;            // z-axis
   TF1NormSum          *fSumFunc;          //! TF1NormSum object
   TF1Convolution      *fConvFunc;         //! TF1Convolution object
   
   // structure holding parameter value and limits
   struct FuncParamData_t {
      FuncParamData_t() {
         fP[0] = 0; fP[1] = 0; fP[2] = 0;
      }
      Double_t & operator[](UInt_t i) { return fP[i];}
      Double_t fP[3];
   };
   std::vector<FuncParamData_t> fFuncPars; // function parameters (value + limits)

   std::multimap<TObject*, TF1*> fPrevFit; // Previous successful fits.
   std::vector<TF1*> fSystemFuncs;         // functions managed by the fitpanel

   TGRadioButton       *fLibMinuit;        // set default minimization library (Minuit)
   TGRadioButton       *fLibMinuit2;       // set Minuit2 as minimization library
   TGRadioButton       *fLibFumili;        // set Fumili as minimization library
   TGRadioButton       *fLibGSL;           // set GSL as minimization library
   TGRadioButton       *fLibGenetics;      // set Genetic/GALib as minimization library
   TGComboBox          *fMinMethodList;    // set the minimization method
   TGNumberEntryField  *fErrorScale;       // contains error scale set for minimization
   TGNumberEntryField  *fTolerance;        // contains tolerance set for minimization
   TGNumberEntryField  *fIterations;       // contains maximum number of iterations

   TGStatusBar         *fStatusBar;        // statusbar widget

   static TFitEditor *fgFitDialog;         // singleton fit panel

protected:
   void        GetFunctionsFromSystem();
   void        ProcessTreeInput(TObject* objSelected, Int_t selected,
                                TString variables, TString cuts);
   TF1*        FindFunction();
   void        FillDataSetList();
   TGComboBox* BuildMethodList(TGFrame *parent, Int_t id);
   void        GetRanges(ROOT::Fit::DataRange&);
   TF1*        GetFitFunction();
   TList*      GetFitObjectListOfFunctions();
   void        DrawSelection(bool restore = false);
   Int_t       CheckFunctionString(const char* str);
   void        CreateFunctionGroup();
   void        CreateGeneralTab();
   void        CreateMinimizationTab();
   void        MakeTitle(TGCompositeFrame *parent, const char *title);
   TF1*        HasFitFunction();
   void        SetEditable(Bool_t);

private:
   TFitEditor(const TFitEditor&);              // not implemented
   TFitEditor& operator=(const TFitEditor&);   // not implemented

   void RetrieveOptions(Foption_t&, TString&, ROOT::Math::MinimizerOptions&, Int_t);

public:
   TFitEditor(TVirtualPad* pad, TObject *obj);
   virtual ~TFitEditor();

   TList*  GetListOfFittingFunctions(TObject* obj = 0);

   static  TFitEditor *GetInstance(TVirtualPad* pad = 0, TObject *obj = 0);
   virtual Option_t  *GetDrawOption() const;
   virtual void       Hide();
   virtual void       Show(TVirtualPad* pad, TObject *obj);

           void       ShowObjectName(TObject* obj);
           Bool_t     SetObjectType(TObject* obj);
   virtual void       Terminate();
           void       UpdateGUI();

   virtual void   CloseWindow();
   virtual void   ConnectSlots();
   virtual void   DisconnectSlots();
   virtual void   RecursiveRemove(TObject* obj);

protected:
   virtual void   SetCanvas(TCanvas *c);

public:
   virtual void   SetFitObject(TVirtualPad *pad, TObject *obj, Int_t event);
   virtual void   SetFunction(const char *function);

   // slot methods 'General' tab
   void           FillFunctionList(Int_t selected = -1);
   void           FillMinMethodList(Int_t selected = -1);
   virtual void   DoAddition(Bool_t on);
   virtual void   DoNormAddition(Bool_t on);
   virtual void   DoConvolution(Bool_t on);
   virtual void   DoAdvancedOptions();
   virtual void   DoAllWeights1();
   virtual void   DoClose();
   virtual void   DoEmptyBinsAllWeights1();
   virtual void   DoEnteredFunction();
   virtual void   DoUpdate();
   virtual void   DoFit();
   virtual void   DoMaxIterations();
   virtual void   DoDataSet(Int_t sel);
   virtual void   DoFunction(Int_t sel);
   virtual void   DoLinearFit();
   virtual void   DoNoChi2();
   virtual void   DoNoSelection();
   virtual void   DoNoStoreDrawing();
   virtual void   DoReset();
   virtual void   DoRobustFit();
   virtual void   DoSetParameters();
   virtual void   DoSliderXMoved();
   virtual void   DoNumericSliderXChanged();
   virtual void   DoSliderYMoved();
   virtual void   DoNumericSliderYChanged();
   virtual void   DoSliderZMoved();
   virtual void   DoUserDialog();
   virtual void   DoUseFuncRange();

   // slot methods 'Minimization' tab
   virtual void   DoLibrary(Bool_t on);
   virtual void   DoMinMethod(Int_t );
   virtual void   DoPrintOpt(Bool_t on);

public:
   typedef std::vector<FuncParamData_t > FuncParams_t;

   friend class FitEditorUnitTesting;
   ClassDef(TFitEditor,0)  //Fit Panel interface
};

#endif
 TFitEditor.h:1
 TFitEditor.h:2
 TFitEditor.h:3
 TFitEditor.h:4
 TFitEditor.h:5
 TFitEditor.h:6
 TFitEditor.h:7
 TFitEditor.h:8
 TFitEditor.h:9
 TFitEditor.h:10
 TFitEditor.h:11
 TFitEditor.h:12
 TFitEditor.h:13
 TFitEditor.h:14
 TFitEditor.h:15
 TFitEditor.h:16
 TFitEditor.h:17
 TFitEditor.h:18
 TFitEditor.h:19
 TFitEditor.h:20
 TFitEditor.h:21
 TFitEditor.h:22
 TFitEditor.h:23
 TFitEditor.h:24
 TFitEditor.h:25
 TFitEditor.h:26
 TFitEditor.h:27
 TFitEditor.h:28
 TFitEditor.h:29
 TFitEditor.h:30
 TFitEditor.h:31
 TFitEditor.h:32
 TFitEditor.h:33
 TFitEditor.h:34
 TFitEditor.h:35
 TFitEditor.h:36
 TFitEditor.h:37
 TFitEditor.h:38
 TFitEditor.h:39
 TFitEditor.h:40
 TFitEditor.h:41
 TFitEditor.h:42
 TFitEditor.h:43
 TFitEditor.h:44
 TFitEditor.h:45
 TFitEditor.h:46
 TFitEditor.h:47
 TFitEditor.h:48
 TFitEditor.h:49
 TFitEditor.h:50
 TFitEditor.h:51
 TFitEditor.h:52
 TFitEditor.h:53
 TFitEditor.h:54
 TFitEditor.h:55
 TFitEditor.h:56
 TFitEditor.h:57
 TFitEditor.h:58
 TFitEditor.h:59
 TFitEditor.h:60
 TFitEditor.h:61
 TFitEditor.h:62
 TFitEditor.h:63
 TFitEditor.h:64
 TFitEditor.h:65
 TFitEditor.h:66
 TFitEditor.h:67
 TFitEditor.h:68
 TFitEditor.h:69
 TFitEditor.h:70
 TFitEditor.h:71
 TFitEditor.h:72
 TFitEditor.h:73
 TFitEditor.h:74
 TFitEditor.h:75
 TFitEditor.h:76
 TFitEditor.h:77
 TFitEditor.h:78
 TFitEditor.h:79
 TFitEditor.h:80
 TFitEditor.h:81
 TFitEditor.h:82
 TFitEditor.h:83
 TFitEditor.h:84
 TFitEditor.h:85
 TFitEditor.h:86
 TFitEditor.h:87
 TFitEditor.h:88
 TFitEditor.h:89
 TFitEditor.h:90
 TFitEditor.h:91
 TFitEditor.h:92
 TFitEditor.h:93
 TFitEditor.h:94
 TFitEditor.h:95
 TFitEditor.h:96
 TFitEditor.h:97
 TFitEditor.h:98
 TFitEditor.h:99
 TFitEditor.h:100
 TFitEditor.h:101
 TFitEditor.h:102
 TFitEditor.h:103
 TFitEditor.h:104
 TFitEditor.h:105
 TFitEditor.h:106
 TFitEditor.h:107
 TFitEditor.h:108
 TFitEditor.h:109
 TFitEditor.h:110
 TFitEditor.h:111
 TFitEditor.h:112
 TFitEditor.h:113
 TFitEditor.h:114
 TFitEditor.h:115
 TFitEditor.h:116
 TFitEditor.h:117
 TFitEditor.h:118
 TFitEditor.h:119
 TFitEditor.h:120
 TFitEditor.h:121
 TFitEditor.h:122
 TFitEditor.h:123
 TFitEditor.h:124
 TFitEditor.h:125
 TFitEditor.h:126
 TFitEditor.h:127
 TFitEditor.h:128
 TFitEditor.h:129
 TFitEditor.h:130
 TFitEditor.h:131
 TFitEditor.h:132
 TFitEditor.h:133
 TFitEditor.h:134
 TFitEditor.h:135
 TFitEditor.h:136
 TFitEditor.h:137
 TFitEditor.h:138
 TFitEditor.h:139
 TFitEditor.h:140
 TFitEditor.h:141
 TFitEditor.h:142
 TFitEditor.h:143
 TFitEditor.h:144
 TFitEditor.h:145
 TFitEditor.h:146
 TFitEditor.h:147
 TFitEditor.h:148
 TFitEditor.h:149
 TFitEditor.h:150
 TFitEditor.h:151
 TFitEditor.h:152
 TFitEditor.h:153
 TFitEditor.h:154
 TFitEditor.h:155
 TFitEditor.h:156
 TFitEditor.h:157
 TFitEditor.h:158
 TFitEditor.h:159
 TFitEditor.h:160
 TFitEditor.h:161
 TFitEditor.h:162
 TFitEditor.h:163
 TFitEditor.h:164
 TFitEditor.h:165
 TFitEditor.h:166
 TFitEditor.h:167
 TFitEditor.h:168
 TFitEditor.h:169
 TFitEditor.h:170
 TFitEditor.h:171
 TFitEditor.h:172
 TFitEditor.h:173
 TFitEditor.h:174
 TFitEditor.h:175
 TFitEditor.h:176
 TFitEditor.h:177
 TFitEditor.h:178
 TFitEditor.h:179
 TFitEditor.h:180
 TFitEditor.h:181
 TFitEditor.h:182
 TFitEditor.h:183
 TFitEditor.h:184
 TFitEditor.h:185
 TFitEditor.h:186
 TFitEditor.h:187
 TFitEditor.h:188
 TFitEditor.h:189
 TFitEditor.h:190
 TFitEditor.h:191
 TFitEditor.h:192
 TFitEditor.h:193
 TFitEditor.h:194
 TFitEditor.h:195
 TFitEditor.h:196
 TFitEditor.h:197
 TFitEditor.h:198
 TFitEditor.h:199
 TFitEditor.h:200
 TFitEditor.h:201
 TFitEditor.h:202
 TFitEditor.h:203
 TFitEditor.h:204
 TFitEditor.h:205
 TFitEditor.h:206
 TFitEditor.h:207
 TFitEditor.h:208
 TFitEditor.h:209
 TFitEditor.h:210
 TFitEditor.h:211
 TFitEditor.h:212
 TFitEditor.h:213
 TFitEditor.h:214
 TFitEditor.h:215
 TFitEditor.h:216
 TFitEditor.h:217
 TFitEditor.h:218
 TFitEditor.h:219
 TFitEditor.h:220
 TFitEditor.h:221
 TFitEditor.h:222
 TFitEditor.h:223
 TFitEditor.h:224
 TFitEditor.h:225
 TFitEditor.h:226
 TFitEditor.h:227
 TFitEditor.h:228
 TFitEditor.h:229
 TFitEditor.h:230
 TFitEditor.h:231
 TFitEditor.h:232
 TFitEditor.h:233
 TFitEditor.h:234
 TFitEditor.h:235
 TFitEditor.h:236
 TFitEditor.h:237
 TFitEditor.h:238
 TFitEditor.h:239
 TFitEditor.h:240
 TFitEditor.h:241
 TFitEditor.h:242
 TFitEditor.h:243
 TFitEditor.h:244
 TFitEditor.h:245
 TFitEditor.h:246
 TFitEditor.h:247
 TFitEditor.h:248
 TFitEditor.h:249
 TFitEditor.h:250
 TFitEditor.h:251
 TFitEditor.h:252
 TFitEditor.h:253
 TFitEditor.h:254
 TFitEditor.h:255
 TFitEditor.h:256
 TFitEditor.h:257