Index: trunk/MagicSoft/Cosy/Changelog
===================================================================
--- trunk/MagicSoft/Cosy/Changelog	(revision 2406)
+++ trunk/MagicSoft/Cosy/Changelog	(revision 2407)
@@ -1,3 +1,34 @@
                                                                   -*-*- END -*-*-
+ 2003/10/20 - Thomas Bretz
+ 
+   * base/File.cc:
+     - only close file if f!=NULL
+     
+   * base/MThread.cc:
+     - added debug output (depending on DEBUG macro)
+     
+   * candrv/vmodican.cc:
+     - do not call exit in case the module couldn't be opened
+     - changes some comments
+     
+   * catalog/StarCatalog.cc:
+     - do not exit in case the catalog was not found
+     - initialize fSao and fSrt with NULL
+     - only delete fSao and fSrt if != NULL
+     - only calculate stars if catalog was loaded successfully
+     
+   * main/MBending.[h,cc]:
+     - added file header
+     - added class decription
+     - added FLOP, TF and TX
+     - changed such that adding new stuff is easier
+     - changed order in correction
+     - replaced all switch statement by simple loops
+     
+   * tpoint/gui.C:
+     - full support for fitting a pointing correction
+
+
+     
  2003/10/15 - Thomas Bretz (La Palma)
  
Index: trunk/MagicSoft/Cosy/base/File.cc
===================================================================
--- trunk/MagicSoft/Cosy/base/File.cc	(revision 2406)
+++ trunk/MagicSoft/Cosy/base/File.cc	(revision 2407)
@@ -12,5 +12,6 @@
 File::~File()
 {
-    fclose(f);
+    if (f)
+        fclose(f);
 }
 
Index: trunk/MagicSoft/Cosy/base/MThread.cc
===================================================================
--- trunk/MagicSoft/Cosy/base/MThread.cc	(revision 2406)
+++ trunk/MagicSoft/Cosy/base/MThread.cc	(revision 2407)
@@ -7,4 +7,5 @@
 
 #undef DEBUG
+//#define DEBUG
 
 // ----------------------------------------------------------------------
@@ -91,5 +92,13 @@
     fStop = false;
 
+#ifdef DEBUG
+    cout << "MThread::RunThread" << endl;
+#endif
+
     void *rc = Thread();
+
+#ifdef DEBUG
+    cout << "MThread::RunThread...done." << endl;
+#endif
 
     fIsRunning = false;
@@ -106,5 +115,7 @@
 {
     MThread *thread = (MThread*)arg;
-
+#ifdef DEBUG
+    cout << "MThread::MapThread" << endl;
+#endif
     return thread->RunThread();
 }
Index: trunk/MagicSoft/Cosy/candrv/vmodican.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 2406)
+++ trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 2407)
@@ -762,6 +762,6 @@
 //  /* can_send - send message to standard host interface (mid priority)
 //   *
-//   * This function sends a complete message to the standard host interface of
-//   * a VMOD-ICAN.
+//   * This function sends a complete message to the standard host interface
+//   * of a VMOD-ICAN.
 //   *
 //   * The message <pm> will be queued to the middle prioritized of the three
@@ -951,5 +951,4 @@
         cout << strerror(errno) << endl;
         return;
-//        exit(1);                       // open module
     }
 
Index: trunk/MagicSoft/Cosy/catalog/StarCatalog.cc
===================================================================
--- trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 2406)
+++ trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 2407)
@@ -14,5 +14,5 @@
 ClassImp(StarCatalog);
 
-StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fEntries(0), fSinAngle(0), fCosAngle(1)
+StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fSao(NULL), fSrt(NULL), fEntries(0), fSinAngle(0), fCosAngle(1)
 {
     // p = pointer to MainFrame (not owner)
@@ -22,7 +22,6 @@
     //
     File idx("sao/sao-sort.idx", "r");
-
     if (!idx)
-        exit(0);
+        return;
 
     while (!idx.Eof())
@@ -52,6 +51,8 @@
 StarCatalog::~StarCatalog()
 {
-    delete fSrt;
-    delete fSao;
+    if (fSrt)
+        delete fSrt;
+    if (fSao)
+        delete fSao;
 }
 
@@ -647,4 +648,7 @@
     // --------- search for stars in catalog ----------
     //
+    if (fEntries==0)
+        return;
+
     int count   = 0;
     int deleted = 0;
Index: trunk/MagicSoft/Cosy/main/MBending.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MBending.cc	(revision 2406)
+++ trunk/MagicSoft/Cosy/main/MBending.cc	(revision 2407)
@@ -1,2 +1,60 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!
+!   Author(s): Thomas Bretz, 2003 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2003
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// MBending
+//
+//    Double_t fIe   ; // [rad] Index Error in Elevation
+//    Double_t fIa   ; // [rad] Index Error in Azimuth
+//
+//    Double_t fFlop ; // [rad] Vertical Sag
+//     * do not use if not data: Zd<0
+//
+//    Double_t fNpae ; // [rad] Az-El Nonperpendicularity
+//
+//    Double_t fCa   ; // [rad] Left-Right Collimation Error
+//
+//    Double_t fAn   ; // [rad] Azimuth Axis Misalignment (N-S)
+//    Double_t fAw   ; // [rad] Azimuth Axis Misalignment (E-W)
+//
+//    Double_t fTf   ; // [rad] Tube fluxture (sin)
+//     * same as ecec if no data: Zd<0
+//    Double_t fTx   ; // [rad] Tube fluxture (tan)
+//     * do not use with NPAE if no data: Zd<0
+//
+//    Double_t fNrx  ; // [rad] Nasmyth rotator displacement, horizontan
+//    Double_t fNry  ; // [rad] Nasmyth rotator displacement, vertical
+//
+//    Double_t fCrx  ; // [rad] Alt/Az Coude Displacement (N-S)
+//    Double_t fCry  ; // [rad] Alt/Az Coude Displacement (E-W)
+//
+//    Double_t fEces ; // [rad] Elevation Centering Error (sin)
+//    Double_t fAces ; // [rad] Azimuth Centering Error (sin)
+//    Double_t fEcec ; // [rad] Elevation Centering Error (cos)
+//    Double_t fAcec ; // [rad] Azimuth Centering Error (cos)
+//
+////////////////////////////////////////////////////////////////////////////
 #include "MBending.h"
 
@@ -10,4 +68,52 @@
 ClassImp(MBending);
 
+const Int_t MBending::fNumPar=19;
+
+void MBending::Init()
+{
+    fCoeff = new Double_t*[fNumPar];
+    fName  = new TString[fNumPar];
+    fDescr = new TString[fNumPar];
+
+    fCoeff[ 0] = &fIa;      fName[ 0] = "IA";
+    fCoeff[ 1] = &fIe;      fName[ 1] = "IE";
+    fCoeff[ 2] = &fFlop;    fName[ 2] = "FLOP";
+    fCoeff[ 3] = &fNpae;    fName[ 3] = "NPAE";
+    fCoeff[ 4] = &fCa;      fName[ 4] = "CA";
+    fCoeff[ 5] = &fAn;      fName[ 5] = "AN";
+    fCoeff[ 6] = &fAw;      fName[ 6] = "AW";
+    fCoeff[ 7] = &fTf;      fName[ 7] = "TF";
+    fCoeff[ 8] = &fTx;      fName[ 8] = "TX";
+    fCoeff[ 9] = &fEces;    fName[ 9] = "ECES";
+    fCoeff[10] = &fAces;    fName[10] = "ACES";
+    fCoeff[11] = &fEcec;    fName[11] = "ECEC";
+    fCoeff[12] = &fAcec;    fName[12] = "ACEC";
+    fCoeff[13] = &fNrx;     fName[13] = "NRX";
+    fCoeff[14] = &fNry;     fName[14] = "NRY";
+    fCoeff[15] = &fCrx;     fName[15] = "CRX";
+    fCoeff[16] = &fCry;     fName[16] = "CRY";
+    fCoeff[17] = &fMagic1;  fName[17] = "MAGIC1";
+    fCoeff[18] = &fMagic2;  fName[18] = "MAGIC2";
+
+    fDescr[ 0] =  "Index Error Azimuth";
+    fDescr[ 1] =  "Index Error Zenith Distance";
+    fDescr[ 2] =  "Vertical Sag";
+    fDescr[ 3] =  "Az-El Nonperpendicularity";
+    fDescr[ 4] =  "Left-Right Collimation Error";
+    fDescr[ 5] =  "Azimuth Axis Misalignment (N-S)";
+    fDescr[ 6] =  "Azimuth Axis Misalignment (E-W)";
+    fDescr[ 7] =  "Tube fluxture (sin)";
+    fDescr[ 8] =  "Tube fluxture (tan)";
+    fDescr[ 9] =  "Elevation Centering Error (sin)";
+    fDescr[10] =  "Azimuth Centering Error (sin)";
+    fDescr[11] =  "Elevation Centering Error (cos)";
+    fDescr[12] =  "Azimuth Centering Error (cos)";
+    fDescr[13] =  "Nasmyth rotator displacement (horizontal)";
+    fDescr[14] =  "Nasmyth rotator displacement (vertical)";
+    fDescr[15] =  "Alt/Az Coude Displacement (N-S)";
+    fDescr[16] =  "Alt/Az Coude Displacement (E-W)";
+    fDescr[17] =  "n/a";
+    fDescr[18] =  "n/a";
+}
 void MBending::Reset()
 {
@@ -62,6 +168,6 @@
     cout << endl;
 
-    cout << "  & = Name      Value           Sigma" << endl;
-    cout << "------------------------------------------" << endl;
+    cout << "  & = Name            Value                  Sigma" << endl;
+    cout << "--------------------------------------------------" << endl;
 
     while (fin)
@@ -90,26 +196,40 @@
 
         fin >> val;
-        cout << str << "\t" << val << "°      \t";
+        cout << str << "\t" << setw(11) << val << "°     \t";
         val *= kDeg2Rad;
 
-        if (str=="IA")     fIa   = val;
-        if (str=="IE")     fIe   = val;
-        if (str=="CA")     fCa   = val;
-        if (str=="AN")     fAn   = val;
-        if (str=="AW")     fAw   = val;
-        if (str=="NRX")    fNrx  = val;
-        if (str=="NRY")    fNry  = val;
-        if (str=="CRX")    fCrx  = val;
-        if (str=="CRY")    fCry  = val;
-        if (str=="NPAE")   fNpae = val;
-        if (str=="ECES")   fEces = val;
-        if (str=="ACES")   fAces = val;
-        if (str=="ECEC")   fEcec = val;
-        if (str=="ACEC")   fAcec = val;
-        //if (str=="MAGIC1") fMagic1 = val;
-        //if (str=="MAGIC2") fMagic2 = val;
+        Double_t *dest=NULL;
+
+        if (str=="IA")   dest = &fIa;
+        if (str=="IE")   dest = &fIe;
+        if (str=="FLOP") dest = &fFlop;
+        if (str=="NPAE") dest = &fNpae;
+        if (str=="CA")   dest = &fCa;
+        if (str=="AN")   dest = &fAn;
+        if (str=="AW")   dest = &fAw;
+        if (str=="TF")   dest = &fTf;
+        if (str=="TX")   dest = &fTx;
+        if (str=="NRX")  dest = &fNrx;
+        if (str=="NRY")  dest = &fNry;
+        if (str=="CRX")  dest = &fCrx;
+        if (str=="CRY")  dest = &fCry;
+        if (str=="ECES") dest = &fEces;
+        if (str=="ACES") dest = &fAces;
+        if (str=="ECEC") dest = &fEcec;
+        if (str=="ACEC") dest = &fAcec;
+
+        if (dest)
+            *dest = val;
 
         fin >> val;
-        cout << val*kDeg2Rad << endl;
+        cout << setw(9) << val << "°" << endl;
+
+        // Find corresponding error
+        for (int i=0; i<MBending::GetNumPar(); i++)
+            if (dest==fCoeff[i])
+            {
+                fError[i] = val*kDeg2Rad;
+                break;
+            }
     }
     cout << endl;
@@ -143,21 +263,19 @@
     fout << "S   00   000000   000000  0000000" << endl;
     fout << setprecision(8);
-    fout << " IA     " << kRad2Deg*fIa   << " -1" << endl;
-    fout << " IE     " << kRad2Deg*fIe   << " -1" << endl;
-    fout << " CA     " << kRad2Deg*fNpae << " -1" << endl;
-    fout << " NPAE   " << kRad2Deg*fCa   << " -1" << endl;
-    fout << " AN     " << kRad2Deg*fAn   << " -1" << endl;
-    fout << " AW     " << kRad2Deg*fAw   << " -1" << endl;
-    fout << " NRX    " << kRad2Deg*fNrx  << " -1" << endl;
-    fout << " NRY    " << kRad2Deg*fNry  << " -1" << endl;
-    fout << " CRX    " << kRad2Deg*fCrx  << " -1" << endl;
-    fout << " CRY    " << kRad2Deg*fCry  << " -1" << endl;
-    fout << " ECES   " << kRad2Deg*fEces << " -1" << endl;
-    fout << " ACES   " << kRad2Deg*fAces << " -1" << endl;
-    fout << " ECEC   " << kRad2Deg*fEcec << " -1" << endl;
-    fout << " ACEC   " << kRad2Deg*fAcec << " -1" << endl;
-    //fout << " MAGIC1 " << -kRad2Deg*fMagic1 << " -1" << endl;
-    //fout << " MAGIC2 " << -kRad2Deg*fMagic2 << " -1" << endl;
+    for (int i=0; i<fNumPar; i++)
+    {
+        fout << " " << setw(6) << GetName(i) << " ";
+        fout << setw(13) << *fCoeff[i]*kRad2Deg << "   ";
+        fout << setw(11) << fError[i]*kRad2Deg << endl;
+    }
     fout << "END" << endl;
+}
+
+Double_t MBending::Sign(Double_t val, Double_t alt)
+{
+    // Some pointing corrections are defined as Delta ZA, which
+    // is (P. Wallace) defined [0,90]deg while Alt is defined
+    // [0,180]deg
+    return (TMath::Pi()/2-alt < 0 ? -val : val);
 }
 
