]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TPC/AliTPCCalibViewer.cxx
add h-jet jet mass analysis class
[u/mrichter/AliRoot.git] / TPC / AliTPCCalibViewer.cxx
index 247588dca768a846ab92a84991e634867b7fbf59..7e2b57ed2ab0dc2ba8a10cb2ebe68cafb7d4c26d 100644 (file)
 //
 // ROOT includes 
 //
-#include <iostream>
+
 #include <fstream>
-#include <TString.h>
-#include <TRandom.h>
-#include <TLegend.h>
-#include <TLine.h>
-#include <TCanvas.h>
-#include <TROOT.h>
-#include <TStyle.h>
+#include <iostream>
+
+#include <TFile.h>
+#include <TFriendElement.h>
+#include <TGraph.h>
+#include <TKey.h>
+#include <TPad.h>
+//#include <TCanvas.h>
 #include <TH1.h> 
 #include <TH1F.h>
-#include <THashTable.h>
+#include <TLegend.h>
+#include <TLine.h>
+#include <TMath.h>
 #include <TObjString.h>
-#include "TTreeStream.h"
-#include "TFile.h"
-#include "TKey.h"
-#include "TGraph.h"
-#include "AliTPCCalibPulser.h"
-#include "AliTPCCalibPedestal.h"
-#include "AliTPCCalibCE.h"
-// #include "TObjArray.h"
-// #include "TObjString.h"
-// #include "TString.h"
-// #include "AliTPCCalPad.h"
+//#include <TROOT.h>
+#include <TRandom.h>
+#include <TString.h>
+#include <TStyle.h>
+#include <TTreeStream.h>
 
+#include "AliTPCCalibCE.h"
+#include "AliMathBase.h"
+#include "AliTPCCalPad.h"
+#include "AliTPCCalROC.h"
+#include "AliTPCCalibPedestal.h"
+#include "AliTPCCalibPulser.h"
 
 //
 // AliRoot includes
 //
 #include "AliTPCCalibViewer.h"
 
+using std::ifstream;
 ClassImp(AliTPCCalibViewer)
 
+
 AliTPCCalibViewer::AliTPCCalibViewer()
                   :TObject(),
                    fTree(0),
                    fFile(0),
                    fListOfObjectsToBeDeleted(0),
-                   fTreeMustBeDeleted(0)
+                   fTreeMustBeDeleted(0), 
+                   fAbbreviation(0), 
+                   fAppendString(0)
 {
   //
   // Default constructor
@@ -88,7 +95,9 @@ AliTPCCalibViewer::AliTPCCalibViewer(const AliTPCCalibViewer &c)
                    fTree(0),
                    fFile(0),
                    fListOfObjectsToBeDeleted(0),
-                   fTreeMustBeDeleted(0)
+                   fTreeMustBeDeleted(0),
+                   fAbbreviation(0), 
+                   fAppendString(0)
 {
   //
   // dummy AliTPCCalibViewer copy constructor
@@ -98,15 +107,19 @@ AliTPCCalibViewer::AliTPCCalibViewer(const AliTPCCalibViewer &c)
   fTreeMustBeDeleted = c.fTreeMustBeDeleted;
   //fFile = new TFile(*(c.fFile));
   fListOfObjectsToBeDeleted = c.fListOfObjectsToBeDeleted;
+  fAbbreviation = c.fAbbreviation;
+  fAppendString = c.fAppendString;
 }
 
 //_____________________________________________________________________________
-AliTPCCalibViewer::AliTPCCalibViewer(TTree* tree)
+AliTPCCalibViewer::AliTPCCalibViewer(TTree *const tree)
                   :TObject(),
                    fTree(0),
                    fFile(0),
                    fListOfObjectsToBeDeleted(0),
-                   fTreeMustBeDeleted(0)
+                   fTreeMustBeDeleted(0),
+                   fAbbreviation(0), 
+                   fAppendString(0)
 {
   //
   // Constructor that initializes the calibration viewer
@@ -114,15 +127,20 @@ AliTPCCalibViewer::AliTPCCalibViewer(TTree* tree)
   fTree = tree;
   fTreeMustBeDeleted = kFALSE;
   fListOfObjectsToBeDeleted = new TObjArray();
+  fAbbreviation = "~";
+  fAppendString = ".fElements";
 }
 
 //_____________________________________________________________________________
-AliTPCCalibViewer::AliTPCCalibViewer(char* fileName, char* treeName)
+AliTPCCalibViewer::AliTPCCalibViewer(const char* fileName, const char* treeName)
                   :TObject(),
                    fTree(0),
                    fFile(0),
                    fListOfObjectsToBeDeleted(0),
-                   fTreeMustBeDeleted(0)
+                   fTreeMustBeDeleted(0),
+                   fAbbreviation(0), 
+                   fAppendString(0)
+                   
 {
    //
    // Constructor to initialize the calibration viewer
@@ -132,6 +150,8 @@ AliTPCCalibViewer::AliTPCCalibViewer(char* fileName, char* treeName)
    fTree = (TTree*) fFile->Get(treeName);
    fTreeMustBeDeleted = kTRUE;
    fListOfObjectsToBeDeleted = new TObjArray();
+   fAbbreviation = "~";
+   fAppendString = ".fElements";
 }
                    
 //____________________________________________________________________________
@@ -141,10 +161,14 @@ AliTPCCalibViewer & AliTPCCalibViewer::operator =(const AliTPCCalibViewer & para
    // assignment operator - dummy
    // not yet working!!!
    //
+  if (this == &param) return (*this);
+
    fTree = param.fTree;
    fTreeMustBeDeleted = param.fTreeMustBeDeleted;
    //fFile = new TFile(*(param.fFile));
    fListOfObjectsToBeDeleted = param.fListOfObjectsToBeDeleted;
+   fAbbreviation = param.fAbbreviation;
+   fAppendString = param.fAppendString;
    return (*this);
 }
 
@@ -173,23 +197,125 @@ AliTPCCalibViewer::~AliTPCCalibViewer()
 }
 
 //_____________________________________________________________________________
