]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TPC/AliTPCCalibViewerGUI.cxx
rho invariance tests
[u/mrichter/AliRoot.git] / TPC / AliTPCCalibViewerGUI.cxx
index f89ac1e0d31b3e889f84fd45d283588dab22a6ab..837f83428c6e92d5112e5a669209369d023f9eaf 100644 (file)
@@ -32,6 +32,7 @@
 
 
 #include "AliTPCCalibViewerGUI.h"
+#include "AliTPCCalPad.h"
 #include <iostream>
 
 #include <TCanvas.h>
 #include <TVirtualPad.h>
 
 #include <TROOT.h>
+#include <TMath.h>
 #include <TObjArray.h>
 #include <TObjString.h>
 #include <TVector.h>
 #include <string.h>
 #include <TH1.h>
 #include "TStyle.h"
-#include "AliTPCCalibViewer.h"
-#include "TGFileDialog.h"
 #include "TGFileDialog.h"
+#include "TGInputDialog.h"
+#include "AliTPCCalibViewer.h"
+#include "AliTPCPreprocessorOnline.h"
 
 // #include "TGListBox.h"
 // #include "TGNumberEntry"
@@ -70,6 +73,7 @@ ClassImp(AliTPCCalibViewerGUI)
 AliTPCCalibViewerGUI::AliTPCCalibViewerGUI(const TGWindow *p, UInt_t w, UInt_t h, char* fileName)
   : TGCompositeFrame(p, w, h),
     fViewer(0),
+    fPreprocessor(0),
     fContTopBottom(0),
     fContLCR(0),
     fContLeft(0),