@@ -167,4 +285,14 @@
     // zdaz    [rad]
     AltAz p = aa;
+
+    const AltAz CRX(-fCrx*sin(p.Az()-p.Alt()),  fCrx*cos(p.Az()-p.Alt())/cos(p.Alt()));
+    const AltAz CRY(-fCry*cos(p.Az()-p.Alt()), -fCry*sin(p.Az()-p.Alt())/cos(p.Alt()));
+    p += CRX;
+    p += CRY;
+
+    const AltAz NRX(fNrx*sin(p.Alt()), -fNrx);
+    const AltAz NRY(fNry*cos(p.Alt()), -fNry*tan(p.Alt()));
+    p += NRX;
+    p += NRY;
 
     const AltAz CES(-fEces*sin(p.Alt()), -fAces*sin(p.Az()));
@@ -173,19 +301,8 @@
     p += CEC;
 
-    //    const AltAz MAGIC(0, -fMagic1*tan(p.Alt())-fMagic2);
-    //    p += MAGIC;
-
-    const AltAz CRX(-fCrx*sin(p.Az()-p.Alt()),  fCrx*cos(p.Az()-p.Alt())/cos(p.Alt()));
-    const AltAz CRY(-fCry*cos(p.Az()-p.Alt()), -fCry*sin(p.Az()-p.Alt())/cos(p.Alt()));
-    p += CRX;
-    p += CRY;
-
-    const AltAz NRX(fNrx*sin(p.Alt()), -fNrx);
-    const AltAz NRY(fNry*cos(p.Alt()), -fNry*tan(p.Alt()));
-    p += NRX;
-    p += NRY;
-
-    //    const AltAz MAGIC(-fMagic1*sin(p.Az()-fMagic2), 0);
-    //    p += MAGIC;
+    const AltAz TX(Sign(fTx/tan(p.Alt()), p.Alt()), 0);
+    const AltAz TF(Sign(fTf*cos(p.Alt()), p.Alt()), 0);
+    p += TX;
+    p += TF;
 
     const AltAz AW( fAw*sin(p.Az()), -fAw*cos(p.Az())*tan(p.Alt()));
@@ -194,7 +311,4 @@
     p += AN;
 
-    //    const AltAz MAGIC(-fMagic1*sin(p.Az()-fMagic2), 0);
-    //    p += MAGIC;
-
     const AltAz CA(0, -fCa/cos(p.Alt()));
     p += CA;
@@ -202,4 +316,7 @@
     const AltAz NPAE(0, -fNpae*tan(p.Alt()));
     p += NPAE;
+
+    const AltAz FLOP(Sign(fFlop, p.Alt()), 0);
+    p += FLOP;
 
     const AltAz I(fIe, fIa);
@@ -217,4 +334,7 @@
     const AltAz I(fIe, fIa);
     p -= I;
+
+    const AltAz FLOP(Sign(fFlop, p.Alt()), 0);
+    p -= FLOP;
 
     const AltAz NPAE(0, -fNpae*tan(p.Alt()));
@@ -229,6 +349,13 @@
     p -= AW;
 
-    //    const AltAz MAGIC(-fMagic1*sin(p.Az()-fMagic2), 0);
-    //    p -= MAGIC;
+    const AltAz TF(Sign(fTf*cos(p.Alt()), p.Alt()), 0);
+    const AltAz TX(Sign(fTx/tan(p.Alt()), p.Alt()), 0);
+    p -= TF;
+    p -= TX;
+
+    const AltAz CEC(-fEcec*cos(p.Alt()), -fAcec*cos(p.Az()));
+    const AltAz CES(-fEces*sin(p.Alt()), -fAces*sin(p.Az()));
+    p -= CEC;
+    p -= CES;
 
     const AltAz NRY(fNry*cos(p.Alt()), -fNry*tan(p.Alt()));
@@ -241,9 +368,4 @@
     p -= CRY;
     p -= CRX;