-void AliTPCCalibViewer::Delete(Option_t* option) {
+void AliTPCCalibViewer::Delete(Option_t* /*option*/) {
    //
    // Should be called from AliTPCCalibViewerGUI class only.
    // If you use Delete() do not call the destructor.
    // All objects (except those contained in fListOfObjectsToBeDeleted) will be deleted, the file will be closed.
    //
    
-   option = option;  // to avoid warnings on compiling   
    if (fTree && fTreeMustBeDeleted) {
       fTree->SetCacheSize(0);
       fTree->Delete();
    }
-   if (fFile)
-      delete fFile;
+   delete fFile;
    delete fListOfObjectsToBeDeleted;
 }
 
+
+const char* AliTPCCalibViewer::AddAbbreviations(const Char_t *c, Bool_t printDrawCommand){ 
+   // Replace all "<variable>" with "<variable><fAbbreviation>" (Adds forgotten "~")
+   // but take care on the statistical information, like "CEQmean_Mean"
+   // and also take care on correct given variables, like "CEQmean~"
+   // 
+   // For each variable out of "listOfVariables":
+   // - 'Save' correct items:
+   //   - form <replaceString>, take <variable>'s first char, add <removeString>, add rest of <variable>, e.g. "C!#EQmean" (<removeString> = "!#")
+   //   - For each statistical information in "listOfNormalizationVariables":
+   //     - ReplaceAll <variable><statistical_Information> with <replaceString><statistical_Information>
+   //   - ReplaceAll <variable><abbreviation> with <replaceString><abbreviation>, e.g. "CEQmean~" -> "C!#EQmean~"
+   //   - ReplaceAll <variable><appendStr> with <replaceString><appendStr>, e.g. "CEQmean.fElements" -> "C!#EQmean.fElements"
+   //
+   // - Do actual replacing:
+   //   - ReplaceAll <variable> with <variable><fAbbreviation>, e.g. "CEQmean" -> "CEQmean~"
+   //
+   // - Undo saving:
+   //   - For each statistical information in "listOfNormalizationVariables":
+   //     - ReplaceAll <replaceString><statistical_Information> with <variable><statistical_Information> 
+   //   - ReplaceAll <replaceString><abbreviation> with <variable><abbreviation>, e.g. "C!#EQmean~" -> "CEQmean~"
+   //   - ReplaceAll <replaceString><appendStr> with <variable><appendStr>, e.g. "C!#EQmean.fElements" -> "CEQmean.fElements"
+   // 
+   // Now all the missing "~" should be added.
+   
+   TString str(c);
+   TString removeString = "!#";  // very unpropable combination of chars
+   TString replaceString = "";
+   TString searchString = "";
+   TString normString = "";
+   TObjArray *listOfVariables = GetListOfVariables();
+   listOfVariables->Add(new TObjString("channel"));
+   listOfVariables->Add(new TObjString("gx"));
+   listOfVariables->Add(new TObjString("gy"));
+   listOfVariables->Add(new TObjString("lx"));
+   listOfVariables->Add(new TObjString("ly"));
+   listOfVariables->Add(new TObjString("pad"));
+   listOfVariables->Add(new TObjString("row"));
+   listOfVariables->Add(new TObjString("rpad"));
+   listOfVariables->Add(new TObjString("sector"));
+   TObjArray *listOfNormalizationVariables = GetListOfNormalizationVariables();
+   Int_t nVariables = listOfVariables->GetEntriesFast();
+   Int_t nNorm = listOfNormalizationVariables->GetEntriesFast();
+   
+   Int_t *varLengths = new Int_t[nVariables];
+   for (Int_t i = 0; i < nVariables; i++) {
+      varLengths[i] = ((TObjString*)listOfVariables->At(i))->String().Length();
+   }
+   Int_t *normLengths = new Int_t[nNorm];
+   for (Int_t i = 0; i < nNorm; i++) {
+      normLengths[i] = ((TObjString*)listOfNormalizationVariables->At(i))->String().Length();
+      // printf("normLengths[%i] (%s) = %i \n", i,((TObjString*)listOfNormalizationVariables->At(i))->String().Data(), normLengths[i]);
+   }
+   Int_t *varSort = new Int_t[nVariables];
+   TMath::Sort(nVariables, varLengths, varSort, kTRUE);
+   Int_t *normSort = new Int_t[nNorm];
+   TMath::Sort(nNorm, normLengths, normSort, kTRUE);
+   // for (Int_t i = 0; i<nNorm; i++)  printf("normLengths: %i\n", normLengths[normSort[i]]);
+   // for (Int_t i = 0; i<nVariables; i++) printf("varLengths: %i\n", varLengths[varSort[i]]);
+   
+   for (Int_t ivar = 0; ivar < nVariables; ivar++) {
+      // ***** save correct tokens *****
+      // first get the next variable:
+      searchString = ((TObjString*)listOfVariables->At(varSort[ivar]))->String();
+      // printf("searchString: %s ++++++++++++++\n", searchString.Data());
+      // form replaceString:
+      replaceString = "";
+      for (Int_t i = 0; i < searchString.Length(); i++) {
+         replaceString.Append(searchString[i]);
+         if (i == 0) replaceString.Append(removeString);
+      }
+      // go through normalization:
+      // printf("go through normalization\n");
+      for (Int_t inorm = 0; inorm < nNorm; inorm++) {
+         // printf(" inorm=%i, nNorm=%i, normSort[inorm]=%i \n", inorm, nNorm, normSort[inorm]);
+         normString = ((TObjString*)listOfNormalizationVariables->At(normSort[inorm]))->String();
+         // printf(" walking in normalization, i=%i, normString=%s \n", inorm, normString.Data());
+         str.ReplaceAll(searchString + normString, replaceString + normString);
+         // like: str.ReplaceAll("CEQmean_Mean", "C!EQmean_Mean");
+      }
+      str.ReplaceAll(searchString + fAbbreviation, replaceString + fAbbreviation);
+      // like: str.ReplaceAll("CEQmean~", "C!EQmean~");
+      str.ReplaceAll(searchString + fAppendString,    replaceString + fAppendString);
+      // like: str.ReplaceAll("CEQmean.fElements", "C!EQmean.fElements");
+      
+      // ***** add missing extensions *****
+      str.ReplaceAll(searchString, replaceString + fAbbreviation);
+      // like: str.ReplaceAll("CEQmean", "C!EQmean~");
+   }
+   
+   // ***** undo saving *****
+   str.ReplaceAll(removeString, "");
+  
+   if (printDrawCommand) std::cout << "The string looks now like: " << str.Data() << std::endl;
+   delete [] varLengths;
+   delete [] normLengths;
+   delete [] varSort;
+   delete [] normSort;
+   return str.Data();
+}
+
+
+
+
 //_____________________________________________________________________________
 Int_t AliTPCCalibViewer::EasyDraw(const char* drawCommand, const char* sector, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
   //
@@ -208,6 +334,14 @@ Int_t AliTPCCalibViewer::EasyDraw(const char* drawCommand, const char* sector, c
    sectorStr.ToUpper();
    TString cutStr("");
    //TString drawOptionsStr("profcolz ");
+   Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
+   if (dangerousToDraw) {
+      Warning("EasyDraw", "The draw string must not contain ':' or '>>'. Using only first variable for drawing!");
+//      return -1;
+//      drawStr.Resize(drawStr.First(">"));
+      drawStr.Resize(drawStr.First(":"));
+   }
+
    TString drawOptionsStr("");
    TRandom rnd(0);
    Int_t rndNumber = rnd.Integer(10000);
@@ -218,25 +352,33 @@ Int_t AliTPCCalibViewer::EasyDraw(const char* drawCommand, const char* sector, c
       drawOptionsStr += "profcolz";
 
    if (sectorStr == "A") {
-      drawStr += ":gy.fElements:gx.fElements>>prof";
+      drawStr += Form(":gy%s:gx%s>>prof", fAppendString.Data(), fAppendString.Data());
       drawStr += rndNumber;
       drawStr += "(330,-250,250,330,-250,250)";
       cutStr += "(sector/18)%2==0 ";
    }
    else if  (sectorStr == "C") {
-      drawStr += ":gy.fElements:gx.fElements>>prof";
+      drawStr += Form(":gy%s:gx%s>>prof", fAppendString.Data(), fAppendString.Data());
       drawStr += rndNumber;
       drawStr += "(330,-250,250,330,-250,250)";
       cutStr += "(sector/18)%2==1 ";
    }
    else if  (sectorStr == "ALL") {
-      drawStr += ":gy.fElements:gx.fElements>>prof";
+      drawStr += Form(":gy%s:gx%s>>prof", fAppendString.Data(), fAppendString.Data());
       drawStr += rndNumber;
       drawStr += "(330,-250,250,330,-250,250)";
    }
+   else if  (sectorStr.Contains("S")) {
+      drawStr += Form(":rpad%s:row%s+(sector>35)*63>>prof", fAppendString.Data(), fAppendString.Data());
+      drawStr += rndNumber;
+      drawStr += "(159,0,159,140,-70,70)";
+      TString sec=sectorStr;
+      sec.Remove(0,1);
+      cutStr += "sector%36=="+sec+" ";
+   }
    else if (sectorStr.IsDigit()) {
       Int_t isec = sectorStr.Atoi();
-      drawStr += ":rpad.fElements:row.fElements>>prof";
+      drawStr += Form(":rpad%s:row%s>>prof", fAppendString.Data(), fAppendString.Data());
       drawStr += rndNumber;
       if (isec < 36 && isec >= 0)
          drawStr += "(63,0,63,108,-54,54)";
@@ -257,10 +399,15 @@ Int_t AliTPCCalibViewer::EasyDraw(const char* drawCommand, const char* sector, c
       cutStr += cuts;
       cutStr += ")";
    }
-   drawStr.ReplaceAll("~", ".fElements");
-   cutStr.ReplaceAll("~", ".fElements");
+   drawStr.ReplaceAll(fAbbreviation, fAppendString);
+   cutStr.ReplaceAll(fAbbreviation, fAppendString);
    if (writeDrawCommand) std::cout << "fTree->Draw(\"" << drawStr << "\", \"" <<  cutStr << "\", \"" << drawOptionsStr << "\");" << std::endl;
-   return fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
+   Int_t returnValue = fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
+   TString profName("prof");
+   profName += rndNumber;
+   TObject *obj = gDirectory->Get(profName.Data());
+   if (obj && obj->InheritsFrom("TH1")) FormatHistoLabels((TH1*)obj);
+   return returnValue;
 }
 
 
@@ -274,9 +421,7 @@ Int_t AliTPCCalibViewer::EasyDraw(const char* drawCommand, Int_t sector, const c
   // writeDrawCommand: write the command, that is passed to TTree::Draw
   //
    if (sector >= 0 && sector < 72) {
-      char sectorChr[3];
-      sprintf(sectorChr, "%i", sector);
-      return EasyDraw(drawCommand, sectorChr, cuts, drawOptions, writeDrawCommand);
+      return EasyDraw(drawCommand, Form("%i", sector), cuts, drawOptions, writeDrawCommand);
    }
    Error("EasyDraw","The TPC contains only sectors between 0 and 71.");
    return -1;
@@ -316,6 +461,11 @@ Int_t AliTPCCalibViewer::EasyDraw1D(const char* drawCommand, const char* sector,
       cutStr += isec;
       cutStr += ") ";
    }
+   else if  (sectorStr.Contains("S")) {
+      TString sec=sectorStr;
+      sec.Remove(0,1);
+      cutStr += "sector%36=="+sec+" ";
+   }
 
    if (cuts && cuts[0] != 0) {
       if (cutStr.Length() != 0) cutStr += "&& ";
@@ -324,10 +474,20 @@ Int_t AliTPCCalibViewer::EasyDraw1D(const char* drawCommand, const char* sector,
       cutStr += ")";
    }
 
-   drawStr.ReplaceAll("~", ".fElements");
-   cutStr.ReplaceAll("~", ".fElements");
+   drawStr.ReplaceAll(fAbbreviation, fAppendString);
+   cutStr.ReplaceAll(fAbbreviation, fAppendString);
    if (writeDrawCommand) std::cout << "fTree->Draw(\"" << drawStr << "\", \"" <<  cutStr << "\", \"" << drawOptionsStr << "\");" << std::endl;
-   return fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
+   Int_t returnValue = fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
+   if (returnValue == -1) return -1;
+   
+   TObject *obj = (gPad) ? gPad->GetPrimitive("htemp") : 0; 
+   if (!obj) obj = (TH1F*)gDirectory->Get("htemp");
+   if (!obj) obj = gPad->GetPrimitive("tempHist");
+   if (!obj) obj = (TH1F*)gDirectory->Get("tempHist");
+   if (!obj) obj = gPad->GetPrimitive("Graph");
+   if (!obj) obj = (TH1F*)gDirectory->Get("Graph");
+   if (obj && obj->InheritsFrom("TH1")) FormatHistoLabels((TH1*)obj);
+   return returnValue;
 }
 
 
@@ -342,15 +502,45 @@ Int_t AliTPCCalibViewer::EasyDraw1D(const char* drawCommand, Int_t sector, const
   //
 
    if (sector >= 0 && sector < 72) {
-      char sectorChr[3];
-      sprintf(sectorChr, "%i", sector);
-      return EasyDraw1D(drawCommand, sectorChr, cuts, drawOptions, writeDrawCommand);
+      return EasyDraw1D(drawCommand, Form("%i",sector), cuts, drawOptions, writeDrawCommand);
    }
   Error("EasyDraw","The TPC contains only sectors between 0 and 71.");
   return -1;
 }
 
 
+void AliTPCCalibViewer::FormatHistoLabels(TH1 *histo) const {
+   // 
+   // formats title and axis labels of histo 
+   // removes '.fElements'
+   // 
+   if (!histo) return;
+   TString replaceString(fAppendString.Data());
+   TString *str = new TString(histo->GetTitle());
+   str->ReplaceAll(replaceString, "");
+   histo->SetTitle(str->Data());
+   delete str;
+   if (histo->GetXaxis()) {
+      str = new TString(histo->GetXaxis()->GetTitle());
+      str->ReplaceAll(replaceString, "");
+      histo->GetXaxis()->SetTitle(str->Data());
+      delete str;
+   }
+   if (histo->GetYaxis()) {
+      str = new TString(histo->GetYaxis()->GetTitle());
+      str->ReplaceAll(replaceString, "");
+      histo->GetYaxis()->SetTitle(str->Data());
+      delete str;
+   }
+   if (histo->GetZaxis()) {
+      str = new TString(histo->GetZaxis()->GetTitle());
+      str->ReplaceAll(replaceString, "");
+      histo->GetZaxis()->SetTitle(str->Data());
+      delete str;
+   }
+}
+
+
 Int_t  AliTPCCalibViewer::DrawHisto1D(const char* drawCommand, Int_t sector, const char* cuts, const char *sigmas, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM) const {
    // 
    // Easy drawing of data, in principle the same as EasyDraw1D
@@ -360,9 +550,7 @@ Int_t  AliTPCCalibViewer::DrawHisto1D(const char* drawCommand, Int_t sector, con
    // "plotMean", "plotMedian" and "plotLTM": what kind of lines do you want to see?
    // 
    if (sector >= 0 && sector < 72) {
-      char sectorChr[3];
-      sprintf(sectorChr, "%i", sector);
-      return DrawHisto1D(drawCommand, sectorChr, cuts, sigmas, plotMean, plotMedian, plotLTM);
+      return DrawHisto1D(drawCommand, Form("%i", sector), cuts, sigmas, plotMean, plotMedian, plotLTM);
    }
    Error("DrawHisto1D","The TPC contains only sectors between 0 and 71.");
    return -1;
@@ -388,8 +576,14 @@ Int_t  AliTPCCalibViewer::DrawHisto1D(const char* drawCommand, const char* secto
       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
       nsigma[i] = sig;
    }
-   
+   delete sigmasTokens;
+
    TString drawStr(drawCommand);
+   Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
+   if (dangerousToDraw) {
+      Warning("DrawHisto1D", "The draw string must not contain ':' or '>>'.");
+      return -1;
+   }
    drawStr += " >> tempHist";
    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts);
    TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
@@ -401,61 +595,55 @@ Int_t  AliTPCCalibViewer::DrawHisto1D(const char* drawCommand, const char* secto
    Double_t sigma = TMath::RMS(entries, values);
    Double_t maxY = htemp->GetMaximum();
    
-   char c[500];
    TLegend * legend = new TLegend(.7,.7, .99, .99, "Statistical information");
-//    sprintf(c, "%s, sector: %i", type, sector);
-   fListOfObjectsToBeDeleted->Add(legend);
+   //fListOfObjectsToBeDeleted->Add(legend);
 
    if (plotMean) {
       // draw Mean
       TLine* line = new TLine(mean, 0, mean, maxY);
-      fListOfObjectsToBeDeleted->Add(line);
+      //fListOfObjectsToBeDeleted->Add(line);
       line->SetLineColor(kRed);
       line->SetLineWidth(2);
       line->SetLineStyle(1);
       line->Draw();
-      sprintf(c, "Mean: %f", mean);
-      legend->AddEntry(line, c, "l");
+      legend->AddEntry(line, Form("Mean: %f", mean), "l");
       // draw sigma lines
       for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
          TLine* linePlusSigma = new TLine(mean + nsigma[i] * sigma, 0, mean + nsigma[i] * sigma, maxY);
-         fListOfObjectsToBeDeleted->Add(linePlusSigma);
+         //fListOfObjectsToBeDeleted->Add(linePlusSigma);
          linePlusSigma->SetLineColor(kRed);
          linePlusSigma->SetLineStyle(2 + i);
          linePlusSigma->Draw();
          TLine* lineMinusSigma = new TLine(mean - nsigma[i] * sigma, 0, mean - nsigma[i] * sigma, maxY);
-         fListOfObjectsToBeDeleted->Add(lineMinusSigma);
+         //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
          lineMinusSigma->SetLineColor(kRed);
          lineMinusSigma->SetLineStyle(2 + i);
          lineMinusSigma->Draw();
-         sprintf(c, "%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma));
-         legend->AddEntry(lineMinusSigma, c, "l");
+         legend->AddEntry(lineMinusSigma, Form("%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma)), "l");
       }
    }
    if (plotMedian) {
       // draw median
       TLine* line = new TLine(median, 0, median, maxY);
-      fListOfObjectsToBeDeleted->Add(line);
+      //fListOfObjectsToBeDeleted->Add(line);
       line->SetLineColor(kBlue);
       line->SetLineWidth(2);
       line->SetLineStyle(1);
       line->Draw();
-      sprintf(c, "Median: %f", median);
-      legend->AddEntry(line, c, "l");
+      legend->AddEntry(line, Form("Median: %f", median), "l");
       // draw sigma lines
       for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
          TLine* linePlusSigma = new TLine(median + nsigma[i] * sigma, 0, median + nsigma[i]*sigma, maxY);
-         fListOfObjectsToBeDeleted->Add(linePlusSigma);
+         //fListOfObjectsToBeDeleted->Add(linePlusSigma);
          linePlusSigma->SetLineColor(kBlue);
          linePlusSigma->SetLineStyle(2 + i);
          linePlusSigma->Draw();
          TLine* lineMinusSigma = new TLine(median - nsigma[i] * sigma, 0, median - nsigma[i]*sigma, maxY);
-         fListOfObjectsToBeDeleted->Add(lineMinusSigma);
+         //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
          lineMinusSigma->SetLineColor(kBlue);
          lineMinusSigma->SetLineStyle(2 + i);
          lineMinusSigma->Draw();
-         sprintf(c, "%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma));
-         legend->AddEntry(lineMinusSigma, c, "l");
+         legend->AddEntry(lineMinusSigma, Form("%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma)), "l");
       }
    }
    if (plotLTM) {
@@ -463,28 +651,26 @@ Int_t  AliTPCCalibViewer::DrawHisto1D(const char* drawCommand, const char* secto
       Double_t ltmRms = 0;
       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
       TLine* line = new TLine(ltm, 0, ltm, maxY);
-      fListOfObjectsToBeDeleted->Add(line);
+      //fListOfObjectsToBeDeleted->Add(line);
       line->SetLineColor(kGreen+2);
       line->SetLineWidth(2);
       line->SetLineStyle(1);
       line->Draw();
-      sprintf(c, "LTM: %f", ltm);
-      legend->AddEntry(line, c, "l");
+      legend->AddEntry(line, Form("LTM: %f", ltm), "l");
       // draw sigma lines
       for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
          TLine* linePlusSigma = new TLine(ltm + nsigma[i] * ltmRms, 0, ltm + nsigma[i] * ltmRms, maxY);
-         fListOfObjectsToBeDeleted->Add(linePlusSigma);
+         //fListOfObjectsToBeDeleted->Add(linePlusSigma);
          linePlusSigma->SetLineColor(kGreen+2);
          linePlusSigma->SetLineStyle(2+i);
          linePlusSigma->Draw();
    
          TLine* lineMinusSigma = new TLine(ltm - nsigma[i] * ltmRms, 0, ltm - nsigma[i] * ltmRms, maxY);
-         fListOfObjectsToBeDeleted->Add(lineMinusSigma);
+         //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
          lineMinusSigma->SetLineColor(kGreen+2);
          lineMinusSigma->SetLineStyle(2+i);
          lineMinusSigma->Draw();
-         sprintf(c, "%i #sigma = %f", (Int_t)(nsigma[i]), (Float_t)(nsigma[i] * ltmRms));
-         legend->AddEntry(lineMinusSigma, c, "l");
+         legend->AddEntry(lineMinusSigma, Form("%i #sigma = %f", (Int_t)(nsigma[i]), (Float_t)(nsigma[i] * ltmRms)), "l");
       }
    }
    if (!plotMean && !plotMedian && !plotLTM) return -1;
@@ -503,7 +689,7 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, Int_t sector, const c
    // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma, Begin_Latex t #sigma End_Latex)
    // sigmaStep: the binsize of the generated histogram
    // Begin_Latex 
-   // f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #frac{#int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx + #int_{#mu}^{#mu - t #sigma} f(x, #mu, #sigma) dx }{ #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx }
+   // f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = (#int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx + #int_{#mu}^{#mu - t #sigma} f(x, #mu, #sigma) dx) / (#int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx)
    // End_Latex
    // 
    //
@@ -515,9 +701,7 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, Int_t sector, const c
    // plotMean/plotMedian/plotLTM: specifies where to put the center
    //
    if (sector >= 0 && sector < 72) {
-      char sectorChr[3];
-      sprintf(sectorChr, "%i", sector);
-      return SigmaCut(drawCommand, sectorChr, cuts, sigmaMax, plotMean, plotMedian, plotLTM, pm, sigmas, sigmaStep);
+      return SigmaCut(drawCommand, Form("%i", sector), cuts, sigmaMax, plotMean, plotMedian, plotLTM, pm, sigmas, sigmaStep);
    }
    Error("SigmaCut","The TPC contains only sectors between 0 and 71.");
    return -1;
@@ -537,6 +721,11 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, const char* sector, c
    Double_t ltmFraction = 0.8;
    
    TString drawStr(drawCommand);
+   Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
+   if (dangerousToDraw) {
+      Warning("SigmaCut", "The draw string must not contain ':' or '>>'.");
+      return -1;
+   }
    drawStr += " >> tempHist";
    
    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
@@ -549,7 +738,7 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, const char* sector, c
    Double_t sigma = TMath::RMS(entries, values);
    
    TLegend * legend = new TLegend(.7,.7, .99, .99, "Cumulative");
-   fListOfObjectsToBeDeleted->Add(legend);
+   //fListOfObjectsToBeDeleted->Add(legend);
    TH1F *cutHistoMean = 0;
    TH1F *cutHistoMedian = 0;
    TH1F *cutHistoLTM = 0;
@@ -561,11 +750,12 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, const char* sector, c
       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
       nsigma[i] = sig;
    }
-  
+   delete sigmasTokens;
+
    if (plotMean) {
       cutHistoMean = AliTPCCalibViewer::SigmaCut(htemp, mean, sigma, sigmaMax, sigmaStep, pm);
       if (cutHistoMean) {
-         fListOfObjectsToBeDeleted->Add(cutHistoMean);
+         //fListOfObjectsToBeDeleted->Add(cutHistoMean);
          cutHistoMean->SetLineColor(kRed);
          legend->AddEntry(cutHistoMean, "Mean", "l");
          cutHistoMean->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
@@ -577,7 +767,7 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, const char* sector, c
    if (plotMedian) {
       cutHistoMedian = AliTPCCalibViewer::SigmaCut(htemp, median, sigma, sigmaMax, sigmaStep, pm);
       if (cutHistoMedian) {
-         fListOfObjectsToBeDeleted->Add(cutHistoMedian);
+         //fListOfObjectsToBeDeleted->Add(cutHistoMedian);
          cutHistoMedian->SetLineColor(kBlue);
          legend->AddEntry(cutHistoMedian, "Median", "l");
          cutHistoMedian->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
@@ -591,11 +781,11 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, const char* sector, c
       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
       cutHistoLTM = AliTPCCalibViewer::SigmaCut(htemp, ltm, ltmRms, sigmaMax, sigmaStep, pm);
       if (cutHistoLTM) {
-         fListOfObjectsToBeDeleted->Add(cutHistoLTM);
+         //fListOfObjectsToBeDeleted->Add(cutHistoLTM);
          cutHistoLTM->SetLineColor(kGreen+2);
          legend->AddEntry(cutHistoLTM, "LTM", "l");
          cutHistoLTM->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
-         if (plotMean && cutHistoMean || plotMedian && cutHistoMedian) cutHistoLTM->Draw("same");
+         if ((plotMean && cutHistoMean) || (plotMedian && cutHistoMedian)) cutHistoLTM->Draw("same");
             else cutHistoLTM->Draw();
          DrawLines(cutHistoLTM, nsigma, legend, kGreen+2, pm);
       }
@@ -605,7 +795,8 @@ Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, const char* sector, c
    return 1;
 }
 
-Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector, const char* cuts, Float_t sigmaMax, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM, Bool_t pm, const char *sigmas, Float_t sigmaStep) const {
+
+Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector, const char* cuts, Float_t /*sigmaMax*/, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM, Bool_t /*pm*/, const char *sigmas, Float_t /*sigmaStep*/) const {
    //
    // Creates a histogram, where you can see, how much of the data are inside sigma-intervals 
    // around the mean/median/LTM
@@ -616,10 +807,6 @@ Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector
    //
   
    // Double_t ltmFraction = 0.8;  //unused
-   // avoid compiler warnings:
-   sigmaMax = sigmaMax;
-   pm = pm;
-   sigmaStep = sigmaStep;
    
    TString drawStr(drawCommand);
    drawStr += " >> tempHist";
@@ -640,7 +827,7 @@ Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector
    Double_t sigma = TMath::RMS(entries, values);
    
    TLegend * legend = new TLegend(.7,.7, .99, .99, "Cumulative");
-   fListOfObjectsToBeDeleted->Add(legend);
+   //fListOfObjectsToBeDeleted->Add(legend);
    
    // parse sigmas string
    TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");  
@@ -650,6 +837,7 @@ Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector
       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
       nsigma[i] = sig;
    }
+   delete sigmasTokens;
    
    if (plotMean) {
       for (Int_t i = 0; i < entries; i++) {
@@ -658,7 +846,7 @@ Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector
       }
       cutGraphMean = new TGraph(entries, xarray, yarray);
       if (cutGraphMean) {
-         fListOfObjectsToBeDeleted->Add(cutGraphMean);
+         //fListOfObjectsToBeDeleted->Add(cutGraphMean);
          cutGraphMean->SetLineColor(kRed);
          legend->AddEntry(cutGraphMean, "Mean", "l");
          cutGraphMean->SetTitle(Form("%s, Cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
@@ -666,6 +854,9 @@ Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector
          DrawLines(cutGraphMean, nsigma, legend, kRed, kTRUE);
       }
    }
+  delete [] index;
+  delete [] xarray;
+  delete [] yarray;
    /*
    if (plotMedian) {
       cutHistoMedian = AliTPCCalibViewer::SigmaCut(htemp, median, sigma, sigmaMax, sigmaStep, pm);
@@ -699,8 +890,6 @@ Int_t AliTPCCalibViewer::SigmaCutNew(const char* drawCommand, const char* sector
 }
 
 
-
-
 Int_t AliTPCCalibViewer::Integrate(const char* drawCommand,       Int_t sector, const char* cuts, Float_t sigmaMax, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM, const char *sigmas, Float_t sigmaStep) const {
    //
    // Creates an integrated histogram Begin_Latex S(t, #mu, #sigma) End_Latex, out of the input distribution distribution Begin_Latex f(x, #mu, #sigma) End_Latex, given in "histogram"   
@@ -710,13 +899,11 @@ Int_t AliTPCCalibViewer::Integrate(const char* drawCommand,       Int_t sector,
    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
    // The actual work is done on the array.
    /* Begin_Latex 
-         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #frac{#int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx}{ #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx }
+         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
       End_Latex  
    */
    if (sector >= 0 && sector < 72) {
-      char sectorChr[3];
-      sprintf(sectorChr, "%i", sector);
-      return Integrate(drawCommand, sectorChr, cuts, sigmaMax, plotMean, plotMedian, plotLTM, sigmas, sigmaStep);
+      return Integrate(drawCommand, Form("%i", sector), cuts, sigmaMax, plotMean, plotMedian, plotLTM, sigmas, sigmaStep);
    }
    Error("Integrate","The TPC contains only sectors between 0 and 71.");
    return -1;
@@ -733,7 +920,7 @@ Int_t AliTPCCalibViewer::IntegrateOld(const char* drawCommand, const char* secto
    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
    // The actual work is done on the array.
    /* Begin_Latex 
-         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #frac{#int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx}{ #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx }
+         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
       End_Latex  
    */
    
@@ -758,9 +945,10 @@ Int_t AliTPCCalibViewer::IntegrateOld(const char* drawCommand, const char* secto
       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
       nsigma[i] = sig;
    }
-  
+   delete sigmasTokens;
+
    TLegend * legend = new TLegend(.7,.7, .99, .99, "Integrated histogram");
-   fListOfObjectsToBeDeleted->Add(legend);
+   //fListOfObjectsToBeDeleted->Add(legend);
    TH1F *integralHistoMean = 0;
    TH1F *integralHistoMedian = 0;
    TH1F *integralHistoLTM = 0;
@@ -768,7 +956,7 @@ Int_t AliTPCCalibViewer::IntegrateOld(const char* drawCommand, const char* secto
    if (plotMean) {
       integralHistoMean = AliTPCCalibViewer::Integrate(htemp, mean, sigma, sigmaMax, sigmaStep);
       if (integralHistoMean) {
-         fListOfObjectsToBeDeleted->Add(integralHistoMean);
+         //fListOfObjectsToBeDeleted->Add(integralHistoMean);
          integralHistoMean->SetLineColor(kRed);
          legend->AddEntry(integralHistoMean, "Mean", "l");
          integralHistoMean->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
@@ -779,7 +967,7 @@ Int_t AliTPCCalibViewer::IntegrateOld(const char* drawCommand, const char* secto
    if (plotMedian) {
       integralHistoMedian = AliTPCCalibViewer::Integrate(htemp, median, sigma, sigmaMax, sigmaStep);
       if (integralHistoMedian) {
-         fListOfObjectsToBeDeleted->Add(integralHistoMedian);
+         //fListOfObjectsToBeDeleted->Add(integralHistoMedian);
          integralHistoMedian->SetLineColor(kBlue);
          legend->AddEntry(integralHistoMedian, "Median", "l");
          integralHistoMedian->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
@@ -793,11 +981,11 @@ Int_t AliTPCCalibViewer::IntegrateOld(const char* drawCommand, const char* secto
       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
       integralHistoLTM = AliTPCCalibViewer::Integrate(htemp, ltm, ltmRms, sigmaMax, sigmaStep);
       if (integralHistoLTM) {
-         fListOfObjectsToBeDeleted->Add(integralHistoLTM);
+         //fListOfObjectsToBeDeleted->Add(integralHistoLTM);
          integralHistoLTM->SetLineColor(kGreen+2);
          legend->AddEntry(integralHistoLTM, "LTM", "l");
          integralHistoLTM->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
-         if (plotMean && integralHistoMean || plotMedian && integralHistoMedian) integralHistoLTM->Draw("same");
+         if ((plotMean && integralHistoMean) || (plotMedian && integralHistoMedian)) integralHistoLTM->Draw("same");
             else integralHistoLTM->Draw();
          DrawLines(integralHistoLTM, nsigma, legend, kGreen+2, kTRUE);
       }
@@ -808,7 +996,7 @@ Int_t AliTPCCalibViewer::IntegrateOld(const char* drawCommand, const char* secto
 }
 
 
-Int_t AliTPCCalibViewer::Integrate(const char* drawCommand, const char* sector, const char* cuts, Float_t sigmaMax, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM, const char *sigmas, Float_t sigmaStep) const {
+Int_t AliTPCCalibViewer::Integrate(const char* drawCommand, const char* sector, const char* cuts, Float_t /*sigmaMax*/, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM, const char *sigmas, Float_t /*sigmaStep*/) const {
    //
    // Creates an integrated histogram Begin_Latex S(t, #mu, #sigma) End_Latex, out of the input distribution distribution Begin_Latex f(x, #mu, #sigma) End_Latex, given in "histogram"   
    // "mean" and "sigma" are Begin_Latex #mu End_Latex and  Begin_Latex #sigma End_Latex of the distribution in "histogram", to be specified by the user
@@ -817,16 +1005,18 @@ Int_t AliTPCCalibViewer::Integrate(const char* drawCommand, const char* sector,
    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
    // The actual work is done on the array.
    /* Begin_Latex 
-         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #frac{#int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx}{ #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx }
+         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
       End_Latex  
    */
    
    Double_t ltmFraction = 0.8;
-   // avoid compiler warnings:
-   sigmaMax = sigmaMax;
-   sigmaStep = sigmaStep;
    
    TString drawStr(drawCommand);
+   Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
+   if (dangerousToDraw) {
+      Warning("Integrate", "The draw string must not contain ':' or '>>'.");
+      return -1;
+   }
    drawStr += " >> tempHist";
    
    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
@@ -852,9 +1042,10 @@ Int_t AliTPCCalibViewer::Integrate(const char* drawCommand, const char* sector,
       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
       nsigma[i] = sig;
    }
-  
+   delete sigmasTokens;
+
    TLegend * legend = new TLegend(.7,.7, .99, .99, "Integrated histogram");
-   fListOfObjectsToBeDeleted->Add(legend);
+   //fListOfObjectsToBeDeleted->Add(legend);
   
    if (plotMean) {
       for (Int_t i = 0; i < entries; i++) {
@@ -863,7 +1054,7 @@ Int_t AliTPCCalibViewer::Integrate(const char* drawCommand, const char* sector,
       }
       integralGraphMean = new TGraph(entries, xarray, yarray);
       if (integralGraphMean) {
-         fListOfObjectsToBeDeleted->Add(integralGraphMean);
+         //fListOfObjectsToBeDeleted->Add(integralGraphMean);
          integralGraphMean->SetLineColor(kRed);
          legend->AddEntry(integralGraphMean, "Mean", "l");
          integralGraphMean->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
@@ -878,7 +1069,7 @@ Int_t AliTPCCalibViewer::Integrate(const char* drawCommand, const char* sector,
       }
       integralGraphMedian = new TGraph(entries, xarray, yarray);
       if (integralGraphMedian) {
-         fListOfObjectsToBeDeleted->Add(integralGraphMedian);
+         //fListOfObjectsToBeDeleted->Add(integralGraphMedian);
          integralGraphMedian->SetLineColor(kBlue);
          legend->AddEntry(integralGraphMedian, "Median", "l");
          integralGraphMedian->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
@@ -896,15 +1087,19 @@ Int_t AliTPCCalibViewer::Integrate(const char* drawCommand, const char* sector,
       }
       integralGraphLTM = new TGraph(entries, xarray, yarray);
       if (integralGraphLTM) {
-         fListOfObjectsToBeDeleted->Add(integralGraphLTM);
+         //fListOfObjectsToBeDeleted->Add(integralGraphLTM);
          integralGraphLTM->SetLineColor(kGreen+2);
          legend->AddEntry(integralGraphLTM, "LTM", "l");
          integralGraphLTM->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
-         if (plotMean && integralGraphMean || plotMedian && integralGraphMedian) integralGraphLTM->Draw("samelu");
+         if ((plotMean && integralGraphMean) || (plotMedian && integralGraphMedian)) integralGraphLTM->Draw("samelu");
             else integralGraphLTM->Draw("alu");
          DrawLines(integralGraphLTM, nsigma, legend, kGreen+2, kTRUE);
       }
    }
+   delete [] index;
+   delete [] xarray;
+   delete [] yarray;
+  
    if (!plotMean && !plotMedian && !plotLTM) return -1;
    legend->Draw();
    return entries;
@@ -918,50 +1113,46 @@ void AliTPCCalibViewer::DrawLines(TH1F *histogram, TVectorF nsigma, TLegend *leg
    // 
    
    // start to draw the lines, loop over requested sigmas
-   char c[500];
    for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
       if (!pm) { 
          Int_t bin = histogram->GetXaxis()->FindBin(nsigma[i]);
          TLine* lineUp = new TLine(nsigma[i], 0, nsigma[i], histogram->GetBinContent(bin));
-         fListOfObjectsToBeDeleted->Add(lineUp);
+         //fListOfObjectsToBeDeleted->Add(lineUp);
          lineUp->SetLineColor(color);
          lineUp->SetLineStyle(2 + i);
          lineUp->Draw();
          TLine* lineLeft = new TLine(nsigma[i], histogram->GetBinContent(bin), 0, histogram->GetBinContent(bin));
-         fListOfObjectsToBeDeleted->Add(lineLeft);
+         //fListOfObjectsToBeDeleted->Add(lineLeft);
          lineLeft->SetLineColor(color);
          lineLeft->SetLineStyle(2 + i);
          lineLeft->Draw();
-         sprintf(c, "Fraction(%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin));
-         legend->AddEntry(lineLeft, c, "l");
+         legend->AddEntry(lineLeft, Form("Fraction(%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin)), "l");
       }
       else { // if (pm)
          Int_t bin = histogram->GetXaxis()->FindBin(nsigma[i]);
          TLine* lineUp1 = new TLine(nsigma[i], 0, nsigma[i], histogram->GetBinContent(bin));
-         fListOfObjectsToBeDeleted->Add(lineUp1);
+         //fListOfObjectsToBeDeleted->Add(lineUp1);
          lineUp1->SetLineColor(color);
          lineUp1->SetLineStyle(2 + i);
          lineUp1->Draw();
          TLine* lineLeft1 = new TLine(nsigma[i], histogram->GetBinContent(bin), histogram->GetBinLowEdge(0)+histogram->GetBinWidth(0), histogram->GetBinContent(bin));
-         fListOfObjectsToBeDeleted->Add(lineLeft1);
+         //fListOfObjectsToBeDeleted->Add(lineLeft1);
          lineLeft1->SetLineColor(color);
          lineLeft1->SetLineStyle(2 + i);
          lineLeft1->Draw();
-         sprintf(c, "Fraction(+%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin));
-         legend->AddEntry(lineLeft1, c, "l");
+         legend->AddEntry(lineLeft1, Form("Fraction(+%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin)), "l");
          bin = histogram->GetXaxis()->FindBin(-nsigma[i]);
          TLine* lineUp2 = new TLine(-nsigma[i], 0, -nsigma[i], histogram->GetBinContent(bin));
-         fListOfObjectsToBeDeleted->Add(lineUp2);
+         //fListOfObjectsToBeDeleted->Add(lineUp2);
          lineUp2->SetLineColor(color);
          lineUp2->SetLineStyle(2 + i);
          lineUp2->Draw();
          TLine* lineLeft2 = new TLine(-nsigma[i], histogram->GetBinContent(bin), histogram->GetBinLowEdge(0)+histogram->GetBinWidth(0), histogram->GetBinContent(bin));
-         fListOfObjectsToBeDeleted->Add(lineLeft2);
+         //fListOfObjectsToBeDeleted->Add(lineLeft2);
          lineLeft2->SetLineColor(color);
          lineLeft2->SetLineStyle(2 + i);
          lineLeft2->Draw();
-         sprintf(c, "Fraction(-%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin));
-         legend->AddEntry(lineLeft2, c, "l");
+         legend->AddEntry(lineLeft2, Form("Fraction(-%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin)), "l");
       }
    }  // for (Int_t i = 0; i < nsigma.GetNoElements(); i++)   
 }
@@ -974,47 +1165,43 @@ void AliTPCCalibViewer::DrawLines(TGraph *graph, TVectorF nsigma, TLegend *legen
    // 
    
    // start to draw the lines, loop over requested sigmas
-   char c[500];
    for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
       if (!pm) { 
          TLine* lineUp = new TLine(nsigma[i], 0, nsigma[i], graph->Eval(nsigma[i]));
-         fListOfObjectsToBeDeleted->Add(lineUp);
+         //fListOfObjectsToBeDeleted->Add(lineUp);
          lineUp->SetLineColor(color);
          lineUp->SetLineStyle(2 + i);
          lineUp->Draw();
          TLine* lineLeft = new TLine(nsigma[i], graph->Eval(nsigma[i]), 0, graph->Eval(nsigma[i]));
-         fListOfObjectsToBeDeleted->Add(lineLeft);
+         //fListOfObjectsToBeDeleted->Add(lineLeft);
          lineLeft->SetLineColor(color);
          lineLeft->SetLineStyle(2 + i);
          lineLeft->Draw();
-         sprintf(c, "Fraction(%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i]));
-         legend->AddEntry(lineLeft, c, "l");
+         legend->AddEntry(lineLeft, Form("Fraction(%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i])), "l");
       }
       else { // if (pm)
          TLine* lineUp1 = new TLine(nsigma[i], 0, nsigma[i], graph->Eval(nsigma[i]));
-         fListOfObjectsToBeDeleted->Add(lineUp1);
+         //fListOfObjectsToBeDeleted->Add(lineUp1);
          lineUp1->SetLineColor(color);
          lineUp1->SetLineStyle(2 + i);
          lineUp1->Draw();
          TLine* lineLeft1 = new TLine(nsigma[i], graph->Eval(nsigma[i]), graph->GetHistogram()->GetXaxis()->GetBinLowEdge(0), graph->Eval(nsigma[i]));
-         fListOfObjectsToBeDeleted->Add(lineLeft1);
+         //fListOfObjectsToBeDeleted->Add(lineLeft1);
          lineLeft1->SetLineColor(color);
          lineLeft1->SetLineStyle(2 + i);
          lineLeft1->Draw();
-         sprintf(c, "Fraction(+%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i]));
-         legend->AddEntry(lineLeft1, c, "l");
+         legend->AddEntry(lineLeft1, Form("Fraction(+%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i])), "l");
          TLine* lineUp2 = new TLine(-nsigma[i], 0, -nsigma[i], graph->Eval(-nsigma[i]));
-         fListOfObjectsToBeDeleted->Add(lineUp2);
+         //fListOfObjectsToBeDeleted->Add(lineUp2);
          lineUp2->SetLineColor(color);
          lineUp2->SetLineStyle(2 + i);
          lineUp2->Draw();
          TLine* lineLeft2 = new TLine(-nsigma[i], graph->Eval(-nsigma[i]), graph->GetHistogram()->GetXaxis()->GetBinLowEdge(0), graph->Eval(-nsigma[i]));
-         fListOfObjectsToBeDeleted->Add(lineLeft2);
+         //fListOfObjectsToBeDeleted->Add(lineLeft2);
          lineLeft2->SetLineColor(color);
          lineLeft2->SetLineStyle(2 + i);
          lineLeft2->Draw();
-         sprintf(c, "Fraction(-%f #sigma) = %f",nsigma[i], graph->Eval(-nsigma[i]));
-         legend->AddEntry(lineLeft2, c, "l");
+         legend->AddEntry(lineLeft2, Form("Fraction(-%f #sigma) = %f",nsigma[i], graph->Eval(-nsigma[i])), "l");
       }
    }  // for (Int_t i = 0; i < nsigma.GetNoElements(); i++)   
 }
@@ -1046,7 +1233,7 @@ Int_t AliTPCCalibViewer::GetBin(Float_t value, Int_t nbins, Double_t binLow, Dou
 }   
 
 
-Double_t AliTPCCalibViewer::GetLTM(Int_t n, Double_t *array, Double_t *sigma, Double_t fraction){
+Double_t AliTPCCalibViewer::GetLTM(Int_t n, const Double_t *const array, Double_t *const sigma, Double_t fraction){
    //
    //  returns the LTM and sigma
    //
@@ -1065,7 +1252,7 @@ Double_t AliTPCCalibViewer::GetLTM(Int_t n, Double_t *array, Double_t *sigma, Do
 }
 
 
-TH1F* AliTPCCalibViewer::SigmaCut(TH1F *histogram, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep, Bool_t pm) {
+TH1F* AliTPCCalibViewer::SigmaCut(TH1F *const histogram, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep, Bool_t pm) {
    //
    // Creates a cumulative histogram Begin_Latex S(t, #mu, #sigma) End_Latex, where you can see, how much of the data are inside sigma-intervals around the mean value
    // The data of the distribution Begin_Latex f(x, #mu, #sigma) End_Latex are given in 'histogram'
@@ -1075,22 +1262,22 @@ TH1F* AliTPCCalibViewer::SigmaCut(TH1F *histogram, Float_t mean, Float_t sigma,
    // pm: Decide weather Begin_Latex t > 0 End_Latex (first case) or Begin_Latex t End_Latex arbitrary (secound case)
    // The actual work is done on the array.
    /* Begin_Latex 
-         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #frac{#int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx + #int_{#mu}^{#mu - t #sigma} f(x, #mu, #sigma) dx }{ #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx } ,    for  t > 0    
+         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = (#int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx + #int_{#mu}^{#mu - t #sigma} f(x, #mu, #sigma) dx) / (#int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx),    for  t > 0    
          or      
-         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #frac{#int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx}{ #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx }
+         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #int_{#mu}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
       End_Latex  
-      begin_macro(source)
+      Begin_Macro(source)
       {
          Float_t mean = 0;
          Float_t sigma = 1.5;
          Float_t sigmaMax = 4;
          gROOT->SetStyle("Plain");
-         TH1F *distribution = new TH1F("Distribution", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
+         TH1F *distribution = new TH1F("Distrib1", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
          TRandom rand(23);
          for (Int_t i = 0; i <50000;i++) distribution->Fill(rand.Gaus(mean, sigma));
          Float_t *ar = distribution->GetArray();
          
-         TCanvas* macro_example_canvas = new TCanvas("macro_example_canvas", "", 350, 350);
+         TCanvas* macro_example_canvas = new TCanvas("cAliTPCCalibViewer1", "", 350, 350);
          macro_example_canvas->Divide(0,3);
          TVirtualPad *pad1 = macro_example_canvas->cd(1);
          pad1->SetGridy();
@@ -1110,7 +1297,7 @@ TH1F* AliTPCCalibViewer::SigmaCut(TH1F *histogram, Float_t mean, Float_t sigma,
          shistPM->Draw();   
          return macro_example_canvas;
       }  
-      end_macro
+      End_Macro
    */ 
    
    Float_t *array = histogram->GetArray();
@@ -1121,8 +1308,7 @@ TH1F* AliTPCCalibViewer::SigmaCut(TH1F *histogram, Float_t mean, Float_t sigma,
 }   
    
 
-
-TH1F* AliTPCCalibViewer::SigmaCut(Int_t n, Float_t *array, Float_t mean, Float_t sigma, Int_t nbins, Float_t binLow, Float_t binUp, Float_t sigmaMax, Float_t sigmaStep, Bool_t pm){
+TH1F* AliTPCCalibViewer::SigmaCut(Int_t n, const Float_t *array, Float_t mean, Float_t sigma, Int_t nbins, Float_t binLow, Float_t binUp, Float_t sigmaMax, Float_t sigmaStep, Bool_t pm){
    //
    // Creates a histogram Begin_Latex S(t, #mu, #sigma) End_Latex, where you can see, how much of the data are inside sigma-intervals around the mean value
    // The data of the distribution Begin_Latex f(x, #mu, #sigma) End_Latex are given in 'array', 'n' specifies the length of the array
@@ -1195,26 +1381,18 @@ TH1F* AliTPCCalibViewer::SigmaCut(Int_t n, Float_t *array, Float_t mean, Float_t
 }
 
 
-TH1F* AliTPCCalibViewer::SigmaCut(Int_t n, Double_t *array, Double_t mean, Double_t sigma, Int_t nbins, Double_t *xbins, Double_t sigmaMax){
+TH1F* AliTPCCalibViewer::SigmaCut(Int_t /*n*/, const Double_t */*array*/, Double_t /*mean*/, Double_t /*sigma*/, Int_t /*nbins*/, const Double_t */*xbins*/, Double_t /*sigmaMax*/){
    // 
    // SigmaCut for variable binsize
    // NOT YET IMPLEMENTED !!!
    // 
    printf("SigmaCut with variable binsize, Not yet implemented\n");
-   // avoid compiler warnings:
-   n=n;
-   mean=mean;
-   sigma=sigma;
-   nbins=nbins;
-   sigmaMax=sigmaMax;
-   array=array;
-   xbins=xbins;
    
    return 0;
 }   
 
 
-TH1F* AliTPCCalibViewer::Integrate(TH1F *histogram, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep){
+TH1F* AliTPCCalibViewer::Integrate(TH1F *const histogram, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep){
    //
    // Creates an integrated histogram Begin_Latex S(t, #mu, #sigma) End_Latex, out of the input distribution distribution Begin_Latex f(x, #mu, #sigma) End_Latex, given in "histogram"   
    // "mean" and "sigma" are Begin_Latex #mu End_Latex and  Begin_Latex #sigma End_Latex of the distribution in "histogram", to be specified by the user
@@ -1223,20 +1401,20 @@ TH1F* AliTPCCalibViewer::Integrate(TH1F *histogram, Float_t mean, Float_t sigma,
    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
    // The actual work is done on the array.
    /* Begin_Latex 
-         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #frac{#int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx}{ #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx }
+         f(x, #mu, #sigma)     #Rightarrow       S(t, #mu, #sigma) = #int_{-#infty}^{#mu + t #sigma} f(x, #mu, #sigma) dx / #int_{-#infty}^{+#infty} f(x, #mu, #sigma) dx
       End_Latex  
-      begin_macro(source)
+      Begin_Macro(source)
       {
          Float_t mean = 0;
          Float_t sigma = 1.5;
          Float_t sigmaMax = 4;
          gROOT->SetStyle("Plain");
-         TH1F *distribution = new TH1F("Distribution", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
+         TH1F *distribution = new TH1F("Distrib2", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
          TRandom rand(23);
          for (Int_t i = 0; i <50000;i++) distribution->Fill(rand.Gaus(mean, sigma));
          Float_t *ar = distribution->GetArray();
          
-         TCanvas* macro_example_canvas = new TCanvas("macro_example_canvas", "", 350, 350);
+         TCanvas* macro_example_canvas = new TCanvas("cAliTPCCalibViewer2", "", 350, 350);
          macro_example_canvas->Divide(0,2);
          TVirtualPad *pad1 = macro_example_canvas->cd(1);
          pad1->SetGridy();
@@ -1249,9 +1427,8 @@ TH1F* AliTPCCalibViewer::Integrate(TH1F *histogram, Float_t mean, Float_t sigma,
          shist->SetNameTitle("Cumulative","Cumulative S(t, #mu, #sigma)");
          shist->Draw();  
          
-         return macro_example_canvas;
       }  
-      end_macro
+      End_Macro
    */ 
 
    
@@ -1263,7 +1440,7 @@ TH1F* AliTPCCalibViewer::Integrate(TH1F *histogram, Float_t mean, Float_t sigma,
 }   
 
 
-TH1F* AliTPCCalibViewer::Integrate(Int_t n, Float_t *array, Int_t nbins, Float_t binLow, Float_t binUp, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep){
+TH1F* AliTPCCalibViewer::Integrate(Int_t n, const Float_t *const array, Int_t nbins, Float_t binLow, Float_t binUp, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep){
    // Creates an integrated histogram Begin_Latex S(t, #mu, #sigma) End_Latex, out of the input distribution distribution Begin_Latex f(x, #mu, #sigma) End_Latex, given in "histogram"   
    // "mean" and "sigma" are Begin_Latex #mu End_Latex and  Begin_Latex #sigma End_Latex of the distribution in "histogram", to be specified by the user
    // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate 
@@ -1332,7 +1509,7 @@ TH1F* AliTPCCalibViewer::Integrate(Int_t n, Float_t *array, Int_t nbins, Float_t
 
 
 //_____________________________________________________________________________
-AliTPCCalPad* AliTPCCalibViewer::GetCalPad(const char* desiredData, char* cuts, char* calPadName) const {
+AliTPCCalPad* AliTPCCalibViewer::GetCalPadOld(const char* desiredData, const char* cuts, const char* calPadName) const {
   //
   // creates a AliTPCCalPad out of the 'desiredData'
   // the functionality of EasyDraw1D is used
@@ -1340,27 +1517,66 @@ AliTPCCalPad* AliTPCCalibViewer::GetCalPad(const char* desiredData, char* cuts,
   //  - this takes a while -
   //
    TString drawStr(desiredData);
-   drawStr.Append(":channel~");
+   drawStr.Append(":channel");
+   drawStr.Append(fAbbreviation);
    AliTPCCalPad * createdCalPad = new AliTPCCalPad(calPadName, calPadName);
    Int_t entries = 0;
    for (Int_t sec = 0; sec < 72; sec++) {
+     AliTPCCalROC * roc = createdCalPad->GetCalROC(sec);
       entries = EasyDraw1D(drawStr.Data(), (Int_t)sec, cuts, "goff");
       if (entries == -1) return 0;
+      const Double_t *pchannel = fTree->GetV2();
+      const Double_t *pvalue   = fTree->GetV1();
       for (Int_t i = 0; i < entries; i++) 
-         createdCalPad->GetCalROC(sec)->SetValue((UInt_t)(fTree->GetV2()[i]), (Float_t)(fTree->GetV1()[i]));
+         roc->SetValue((UInt_t)(pchannel[i]), (Float_t)(pvalue[i]));
    }
    return createdCalPad;   
 }
 
+
 //_____________________________________________________________________________
-AliTPCCalROC* AliTPCCalibViewer::GetCalROC(const char* desiredData, UInt_t sector, char* cuts) const {
+AliTPCCalPad* AliTPCCalibViewer::GetCalPad(const char* desiredData, const char* cuts, const char* calPadName) const {
+  //
+  // creates a AliTPCCalPad out of the 'desiredData'
+  // the functionality of EasyDraw1D is used
+  // calPadName specifies the name of the created AliTPCCalPad
+  //  - this takes a while -
+  //
+   TString drawStr(desiredData);
+   drawStr.Append(":channel.fElements:sector");
+   AliTPCCalPad * createdCalPad = new AliTPCCalPad(calPadName, calPadName);
+   //
+   Int_t entries = fTree->Draw(drawStr, cuts,"goff");
+   const Double_t *pvalue   = fTree->GetV1();
+   const Double_t *pchannel = fTree->GetV2();
+   const Double_t *psector  = fTree->GetV3();
+
+   for (Int_t ientry=0; ientry<entries; ientry++){
+     Int_t sector= TMath::Nint(psector[ientry]);
+     AliTPCCalROC * roc = createdCalPad->GetCalROC(sector);
+     if (roc) roc->SetValue((UInt_t)(pchannel[ientry]), (Float_t)(pvalue[ientry]));
+   }
+
+  //  for (Int_t sec = 0; sec < 72; sec++) {
+//      AliTPCCalROC * roc = createdCalPad->GetCalROC(sec);
+//       entries = EasyDraw1D(drawStr.Data(), (Int_t)sec, cuts, "goff");
+//       if (entries == -1) return 0;
+//       for (Int_t i = 0; i < entries; i++) 
+//          roc->SetValue((UInt_t)(pchannel[i]), (Float_t)(pvalue[i]));
+//    }
+   return createdCalPad;   
+}
+
+//_____________________________________________________________________________
+AliTPCCalROC* AliTPCCalibViewer::GetCalROC(const char* desiredData, UInt_t sector, const char* cuts) const {
   //
   // creates a AliTPCCalROC out of the desiredData
   // the functionality of EasyDraw1D is used
   // sector specifies the sector of the created AliTPCCalROC
   //
    TString drawStr(desiredData);
-   drawStr.Append(":channel~");
+   drawStr.Append(":channel");
+   drawStr.Append(fAbbreviation);
    Int_t entries = EasyDraw1D(drawStr.Data(), (Int_t)sector, cuts, "goff");
    if (entries == -1) return 0;
    AliTPCCalROC * createdROC = new AliTPCCalROC(sector);
@@ -1377,6 +1593,7 @@ TObjArray* AliTPCCalibViewer::GetListOfVariables(Bool_t printList) {
   //
    TObjArray* arr = new TObjArray();
    TObjString* str = 0;
+   if (!fTree) return 0;
    Int_t nentries = fTree->GetListOfBranches()->GetEntries();
    for (Int_t i = 0; i < nentries; i++) {
       str = new TObjString(fTree->GetListOfBranches()->At(i)->GetName());
@@ -1392,7 +1609,42 @@ TObjArray* AliTPCCalibViewer::GetListOfVariables(Bool_t printList) {
             str->String() == "row" || str->String() == "rpad" || str->String() == "sector"  ))
          arr->Add(str);
    }
+   
+   // loop over all friends (if there are some) and add them to the list
+   if (fTree->GetListOfFriends()) {
+      for (Int_t ifriend = 0; ifriend < fTree->GetListOfFriends()->GetEntries(); ifriend++){
+         // printf("iterating through friendlist, currently at %i\n", ifriend);
+         // printf("working with %s\n", fTree->GetListOfFriends()->At(ifriend)->ClassName());
+         if (TString(fTree->GetListOfFriends()->At(ifriend)->ClassName()) != "TFriendElement") continue; // no friendElement found
+         TFriendElement *friendElement = (TFriendElement*)fTree->GetListOfFriends()->At(ifriend);
+         if (friendElement->GetTree() == 0) continue; // no tree found in friendElement
+         // printf("friend found \n");
+         for (Int_t i = 0; i < friendElement->GetTree()->GetListOfBranches()->GetEntries(); i++) {
+            // printf("iterating through friendelement entries, currently at %i\n", i);
+            str = new TObjString(friendElement->GetTree()->GetListOfBranches()->At(i)->GetName());
+            str->String().ReplaceAll("_Median", "");
+            str->String().ReplaceAll("_Mean", "");
+            str->String().ReplaceAll("_RMS", "");
+            str->String().ReplaceAll("_LTM", "");
+            str->String().ReplaceAll("_OutlierCutted", "");
+            str->String().ReplaceAll(".", "");
+            if (!(str->String() == "channel" || str->String() == "gx" || str->String() == "gy" || 
+                  str->String() == "lx" || str->String() == "ly" || str->String() == "pad" || 
+                  str->String() == "row" || str->String() == "rpad" || str->String() == "sector"  )){
+               // insert "<friendName>." at the beginning: (<friendName> is per default "R")
+               str->String().Insert(0, ".");
+               str->String().Insert(0, friendElement->GetName());
+               if (!arr->FindObject(str)) arr->Add(str);
+               // printf("added string %s \n", str->String().Data());
+            }
+         }
+      }
+   } // if (fTree->GetListOfFriends())
+   
    arr->Sort();
+//   ((TFriendElement*)gui->GetViewer()->GetTree()->GetListOfFriends()->At(0))->GetTree()->GetListOfBranches()->At(0)->GetName()
+// ((TFriendElement*)gui->GetViewer()->GetTree()->GetListOfFriends()->At(0))->GetTree()->GetListOfBranches()
+
 
    if (printList) {
       TIterator* iter = arr->MakeIterator();
@@ -1419,9 +1671,9 @@ TObjArray* AliTPCCalibViewer::GetListOfNormalizationVariables(Bool_t printList)
    arr->Add(new TObjString("_Median_OutlierCutted"));
    arr->Add(new TObjString("_LTM"));
    arr->Add(new TObjString("_LTM_OutlierCutted"));
-   arr->Add(new TObjString("LFitIntern_4_8.fElements"));
-   arr->Add(new TObjString("GFitIntern_Lin.fElements"));
-   arr->Add(new TObjString("GFitIntern_Par.fElements"));
+   arr->Add(new TObjString(Form("LFitIntern_4_8%s", fAppendString.Data())));
+   arr->Add(new TObjString(Form("GFitIntern_Lin%s", fAppendString.Data())));
+   arr->Add(new TObjString(Form("GFitIntern_Par%s", fAppendString.Data())));
    arr->Add(new TObjString("FitLinLocal"));
    arr->Add(new TObjString("FitLinGlobal"));
    arr->Add(new TObjString("FitParLocal"));
@@ -1461,10 +1713,10 @@ TObjArray* AliTPCCalibViewer::GetArrayOfCalPads(){
    TObjArray *calPadsArray = new TObjArray();
    Int_t numberOfCalPads = listOfCalPads->GetEntries();
    for (Int_t i = 0; i < numberOfCalPads; i++) {
-     std::cout << "Creating calPad " << (i+1) << " of " << numberOfCalPads << "\r" << std::flush;
+      std::cout << "Creating calPad " << (i+1) << " of " << numberOfCalPads << "\r" << std::flush;
       char* calPadName = (char*)((TObjString*)(listOfCalPads->At(i)))->GetString().Data();
       TString drawCommand = ((TObjString*)(listOfCalPads->At(i)))->GetString();
-      drawCommand.Append("~");
+      drawCommand.Append(fAbbreviation.Data());
       AliTPCCalPad* calPad = GetCalPad(drawCommand.Data(), "", calPadName); 
       calPadsArray->Add(calPad); 
    }
@@ -1487,12 +1739,12 @@ TString* AliTPCCalibViewer::Fit(const char* drawCommand, const char* formula, co
    TString cutStr(cuts);
    
    // abbreviations:
-   drawStr.ReplaceAll("~",".fElements");
-   cutStr.ReplaceAll("~",".fElements");
-   formulaStr.ReplaceAll("~", ".fElements");
+   drawStr.ReplaceAll(fAbbreviation, fAppendString);
+   cutStr.ReplaceAll(fAbbreviation, fAppendString);
+   formulaStr.ReplaceAll(fAbbreviation, fAppendString);
    
-   formulaStr.ReplaceAll("++", "~");
-   TObjArray* formulaTokens = formulaStr.Tokenize("~"); 
+   formulaStr.ReplaceAll("++", fAbbreviation);
+   TObjArray* formulaTokens = formulaStr.Tokenize(fAbbreviation.Data()); 
    Int_t dim = formulaTokens->GetEntriesFast();
    
    fitParam.ResizeTo(dim);
@@ -1503,7 +1755,10 @@ TString* AliTPCCalibViewer::Fit(const char* drawCommand, const char* formula, co
    fitter->ClearPoints();
    
    Int_t entries = Draw(drawStr.Data(), cutStr.Data(), "goff");
-   if (entries == -1) return new TString("An ERROR has occured during fitting!");
+   if (entries == -1) {
+     delete formulaTokens;
+     return new TString("An ERROR has occured during fitting!");
+   }
    Double_t **values = new Double_t*[dim+1] ; 
    
    for (Int_t i = 0; i < dim + 1; i++){
@@ -1511,7 +1766,11 @@ TString* AliTPCCalibViewer::Fit(const char* drawCommand, const char* formula, co
       if (i < dim) centries = fTree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff");
       else  centries = fTree->Draw(drawStr.Data(), cutStr.Data(), "goff");
       
-      if (entries != centries) return new TString("An ERROR has occured during fitting!");
+      if (entries != centries) {
+        delete [] values;
+       delete formulaTokens;
+        return new TString("An ERROR has occured during fitting!");
+      }
       values[i] = new Double_t[entries];
       memcpy(values[i],  fTree->GetV1(), entries*sizeof(Double_t)); 
    }
@@ -1527,23 +1786,26 @@ TString* AliTPCCalibViewer::Fit(const char* drawCommand, const char* formula, co
    fitter->GetParameters(fitParam);
    fitter->GetCovarianceMatrix(covMatrix);
    chi2 = fitter->GetChisquare();
-   chi2 = chi2;
+//    chi2 = chi2;
    
-   TString *preturnFormula = new TString(Form("( %f+",fitParam[0])), &returnFormula = *preturnFormula; 
+   TString *preturnFormula = new TString(Form("( %e+",fitParam[0])), &returnFormula = *preturnFormula;
    
    for (Int_t iparam = 0; iparam < dim; iparam++) {
-     returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
+     returnFormula.Append(Form("%s*(%e)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
      if (iparam < dim-1) returnFormula.Append("+");
    }
    returnFormula.Append(" )");
    delete formulaTokens;
    delete fitter;
-   delete[] values;
+   for (Int_t i = 0; i < dim + 1; i++){
+     delete [] values[i];
+   }
+   delete [] values;
    return preturnFormula;
 }
 
 
-void AliTPCCalibViewer::MakeTreeWithObjects(const char * fileName, TObjArray * array, const char * mapFileName) {
+void AliTPCCalibViewer::MakeTreeWithObjects(const char *fileName, const TObjArray *const array, const char * mapFileName) {
   //
   // Write tree with all available information
   // im mapFileName is speciefied, the Map information are also written to the tree
@@ -1692,7 +1954,7 @@ void AliTPCCalibViewer::MakeTreeWithObjects(const char * fileName, TObjArray * a
 }
 
 
-void AliTPCCalibViewer::MakeTree(const char * fileName, TObjArray * array, const char * mapFileName, AliTPCCalPad* outlierPad, Float_t ltmFraction) {
+void AliTPCCalibViewer::MakeTree(const char * fileName, TObjArray * array, const char * mapFileName, AliTPCCalPad *const outlierPad, Float_t ltmFraction) {
   //
   // Write a tree with all available information
   // if mapFileName is speciefied, the Map information are also written to the tree
@@ -1705,208 +1967,256 @@ void AliTPCCalibViewer::MakeTree(const char * fileName, TObjArray * array, const
   // 
   // The tree out of this function is the basis for the AliTPCCalibViewer and the AliTPCCalibViewerGUI.
    
-   AliTPCROC* tpcROCinstance = AliTPCROC::Instance();
-
-   TObjArray* mapIROCs = 0;
-   TObjArray* mapOROCs = 0;
-   TVectorF *mapIROCArray = 0;
-   TVectorF *mapOROCArray = 0;
-   Int_t mapEntries = 0;
-   TString* mapNames = 0;
-   
-   if (mapFileName) {
-      TFile mapFile(mapFileName, "read");
-      
-      TList* listOfROCs = mapFile.GetListOfKeys();
-      mapEntries = listOfROCs->GetEntries()/2;
-      mapIROCs = new TObjArray(mapEntries*2);
-      mapOROCs = new TObjArray(mapEntries*2);
-      mapIROCArray = new TVectorF[mapEntries];
-      mapOROCArray = new TVectorF[mapEntries];
-      
-      mapNames = new TString[mapEntries];
-      for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
-         TString rocName(((TKey*)(listOfROCs->At(ivalue*2)))->GetName());
-         rocName.Remove(rocName.Length()-4, 4);
-         mapIROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "IROC").Data()), ivalue);
-         mapOROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "OROC").Data()), ivalue);
-         mapNames[ivalue].Append(rocName);
-      }
-      
-      for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
-         mapIROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(0));
-         mapOROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(36));
+  AliTPCROC* tpcROCinstance = AliTPCROC::Instance();
+
+  TObjArray* mapIROCs = 0;
+  TObjArray* mapOROCs = 0;
+  TVectorF *mapIROCArray = 0;
+  TVectorF *mapOROCArray = 0;
+  Int_t mapEntries = 0;
+  TString* mapNames = 0;
+  
+  if (mapFileName) {
+    TFile mapFile(mapFileName, "read");
+    
+    TList* listOfROCs = mapFile.GetListOfKeys();
+    mapEntries = listOfROCs->GetEntries()/2;
+    mapIROCs = new TObjArray(mapEntries*2);
+    mapOROCs = new TObjArray(mapEntries*2);
+    mapIROCArray = new TVectorF[mapEntries];
+    mapOROCArray = new TVectorF[mapEntries];
+    
+    mapNames = new TString[mapEntries];
+    for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
+      TString rocName(((TKey*)(listOfROCs->At(ivalue*2)))->GetName());
+      rocName.Remove(rocName.Length()-4, 4);
+      mapIROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "IROC").Data()), ivalue);
+      mapOROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "OROC").Data()), ivalue);
+      mapNames[ivalue].Append(rocName);
+    }
+    
+    for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
+      mapIROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(0));
+      mapOROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(36));
       
-         for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(0); ichannel++)
-            (mapIROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapIROCs->At(ivalue)))->GetValue(ichannel);
-         for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(36); ichannel++)
-            (mapOROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapOROCs->At(ivalue)))->GetValue(ichannel);
-      }
-
-   } //  if (mapFileName)
+      for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(0); ichannel++)
+        (mapIROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapIROCs->At(ivalue)))->GetValue(ichannel);
+      for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(36); ichannel++)
+        (mapOROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapOROCs->At(ivalue)))->GetValue(ichannel);
+    }
+    
+  } //  if (mapFileName)
   
-   TTreeSRedirector cstream(fileName);
-   Int_t arrayEntries = 0;
-   if (array) arrayEntries = array->GetEntries();
-   
-   TString* names = new TString[arrayEntries];
-   for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
-      names[ivalue].Append(((AliTPCCalPad*)array->At(ivalue))->GetName());
-
-   for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++) {
+  TTreeSRedirector cstream(fileName,"recreate");
+  Int_t arrayEntries = 0;
+  if (array) arrayEntries = array->GetEntries();
+  
+  TString* names = new TString[arrayEntries];
+  for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
+    names[ivalue].Append(((AliTPCCalPad*)array->At(ivalue))->GetName());
+  
+  for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++) {
       //
       // get statistic for given sector
       //
-      TVectorF median(arrayEntries);
-      TVectorF mean(arrayEntries);
-      TVectorF rms(arrayEntries);
-      TVectorF ltm(arrayEntries);
-      TVectorF ltmrms(arrayEntries);
-      TVectorF medianWithOut(arrayEntries);
-      TVectorF meanWithOut(arrayEntries);
-      TVectorF rmsWithOut(arrayEntries);
-      TVectorF ltmWithOut(arrayEntries);
-      TVectorF ltmrmsWithOut(arrayEntries);
-      
-      TVectorF *vectorArray = new TVectorF[arrayEntries];
-      for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
-         vectorArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
-      
-      for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
-         AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
-         AliTPCCalROC* calROC = calPad->GetCalROC(isector);
-         AliTPCCalROC* outlierROC = 0;
-         if (outlierPad) outlierROC = outlierPad->GetCalROC(isector);
-         if (calROC) {
-            median[ivalue] = calROC->GetMedian();
-            mean[ivalue] = calROC->GetMean();
-            rms[ivalue] = calROC->GetRMS();
-            Double_t ltmrmsValue = 0;
-            ltm[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction);
-            ltmrms[ivalue] = ltmrmsValue;
-            if (outlierROC) {
-               medianWithOut[ivalue] = calROC->GetMedian(outlierROC);
-               meanWithOut[ivalue] = calROC->GetMean(outlierROC);
-               rmsWithOut[ivalue] = calROC->GetRMS(outlierROC);
-               ltmrmsValue = 0;
-               ltmWithOut[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction, outlierROC);
-               ltmrmsWithOut[ivalue] = ltmrmsValue;
-            }
-         }
-         else {
-            median[ivalue] = 0.;
-            mean[ivalue] = 0.;
-            rms[ivalue] = 0.;
-            ltm[ivalue] = 0.;
-            ltmrms[ivalue] = 0.;
-            medianWithOut[ivalue] = 0.;
-            meanWithOut[ivalue] = 0.;
-            rmsWithOut[ivalue] = 0.;
-            ltmWithOut[ivalue] = 0.;
-            ltmrmsWithOut[ivalue] = 0.;
-         }
+    TVectorF median(arrayEntries);
+    TVectorF mean(arrayEntries);
+    TVectorF rms(arrayEntries);
+    TVectorF ltm(arrayEntries);
+    TVectorF ltmrms(arrayEntries);
+    TVectorF medianWithOut(arrayEntries);
+    TVectorF meanWithOut(arrayEntries);
+    TVectorF rmsWithOut(arrayEntries);
+    TVectorF ltmWithOut(arrayEntries);
+    TVectorF ltmrmsWithOut(arrayEntries);
+    
+    TVectorF *vectorArray = new TVectorF[arrayEntries];
+    for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++){
+      vectorArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
+      vectorArray[ivalue].SetUniqueID(array->UncheckedAt(ivalue)->GetUniqueID());
+//       printf("UniqueID: %d\n",vectorArray[ivalue].GetUniqueID());
+    }
+    
+    for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
+      AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
+      AliTPCCalROC* calROC = calPad->GetCalROC(isector);
+      AliTPCCalROC* outlierROC = 0;
+      if (outlierPad) outlierROC = outlierPad->GetCalROC(isector);
+      if (calROC) {
+        median[ivalue] = calROC->GetMedian();
+        mean[ivalue] = calROC->GetMean();
+        rms[ivalue] = calROC->GetRMS();
+        Double_t ltmrmsValue = 0;
+        ltm[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction);
+        ltmrms[ivalue] = ltmrmsValue;
+        if (outlierROC) {
+          medianWithOut[ivalue] = calROC->GetMedian(outlierROC);
+          meanWithOut[ivalue] = calROC->GetMean(outlierROC);
+          rmsWithOut[ivalue] = calROC->GetRMS(outlierROC);
+          ltmrmsValue = 0;
+          ltmWithOut[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction, outlierROC);
+          ltmrmsWithOut[ivalue] = ltmrmsValue;
+        }
       }
-      
+      else {
+        median[ivalue] = 0.;
+        mean[ivalue] = 0.;
+        rms[ivalue] = 0.;
+        ltm[ivalue] = 0.;
+        ltmrms[ivalue] = 0.;
+        medianWithOut[ivalue] = 0.;
+        meanWithOut[ivalue] = 0.;
+        rmsWithOut[ivalue] = 0.;
+        ltmWithOut[ivalue] = 0.;
+        ltmrmsWithOut[ivalue] = 0.;
+      }
+    }
+    
       //
       // fill vectors of variable per pad
       //
-      TVectorF *posArray = new TVectorF[8];
-      for (Int_t ivalue = 0; ivalue < 8; ivalue++)
-         posArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
-
-      Float_t posG[3] = {0};
-      Float_t posL[3] = {0};
-      Int_t ichannel = 0;
-      for (UInt_t irow = 0; irow < tpcROCinstance->GetNRows(isector); irow++) {
-         for (UInt_t ipad = 0; ipad < tpcROCinstance->GetNPads(isector, irow); ipad++) {
-            tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
-            tpcROCinstance->GetPositionGlobal(isector, irow, ipad, posG);
-            posArray[0][ichannel] = irow;
-            posArray[1][ichannel] = ipad;
-            posArray[2][ichannel] = posL[0];
-            posArray[3][ichannel] = posL[1];
-            posArray[4][ichannel] = posG[0];
-            posArray[5][ichannel] = posG[1];
-            posArray[6][ichannel] = (Int_t)(ipad - (Double_t)(tpcROCinstance->GetNPads(isector, irow))/2);
-            posArray[7][ichannel] = ichannel;
-            
+    TVectorF *posArray = new TVectorF[8];
+    for (Int_t ivalue = 0; ivalue < 8; ivalue++)
+      posArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
+    
+    Float_t posG[3] = {0};
+    Float_t posL[3] = {0};
+    Int_t ichannel = 0;
+    for (UInt_t irow = 0; irow < tpcROCinstance->GetNRows(isector); irow++) {
+      for (UInt_t ipad = 0; ipad < tpcROCinstance->GetNPads(isector, irow); ipad++) {
+        tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
+        tpcROCinstance->GetPositionGlobal(isector, irow, ipad, posG);
+        posArray[0][ichannel] = irow;
+        posArray[1][ichannel] = ipad;
+        posArray[2][ichannel] = posL[0];
+        posArray[3][ichannel] = posL[1];
+        posArray[4][ichannel] = posG[0];
+        posArray[5][ichannel] = posG[1];
+        posArray[6][ichannel] = (Int_t)(ipad - (Double_t)(tpcROCinstance->GetNPads(isector, irow))/2);
+        posArray[7][ichannel] = ichannel;
+        
             // loop over array containing AliTPCCalPads
-            for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
-               AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
-               AliTPCCalROC* calROC = calPad->GetCalROC(isector);
-               if (calROC)
+        for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
+          AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
+          AliTPCCalROC* calROC = calPad->GetCalROC(isector);
+                if (calROC)
                   (vectorArray[ivalue])[ichannel] = calROC->GetValue(irow, ipad);
-               else
-                  (vectorArray[ivalue])[ichannel] = 0;
-            }
-            ichannel++;
-         }
+          else
+            (vectorArray[ivalue])[ichannel] = 0;
+        }
+        ichannel++;
       }
-      
-      cstream << "calPads" <<
-         "sector=" << isector;
-      
-      for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
-         cstream << "calPads" <<
-            (Char_t*)((names[ivalue] + "_Median=").Data()) << median[ivalue] <<
-            (Char_t*)((names[ivalue] + "_Mean=").Data()) << mean[ivalue] <<
-            (Char_t*)((names[ivalue] + "_RMS=").Data()) << rms[ivalue] <<
-            (Char_t*)((names[ivalue] + "_LTM=").Data()) << ltm[ivalue] <<
-            (Char_t*)((names[ivalue] + "_RMS_LTM=").Data()) << ltmrms[ivalue];
-         if (outlierPad) {
-            cstream << "calPads" <<
-               (Char_t*)((names[ivalue] + "_Median_OutlierCutted=").Data()) << medianWithOut[ivalue] <<
-               (Char_t*)((names[ivalue] + "_Mean_OutlierCutted=").Data()) << meanWithOut[ivalue] <<
-               (Char_t*)((names[ivalue] + "_RMS_OutlierCutted=").Data()) << rmsWithOut[ivalue] <<
-               (Char_t*)((names[ivalue] + "_LTM_OutlierCutted=").Data()) << ltmWithOut[ivalue] <<
-               (Char_t*)((names[ivalue] + "_RMS_LTM_OutlierCutted=").Data()) << ltmrmsWithOut[ivalue];
-         }
+    }
+    
+    cstream << "calPads" <<
+      "sector=" << isector;
+    
+    for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
+      if (outlierPad==0) {
+       cstream << "calPads" <<
+         (Char_t*)((names[ivalue] + "_Median=").Data()) << median[ivalue] <<
+         (Char_t*)((names[ivalue] + "_Mean=").Data()) << mean[ivalue] <<
+         (Char_t*)((names[ivalue] + "_RMS=").Data()) << rms[ivalue] <<
+         (Char_t*)((names[ivalue] + "_LTM=").Data()) << ltm[ivalue] <<
+         (Char_t*)((names[ivalue] + "_RMS_LTM=").Data()) << ltmrms[ivalue];
       }
-
-      for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
-         cstream << "calPads" <<
-            (Char_t*)((names[ivalue] + ".=").Data()) << &vectorArray[ivalue];
+      if (outlierPad) {
+        cstream << "calPads" <<
+          (Char_t*)((names[ivalue] + "_Median=").Data()) << medianWithOut[ivalue] <<
+          (Char_t*)((names[ivalue] + "_Mean").Data()) << meanWithOut[ivalue] <<
+          (Char_t*)((names[ivalue] + "_RMS=").Data()) << rmsWithOut[ivalue] <<
+          (Char_t*)((names[ivalue] + "_LTM=").Data()) << ltmWithOut[ivalue] <<
+          (Char_t*)((names[ivalue] + "_RMS_LTM=").Data()) << ltmrmsWithOut[ivalue];
       }
-
-      if (mapFileName) {
-         for  (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
+      //timestamp and run, if given in title
+      /*      TString title(((AliTPCCalPad*) array->At(ivalue))->GetTitle());
+             TObjArray *arrtitle=title.Tokenize(",");
+             Int_t run=-1;
+             UInt_t time=0;
+             TIter next(arrtitle);
+             TObject *o=0;
+             while ( (o=next()) ){
+             TString &entry=((TObjString*)o)->GetString();
+             entry.Remove(TString::kBoth,' ');
+             entry.Remove(TString::kBoth,'\t');
+             if (entry.BeginsWith("Run:")) {
+             run=entry(4,entry.Length()).Atoi();
+             } else if (entry.BeginsWith("Time:")) {
+             time=entry(6,entry.Length()).Atoi();
+             }
+             }
+             delete arrtitle;*/
+      
+    }
+    
+    for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
+      cstream << "calPads" <<
+        (Char_t*)((names[ivalue] + ".=").Data()) << &vectorArray[ivalue];
+    }
+    
+    if (mapFileName) {
+          for  (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
             if (isector < 36)
-               cstream << "calPads" <<
-                  (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapIROCArray[ivalue];
+              cstream << "calPads" <<
+              (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapIROCArray[ivalue];
             else
-               cstream << "calPads" <<
-                  (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapOROCArray[ivalue];
+                cstream << "calPads" <<
+              (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapOROCArray[ivalue];
          }
-      }
-
-      cstream << "calPads" <<
-         "row.=" << &posArray[0] <<
-         "pad.=" << &posArray[1] <<
-         "lx.=" << &posArray[2] <<
-         "ly.=" << &posArray[3] <<
-         "gx.=" << &posArray[4] <<
-         "gy.=" << &posArray[5] <<
-         "rpad.=" << &posArray[6] <<
-         "channel.=" << &posArray[7];
-         
-      cstream << "calPads" <<
-         "\n";
-
-      delete[] posArray;
-      delete[] vectorArray;
-   }
-   
-
-   delete[] names;
-   if (mapFileName) {
-      delete mapIROCs;
-      delete mapOROCs;
-      delete[] mapIROCArray;
-      delete[] mapOROCArray;
-      delete[] mapNames;
-   }
+    }
+
+    cstream << "calPads" <<
+      "row.=" << &posArray[0] <<
+      "pad.=" << &posArray[1] <<
+      "lx.=" << &posArray[2] <<
+      "ly.=" << &posArray[3] <<
+      "gx.=" << &posArray[4] <<
+      "gy.=" << &posArray[5] <<
+      "rpad.=" << &posArray[6] <<
+      "channel.=" << &posArray[7];
+    
+    cstream << "calPads" <<
+      "\n";
+    
+    delete[] posArray;
+    delete[] vectorArray;
+  }
+  TTree * tree = (cstream << "calPads").GetTree();
+  MakeCalPadAliases(tree);
+  for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
+    //(Char_t*)((names[ivalue] + "_Median=").Data()) << median[ivalue] <<
+    tree->SetAlias(TString::Format("%s_LTMRatio",names[ivalue].Data()).Data(), TString::Format("%s.fElements/%s_LTM",names[ivalue].Data(),names[ivalue].Data()).Data());
+    tree->SetAlias(TString::Format("%s_MeanRatio",names[ivalue].Data()).Data(), TString::Format("%s.fElements/%s_Mean",names[ivalue].Data(),names[ivalue].Data()).Data());
+    tree->SetAlias(TString::Format("%s_MedianRatio",names[ivalue].Data()).Data(), TString::Format("%s.fElements/%s_Median",names[ivalue].Data(),names[ivalue].Data()).Data());
+    tree->SetAlias(names[ivalue].Data(), TString::Format("%s.fElements",names[ivalue].Data()).Data());
+  }
+  delete[] names;
+  if (mapFileName) {
+    delete mapIROCs;
+    delete mapOROCs;
+    delete[] mapIROCArray;
+    delete[] mapOROCArray;
+    delete[] mapNames;
+  }
+}
+void AliTPCCalibViewer::MakeCalPadAliases(TTree * tree){
+  //
+  // make standard aliases for standard calibration trees
+  //
+  tree->SetAlias("Pad0","MapPadLength.fElements==7.5");   // pad types
+  tree->SetAlias("Pad1","MapPadLength.fElements==10.0");
+  tree->SetAlias("Pad2","MapPadLength.fElements==15.0");
+  tree->SetAlias("ly","(ly.fElements+((sector<36)*(2*((sector%36)<18)-1)*0.2)+((sector>=36)*(2*((sector%36)<18)-1)*0.3))"); // correction for bug in tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
+  tree->SetAlias("dRowNorm0","(row.fElements/32-1)");      // normalized distance to the center of the chamber in lx (-1,1)
+  tree->SetAlias("dRowNorm1","(row.fElements/32-1)");      // 
+  tree->SetAlias("dRowNorm2","((row.fElements-63)/16-1)"); //
+  tree->SetAlias("dRowNorm","(row.fElements<63)*(row.fElements/32-1)+(row.fElements>63)*((row.fElements-63)/16-1)");
+  tree->SetAlias("dPhiNorm","(ly/lx.fElements)/(pi/18)"); // normalized distance to the center of the chamber in phi (-1,1)
+  //
+  tree->SetAlias("fsector","(sector%36)+(0.5+9*atan2(ly,lx.fElements)/pi)"); // float sector number
+  tree->SetAlias("posEdge","((pi/18.)-abs(atan(ly/lx.fElements)))*lx.fElements"); //distance to the edge
+  tree->SetAlias("lphi","atan(ly/lx.fElements)"); //local phi angle
 }
-
 
 void AliTPCCalibViewer::MakeTree(const char *outPutFileName, const Char_t *inputFileName, AliTPCCalPad *outlierPad, Float_t ltmFraction, const char *mapFileName ){
    // 
@@ -1935,6 +2245,7 @@ void AliTPCCalibViewer::MakeTree(const char *outPutFileName, const Char_t *input
    MakeTree(outPutFileName, &objArray, mapFileName, outlierPad, ltmFraction);   
 }
 
+
 void AliTPCCalibViewer::CreateObjectList(const Char_t *filename, TObjArray *calibObjects){
    // 
    // Function to create a TObjArray out of a given file
@@ -2082,6 +2393,7 @@ void AliTPCCalibViewer::CreateObjectList(const Char_t *filename, TObjArray *cali
       }
       delete fIn;
    }
+   delete arrFileLine;
 }