@@ -110,9 +114,11 @@ AliTPCCalibViewerGUI::AliTPCCalibViewerGUI(const TGWindow *p, UInt_t w, UInt_t h
     fRadioTPC(0),
     fRadioSideA(0),
     fRadioSideC(0),
+    fRadioROC(0),
     fRadioSector(0),
     fComboAddDrawOpt(0),
     fChkAuto(0),
+    fChkAutoAppend(0),
     fComboMethod(0),
     fListNormalization(0),
     fComboCustom(0),
@@ -184,24 +190,31 @@ AliTPCCalibViewerGUI::AliTPCCalibViewerGUI(const TGWindow *p, UInt_t w, UInt_t h
     fContAddExport(0),
     fComboExportName(0),
     fBtnExport(0),
-    fBtnAddNorm(0)
+    fBtnAddNorm(0), 
+    fContTree(0),
+    fBtnDumpToFile(0),
+    fBtnLoadTree(0),
+    fChkAddAsReference(0),
+    fTxtRefName(0), 
+    fInitialized(0)
 {
    //
    // AliTPCCalibViewerGUI constructor; fileName specifies the ROOT tree used for drawing 
    //
 
    // draw the GUI:
+   fPreprocessor = new AliTPCPreprocessorOnline();
    DrawGUI(p, w, h);
    // initialize the AliTPCCalibViewer:
    if (fileName) Initialize(fileName);
    // set default button states:
    SetInitialValues();
    // do first drawing: 
-   DoDraw();
+   if (fileName) DoDraw();
 }
 
 
-void AliTPCCalibViewerGUI::DrawGUI(const TGWindow *p, UInt_t w, UInt_t h) {
+void AliTPCCalibViewerGUI::DrawGUI(const TGWindow */*p*/, UInt_t w, UInt_t h) {
    // 
    // draw the GUI
    // 
@@ -311,6 +324,11 @@ void AliTPCCalibViewerGUI::DrawGUI(const TGWindow *p, UInt_t w, UInt_t h) {
          fChkAuto = new TGCheckButton(fContDrawOpt, "Auto redraw");
          fContDrawOpt->AddFrame(fChkAuto, new TGLayoutHints(kLHintsNormal, 0, 2, 0, 0));
          fChkAuto->SetToolTipText("Decide if you want an automatic redraw on each new selection.\nNot recommended on a slow machine, during remote connection or if your draw option is 'same'.");
+         
+         // automatic append ending check button
+         fChkAutoAppend = new TGCheckButton(fContDrawOpt, "Auto add appending");
+         fContDrawOpt->AddFrame(fChkAutoAppend, new TGLayoutHints(kLHintsNormal, 0, 2, 0, 0));
+         fChkAutoAppend->SetToolTipText("Tries to repair your custom draw string or custom cut string, if you forgot '~' or '.fElements' \nThis function may be buggy!");
                
       
       // *** predefined radio button ***  " Predefined "
@@ -619,11 +637,17 @@ void AliTPCCalibViewerGUI::DrawGUI(const TGWindow *p, UInt_t w, UInt_t h) {
          fRadioSideC->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "HandleButtonsCuts()");
          fRadioSideC->SetToolTipText("Use only side C.");
       
-         // sector radio button
-         fRadioSector = new TGRadioButton(fContCuts, "ROC", 23);
+         // roc radio button
+         fRadioROC = new TGRadioButton(fContCuts, "ROC", 23);
+         fContCuts->AddFrame(fRadioROC, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
+         fRadioROC->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "HandleButtonsCuts()");
+         fRadioROC->SetToolTipText("Use only one ROC (readout chamber).");
+
+        // sector radio button
+         fRadioSector = new TGRadioButton(fContCuts, "Sector", 24);
          fContCuts->AddFrame(fRadioSector, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
          fRadioSector->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "HandleButtonsCuts()");
-         fRadioSector->SetToolTipText("Use only one ROC (readout chamber).");
+         fRadioSector->SetToolTipText("Use only one Sector (0-17 A-Side, 18-35 C-Side).");
       
          // sector options container
          fContSector = new TGCompositeFrame(fContCuts, 200, 200, kHorizontalFrame | kFitWidth | kFitHeight);
@@ -798,13 +822,39 @@ void AliTPCCalibViewerGUI::DrawGUI(const TGWindow *p, UInt_t w, UInt_t h) {
          fBtnExport->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "DoExport()");
          fBtnExport->SetToolTipText("Export the current 2D view as AliTPCCalPad to the CINT command line interpreter, use the specified name. \nThis works only in 2D mode.");
       
-         // export button
+         // add to normalisation button
          fBtnAddNorm = new TGTextButton(fContExport, "&Add to normalization");
          fContExport->AddFrame(fBtnAddNorm, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
          fBtnAddNorm->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "DoExportNorm()");
          fBtnAddNorm->SetToolTipText("Use the current 2D view as normalization variable, use the specified name. \nNot yet working!");
-         
-      
+
+      // Tree container
+      fContTree = new TGGroupFrame(fTabRight1, "Tree", kVerticalFrame | kFitWidth | kFitHeight);
+      fTabRight1->AddFrame(fContTree, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
+
+         // dump tree to file button
+         fBtnDumpToFile = new TGTextButton(fContTree, "&Dump to File");
+         fContTree->AddFrame(fBtnDumpToFile, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
+         fBtnDumpToFile->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "DoDumpToFile()");
+         fBtnDumpToFile->SetToolTipText("Write the exported CalPads to a new CalibTree");
+
+         // dump tree to file button
+         fBtnLoadTree = new TGTextButton(fContTree, "&Load Tree");
+         fContTree->AddFrame(fBtnLoadTree, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
+         fBtnLoadTree->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "DoLoadTree()");
+         fBtnLoadTree->SetToolTipText("Load and initialize a new calibration tree. ");
+
+         fChkAddAsReference = new TGCheckButton(fContTree, "as reference:");
+         fContTree->AddFrame(fChkAddAsReference, new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
+         fChkAddAsReference->Connect("Clicked()", "AliTPCCalibViewerGUI", this, "DoLoadTree()");
+         fChkAddAsReference->SetToolTipText("To add a new tree as reference tree.");
+            
+         fTxtRefName = new TGTextEntry(fContTree, "R", 500);
+         fContTree->AddFrame(fTxtRefName, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 15, 0, 0, 0));
+         // fTxtRefName->Connect("ReturnPressed()", "AliTPCCalibViewerGUI", this, "HandleButtonsNoRedraw(=50)");
+         fTxtRefName->SetToolTipText("Reference Name");
+        
+            
       // Fit options container
       fContFit = new TGGroupFrame(fTabRight1, "Custom fit", kVerticalFrame | kFitWidth | kFitHeight);
       fTabRight1->AddFrame(fContFit, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
@@ -845,11 +895,11 @@ void AliTPCCalibViewerGUI::SetInitialValues() {
    // 
    // Set the default button states
    // 
+   fChkAuto->SetState(kButtonUp);
    fRadioPredefined->SetState(kButtonDown);
    fRadioRaw->SetState(kButtonDown);
    fRadioTPC->SetState(kButtonDown);
    fRadio1D->SetState(kButtonDown);
-   fChkAuto->SetState(kButtonDown);
    fChkAddCuts->SetState(kButtonUp);
    fChkGetMinMaxAuto->SetState(kButtonDown);
    fChkSetMin->SetState(kButtonUp);
@@ -1002,7 +1052,8 @@ void AliTPCCalibViewerGUI::SetInitialValues() {
    //fCanvMain->GetCanvas()->GetCanvasImp()->ShowStatusBar(kTRUE); // klappt auch nicht
    fListVariables->IntegralHeight(kFALSE);         // naja
    fListNormalization->IntegralHeight(kFALSE);     // naja
-   
+   fChkAuto->SetState(kButtonDown);
+  
    // Make first drawing:
    // DoDraw();
 }
@@ -1011,6 +1062,7 @@ void AliTPCCalibViewerGUI::SetInitialValues() {
 AliTPCCalibViewerGUI::AliTPCCalibViewerGUI(const AliTPCCalibViewerGUI &c)
    : TGCompositeFrame(c.fParent, c.fWidth, c.fHeight),
     fViewer(0),
+    fPreprocessor(0),
     fContTopBottom(0),
     fContLCR(0),
     fContLeft(0),
@@ -1051,9 +1103,11 @@ AliTPCCalibViewerGUI::AliTPCCalibViewerGUI(const AliTPCCalibViewerGUI &c)
     fRadioTPC(0),
     fRadioSideA(0),
     fRadioSideC(0),
+    fRadioROC(0),
     fRadioSector(0),
     fComboAddDrawOpt(0),
     fChkAuto(0),
+    fChkAutoAppend(0),
     fComboMethod(0),
     fListNormalization(0),
     fComboCustom(0),
@@ -1125,7 +1179,13 @@ AliTPCCalibViewerGUI::AliTPCCalibViewerGUI(const AliTPCCalibViewerGUI &c)
     fContAddExport(0),
     fComboExportName(0),
     fBtnExport(0),
-    fBtnAddNorm(0)
+    fBtnAddNorm(0), 
+    fContTree(0),
+    fBtnDumpToFile(0),
+    fBtnLoadTree(0),
+    fChkAddAsReference(0),
+    fTxtRefName(0),
+    fInitialized(0)
 {
   //
   // dummy AliTPCCalibViewerGUI copy constructor
@@ -1133,7 +1193,7 @@ AliTPCCalibViewerGUI::AliTPCCalibViewerGUI(const AliTPCCalibViewerGUI &c)
 }
 
 
-AliTPCCalibViewerGUI & AliTPCCalibViewerGUI::operator =(const AliTPCCalibViewerGUI & param) {
+AliTPCCalibViewerGUI & AliTPCCalibViewerGUI::operator =(const AliTPCCalibViewerGUI & /*param*/) {
    //
    // dummy assignment operator
    //
@@ -1153,6 +1213,7 @@ AliTPCCalibViewerGUI::~AliTPCCalibViewerGUI() {
    }
    Cleanup();
    if (fViewer) fViewer->Delete();
+   delete fPreprocessor;
 }
 
 
@@ -1163,7 +1224,7 @@ void AliTPCCalibViewerGUI::CloseWindow() {
 */
 
 
-void AliTPCCalibViewerGUI::Initialize(char* fileName, char* treeName) {
+void AliTPCCalibViewerGUI::Initialize(const char* fileName, const char* treeName) {
    // 
    // initialize the GUI with a calibrationTree from fileName
    // 
@@ -1193,6 +1254,7 @@ void AliTPCCalibViewerGUI::Initialize(AliTPCCalibViewer *viewer) {
    
    // fill fListVariables, list of drawable variables:
    TObjArray* arr = fViewer->GetListOfVariables();
+   if (!arr) return;
    TIterator* iter = arr->MakeIterator();
    iter->Reset();
    TObjString* currentStr = 0;
@@ -1203,22 +1265,33 @@ void AliTPCCalibViewerGUI::Initialize(AliTPCCalibViewer *viewer) {
       if (fInitialized && currentStr->GetString() == selectedVariable) variableId = id;
       id++;
    }
-   delete iter;
-   arr->Delete();
-   delete arr;
 
    // fill fListNorm, list of normalization variables:
-   arr = fViewer->GetListOfNormalizationVariables();
-   iter = arr->MakeIterator();
-   iter->Reset();
+   TObjArray *arrNorm = fViewer->GetListOfNormalizationVariables();
+   TIterator *iterNorm = arrNorm->MakeIterator();
+   iterNorm->Reset();
    currentStr = 0;
    id = 0;
    fListNormalization->RemoveAll();
+   while ((currentStr = (TObjString*)(iterNorm->Next()))) {
+      fListNormalization->AddEntry(currentStr->GetString().Data(), id);
+      if (fInitialized && currentStr->GetString() == selectedNormalization) normalizationId = id;
+      id++;
+   }
+   currentStr = 0;
+   iter->Reset();
+   //Add draw variables to the list of normalisation
    while ((currentStr = (TObjString*)(iter->Next()))) {
+      if (currentStr->GetString().BeginsWith("Map")) continue; //don't add mapping information
       fListNormalization->AddEntry(currentStr->GetString().Data(), id);
       if (fInitialized && currentStr->GetString() == selectedNormalization) normalizationId = id;
       id++;
    }
+
+   delete iterNorm;
+   arrNorm->Delete();
+   delete arrNorm;
+
    delete iter;
    arr->Delete();
    delete arr;
@@ -1240,6 +1313,15 @@ void AliTPCCalibViewerGUI::Initialize(AliTPCCalibViewer *viewer) {
 
 }
 
+void AliTPCCalibViewerGUI::Reset(){
+  //
+  // reset variables, delete calib viewer
+  //
+  if (fViewer) delete fViewer;
+  fListVariables->RemoveAll();
+  fListNormalization->RemoveAll();
+  fInitialized = kFALSE;
+}
 
 void AliTPCCalibViewerGUI::HandleButtonsGeneral(Int_t id) {
    //
@@ -1382,22 +1464,32 @@ void AliTPCCalibViewerGUI::HandleButtonsCuts(Int_t id) {
       case 20:             // fRadioTPC
          fRadioSideA->SetState(kButtonUp);
          fRadioSideC->SetState(kButtonUp);
+         fRadioROC->SetState(kButtonUp);
          fRadioSector->SetState(kButtonUp);
          break;
       case 21:             // fRadioSideA
          fRadioTPC->SetState(kButtonUp);
          fRadioSideC->SetState(kButtonUp);
+         fRadioROC->SetState(kButtonUp);
          fRadioSector->SetState(kButtonUp);
          break;
       case 22:             // fRadioSideC
          fRadioTPC->SetState(kButtonUp);
          fRadioSideA->SetState(kButtonUp);
+         fRadioROC->SetState(kButtonUp);
          fRadioSector->SetState(kButtonUp);
          break;
-      case 23:             // fRadioSector
+      case 23:             // fRadioROC
          fRadioTPC->SetState(kButtonUp);
          fRadioSideA->SetState(kButtonUp);
          fRadioSideC->SetState(kButtonUp);
+         fRadioSector->SetState(kButtonUp);
+         break;
+      case 24:             // fRadioSector
+         fRadioTPC->SetState(kButtonUp);
+         fRadioSideA->SetState(kButtonUp);
+         fRadioSideC->SetState(kButtonUp);
+         fRadioROC->SetState(kButtonUp);
          break;
       case 31:            // fComboAddCuts
          fChkAddCuts->SetState(kButtonDown);
@@ -1437,7 +1529,33 @@ void AliTPCCalibViewerGUI::HandleButtonsNoRedraw(Int_t id) {
    SetMinMaxLabel();
 }
 
-
+void AliTPCCalibViewerGUI::ReplacePlaceHolders(TString &str)
+{
+    //
+    // replace the defined placeholders in the custom draw string and cut string
+    //
+    TString drawPlaceHolder("#draw#");
+    TString normPlaceHolder("#norm#");
+
+    //current draw variable
+    TString desiredData("");
+    if (fListVariables->GetSelectedEntry()){
+      desiredData += ((TGTextLBEntry*)(fListVariables->GetSelectedEntry()))->GetTitle();
+      str.ReplaceAll(drawPlaceHolder,desiredData);
+    }
+//    desiredData += fViewer->GetAbbreviation();
+
+    //current normalisation
+    TString normalizationData("");
+    if (fListNormalization->GetSelectedEntry()){
+      normalizationData += ((TGTextLBEntry*)(fListNormalization->GetSelectedEntry()))->GetTitle();
+      if (! (TString(((TGTextLBEntry*)(fListNormalization->GetSelectedEntry()))->GetTitle())).BeginsWith("Fit"))
+        if ( normalizationData.BeginsWith("_") ) normalizationData = desiredData+normalizationData;
+      if ( fListVariables->FindEntry(normalizationData.Data()) )
+        normalizationData+="~";
+      str.ReplaceAll(normPlaceHolder,normalizationData);
+    }
+}
 
 void AliTPCCalibViewerGUI::DoNewSelection() {
    //
@@ -1483,11 +1601,16 @@ TString* AliTPCCalibViewerGUI::GetDrawString() {
             cutStr += "(sector/18)%2==0"; // side A
          if (fRadioSideC->GetState() == kButtonDown)
             cutStr+= "(sector/18)%2==1"; // side C
-         if (fRadioSector->GetState() == kButtonDown) {
+         if (fRadioROC->GetState() == kButtonDown) {
             Int_t sector = (Int_t)(fNmbSector->GetNumber());
             cutStr += "sector==";
             cutStr += sector; 
          }
+         if (fRadioSector->GetState() == kButtonDown) {
+            Int_t sector = ((Int_t)(fNmbSector->GetNumber()))%36;
+            cutStr += "sector%36==";
+            cutStr += sector; 
+         }
          if (fChkAddCuts->GetState() == kButtonDown && strcmp(fComboAddCuts->GetTextEntry()->GetText(), "") != 0){
             if (fRadioTPC->GetState() != kButtonDown) cutStr += " && ";
             cutStr += fComboAddCuts->GetTextEntry()->GetText();  
@@ -1510,14 +1633,20 @@ TString* AliTPCCalibViewerGUI::GetDrawString() {
 
       desiredData += op;
       if (! (TString(((TGTextLBEntry*)(fListNormalization->GetSelectedEntry()))->GetTitle())).BeginsWith("Fit"))
-         desiredData += ((TGTextLBEntry*)(fListVariables->GetSelectedEntry()))->GetTitle();
+         if ( normalizationData.BeginsWith("_") ) desiredData += ((TGTextLBEntry*)(fListVariables->GetSelectedEntry()))->GetTitle();
+      if ( fListVariables->FindEntry(normalizationData.Data()) )
+          normalizationData+="~";
       desiredData += normalizationData;
    }
    else if (fRadioCustom->GetState() == kButtonDown) {
       desiredData = fComboCustom->GetTextEntry()->GetText();
       if (desiredData == "") return 0;
+      ReplacePlaceHolders(desiredData);
    }
    
+   // try to add forgotten '~'
+   if (fChkAutoAppend->GetState() == kButtonDown) 
+      desiredData = TString(fViewer->AddAbbreviations((char*)desiredData.Data()));
    return new TString(desiredData.Data());
 }   
 
@@ -1534,10 +1663,15 @@ TString* AliTPCCalibViewerGUI::GetSectorString() {
       sectorStr += "A"; //cuts += "(sector/18)%2==0";
    if (fRadioSideC->GetState() == kButtonDown)
       sectorStr+= "C"; //cuts += "(sector/18)%2==1";
-   if (fRadioSector->GetState() == kButtonDown) {
+   if (fRadioROC->GetState() == kButtonDown) {
       Int_t sector = (Int_t)(fNmbSector->GetNumber());
       sectorStr += sector; //cuts += "sector==";
    }
+   if (fRadioSector->GetState() == kButtonDown) {
+      Int_t sector = ((Int_t)(fNmbSector->GetNumber()))%36;
+      sectorStr += "S";
+      sectorStr += sector; //cuts += "sector%36==";
+   }
    return new TString(sectorStr.Data());
 }   
   
@@ -1560,8 +1694,15 @@ TString* AliTPCCalibViewerGUI::GetSectorString() {
       cutsStr += "!=0";
       if (fChkAddCuts->GetState() == kButtonDown) cutsStr += " && ";
    }
-   if (fChkAddCuts->GetState() == kButtonDown)
-      cutsStr += fComboAddCuts->GetTextEntry()->GetText();
+   if (fChkAddCuts->GetState() == kButtonDown){
+       cutsStr += fComboAddCuts->GetTextEntry()->GetText();
+       ReplacePlaceHolders(cutsStr);
+   }
+
+   
+   // try to add forgotten '~'
+   if (fChkAutoAppend->GetState() == kButtonDown) 
+      cutsStr = TString(fViewer->AddAbbreviations((char*)cutsStr.Data()));
    return new TString(cutsStr.Data());
 }
 
@@ -1572,6 +1713,7 @@ void AliTPCCalibViewerGUI::DoDraw() {
    //
    
    // specify data to plot:
+  if (!GetDrawString()) return;
    TString desiredData(GetDrawString()->Data());
    // specify sector:
    TString sectorStr(GetSectorString()->Data());
@@ -1634,12 +1776,10 @@ void AliTPCCalibViewerGUI::DoFit() {
    TMatrixD covMatrix(0,0);
    TString cutStr("");
    TString formulaStr("");
-   TString *returnStr = new TString("");
 
    // specify data to plot:
    TString drawStr(GetDrawString()->Data());
-
-
+   
    // ********** create cut string **********
    if (fRadioTPC->GetState() == kButtonDown)
       cutStr += ""; // whole TPC is used for fitting
@@ -1647,26 +1787,40 @@ void AliTPCCalibViewerGUI::DoFit() {
       cutStr += "(sector/18)%2==0"; // side A
    if (fRadioSideC->GetState() == kButtonDown)
       cutStr+= "(sector/18)%2==1"; // side C
-   if (fRadioSector->GetState() == kButtonDown) {
+   if (fRadioROC->GetState() == kButtonDown) {
       Int_t sector = (Int_t)(fNmbSector->GetNumber());
       cutStr += "sector==";
       cutStr += sector; 
    }
+   if (fRadioSector->GetState() == kButtonDown) {
+      Int_t sector = (Int_t)(fNmbSector->GetNumber())%36;
+      cutStr += "sector%36==";
+      cutStr += sector; 
+   }
    if (fChkAddCuts->GetState() == kButtonDown && strcmp(fComboAddCuts->GetTextEntry()->GetText(), "") != 0){
       if (fRadioTPC->GetState() != kButtonDown) cutStr += " && ";
       cutStr += fComboAddCuts->GetTextEntry()->GetText();  
    }
+   // try to add forgotten '~'
+   if (fChkAutoAppend->GetState() == kButtonDown) 
+      cutStr = TString(fViewer->AddAbbreviations((char*)cutStr.Data()));
    
    // ********** get formula string **********
    formulaStr += fComboCustomFit->GetTextEntry()->GetText();
+   if (fChkAutoAppend->GetState() == kButtonDown) 
+      formulaStr = TString(fViewer->AddAbbreviations((char*)formulaStr.Data()));
 
    // ********** call AliTPCCalibViewer's fit-function
-   returnStr = fViewer->Fit(drawStr.Data(), formulaStr.Data(), cutStr.Data(), chi2, fitParam, covMatrix);
+  ReplacePlaceHolders(drawStr);
+  ReplacePlaceHolders(cutStr);
+  TString *returnStr = fViewer->Fit(drawStr.Data(), formulaStr.Data(), cutStr.Data(), chi2, fitParam, covMatrix);
    
    std::cout << std::endl;
    std::cout << "Your fit formula reads as follows:" << std::endl;
    std::cout << returnStr->Data() << std::endl;
    std::cout << "chi2 = " << chi2 << std::endl;
+
+  delete returnStr;
 }
 
 
@@ -1687,7 +1841,8 @@ void AliTPCCalibViewerGUI::DoExport() {
    // create calPad according to drawCommand and cuts
    AliTPCCalPad *calPad = fViewer->GetCalPad(desiredData.Data(), (char*)cutsStr.Data(), (char*)calPadName);
    // finally export calPad to Cint:
-   gROOT->ProcessLine(Form("AliTPCCalPad* %s = (AliTPCCalPad*)0x%lx;", calPadName, calPad));
+   gROOT->ProcessLine(Form("AliTPCCalPad* %s = (AliTPCCalPad*)0x%lx;", calPadName, (ULong_t)calPad));
+   fPreprocessor->AddComponent(calPad);
    Info("ExportCalPad", "Current 2D view has been exported to an AliTPCCalPad* with name '%s'", calPadName);
 }
 
@@ -1703,6 +1858,7 @@ void AliTPCCalibViewerGUI::DoExportNorm() {
    }
    
    Error("DoExportNorm", "Not yet implemented.");
+/*  
    return;
    
    // specify data to plot:
@@ -1717,8 +1873,9 @@ void AliTPCCalibViewerGUI::DoExportNorm() {
    // create calPad according to drawCommand and cuts
    AliTPCCalPad *calPad = fViewer->GetCalPad(desiredData.Data(), (char*)cutsStr.Data(), (char*)fComboExportName->GetTextEntry()->GetText());
    // finally export calPad to Cint:
-   gROOT->ProcessLine(Form("AliTPCCalPad* %s = (AliTPCCalPad*)0x%lx;", calPadName, calPad));
+   gROOT->ProcessLine(Form("AliTPCCalPad* %s = (AliTPCCalPad*)0x%lx;", calPadName, (ULong_t) calPad));
    Info("ExportCalPad", "Current 2D view has been exported to an AliTPCCalPad* with name '%s'", calPadName);
+  */
 }
 
 
@@ -1733,12 +1890,26 @@ void AliTPCCalibViewerGUI::GetMinMax() {
       ptr = listOfPrimitives->At(i);
       if ( ptr->InheritsFrom("TH1") ) break;
    }
-   if ( ptr != 0 && !ptr->InheritsFrom("TH1") ) return;      // if the loop did not find a TH1
+   if ( !ptr || !ptr->InheritsFrom("TH1") ) return;      // if the loop did not find a TH1
    TH1 *hist = (TH1*)ptr;
-   Double_t histMax = hist->GetMaximum();
-   Double_t histMin = hist->GetMinimum();
-   fTxtSetMax->SetText(Form("%f",histMax));
-   fTxtSetMin->SetText(Form("%f",histMin));
+
+//    Double_t histMax = hist->GetMaximum();
+//    Double_t histMin = hist->GetMinimum();
+//    fTxtSetMax->SetText(Form("%f",histMax));
+//    fTxtSetMin->SetText(Form("%f",histMin));
+
+   if (fRadio2D->GetState() == kButtonDown) {
+      if (fChkSetMax->GetState() == kButtonUp)
+         fTxtSetMax->SetText(Form("%f", hist->GetMaximum()));
+      if (fChkSetMin->GetState() == kButtonUp)
+         fTxtSetMin->SetText(Form("%f", hist->GetMinimum()));
+   }
+   else if (fRadio1D->GetState() == kButtonDown) {
+      if (fChkSetMax->GetState() == kButtonUp)
+         fTxtSetMax->SetText( Form("%f", hist->GetXaxis()->GetXmax()) );
+      if (fChkSetMin->GetState() == kButtonUp)
+         fTxtSetMin->SetText( Form("%f", hist->GetXaxis()->GetXmin()) );
+   }
 }
 
 
@@ -1765,23 +1936,31 @@ void AliTPCCalibViewerGUI::SetMinMaxLabel() {
    TH1 *hist = (TH1*)ptr; 
    TString minTxt(fTxtSetMin->GetText());
    TString maxTxt(fTxtSetMax->GetText());
+   
    // set min and max according to specified values, if checkbox is checked
-   if (fChkSetMax->GetState() == kButtonDown && (maxTxt.IsDigit() || maxTxt.IsFloat()) )
-      hist->SetMaximum(maxTxt.Atof());
-   if (fChkSetMax->GetState() == kButtonUp)
-      hist->SetMaximum(-1111);  // default value, to unzoom
-   if (fChkSetMin->GetState() == kButtonDown && (minTxt.IsDigit() || minTxt.IsFloat()) )
-      hist->SetMinimum(minTxt.Atof());
-   if (fChkSetMin->GetState() == kButtonUp)
-      hist->SetMinimum(-1111);  // default value, to unzoom
-   // get min and max from plot       
-   if (fChkGetMinMaxAuto->GetState() == kButtonDown) {
-      // maybe call here GetMinMax ???
+   if (fRadio2D->GetState() == kButtonDown) {
+      if (fChkSetMax->GetState() == kButtonDown && fChkSetMax->GetState() == kButtonDown &&(maxTxt.IsDigit() || maxTxt.IsFloat()) )
+         hist->SetMaximum(maxTxt.Atof());
       if (fChkSetMax->GetState() == kButtonUp)
-         fTxtSetMax->SetText(Form("%f", hist->GetMaximum()));
+         hist->SetMaximum(-1111);  // default value, to unzoom
+      if (fChkSetMin->GetState() == kButtonDown && (minTxt.IsDigit() || minTxt.IsFloat()) )
+         hist->SetMinimum(minTxt.Atof());
       if (fChkSetMin->GetState() == kButtonUp)
-         fTxtSetMin->SetText(Form("%f", hist->GetMinimum()));
+         hist->SetMinimum(-1111);  // default value, to unzoom
    }
+   else if (fRadio2D->GetState() == kButtonDown) {
+      if (fChkSetMin->GetState() == kButtonDown && 
+          fChkSetMax->GetState() == kButtonDown && hist->GetXaxis())
+         hist->GetXaxis()->SetRangeUser(hist->GetXaxis()->GetXmin(), hist->GetXaxis()->GetXmax());
+      else if (fChkSetMax->GetState() == kButtonDown && hist->GetXaxis())
+         hist->GetXaxis()->SetRangeUser(hist->GetXaxis()->GetXmin(), maxTxt.Atof());
+      else if (fChkSetMin->GetState() == kButtonDown && hist->GetXaxis())
+         hist->GetXaxis()->SetRangeUser(minTxt.Atof(), hist->GetXaxis()->GetXmax());
+      hist->SetTitle(hist->GetTitle());  // trick to update the histogram
+   }
+   
+   // get min and max from plot       
+   GetMinMax();
    
    // set labels according to specification, if cehckboxes are checked
    if (fChkLabelTitle->GetState() == kButtonDown) 
@@ -1805,17 +1984,29 @@ void AliTPCCalibViewerGUI::ChangeSector(){
    // 
    // function that is called, when the number of the sector is changed
    // to change the sector label
-   // 
+   //
+   if ( fRadioROC->GetState()!=kButtonDown && fRadioSector->GetState()!=kButtonDown ){
+       fLblSector->SetText("not used");
+       return;
+   }
+
    Int_t sector = (Int_t)(fNmbSector->GetNumber());
-   char* secLabel = "";
-   if (sector >= 0 && sector <= 17) // IROC, Side A
-      secLabel = "IROC, A";
-   if (sector >= 18 && sector <= 35) // IROC, Side C
-      secLabel = "IROC, C";
-   if (sector >= 36 && sector <= 53) // OROC, Side A
-      secLabel = "OROC, A";
-   if (sector >= 54 && sector <= 71) // OROC, Side C
-      secLabel = "OROC, C";
+   TString secLabel = "";
+   if ( sector < 36 )
+       secLabel = "IROC";
+   else
+       secLabel = "OROC";
+
+   if (fRadioSector->GetState()==kButtonDown)
+       secLabel="Sector";
+
+   if ( sector%36<18 ) //A-Side
+       secLabel += ", A";
+   else
+       secLabel += ", C";
+
+   secLabel += Form("%02d",sector%18);
+
    fLblSector->SetText(secLabel);
    DoNewSelection();
 }
@@ -1862,7 +2053,7 @@ void AliTPCCalibViewerGUI::MouseMove(Int_t event, Int_t x, Int_t y, TObject *sel
    if (!selectedObject->InheritsFrom("TH2")) return;
    // zoom to sector works ONLY in 2D mode, if one side is specified
    if (fRadio2D->GetState() == kButtonUp) return;
-   if (fRadioSector->GetState() == kButtonDown) { // return to full side view
+   if (fRadioROC->GetState() == kButtonDown) { // return to full side view
       // return to full side view
       Int_t sector = (Int_t)(fNmbSector->GetNumber());
       if ( (sector >= 0 && sector <= 17) || (sector >= 36 &&  sector <= 53) ) {
@@ -1900,7 +2091,7 @@ void AliTPCCalibViewerGUI::MouseMove(Int_t event, Int_t x, Int_t y, TObject *sel
    if (quadrant == 3) phi = pi + phi;
    if (quadrant == 4) phi = 2 * pi - phi;
    Double_t phiGrad = phi / pi * 180;
-   Int_t sector = (Int_t) phiGrad / 20;  // one sector coresponds to 20°
+   Int_t sector = (Int_t) phiGrad / 20;  // one sector coresponds to 20
    // IROC starts at 84.5 cm
    // IROC ends at 135.5 cm, OROC begins
    // OROC ends at 250 cm
@@ -1914,8 +2105,8 @@ void AliTPCCalibViewerGUI::MouseMove(Int_t event, Int_t x, Int_t y, TObject *sel
    }
    // printf("r: %f, phi: %f, phiGrad: %f, gy/gx: %f, quadrant: %i, sector: %i \n", r, phi, phiGrad, gy/gx, quadrant, sector);
    fNmbSector->SetNumber(sector);
-   fRadioSector->Clicked();
-   fRadioSector->SetState(kButtonDown);
+   fRadioROC->Clicked();
+   fRadioROC->SetState(kButtonDown);
    ChangeSector();   
 }
 
@@ -1988,6 +2179,54 @@ void AliTPCCalibViewerGUI::SavePicture() {
 }
    
 
+void AliTPCCalibViewerGUI::DoDumpToFile() {
+   // 
+   // This function is called, when the "Dump to File" button is pressed. 
+   // All the exported CalPads will be written into an new CalibTree, 
+   // a Save File dialog will appear to specify the filename
+   // 
+   const char *kSaveAsTypes[] = {
+      "ROOT file",   "*.root",
+       0,              0
+   };
+   TString dir(".");
+   TGFileInfo fi;
+   fi.fFileTypes = kSaveAsTypes;
+   // fi.fIniDir    = StrDup(dir);
+   fi.fOverwrite = kFALSE;
+   new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDSave, &fi);
+   if (fi.fFilename && strlen(fi.fFilename)) {
+      fPreprocessor->DumpToFile(fi.fFilename);
+      Info("DumpToFile","New CalibTree has been writen to file '%s'", fi.fFilename);
+   }
+}
+
+
+void AliTPCCalibViewerGUI::DoLoadTree() {
+   // function to load a new calib tree
+   // 
+   // 
+   const char *kFileTypes[] = {
+      "ROOT file",   "*.root",
+       0,              0
+   };
+   TString dir(".");
+   TGFileInfo fi;
+   fi.fFileTypes = kFileTypes;
+   // fi.fIniDir    = StrDup(dir);
+   fi.fOverwrite = kFALSE;
+   new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDOpen, &fi);
+   if (fi.fFilename && strlen(fi.fFilename) && fChkAddAsReference->GetState() == kButtonUp) {
+      Initialize(fi.fFilename);
+      Reload();
+   }
+   else if (fi.fFilename && strlen(fi.fFilename) && fChkAddAsReference->GetState() == kButtonDown) {
+      fViewer->AddReferenceTree(fi.fFilename, "calPads", fTxtRefName->GetText());
+      Reload();
+   }
+}
+
+
 TObjArray* AliTPCCalibViewerGUI::ShowGUI(const char* fileName) {
    //
    // Initialize and show GUI for presentation for demonstration purposes