-
-    const AltAz CEC(-fEcec*cos(p.Alt()), -fAcec*cos(p.Az()));
-    const AltAz CES(-fEces*sin(p.Alt()), -fAces*sin(p.Az()));
-    p -= CEC;
-    p -= CES;
 
     return p;
@@ -272,78 +394,12 @@
     Clear();
 
-    switch (n)
-    {
-    case 16:
-        fMagic2=par[15]/kRad2Deg; // Magic User Defined
-    case 15:
-        fMagic1=par[14]/kRad2Deg; // Magic User Defined
-    case 14:
-        fAcec =par[13]/kRad2Deg; // Azimuth Centering Error (cos)
-    case 13:
-        fEcec =par[12]/kRad2Deg; // Elevation Centering Error (cos)
-    case 12:
-        fAces =par[11]/kRad2Deg; // Azimuth Centering Error (sin)
-    case 11:
-        fEces =par[10]/kRad2Deg; // Elevation Centering Error (sin)
-    case 10:
-        fCry  =par[9] /kRad2Deg; // Alt/Az Coude Displacement (E-W)
-    case 9:
-        fCrx  =par[8] /kRad2Deg; // Alt/Az Coude Displacement (N-S)
-    case 8:
-        fNry  =par[7] /kRad2Deg; // Nasmyth rotator displacement, vertical
-    case 7:
-        fNrx  =par[6] /kRad2Deg; // Nasmyth rotator displacement, horizontan
-    case 6:
-        fAw   =par[5] /kRad2Deg; // Azimuth Axis Misalignment (E-W)
-    case 5:
-        fAn   =par[4] /kRad2Deg; // Azimuth Axis Misalignment (N-S)
-    case 4:
-        fCa   =par[3] /kRad2Deg; // Left-Right Collimation Error
-    case 3:
-        fNpae =par[2] /kRad2Deg; // Az-El Nonperpendicularity
-    case 2:
-        fIe   =par[1] /kRad2Deg; // Index Error in Elevation
-    case 1:
-        fIa   =par[0] /kRad2Deg; // Index Error in Azimuth
-    }
+    while (n--)
+        *fCoeff[n] = par[n]/kRad2Deg;
 }
 
 void MBending::GetParameters(Double_t *par, Int_t n) const
 {
-    switch (n)
-    {
-    case 16:
-        par[15]=kRad2Deg*fMagic2; //
-    case 15:
-        par[14]=kRad2Deg*fMagic1; //
-    case 14:
-        par[13]=kRad2Deg*fAcec; // Azimuth Centering Error (cos)
-    case 13:
-        par[12]=kRad2Deg*fEcec; // Elevation Centering Error (cos)
-    case 12:
-        par[11]=kRad2Deg*fAces; // Azimuth Centering Error (sin)
-    case 11:
-        par[10]=kRad2Deg*fEces; // Elevation Centering Error (sin)
-    case 10:
-        par[9] =kRad2Deg*fCry;  // Alt/Az Coude Displacement (E-W)
-    case 9:
-        par[8] =kRad2Deg*fCrx;  // Alt/Az Coude Displacement (N-S)
-    case 8:
-        par[7] =kRad2Deg*fNry;  // Nasmyth rotator displacement, vertical
-    case 7:
-        par[6] =kRad2Deg*fNrx;  // Nasmyth rotator displacement, horizontan
-    case 6:
-        par[5] =kRad2Deg*fAw;   // Azimuth Axis Misalignment (E-W)
-    case 5:
-        par[4] =kRad2Deg*fAn;   // Azimuth Axis Misalignment (N-S)
-    case 4:
-        par[3] =kRad2Deg*fCa;   // Left-Right Collimation Error
-    case 3:
-        par[2] =kRad2Deg*fNpae; // Az-El Nonperpendicularity
-    case 2:
-        par[1] =kRad2Deg*fIe;   // Index Error in Elevation
-    case 1:
-        par[0] =kRad2Deg*fIa;   // Index Error in Azimuth
-    }
+    while (n--)
+        par[n] = *fCoeff[n]*kRad2Deg;
 }
 
@@ -351,43 +407,10 @@
 {
     if (n<0)
-        n = 16;
+        n = fNumPar;
 
     Int_t ierflg = 0;
 
-    switch (n)
-    {
-    case 16:
-        m.mnparm(15,"MAGIC2", fMagic2*kRad2Deg,  1, -360, 360, ierflg);
-    case 15:
-        m.mnparm(14,"MAGIC1", fMagic1*kRad2Deg,  1, -360, 360, ierflg);
-    case 14:
-        m.mnparm(13,"ACEC", fAcec*kRad2Deg,  1, -360, 360, ierflg);
-    case 13:
-        m.mnparm(12,"ECEC", fEcec*kRad2Deg,  1, -360, 360, ierflg);
-    case 12:
-        m.mnparm(11,"ACES", fAcec*kRad2Deg,  1, -360, 360, ierflg);
-    case 11:
-        m.mnparm(10,"ECES", fEcec*kRad2Deg,  1, -360, 360, ierflg);
-    case 10:
-        m.mnparm(9, "CRY",  fCry*kRad2Deg,  1, -360, 360, ierflg);
-    case 9:
-        m.mnparm(8, "CRX",  fCrx*kRad2Deg,  1, -360, 360, ierflg);
-    case 8:
-        m.mnparm(7, "NRY",  fNry*kRad2Deg,  1, -360, 360, ierflg);
-    case 7:
-        m.mnparm(6, "NRX",  fNrx*kRad2Deg,  1, -360, 360, ierflg);
-    case 6:
-        m.mnparm(5, "AW",   fAw*kRad2Deg,   1, -360, 360, ierflg);
-    case 5:
-        m.mnparm(4, "AN",   fAn*kRad2Deg,   1, -360, 360, ierflg);
-    case 4:
-        m.mnparm(3, "CA",   fCa*kRad2Deg,   1, -360, 360, ierflg);
-    case 3:
-        m.mnparm(2, "NPAE", fNpae*kRad2Deg, 1, -360, 360, ierflg);
-    case 2:
-        m.mnparm(1, "IE",   fIe*kRad2Deg,   1,  -90,  90, ierflg);
-    case 1:
-        m.mnparm(0, "IA",   fIa*kRad2Deg,   1, -360, 360, ierflg);
-    }
+    while (n--)
+        m.mnparm(n, fName[n], *fCoeff[n]*kRad2Deg,  1, -360, 360, ierflg);
 }
 
@@ -397,56 +420,9 @@
         n = m.GetNumPars();
 
-    Double_t err;
-
-    switch (n)
-    {
-    case 16:
-        m.GetParameter(15, fMagic2, err);
-        fMagic2 /= kRad2Deg;
-    case 15:
-        m.GetParameter(14, fMagic1, err);
-        fMagic1 /= kRad2Deg;
-    case 14:
-        m.GetParameter(13, fAcec, err);
-        fAcec /= kRad2Deg;
-    case 13:
-        m.GetParameter(12, fEcec, err);
-        fEcec /= kRad2Deg;
-    case 12:
-        m.GetParameter(11, fAces, err);
-        fAces /= kRad2Deg;
-    case 11:
-        m.GetParameter(10, fEces, err);
-        fEces /= kRad2Deg;
-    case 10:
-        m.GetParameter(9, fCry, err);
-        fCry /= kRad2Deg;
-    case 9:
-        m.GetParameter(8, fCrx, err);
-        fCrx /= kRad2Deg;
-    case 8:
-        m.GetParameter(7, fNry, err);
-        fNry /= kRad2Deg;
-    case 7:
-        m.GetParameter(6, fNrx, err);
-        fNrx /= kRad2Deg;
-    case 6:
-        m.GetParameter(5, fAw, err);
-        fAw /= kRad2Deg;
-    case 5:
-        m.GetParameter(4, fAn, err);
-        fAn /= kRad2Deg;
-    case 4:
-        m.GetParameter(3, fCa, err);
-        fCa /= kRad2Deg;
-    case 3:
-        m.GetParameter(2, fNpae, err);
-        fNpae /= kRad2Deg;
-    case 2:
-        m.GetParameter(1, fIe, err);
-        fIe /= kRad2Deg;
-    case 1:
-        m.GetParameter(0, fIa, err);
-        fIa /= kRad2Deg;
+    while (n--)
+    {
+        m.GetParameter(n, *fCoeff[n], fError[n]);
+        *fCoeff[n] /= kRad2Deg;
+        fError[n]  /= kRad2Deg;
     }
 }
@@ -480,54 +456,9 @@
     Double_t par, err;
 
-    switch (n)
-    {
-    case 16:
-        m.GetParameter(15, par, err);
-        cout << " 15 MAGIC2: " << setw(8) << par << " +- " << setw(4) <<  err << endl;
-    case 15:
-        m.GetParameter(14, par, err);
-        cout << " 14 MAGIC1: " << setw(8) << par << " +- " << setw(4) <<  err << endl;
-    case 14:
-        m.GetParameter(13, par, err);
-        cout << " 13 ACEC:   " << setw(8) << par << " +- " << setw(4) <<  err << "  Azimuth Centering Error (cos)" << endl;
-    case 13:
-        m.GetParameter(12, par, err);
-        cout << " 12 ECEC:   " << setw(8) << par << " +- " << setw(4) <<  err << "  Elevation Centering Error (cos)" << endl;
-    case 12:
-        m.GetParameter(11, par, err);
-        cout << " 11 ACES:   " << setw(8) << par << " +- " << setw(4) <<  err << "  Azimuth Centering Error (sin)" << endl;
-    case 11:
-        m.GetParameter(10, par, err);
-        cout << " 10 ECES:   " << setw(8) << par << " +- " << setw(4) <<  err << "  Elevation Centering Error (sin)" << endl;
-    case 10:
-        m.GetParameter(9, par, err);
-        cout << "  9 CRY:    " << setw(8) << par << " +- " << setw(4) <<  err << "  Alt/Az Coude Displacement (E-W)" << endl;
-    case 9:
-        m.GetParameter(8, par, err);
-        cout << "  8 CRX:    " << setw(8) << par << " +- " << setw(4) <<  err << "  Alt/Az Coude Displacement (N-S)" << endl;
-    case 8:
-        m.GetParameter(7, par, err);
-        cout << "  7 NRY:    " << setw(8) << par << " +- " << setw(4) <<  err << "  Nasmyth rotator displacement, vertical" << endl;
-    case 7:
-        m.GetParameter(6, par, err);
-        cout << "  6 NRX:    " << setw(8) << par << " +- " << setw(4) <<  err << "  Nasmyth rotator displacement, horizontan" << endl;
-    case 6:
-        m.GetParameter(5, par, err);
-        cout << "  5 AW:     " << setw(8) << par << " +- " << setw(4) <<  err << "  Azimuth Axis Misalignment (E-W)" << endl;
-    case 5:
-        m.GetParameter(4, par, err);
-        cout << "  4 AN:     " << setw(8) << par << " +- " << setw(4) <<  err << "  Azimuth Axis Misalignment (N-S)" << endl;
-    case 4:
-        m.GetParameter(3, par, err);
-        cout << "  3 CA:     " << setw(8) << par << " +- " << setw(4) <<  err << "  Left-Right Collimation Error" << endl;
-    case 3:
-        m.GetParameter(2, par, err);
-        cout << "  2 NPAE:   " << setw(8) << par << " +- " << setw(4) <<  err << "  Az-El Nonperpendicularity" << endl;
-    case 2:
-        m.GetParameter(1, par, err);
-        cout << "  1 IE:     " << setw(8) << par << " +- " << setw(4) <<  err << "  Index Error Elevation (Offset)" << endl;
-    case 1:
-        m.GetParameter(0, par, err);
-        cout << "  0 IA:     " << setw(8) << par << " +- " << setw(4) <<  err << "  Index Error Azimuth (Offset)" << endl;
-    }
-}
+    while (n--)
+    {
+        m.GetParameter(n, par, err);
+        cout << Form(" %2d %6s: ", n, (const char*)fName[n]);
+        cout << setw(8) << par << " \xb1 " << setw(6) <<  err << endl;
+    }
+}
Index: trunk/MagicSoft/Cosy/main/MBending.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MBending.h	(revision 2406)
+++ trunk/MagicSoft/Cosy/main/MBending.h	(revision 2407)
@@ -3,4 +3,8 @@
 
 #include "coord.h"
+
+#ifndef ROOT_TArrayD
+#include <TArrayD.h>
+#endif
 
 class TMinuit;
@@ -9,11 +13,16 @@
 {
 private:
+    static const Int_t fNumPar;
+
     Double_t fIe   ; // [rad] Index Error in Elevation
     Double_t fIa   ; // [rad] Index Error in Azimuth
+    Double_t fFlop ; // [rad] Vertical Sag
+    Double_t fNpae ; // [rad] Az-El Nonperpendicularity
     Double_t fCa   ; // [rad] Left-Right Collimation Error
     Double_t fAn   ; // [rad] Azimuth Axis Misalignment (N-S)
     Double_t fAw   ; // [rad] Azimuth Axis Misalignment (E-W)
-    Double_t fNpae ; // [rad] Az-El Nonperpendicularity
-    Double_t fNrx  ; // [rad] Nasmyth rotator displacement, horizontan
+    Double_t fTf   ; // [rad] Tube fluxture (sin)
+    Double_t fTx   ; // [rad] Tube fluxture (tan)
+    Double_t fNrx  ; // [rad] Nasmyth rotator displacement, horizontal
     Double_t fNry  ; // [rad] Nasmyth rotator displacement, vertical
     Double_t fCrx  ; // [rad] Alt/Az Coude Displacement (N-S)
@@ -26,28 +35,27 @@
     Double_t fMagic2; // [rad] Magic Term (what is it?)
 
+    Double_t **fCoeff; //!
+    TString   *fName;  //!
+    TString   *fDescr; //!
+
+    TArrayD   fError;
+
+    void Init();
 
     void Clear()
     {
-        fIe  =0 ; // Index Error in Elevation
-        fIa  =0 ; // Index Error in Azimuth
-        fCa  =0 ; // Left-Right Collimation Error
-        fAn  =0 ; // Azimuth Axis Misalignment (N-S)
-        fAw  =0 ; // Azimuth Axis Misalignment (E-W)
-        fNpae=0 ; // Az-El Nonperpendicularity
-        fNrx =0 ; // Nasmyth rotator displacement, horizontan
-        fNry =0 ; // Nasmyth rotator displacement, vertical
-        fCrx =0 ; // Alt/Az Coude Displacement (N-S)
-        fCry =0 ; // Alt/Az Coude Displacement (E-W)
-        fEces=0 ; // Elevation Centering Error (sin)
-        fAces=0 ; // Azimuth Centering Error (sin)
-        fEcec=0 ; // Elevation Centering Error (cos)
-        fAcec=0 ; // Azimuth Centering Error (cos)
-        fMagic1=0; // Azimuth Centering Error (cos)
-        fMagic2=0; // Azimuth Centering Error (cos)
+        for (int i=0; i<fNumPar; i++)
+        {
+            *fCoeff[i] = 0;
+            fError[i] = 0;
+        }
     }
 
+    static Double_t Sign(Double_t val, Double_t alt);
+
 public:
-    MBending() { Clear(); }
-    MBending(const char *name) { Clear(); Load(name); }
+    MBending() { fError.Set(fNumPar); Init(); Clear(); }
+    MBending(const char *name) { fError.Set(fNumPar); Init(); Clear(); Load(name); }
+    virtual ~MBending() { delete fName; delete fCoeff; delete fDescr; }
 
     void Load(const char *name);
@@ -67,5 +75,5 @@
     ZdAz operator()(const ZdAz &zdaz, void (*fcn)(ZdAz &zdaz, Double_t *par)) const
     {
-        Double_t par[16];
+        Double_t par[fNumPar];
         GetParameters(par);
         ZdAz za = zdaz;
@@ -76,5 +84,5 @@
     AltAz operator()(const AltAz &aaz, void (*fcn)(AltAz &aaz, Double_t *par)) const
     {
-        Double_t par[16];
+        Double_t par[fNumPar];
         GetParameters(par);
         AltAz aa = aaz;
@@ -83,10 +91,29 @@
     }
 
-    void SetParameters(const Double_t *par, Int_t n=16);
-    void GetParameters(Double_t *par, Int_t n=16) const;
+    void SetParameters(const Double_t *par, Int_t n=fNumPar);
+    void GetParameters(Double_t *par, Int_t n=fNumPar) const;
+
+    void SetParameters(const TArrayD &par)
+    {
+        SetParameters(par.GetArray(), par.GetSize());
+    }
+    void GetParameters(TArrayD &par) const
+    {
+        par.Set(fNumPar);
+        GetParameters(par.GetArray());
+    }
+    void GetError(TArrayD &par) const
+    {
+        par = fError;
+        for (int i=0; i<fNumPar; i++)
+            par[i] *= kRad2Deg;
+    }
 
     void SetMinuitParameters(TMinuit &m, Int_t n=-1) const;
     void GetMinuitParameters(TMinuit &m, Int_t n=-1);
     void PrintMinuitParameters(TMinuit &m, Int_t n=-1) const;
+
+    const TString &GetName(int i) const { return fName[i]; }
+    const TString &GetDescription(int i) const { return fDescr[i]; }
 
     /*
@@ -107,4 +134,5 @@
      */
 
+    static const Int_t GetNumPar() { return fNumPar; }
     ClassDef(MBending, 1)
 };
Index: trunk/MagicSoft/Cosy/tpoint/gui.C
===================================================================
--- trunk/MagicSoft/Cosy/tpoint/gui.C	(revision 2406)
+++ trunk/MagicSoft/Cosy/tpoint/gui.C	(revision 2407)
@@ -1,3 +1,5 @@
+#include <fstream.h>
 #include <iostream.h>
+#include <iomanip.h>
 
 #include <TGFrame.h>
@@ -5,6 +7,121 @@
 #include <TGButton.h>
 
+#include <TF1.h>
+#include <TH1.h>
+#include <TH2.h>
+#include <TGraph.h>
+
+#include <TList.h>
+#include <TStyle.h>
+#include <TMinuit.h>
+
+#include <TView.h>
+#include <TLine.h>
+#include <TMarker.h>
+#include <TCanvas.h>
+
+#include "coord.h"
+
 #include "MGList.h"
-
+#include "MBending.h"
+
+class Set : public TObject
+{
+    friend ifstream &operator>>(ifstream &fin, Set &set);
+private:
+    Double_t fStarAz;
+    Double_t fStarEl;
+
+    Double_t fRawAz;
+    Double_t fRawEl;
+
+public:
+    Set(Double_t sel=0, Double_t saz=0, Double_t rel=0, Double_t raz=0) :
+        fStarAz(saz*TMath::Pi()/180),
+        fStarEl(sel*TMath::Pi()/180),
+        fRawAz(raz*TMath::Pi()/180),
+        fRawEl(rel*TMath::Pi()/180)
+    {
+    }
+
+    Double_t GetResidual() const
+    {
+         Double_t del = fRawEl-fStarEl;
+         Double_t daz = fRawAz-fStarAz;
+         Double_t dphi2 = daz/2.;
+         Double_t cos2  = cos(dphi2)*cos(dphi2);
+         Double_t sin2  = sin(dphi2)*sin(dphi2);
+         Double_t d = cos(del)*cos2 - cos(fRawEl+fStarEl)*sin2;
+
+         Double_t dist = acos(d);
+
+         return dist * 180 / TMath::Pi();
+    }
+
+    void operator=(Set &set)
+    {
+        fStarAz = set.fStarAz;
+        fStarEl = set.fStarEl;
+        fRawAz  = set.fRawAz;
+        fRawEl  = set.fRawEl;
+    }
+
+    Double_t GetDEl() const     { return (fRawEl-fStarEl)*180/TMath::Pi(); }
+    Double_t GetDZd() const     { return -GetDEl(); }
+    Double_t GetDAz() const     { return (fRawAz-fStarAz)*180/TMath::Pi(); }
+    Double_t GetStarEl() const  { return fStarEl*180/TMath::Pi(); }
+    Double_t GetStarZd() const  { return 90.-fStarEl*180/TMath::Pi(); }
+    Double_t GetStarAz() const  { return fStarAz*180/TMath::Pi(); }
+    Double_t GetRawEl() const   { return fRawEl*180/TMath::Pi(); }
+    Double_t GetRawAz() const   { return fRawAz*180/TMath::Pi(); }
+    Double_t GetRawZd() const   { return 90.-fRawEl*180/TMath::Pi(); }
+
+    ZdAz  GetStarZdAz() const   { return ZdAz(TMath::Pi()/2-fStarEl, fStarAz); }
+    AltAz GetStarAltAz() const  { return AltAz(fStarEl, fStarAz); }
+
+    ZdAz  GetRawZdAz() const    { return ZdAz(TMath::Pi()/2-fRawEl, fRawAz); }
+    AltAz GetRawAltAz() const   { return AltAz(fRawEl, fRawAz); }
+
+    void AdjustEl(Double_t del) { fStarEl += del*TMath::Pi()/180; }
+    void AdjustAz(Double_t daz) { fStarAz += daz*TMath::Pi()/180; }
+
+    void Adjust(const MBending &bend)
+    {
+        AltAz p = bend(GetStarAltAz());
+        fStarEl = p.Alt();
+        fStarAz = p.Az();
+    }
+    void AdjustBack(const MBending &bend)
+    {
+        AltAz p = bend.CorrectBack(GetRawAltAz());
+        fRawEl = p.Alt();
+        fRawAz = p.Az();
+    }
+    ClassDef(Set, 0)
+};
+
+ClassImp(Set);
+
+ifstream &operator>>(ifstream &fin, Set &set)
+{
+    Double_t v[4];
+    fin >> v[0];
+    fin >> v[1];
+    fin >> v[2];
+    fin >> v[3];
+
+    Double_t dummy;
+    fin>>dummy;
+    fin>>dummy;
+    fin>>dummy;
+
+    set.fStarAz = v[0]*TMath::Pi()/180;
+    set.fStarEl = v[1]*TMath::Pi()/180;
+
+    set.fRawAz  = v[2]*TMath::Pi()/180;
+    set.fRawEl  = v[3]*TMath::Pi()/180;
+
+    return fin;
+}
 
 class MFit : public TGMainFrame
@@ -12,22 +129,553 @@
 private:
     enum {
-        kCbIA,
-        kCbIE,
-        kCbNPAE,
-        kCbCA,
-        kCbAN,
-        kCbAW,
-        kCbCRX,
-        kCbCRY,
-        kCbNRX,
-        kCbNRY
+        kTbFit = 19, //MBending::GetNumPar(), // FIXME!!!
+        kTbLoad,
+        kTbSave,
+        kTbLoadStars,
+        kTbReset,
+        kTbResetStars
     };
 
-    MGList *list;
+    MGList *fList;
+
+    TList fOriginal;
+    TList fCoordinates;
+    TList fLabel;
+
+    MBending fBending;
+
+    FontStruct_t fFont;
+
+    void Fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
+    {
+        f = 0;
+
+        MBending bend;
+        bend.SetParameters(par); // Set Parameters [deg] to MBending
+
+        for (int i=0; i<fCoordinates.GetSize(); i++)
+        {
+            Set set = *(Set*)fCoordinates.At(i);
+
+            set.Adjust(bend);
+
+            Double_t err = 0.02; // [deg] = 1SE
+            Double_t res = set.GetResidual()/err;
+
+            f += res*res;
+        }
+
+        //f /= (fCoordinates.GetSize()-7)*(fCoordinates.GetSize()-7);
+        //f /= fCoordinates.GetSize()*fCoordinates.GetSize();
+        f /= fCoordinates.GetSize();
+    }
+
+    static void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
+    {
+        ((MFit*)gMinuit->GetObjectFit())->Fcn(npar, gin, f, par, iflag);
+    }
+
+    void DrawMarker(TVirtualPad *pad, Double_t r0, Double_t phi0, Double_t r1, Double_t phi1)
+    {
+        TView *view = pad->GetView();
+
+        if (!view)
+        {
+            cout << "No View!" << endl;
+            return;
+        }
+
+        TMarker mark0;
+        TMarker mark1;
+        mark0.SetMarkerStyle(kStar);
+        mark1.SetMarkerStyle(kStar);
+        mark1.SetMarkerColor(kRed);
+    
+        r0 /= 90;
+        r1 /= 90;
+        phi0 *= TMath::Pi()/180;
+        phi1 *= TMath::Pi()/180;
+    
+        Double_t x0[3] = { r0*cos(phi0), r0*sin(phi0), 0};
+        Double_t x1[3] = { r1*cos(phi1), r1*sin(phi1), 0};
+    
+        Double_t y0[3], y1[3];
+    
+        view->WCtoNDC(x0, y0);
+        view->WCtoNDC(x1, y1);
+    
+        mark0.DrawMarker(y0[0], y0[1]);
+        mark1.DrawMarker(y1[0], y1[1]);
+    }
+    
+    void DrawPolLine(TVirtualPad *pad, Double_t r0, Double_t phi0, Double_t r1, Double_t phi1)
+    {
+        TView *view = pad->GetView();
+    
+        if (!view)
+        {
+            cout << "No View!" << endl;
+            return;
+        }
+        /*
+        if (r0<0)
+        {
+            r0 = -r0;
+            phi0 += 180;
+        }
+        if (r1<0)
+        {
+            r1 = -r1;
+            phi1 += 180;
+        }
+    
+        phi0 = fmod(phi0+360, 360);
+        phi1 = fmod(phi1+360, 360);
+    
+        if (phi1-phi0<-180)
+            phi1+=360;
+        */
+        TLine line;
+        line.SetLineWidth(2);
+        line.SetLineColor(kBlue);
+    
+        Double_t p0 = phi0<phi1?phi0:phi1;
+        Double_t p1 = phi0<phi1?phi1:phi0;
+    
+        if (phi0>phi1)
+        {
+            Double_t d = r1;
+            r1 = r0;
+            r0 = d;
+        }
+    
+        r0 /= 90;
+        r1 /= 90;
+    
+        Double_t dr = r1-r0;
+        Double_t dp = p1-p0;
+    
+        Double_t x0[3] = { r0*cos(p0*TMath::Pi()/180), r0*sin(p0*TMath::Pi()/180), 0};
+    
+        for (double i=p0+10; i<p1+10; i+=10)
+        {
+            if (i>p1)
+                i=p1;
+    
+            Double_t r = dr/dp*(i-p0)+r0;
+            Double_t p = TMath::Pi()/180*i;
+    
+            Double_t x1[3] = { r*cos(p), r*sin(p), 0};
+    
+            Double_t y0[3], y1[3];
+    
+            view->WCtoNDC(x0, y0);
+            view->WCtoNDC(x1, y1);
+    
+            line.DrawLine(y0[0], y0[1], y1[0], y1[1]);
+    
+            x0[0] = x1[0];
+            x0[1] = x1[1];
+        }
+    }
+    
+    void DrawSet(TVirtualPad *pad, Set &set, Float_t scale=-1)
+    {
+        Double_t r0   = set.GetRawZd();
+        Double_t phi0 = set.GetRawAz();
+        Double_t r1   = set.GetStarZd();
+        Double_t phi1 = set.GetStarAz();
+    
+        if (r0<0)
+        {
+            r0 = -r0;
+            phi0 += 180;
+        }
+        if (r1<0)
+        {
+            r1 = -r1;
+            phi1 += 180;
+        }
+    
+        phi0 = fmod(phi0+360, 360);
+        phi1 = fmod(phi1+360, 360);
+    
+        if (phi1-phi0<-180)
+            phi1+=360;
+    
+        if (scale<0 || scale>1000)
+            scale = -1;
+    
+        if (scale>0)
+        {
+            Double_t d = r1-r0;
+            r0 += scale*d;
+            r1 -= scale*d;
+            d = phi1-phi0;
+            phi0 += scale*d;
+            phi1 -= scale*d;
+    
+            DrawPolLine(pad, r0, phi0, r1, phi1);
+            DrawMarker(pad,  r0, phi0, r1, phi1);
+        }
+        else
+            DrawMarker(pad,  r1, phi1, 0 ,0);
+    }
+
+    void Fit()
+    {
+        if (fOriginal.GetSize()==0)
+        {
+            cout << "Sorry, no input data loaded..." << endl;
+            return;
+        }
+
+        fCoordinates.Delete();
+        for (int i=0; i<fOriginal.GetSize(); i++)
+            fCoordinates.Add(new Set(*(Set*)fOriginal.At(i)));
+
+        cout << "-----------------------------------------------------------------------" << endl;
+    
+        TH1F hres1("Res1", " Residuals before correction ", fOriginal.GetSize()/2, 0, 3);
+        TH1F hres2("Res2", " Residuals after correction ",  fOriginal.GetSize(), 0, 3);
+        TH1F hres3("Res3", " Residuals after backward correction ",  fOriginal.GetSize(), 0, 3);
+    
+        hres1.SetXTitle("\\Delta [\\circ]");
+        hres1.SetYTitle("Counts");
+    
+        hres2.SetXTitle("\\Delta [\\circ]");
+        hres2.SetYTitle("Counts");
+    
+        hres3.SetXTitle("\\Delta [\\circ]");
+        hres3.SetYTitle("Counts");
+    
+        TGraph gdaz;
+        TGraph gdzd;
+        TGraph gaz;
+        TGraph gzd;
+        TGraph graz;
+        TGraph grzd;
+    
+        gdaz.SetTitle(" \\Delta Az vs. Zd ");
+        gdzd.SetTitle(" \\Delta Zd vs. Az ");
+    
+        gaz.SetTitle(" \\Delta Az vs. Az ");
+        gzd.SetTitle(" \\Delta Zd vs. Zd ");
+
+        graz.SetTitle(" \\Delta vs. Az ");
+        grzd.SetTitle(" \\Delta vs. Zd ");
+    
+        TMinuit minuit(MBending::GetNumPar());  //initialize TMinuit with a maximum of 5 params
+        minuit.SetObjectFit(this);
+        minuit.SetPrintLevel(-1);
+        minuit.SetFCN(fcn);
+    
+        fBending.SetMinuitParameters(minuit, MBending::GetNumPar()); // Init Parameters [deg]
+    
+        for (int i=0; i<MBending::GetNumPar(); i++)
+        {
+            TGButton *l = (TGButton*)fList->FindWidget(i);
+            minuit.FixParameter(i);
+            if (l->GetState()==kButtonDown)
+                minuit.Release(i);
+        }
+    
+        //minuit.Command("SHOW PARAMETERS");
+        //minuit.Command("SHOW LIMITS");
+    
+        cout << endl;
+        cout << "Starting fit..." << endl;
+        cout << "For the fit an measurement error in the residual of ";
+        cout << "0.02deg (=1SE) is assumed." << endl;
+        cout << endl;
+    
+        Int_t ierflg = 0;
+        ierflg = minuit.Migrad();
+        cout << "Migrad returns " << ierflg << endl;
+        // minuit.Release(2);
+        ierflg = minuit.Migrad();
+        cout << "Migrad returns " << ierflg << endl << endl;
+    
+        //
+        // Get Fit Results
+        //
+        fBending.GetMinuitParameters(minuit);
+        fBending.PrintMinuitParameters(minuit);
+        //fBending.Save("bending_magic.txt");
+    
+    
+        //
+        // Make a copy of all list entries
+        //
+        TList list;
+        list.SetOwner();
+        for (int i=0; i<fCoordinates.GetSize(); i++)
+            list.Add(new Set(*(Set*)fCoordinates.At(i)));
+    
+        //
+        // Correct for Offsets only
+        //
+        TArrayD par;
+        fBending.GetParameters(par);
+        for (int i=2; i<MBending::GetNumPar(); i++)
+            par[i]=0;
+    
+        MBending b2;
+        b2.SetParameters(par);
+    
+        //
+        // Calculate correction and residuals
+        //
+        for (int i=0; i<fCoordinates.GetSize(); i++)
+        {
+            Set &set0 = *(Set*)fCoordinates.At(i);
+    
+            ZdAz za = set0.GetStarZdAz()*kRad2Deg;
+    
+            //
+            // Correct for offsets only
+            //
+            Set set1(set0);
+            set1.Adjust(b2);
+    
+            hres1.Fill(set1.GetResidual());
+    
+            set0.Adjust(fBending);
+            hres2.Fill(set0.GetResidual());
+    
+            Double_t dz = fmod(set0.GetDAz()+720, 360);
+            if (dz>180)
+                dz -= 360;
+    
+            gdzd.SetPoint(i, za.Az(), set0.GetDZd());
+            gdaz.SetPoint(i, za.Zd(), dz);
+            graz.SetPoint(i, za.Az(), set0.GetResidual());
+            grzd.SetPoint(i, za.Zd(), set0.GetResidual());
+            gaz.SetPoint( i, za.Az(), dz);
+            gzd.SetPoint( i, za.Zd(), set0.GetDZd());
+
+        }
+    
+        //
+        // Check for overflows
+        //
+        const Stat_t ov = hres2.GetBinContent(hres2.GetNbinsX()+1);
+        if (ov>0)
+            cout << "WARNING: " << ov << " overflows in residuals." << endl;
+    
+    
+    
+        cout << dec << endl;
+        cout << "              Number of calls to FCN: " << minuit.fNfcn << endl;
+        cout << "Minimum value found for FCN (Chi^2?): " << minuit.fAmin << endl;
+        cout << "                  Fit-Probability(?): " << TMath::Prob(minuit.fAmin/*fOriginal.GetSize()*/, fOriginal.GetSize()-minuit.GetNumFreePars())*100 << "%" << endl;
+        cout << "                           Chi^2/NDF: " << minuit.fAmin/(fOriginal.GetSize()-minuit.GetNumFreePars()) << endl;
+        //cout << "Prob(?): " << TMath::Prob(fChisquare,ndf);
+    
+    
+    
+        //
+        // Print all data sets for which the backward correction is
+        // twice times worse than the residual gotten from the
+        // bending correction itself
+        //
+        cout << endl;
+        cout << "Checking backward correction (raw-->star):" << endl;
+        for (int i=0; i<fCoordinates.GetSize(); i++)
+        {
+            Set set0(*(Set*)list.At(i));
+            Set &set1 = *(Set*)list.At(i);
+    
+            set0.AdjustBack(fBending);
+            set1.Adjust(fBending);
+    
+            const Double_t res0 = set0.GetResidual();
+            const Double_t res1 = set1.GetResidual();
+            const Double_t diff = TMath::Abs(res0-res1);
+    
+            hres3.Fill(res0);
+    
+            if (diff<hres2.GetMean()*0.66)
+                continue;
+    
+            cout << "DBack: " << setw(6) << set0.GetStarZd() << " " << setw(7) << set0.GetStarAz() << ":  ";
+            cout << "ResB="<< setw(7) << res0*60 << "  ResF=" << setw(7) << res1*60 << "  |ResB-ResF|=" << setw(7) << diff*60 << " arcmin" << endl;
+        }
+        cout << "OK." << endl;
+        cout << endl;
+    
+
+
+
+        TCanvas *c1;
+
+        if ((c1 = (TCanvas*)gROOT->FindObject("CanvGraphs")))
+            delete c1;
+        if ((c1 = (TCanvas*)gROOT->FindObject("CanvResiduals")))
+            delete c1;
+
+
+
+        c1=new TCanvas("CanvGraphs", "Graphs");
+        c1->Divide(2,3,0,0);
+
+        c1->cd(1);
+        gPad->SetBorderMode(0);
+        TGraph *g=(TGraph*)gaz.DrawClone("A*");
+        g->SetBit(kCanDelete);
+        g->GetHistogram()->SetXTitle("Az [\\circ]");
+        g->GetHistogram()->SetYTitle("\\Delta Az [\\circ]");
+    
+        c1->cd(2);
+        gPad->SetBorderMode(0);
+        g=(TGraph*)gdaz.DrawClone("A*");
+        g->SetBit(kCanDelete);
+        g->GetHistogram()->SetXTitle("Zd [\\circ]");
+        g->GetHistogram()->SetYTitle("\\Delta Az [\\circ]");
+        cout << "Mean dAz: " << g->GetMean(2) << " \xb1 " << g->GetRMS(2) <<  endl;
+    
+        c1->cd(3);
+        gPad->SetBorderMode(0);
+        g=(TGraph*)gdzd.DrawClone("A*");
+        g->SetBit(kCanDelete);
+        g->GetHistogram()->SetXTitle("Az [\\circ]");
+        g->GetHistogram()->SetYTitle("\\Delta Zd [\\circ]");
+        cout << "Mean dZd: " << g->GetMean(2) << " \xb1 " << g->GetRMS(2) <<  endl;
+        cout << endl;
+    
+        c1->cd(4);
+        gPad->SetBorderMode(0);
+        g=(TGraph*)gzd.DrawClone("A*");
+        g->SetBit(kCanDelete);
+        g->GetHistogram()->SetXTitle("Zd [\\circ]");
+        g->GetHistogram()->SetYTitle("\\Delta Zd [\\circ]");
+
+        c1->cd(5);
+        gPad->SetBorderMode(0);
+        g=(TGraph*)graz.DrawClone("A*");
+        g->SetBit(kCanDelete);
+        g->GetHistogram()->SetXTitle("Az [\\circ]");
+        g->GetHistogram()->SetYTitle("\\Delta [\\circ]");
+    
+        c1->cd(6);
+        gPad->SetBorderMode(0);
+        g=(TGraph*)grzd.DrawClone("A*");
+        g->SetBit(kCanDelete);
+        g->GetHistogram()->SetXTitle("Zd [\\circ]");
+        g->GetHistogram()->SetYTitle("\\Delta [\\circ]");
+
+
+
+        //
+        // Print out the residual before and after correction in several
+        // units
+        //
+        cout << fCoordinates.GetSize() << " data sets." << endl << endl;
+        cout << "Total Spread of Residual:" << endl;
+        cout << "-------------------------" << endl;
+        cout << "before: " << hres1.GetMean() << " \xb1 " << hres1.GetRMS() << " deg " << endl;
+        cout << "after:  " << hres2.GetMean() << " \xb1 " << hres2.GetRMS() << " deg " << endl;
+        cout << "before: " << (int)(hres1.GetMean()*60+.5) << " \xb1 " << (int)(hres1.GetRMS()*60+.5) << " arcmin" << endl;
+        cout << "after:  " << (int)(hres2.GetMean()*60+.5) << " \xb1 " << (int)(hres2.GetRMS()*60+.5) << " arcmin" << endl;
+        cout << "before: " << (int)(hres1.GetMean()*60*60/23.4+.5) << " \xb1 " << (int)(hres1.GetRMS()*60*60/23.4+.5) << " pix" << endl;
+        cout << "after:  " << (int)(hres2.GetMean()*60*60/23.4+.5) << " \xb1 " << (int)(hres2.GetRMS()*60*60/23.4+.5) << " pix" << endl;
+        cout << "after:  " << (int)(hres2.GetMean()*16384/360+.5) << " \xb1 " << (int)(hres2.GetRMS()*16384/360+.5) << " SE" << endl;
+        cout << endl;
+        cout << "backw:  " << (int)(hres3.GetMean()*60+.5) << " \xb1 " << (int)(hres3.GetRMS()*60+.5) << " arcmin" << endl;
+        cout << endl;                                            // ±
+    
+    
+    
+        gStyle->SetOptStat(1110);
+        gStyle->SetStatFormat("6.2g");
+
+
+        c1=new TCanvas("CanvResiduals", "Residuals", 800, 800);
+        c1->Divide(2, 2, 0, 0);
+    
+        c1->cd(2);
+        gPad->SetBorderMode(0);
+        hres1.SetLineColor(kRed);
+        hres1.DrawCopy();
+    
+        c1->cd(4);
+        gPad->SetBorderMode(0);
+        hres2.SetLineColor(kBlue);
+        TH1 *h=hres2.DrawCopy();
+        TF1 f("mygaus", "(gaus)", 0, 1);
+        f.SetLineColor(kMagenta/*6*/);
+        f.SetLineWidth(1);
+        f.SetParameter(0, h->GetBinContent(1));
+        f.FixParameter(1, 0);
+        f.SetParameter(2, h->GetRMS());
+        h->Fit("mygaus", "QR");
+        hres3.SetLineColor(kGreen);
+        hres3.SetLineStyle(kDashed);
+        hres3.DrawCopy("same");
+        cout << "Gaus-Fit  Sigma: " << f.GetParameter(2) << "\xb0" << endl;
+        cout << "Fit-Probability: " << f.GetProb()*100 << "%" << endl;
+        cout << "      Chi^2/NDF: " << f.GetChisquare()/f.GetNDF() << endl;
+
+        c1->cd(1);
+        gPad->SetBorderMode(0);
+        gPad->SetTheta(90);
+        gPad->SetPhi(90);
+        TH2F h2res1("Res2D1", " Dataset positions on the sky ", 32, 0, 360,  10, 0, 90);
+        h2res1.SetBit(TH1::kNoStats);
+        h2res1.DrawCopy("surf1pol");
+        gPad->Modified();
+        gPad->Update();
+        for (int i=0; i<fCoordinates.GetSize(); i++)
+            DrawSet(gPad, *(Set*)list.At(i));//, 10./hres1.GetMean());
+    
+        c1->cd(3);
+        gPad->SetBorderMode(0);
+        gPad->SetTheta(90);
+        gPad->SetPhi(90);
+        h2res1.SetTitle(" Arb. Residuals after correction (scaled) ");
+        h2res1.DrawCopy("surf1pol");
+        gPad->Modified();
+        gPad->Update();
+        for (int i=0; i<fCoordinates.GetSize(); i++)
+            DrawSet(gPad, *(Set*)fCoordinates.At(i), 10./hres2.GetMean());
+
+        RaiseWindow();
+    }
+
+    void LoadStars()
+    {
+        const Int_t size = fOriginal.GetSize();
+
+        ifstream fin("../tpoint_new.txt");
+
+        while (fin && fin.get()!='\n');
+        while (fin && fin.get()!='\n');
+        while (fin && fin.get()!='\n');
+        if (!fin)
+        {
+            cout << "File not found!" << endl;
+            return;
+        }
+
+        while (1)
+        {
+            Set set;
+
+            fin >> set;  // Read data from file [deg], it is stored in [rad]
+
+            if (!fin)
+                break;
+
+            fOriginal.Add(new Set(set));
+        }
+
+        cout << "Found " << fOriginal.GetSize()-size << " sets of coordinates ";
+        cout << "(Total=" << fOriginal.GetSize() << ")" << endl;
+    }
 
     Bool_t ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
     {
-        cout << "Msg: " << hex << GET_MSG(msg) << endl;
-        cout << "SubMsg: " << hex << GET_SUBMSG(msg) << endl;
+        // cout << "Msg: " << hex << GET_MSG(msg) << endl;
+        // cout << "SubMsg: " << hex << GET_SUBMSG(msg) << dec << endl;
         switch (GET_MSG(msg))
         {
@@ -35,9 +683,31 @@
             switch (GET_SUBMSG(msg))
             {
-            case kCM_CHECKBUTTON:
-                /*
-                 TGCheckButton *but = (TGCheckButton*)list->FindWidget(mp1);
-                 cout << hex << but->GetName() << " " << mp2 << endl;
-                 */
+            case kCM_BUTTON:
+                switch (mp1)
+                {
+                case kTbFit:
+                    Fit();
+                    DisplayBending();
+                    return kTRUE;
+                case kTbLoad:
+                    fBending.Load("bending_magic.txt");
+                    DisplayBending();
+                    return kTRUE;
+                case kTbSave:
+                    fBending.Save("bending_magic.txt");
+                    return kTRUE;
+                case kTbLoadStars:
+                    LoadStars();
+                    DisplayData();
+                    return kTRUE;
+                case kTbReset:
+                    fBending.Clear();
+                    DisplayBending();
+                    return kTRUE;
+                case kTbResetStars:
+                    fOriginal.Delete();
+                    DisplayData();
+                    return kTRUE;
+                }
                 return kTRUE;
             }
@@ -52,5 +722,5 @@
         but->Associate(this);
         f->AddFrame(but, h);
-        list->Add(but);
+        fList->Add(but);
 
     }
@@ -61,53 +731,91 @@
         but->Associate(this);
         f->AddFrame(but, h);
-        list->Add(but);
-    }
-
-    void AddLabel(TGCompositeFrame *f, TString txt, TGLayoutHints *h=0, TList *label=0)
-    {
-        TGLabel *l = new TGLabel(f, txt);
+        fList->Add(but);
+    }
+
+    TGLabel *AddLabel(TGCompositeFrame *f, TString txt, TGLayoutHints *h=0)
+    {
+        TGLabel *l = new TGLabel(f, txt/*, TGLabel::GetDefaultGC()(), fFont*/);
         f->AddFrame(l, h);
-        list->Add(l);
-        if (label)
-            label->Add(l);
+        fList->Add(l);
+        fLabel.Add(l);
+        return l;
+    }
+
+    void DisplayBending()
+    {
+        TArrayD par, err;
+        fBending.GetParameters(par);
+        fBending.GetError(err);
+
+        TGLabel *l;
+
+        for (int i=0; i<MBending::GetNumPar(); i++)
+        {
+            l = (TGLabel*)fLabel.At(i);
+            l->SetText(Form("%.4f\xb0", par[i]));
+
+            l = (TGLabel*)fLabel.At(MBending::GetNumPar()+i);
+            l->SetText(Form("\xb1 %8.4f\xb0", err[i]));
+        }
+    }
+
+    void DisplayData()
+    {
+        TGLabel *l = (TGLabel*)fLabel.At(3*MBending::GetNumPar());
+        l->SetText(Form("%d data sets loaded.", fOriginal.GetSize()));
     }
 
 public:
-    MFit() : TGMainFrame(gClient->GetRoot(), 550, 350, kHorizontalFrame)
-    {
-        list = new MGList;
-        list->SetOwner();
+    ~MFit()
+    {
+        if (fFont)
+            gVirtualX->DeleteFont(fFont);
+    }
+    MFit() : TGMainFrame(gClient->GetRoot(), 740, 370, kHorizontalFrame)
+    {
+        fCoordinates.SetOwner();
+        fOriginal.SetOwner();
+
+        fList = new MGList;
+        fList->SetOwner();
+
+        fFont = gVirtualX->LoadQueryFont("7x13bold");
 
         TGLayoutHints *hints0 = new TGLayoutHints(kLHintsExpandY, 7, 5, 5, 6);
         TGLayoutHints *hints1 = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 5, 7, 5, 6);
         TGLayoutHints *hints2 = new TGLayoutHints(kLHintsCenterX|kLHintsCenterY, 5, 5, 5, 5);
-        list->Add(hints0);
-        list->Add(hints1);
-        list->Add(hints2);
+        fList->Add(hints0);
+        fList->Add(hints1);
+        fList->Add(hints2);
 
         TGGroupFrame *grp1 = new TGGroupFrame(this, "Control", kVerticalFrame);
         AddFrame(grp1, hints0);
-        list->Add(grp1);
+        fList->Add(grp1);
 
         TGGroupFrame *grp2 = new TGGroupFrame(this, "Parameters", kHorizontalFrame);
         AddFrame(grp2, hints1);
-        list->Add(grp2);
-
-
-
-        TGLayoutHints *hints4 = new TGLayoutHints(kLHintsExpandX, 5, 5, 10);
-        AddTextButton(grp1, "Load Bending Model", -1, hints4);
-        AddTextButton(grp1, "Save Bending Model", -1, hints4);
-        AddTextButton(grp1, "Load Observations",  -1, hints4);
-        AddTextButton(grp1, "Fit Parameters",     -1, hints4);
-        AddTextButton(grp1, "Reset Parameters",   -1, hints4);
+        fList->Add(grp2);
+
+
+
+        TGLayoutHints *hints4 = new TGLayoutHints(kLHintsExpandX, 5, 5,  5);
+        TGLayoutHints *hints5 = new TGLayoutHints(kLHintsExpandX, 5, 5, 15);
+        AddTextButton(grp1, "Load Pointing Model", kTbLoad,       hints5);
+        AddTextButton(grp1, "Save Pointing Model", kTbSave,       hints4);
+        AddTextButton(grp1, "Fit Parameters",      kTbFit,        hints5);
+        AddTextButton(grp1, "Reset Parameters",    kTbReset,      hints4);
+        AddTextButton(grp1, "Load Stars",          kTbLoadStars,  hints5);
+        AddTextButton(grp1, "Reset Stars",         kTbResetStars, hints4);
+        fList->Add(hints4);
+        fList->Add(hints5);
 
 
         TGHorizontalFrame *comp = new TGHorizontalFrame(grp2, 1, 1);
         grp2->AddFrame(comp);
-        list->Add(comp);
+        fList->Add(comp);
 
         TGLayoutHints *hints3 = new TGLayoutHints(kLHintsLeft|kLHintsTop, 0, 20, 5, 0);
-        list->Add(hints3);
+        fList->Add(hints3);
 
         TGLabel *l;
@@ -115,58 +823,58 @@
         TGVerticalFrame *vframe = new TGVerticalFrame(comp, 1, 1);
         comp->AddFrame(vframe, hints3);
-        list->Add(vframe);
-
-        AddCheckButton(vframe, "IA",   kCbIA);
-        AddCheckButton(vframe, "IE",   kCbIE);
-        AddCheckButton(vframe, "NPAE", kCbNPAE);
-        AddCheckButton(vframe, "CA",   kCbCA);
-        AddCheckButton(vframe, "AN",   kCbAN);
-        AddCheckButton(vframe, "AW",   kCbAW);
-        AddCheckButton(vframe, "CRX",  kCbCRX);
-        AddCheckButton(vframe, "CRY",  kCbCRY);
-        AddCheckButton(vframe, "NRX",  kCbNRX);
-        AddCheckButton(vframe, "NRY",  kCbNRY);
+        fList->Add(vframe);
+
+        for (int i=0; i<MBending::GetNumPar(); i++)
+            AddCheckButton(vframe, fBending.GetName(i), i);
 
         vframe = new TGVerticalFrame(comp, 1, 1);
         comp->AddFrame(vframe, hints3);
-        list->Add(vframe);
-
-
-        TList label;
-
-        l = new TGLabel(vframe, "0");
-        list->Add(l);
-        label.Add(l);
-
-        TGButton *but = (TGButton*)list->FindWidget(kCbIA);
+        fList->Add(vframe);
+
+        l = new TGLabel(vframe, "+000.0000");
+        l->SetTextJustify(kTextRight);
+        fList->Add(l);
+        fLabel.Add(l);
+
+        TGButton *but = (TGButton*)fList->FindWidget(0);
 
         TGLayoutHints *h = new TGLayoutHints(kLHintsCenterY, 0, 0, but->GetHeight()-l->GetHeight());
-        list->Add(h);
+        fList->Add(h);
 
         vframe->AddFrame(l,h);
 
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
+        for (int i=1; i<MBending::GetNumPar(); i++)
+            AddLabel(vframe, "+000.0000", h)->SetTextJustify(kTextRight);
 
         vframe = new TGVerticalFrame(comp, 1, 1);
         comp->AddFrame(vframe, hints3);
-        list->Add(vframe);
-
-        AddLabel(vframe, "Offset Azimuth",         h, &label);
-        AddLabel(vframe, "Offset Zenith Distance", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
-        AddLabel(vframe, "0", h, &label);
+        fList->Add(vframe);
+
+        for (int i=0; i<MBending::GetNumPar(); i++)
+            AddLabel(vframe, "\xb1 00.0000\xb0", h)->SetTextJustify(kTextRight);
+
+        vframe = new TGVerticalFrame(comp, 1, 1);
+        comp->AddFrame(vframe, hints3);
+        fList->Add(vframe);
+
+        for (int i=0; i<MBending::GetNumPar(); i++)
+            AddLabel(vframe, fBending.GetDescription(i), h);
+
+        l = new TGLabel(grp1, "0000000 Data Sets loaded.");
+        grp1->AddFrame(l, hints5);
+        fList->Add(l);
+        fLabel.Add(l);
+
+
+        ((TGCheckButton*)fList->FindWidget(0))->SetState(kButtonDown);
+        ((TGCheckButton*)fList->FindWidget(1))->SetState(kButtonDown);
+        ((TGCheckButton*)fList->FindWidget(3))->SetState(kButtonDown);
+        ((TGCheckButton*)fList->FindWidget(4))->SetState(kButtonDown);
+        ((TGCheckButton*)fList->FindWidget(5))->SetState(kButtonDown);
+        ((TGCheckButton*)fList->FindWidget(6))->SetState(kButtonDown);
+        ((TGCheckButton*)fList->FindWidget(7))->SetState(kButtonDown);
+
+        SetWindowName("TPoint Fitting Window");
+        SetIconName("TPoint++");
 
         Layout();
@@ -175,16 +883,11 @@
         MapWindow();
 
-
-
-
-        l = (TGLabel*)label.At(0);
-        l->SetText("221.4567");
-
-        l = (TGLabel*)label.At(1);
-        l->SetText("-1.65423");
-
-        Layout();
-    }
+        DisplayBending();
+        DisplayData();
+    }
+    ClassDef(MFit, 0)
 };
+
+ClassImp(MFit);
 
 void gui()
@@ -192,3 +895,2 @@
     new MFit;
 }
-
