]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCCalibViewer.cxx
Adding switch to operate with smaller memory.
[u/mrichter/AliRoot.git] / TPC / AliTPCCalibViewer.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16
17 ///////////////////////////////////////////////////////////////////////////////
18 //                                                                           //
19 //  Class for viewing/visualizing TPC calibration data                       //
20 //  base on  TTree functionality for visualization                           //
21 //                                                                           //
22 //  Create a list of AliTPCCalPads, arrange them in an TObjArray.            //
23 //  Pass this TObjArray to MakeTree and create the calibration Tree          //
24 //  While craating this tree some statistical information are calculated     //
25 //  Open the viewer with this Tree: AliTPCCalibViewer v("CalibTree.root")    //
26 //  Have fun!                                                                //
27 //  EasyDraw("CETmean~-CETmean_mean", "A", "(CETmean~-CETmean_mean)>0")      //
28 //                                                                           //
29 //  If you like to click, we recommand you the                               //
30 //    AliTPCCalibViewerGUI                                                   //
31 //                                                                           //
32 //    THE DOCUMENTATION IS STILL NOT COMPLETED !!!!                          //
33 //                                                                           //
34 ///////////////////////////////////////////////////////////////////////////////
35
36 //
37 // ROOT includes 
38 //
39
40 #include <fstream>
41 #include <iostream>
42
43 #include <TFile.h>
44 #include <TFriendElement.h>
45 #include <TGraph.h>
46 #include <TKey.h>
47 #include <TPad.h>
48 //#include <TCanvas.h>
49 #include <TH1.h> 
50 #include <TH1F.h>
51 #include <TLegend.h>
52 #include <TLine.h>
53 #include <TMath.h>
54 #include <TObjString.h>
55 //#include <TROOT.h>
56 #include <TRandom.h>
57 #include <TString.h>
58 #include <TStyle.h>
59 #include <TTreeStream.h>
60
61 #include "AliTPCCalibCE.h"
62 #include "AliMathBase.h"
63 #include "AliTPCCalPad.h"
64 #include "AliTPCCalROC.h"
65 #include "AliTPCCalibPedestal.h"
66 #include "AliTPCCalibPulser.h"
67
68 //
69 // AliRoot includes
70 //
71 #include "AliTPCCalibViewer.h"
72
73 ClassImp(AliTPCCalibViewer)
74
75
76 AliTPCCalibViewer::AliTPCCalibViewer()
77                   :TObject(),
78                    fTree(0),
79                    fFile(0),
80                    fListOfObjectsToBeDeleted(0),
81                    fTreeMustBeDeleted(0), 
82                    fAbbreviation(0), 
83                    fAppendString(0)
84 {
85   //
86   // Default constructor
87   //
88
89 }
90
91 //_____________________________________________________________________________
92 AliTPCCalibViewer::AliTPCCalibViewer(const AliTPCCalibViewer &c)
93                   :TObject(c),
94                    fTree(0),
95                    fFile(0),
96                    fListOfObjectsToBeDeleted(0),
97                    fTreeMustBeDeleted(0),
98                    fAbbreviation(0), 
99                    fAppendString(0)
100 {
101   //
102   // dummy AliTPCCalibViewer copy constructor
103   // not yet working!!!
104   //
105   fTree = c.fTree;
106   fTreeMustBeDeleted = c.fTreeMustBeDeleted;
107   //fFile = new TFile(*(c.fFile));
108   fListOfObjectsToBeDeleted = c.fListOfObjectsToBeDeleted;
109   fAbbreviation = c.fAbbreviation;
110   fAppendString = c.fAppendString;
111 }
112
113 //_____________________________________________________________________________
114 AliTPCCalibViewer::AliTPCCalibViewer(TTree *const tree)
115                   :TObject(),
116                    fTree(0),
117                    fFile(0),
118                    fListOfObjectsToBeDeleted(0),
119                    fTreeMustBeDeleted(0),
120                    fAbbreviation(0), 
121                    fAppendString(0)
122 {
123   //
124   // Constructor that initializes the calibration viewer
125   //
126   fTree = tree;
127   fTreeMustBeDeleted = kFALSE;
128   fListOfObjectsToBeDeleted = new TObjArray();
129   fAbbreviation = "~";
130   fAppendString = ".fElements";
131 }
132
133 //_____________________________________________________________________________
134 AliTPCCalibViewer::AliTPCCalibViewer(const char* fileName, const char* treeName)
135                   :TObject(),
136                    fTree(0),
137                    fFile(0),
138                    fListOfObjectsToBeDeleted(0),
139                    fTreeMustBeDeleted(0),
140                    fAbbreviation(0), 
141                    fAppendString(0)
142                    
143 {
144    //
145    // Constructor to initialize the calibration viewer
146    // the file 'fileName' contains the tree 'treeName'
147    //
148    fFile = new TFile(fileName, "read");
149    fTree = (TTree*) fFile->Get(treeName);
150    fTreeMustBeDeleted = kTRUE;
151    fListOfObjectsToBeDeleted = new TObjArray();
152    fAbbreviation = "~";
153    fAppendString = ".fElements";
154 }
155                    
156 //____________________________________________________________________________
157 AliTPCCalibViewer & AliTPCCalibViewer::operator =(const AliTPCCalibViewer & param)
158 {
159    //
160    // assignment operator - dummy
161    // not yet working!!!
162    //
163    fTree = param.fTree;
164    fTreeMustBeDeleted = param.fTreeMustBeDeleted;
165    //fFile = new TFile(*(param.fFile));
166    fListOfObjectsToBeDeleted = param.fListOfObjectsToBeDeleted;
167    fAbbreviation = param.fAbbreviation;
168    fAppendString = param.fAppendString;
169    return (*this);
170 }
171
172 //_____________________________________________________________________________
173 AliTPCCalibViewer::~AliTPCCalibViewer()
174 {
175    //
176    // AliTPCCalibViewer destructor
177    // all objects will be deleted, the file will be closed, the pictures will disappear
178    //
179    if (fTree && fTreeMustBeDeleted) {
180       fTree->SetCacheSize(0);
181       fTree->Delete();
182       //delete fTree;
183    }
184    if (fFile) {
185       fFile->Close();
186       fFile = 0;
187    }
188
189    for (Int_t i = fListOfObjectsToBeDeleted->GetEntriesFast()-1; i >= 0; i--) {
190       //cout << "Index " << i << " trying to delete the following object: " << fListOfObjectsToBeDeleted->At(i)->GetName() << "..."<< endl;
191       delete fListOfObjectsToBeDeleted->At(i);
192    }
193    delete fListOfObjectsToBeDeleted;
194 }
195
196 //_____________________________________________________________________________
197 void AliTPCCalibViewer::Delete(Option_t* option) {
198    //
199    // Should be called from AliTPCCalibViewerGUI class only.
200    // If you use Delete() do not call the destructor.
201    // All objects (except those contained in fListOfObjectsToBeDeleted) will be deleted, the file will be closed.
202    //
203    
204    option = option;  // to avoid warnings on compiling   
205    if (fTree && fTreeMustBeDeleted) {
206       fTree->SetCacheSize(0);
207       fTree->Delete();
208    }
209    if (fFile)
210       delete fFile;
211    delete fListOfObjectsToBeDeleted;
212 }
213
214
215 const char* AliTPCCalibViewer::AddAbbreviations(const Char_t *c, Bool_t printDrawCommand){ 
216    // Replace all "<variable>" with "<variable><fAbbreviation>" (Adds forgotten "~")
217    // but take care on the statistical information, like "CEQmean_Mean"
218    // and also take care on correct given variables, like "CEQmean~"
219    // 
220    // For each variable out of "listOfVariables":
221    // - 'Save' correct items:
222    //   - form <replaceString>, take <variable>'s first char, add <removeString>, add rest of <variable>, e.g. "C!#EQmean" (<removeString> = "!#")
223    //   - For each statistical information in "listOfNormalizationVariables":
224    //     - ReplaceAll <variable><statistical_Information> with <replaceString><statistical_Information>
225    //   - ReplaceAll <variable><abbreviation> with <replaceString><abbreviation>, e.g. "CEQmean~" -> "C!#EQmean~"
226    //   - ReplaceAll <variable><appendStr> with <replaceString><appendStr>, e.g. "CEQmean.fElements" -> "C!#EQmean.fElements"
227    //
228    // - Do actual replacing:
229    //   - ReplaceAll <variable> with <variable><fAbbreviation>, e.g. "CEQmean" -> "CEQmean~"
230    //
231    // - Undo saving:
232    //   - For each statistical information in "listOfNormalizationVariables":
233    //     - ReplaceAll <replaceString><statistical_Information> with <variable><statistical_Information> 
234    //   - ReplaceAll <replaceString><abbreviation> with <variable><abbreviation>, e.g. "C!#EQmean~" -> "CEQmean~"
235    //   - ReplaceAll <replaceString><appendStr> with <variable><appendStr>, e.g. "C!#EQmean.fElements" -> "CEQmean.fElements"
236    // 
237    // Now all the missing "~" should be added.
238    
239    TString str(c);
240    TString removeString = "!#";  // very unpropable combination of chars
241    TString replaceString = "";
242    TString searchString = "";
243    TString normString = "";
244    TObjArray *listOfVariables = GetListOfVariables();
245    listOfVariables->Add(new TObjString("channel"));
246    listOfVariables->Add(new TObjString("gx"));
247    listOfVariables->Add(new TObjString("gy"));
248    listOfVariables->Add(new TObjString("lx"));
249    listOfVariables->Add(new TObjString("ly"));
250    listOfVariables->Add(new TObjString("pad"));
251    listOfVariables->Add(new TObjString("row"));
252    listOfVariables->Add(new TObjString("rpad"));
253    listOfVariables->Add(new TObjString("sector"));
254    TObjArray *listOfNormalizationVariables = GetListOfNormalizationVariables();
255    Int_t nVariables = listOfVariables->GetEntriesFast();
256    Int_t nNorm = listOfNormalizationVariables->GetEntriesFast();
257    
258    Int_t *varLengths = new Int_t[nVariables];
259    for (Int_t i = 0; i < nVariables; i++) {
260       varLengths[i] = ((TObjString*)listOfVariables->At(i))->String().Length();
261    }
262    Int_t *normLengths = new Int_t[nNorm];
263    for (Int_t i = 0; i < nNorm; i++) {
264       normLengths[i] = ((TObjString*)listOfNormalizationVariables->At(i))->String().Length();
265       // printf("normLengths[%i] (%s) = %i \n", i,((TObjString*)listOfNormalizationVariables->At(i))->String().Data(), normLengths[i]);
266    }
267    Int_t *varSort = new Int_t[nVariables];
268    TMath::Sort(nVariables, varLengths, varSort, kTRUE);
269    Int_t *normSort = new Int_t[nNorm];
270    TMath::Sort(nNorm, normLengths, normSort, kTRUE);
271    // for (Int_t i = 0; i<nNorm; i++)  printf("normLengths: %i\n", normLengths[normSort[i]]);
272    // for (Int_t i = 0; i<nVariables; i++) printf("varLengths: %i\n", varLengths[varSort[i]]);
273    
274    for (Int_t ivar = 0; ivar < nVariables; ivar++) {
275       // ***** save correct tokens *****
276       // first get the next variable:
277       searchString = ((TObjString*)listOfVariables->At(varSort[ivar]))->String();
278       // printf("searchString: %s ++++++++++++++\n", searchString.Data());
279       // form replaceString:
280       replaceString = "";
281       for (Int_t i = 0; i < searchString.Length(); i++) {
282          replaceString.Append(searchString[i]);
283          if (i == 0) replaceString.Append(removeString);
284       }
285       // go through normalization:
286       // printf("go through normalization\n");
287       for (Int_t inorm = 0; inorm < nNorm; inorm++) {
288          // printf(" inorm=%i, nNorm=%i, normSort[inorm]=%i \n", inorm, nNorm, normSort[inorm]);
289          normString = ((TObjString*)listOfNormalizationVariables->At(normSort[inorm]))->String();
290          // printf(" walking in normalization, i=%i, normString=%s \n", inorm, normString.Data());
291          str.ReplaceAll(searchString + normString, replaceString + normString);
292          // like: str.ReplaceAll("CEQmean_Mean", "C!EQmean_Mean");
293       }
294       str.ReplaceAll(searchString + fAbbreviation, replaceString + fAbbreviation);
295       // like: str.ReplaceAll("CEQmean~", "C!EQmean~");
296       str.ReplaceAll(searchString + fAppendString,    replaceString + fAppendString);
297       // like: str.ReplaceAll("CEQmean.fElements", "C!EQmean.fElements");
298       
299       // ***** add missing extensions *****
300       str.ReplaceAll(searchString, replaceString + fAbbreviation);
301       // like: str.ReplaceAll("CEQmean", "C!EQmean~");
302    }
303    
304    // ***** undo saving *****
305    str.ReplaceAll(removeString, "");
306   
307    if (printDrawCommand) std::cout << "The string looks now like: " << str.Data() << std::endl;
308    delete [] varLengths;
309    delete [] normLengths;
310    delete [] varSort;
311    delete [] normSort;
312    return str.Data();
313 }
314
315
316
317
318 //_____________________________________________________________________________
319 Int_t AliTPCCalibViewer::EasyDraw(const char* drawCommand, const char* sector, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
320   //
321   // easy drawing of data, use '~' for abbreviation of '.fElements'
322   // example: EasyDraw("CETmean~-CETmean_mean", "A", "(CETmean~-CETmean_mean)>0")
323  // sector: sector-number - only the specified sector will be drwawn
324   //         'A'/'C' or 'a'/'c' - side A/C will be drawn
325   //         'ALL' - whole TPC will be drawn, projected on one side
326   // cuts: specifies cuts
327   // drawOptions: draw options like 'same'
328   // writeDrawCommand: write the command, that is passed to TTree::Draw
329   //
330
331    TString drawStr(drawCommand);
332    TString sectorStr(sector);
333    sectorStr.ToUpper();
334    TString cutStr("");
335    //TString drawOptionsStr("profcolz ");
336    Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
337    if (dangerousToDraw) {
338       Warning("EasyDraw", "The draw string must not contain ':' or '>>'. Using only first variable for drawing!");
339 //      return -1;
340 //      drawStr.Resize(drawStr.First(">"));
341       drawStr.Resize(drawStr.First(":"));
342    }
343
344    TString drawOptionsStr("");
345    TRandom rnd(0);
346    Int_t rndNumber = rnd.Integer(10000);
347
348    if (drawOptions && strcmp(drawOptions, "") != 0)
349       drawOptionsStr += drawOptions;
350    else
351       drawOptionsStr += "profcolz";
352
353    if (sectorStr == "A") {
354       drawStr += Form(":gy%s:gx%s>>prof", fAppendString.Data(), fAppendString.Data());
355       drawStr += rndNumber;
356       drawStr += "(330,-250,250,330,-250,250)";
357       cutStr += "(sector/18)%2==0 ";
358    }
359    else if  (sectorStr == "C") {
360       drawStr += Form(":gy%s:gx%s>>prof", fAppendString.Data(), fAppendString.Data());
361       drawStr += rndNumber;
362       drawStr += "(330,-250,250,330,-250,250)";
363       cutStr += "(sector/18)%2==1 ";
364    }
365    else if  (sectorStr == "ALL") {
366       drawStr += Form(":gy%s:gx%s>>prof", fAppendString.Data(), fAppendString.Data());
367       drawStr += rndNumber;
368       drawStr += "(330,-250,250,330,-250,250)";
369    }
370    else if  (sectorStr.Contains("S")) {
371       drawStr += Form(":rpad%s:row%s+(sector>35)*63>>prof", fAppendString.Data(), fAppendString.Data());
372       drawStr += rndNumber;
373       drawStr += "(159,0,159,140,-70,70)";
374       TString sec=sectorStr;
375       sec.Remove(0,1);
376       cutStr += "sector%36=="+sec+" ";
377    }
378    else if (sectorStr.IsDigit()) {
379       Int_t isec = sectorStr.Atoi();
380       drawStr += Form(":rpad%s:row%s>>prof", fAppendString.Data(), fAppendString.Data());
381       drawStr += rndNumber;
382       if (isec < 36 && isec >= 0)
383          drawStr += "(63,0,63,108,-54,54)";
384       else if (isec < 72 && isec >= 36)
385          drawStr += "(96,0,96,140,-70,70)";
386       else {
387          Error("EasyDraw","The TPC contains only sectors between 0 and 71.");
388          return -1;
389       }
390       cutStr += "(sector==";
391       cutStr += isec;
392       cutStr += ") ";
393    }
394
395    if (cuts && cuts[0] != 0) {
396       if (cutStr.Length() != 0) cutStr += "&& ";
397       cutStr += "(";
398       cutStr += cuts;
399       cutStr += ")";
400    }
401    drawStr.ReplaceAll(fAbbreviation, fAppendString);
402    cutStr.ReplaceAll(fAbbreviation, fAppendString);
403    if (writeDrawCommand) std::cout << "fTree->Draw(\"" << drawStr << "\", \"" <<  cutStr << "\", \"" << drawOptionsStr << "\");" << std::endl;
404    Int_t returnValue = fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
405    TString profName("prof");
406    profName += rndNumber;
407    TObject *obj = gDirectory->Get(profName.Data());
408    if (obj && obj->InheritsFrom("TH1")) FormatHistoLabels((TH1*)obj);
409    return returnValue;
410 }
411
412
413 Int_t AliTPCCalibViewer::EasyDraw(const char* drawCommand, Int_t sector, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
414   //
415   // easy drawing of data, use '~' for abbreviation of '.fElements'
416   // example: EasyDraw("CETmean~-CETmean_mean", 34, "(CETmean~-CETmean_mean)>0")
417   // sector: sector-number - only the specified sector will be drwawn
418   // cuts: specifies cuts
419   // drawOptions: draw options like 'same'
420   // writeDrawCommand: write the command, that is passed to TTree::Draw
421   //
422    if (sector >= 0 && sector < 72) {
423       char sectorChr[3];
424       sprintf(sectorChr, "%i", sector);
425       return EasyDraw(drawCommand, sectorChr, cuts, drawOptions, writeDrawCommand);
426    }
427    Error("EasyDraw","The TPC contains only sectors between 0 and 71.");
428    return -1;
429 }
430
431
432 //_____________________________________________________________________________
433 Int_t AliTPCCalibViewer::EasyDraw1D(const char* drawCommand, const char* sector, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
434   //
435   // easy drawing of data, use '~' for abbreviation of '.fElements'
436   // example: EasyDraw("CETmean~-CETmean_mean", "A", "(CETmean~-CETmean_mean)>0")
437   // sector: sector-number - the specified sector will be drwawn
438   //         'A'/'C' or 'a'/'c' - side A/C will be drawn
439   //         'ALL' - whole TPC will be drawn, projected on one side
440   // cuts: specifies cuts
441   // drawOptions: draw options like 'same'
442   // writeDrawCommand: write the command, that is passed to TTree::Draw
443   //
444
445    TString drawStr(drawCommand);
446    TString sectorStr(sector);
447    TString drawOptionsStr(drawOptions);
448    sectorStr.ToUpper();
449    TString cutStr("");
450
451    if (sectorStr == "A")
452       cutStr += "(sector/18)%2==0 ";
453    else if  (sectorStr == "C")
454       cutStr += "(sector/18)%2==1 ";
455    else if (sectorStr.IsDigit()) {
456       Int_t isec = sectorStr.Atoi();
457       if (isec < 0 || isec > 71) {
458          Error("EasyDraw","The TPC contains only sectors between 0 and 71.");
459          return -1;
460       }
461       cutStr += "(sector==";
462       cutStr += isec;
463       cutStr += ") ";
464    }
465    else if  (sectorStr.Contains("S")) {
466       TString sec=sectorStr;
467       sec.Remove(0,1);
468       cutStr += "sector%36=="+sec+" ";
469    }
470
471    if (cuts && cuts[0] != 0) {
472       if (cutStr.Length() != 0) cutStr += "&& ";
473       cutStr += "(";
474       cutStr += cuts;
475       cutStr += ")";
476    }
477
478    drawStr.ReplaceAll(fAbbreviation, fAppendString);
479    cutStr.ReplaceAll(fAbbreviation, fAppendString);
480    if (writeDrawCommand) std::cout << "fTree->Draw(\"" << drawStr << "\", \"" <<  cutStr << "\", \"" << drawOptionsStr << "\");" << std::endl;
481    Int_t returnValue = fTree->Draw(drawStr.Data(), cutStr.Data(), drawOptionsStr.Data());
482    if (returnValue == -1) return -1;
483    
484    TObject *obj = (gPad) ? gPad->GetPrimitive("htemp") : 0; 
485    if (!obj) obj = (TH1F*)gDirectory->Get("htemp");
486    if (!obj) obj = gPad->GetPrimitive("tempHist");
487    if (!obj) obj = (TH1F*)gDirectory->Get("tempHist");
488    if (!obj) obj = gPad->GetPrimitive("Graph");
489    if (!obj) obj = (TH1F*)gDirectory->Get("Graph");
490    if (obj && obj->InheritsFrom("TH1")) FormatHistoLabels((TH1*)obj);
491    return returnValue;
492 }
493
494
495 Int_t AliTPCCalibViewer::EasyDraw1D(const char* drawCommand, Int_t sector, const char* cuts, const char* drawOptions, Bool_t writeDrawCommand) const {
496   //
497   // easy drawing of data, use '~' for abbreviation of '.fElements'
498   // example: EasyDraw("CETmean~-CETmean_mean", 34, "(CETmean~-CETmean_mean)>0")
499   // sector: sector-number - the specified sector will be drwawn
500   // cuts: specifies cuts
501   // drawOptions: draw options like 'same'
502   // writeDrawCommand: write the command, that is passed to TTree::Draw
503   //
504
505    if (sector >= 0 && sector < 72) {
506       char sectorChr[3];
507       sprintf(sectorChr, "%i", sector);
508       return EasyDraw1D(drawCommand, sectorChr, cuts, drawOptions, writeDrawCommand);
509    }
510   Error("EasyDraw","The TPC contains only sectors between 0 and 71.");
511   return -1;
512 }
513
514
515 void AliTPCCalibViewer::FormatHistoLabels(TH1 *histo) const {
516    // 
517    // formats title and axis labels of histo 
518    // removes '.fElements'
519    // 
520    if (!histo) return;
521    TString replaceString(fAppendString.Data());
522    TString *str = new TString(histo->GetTitle());
523    str->ReplaceAll(replaceString, "");
524    histo->SetTitle(str->Data());
525    delete str;
526    if (histo->GetXaxis()) {
527       str = new TString(histo->GetXaxis()->GetTitle());
528       str->ReplaceAll(replaceString, "");
529       histo->GetXaxis()->SetTitle(str->Data());
530       delete str;
531    }
532    if (histo->GetYaxis()) {
533       str = new TString(histo->GetYaxis()->GetTitle());
534       str->ReplaceAll(replaceString, "");
535       histo->GetYaxis()->SetTitle(str->Data());
536       delete str;
537    }
538    if (histo->GetZaxis()) {
539       str = new TString(histo->GetZaxis()->GetTitle());
540       str->ReplaceAll(replaceString, "");
541       histo->GetZaxis()->SetTitle(str->Data());
542       delete str;
543    }
544 }
545
546
547 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 {
548    // 
549    // Easy drawing of data, in principle the same as EasyDraw1D
550    // Difference: A line for the mean / median / LTM is drawn 
551    // in 'sigmas' you can specify in which distance to the mean/median/LTM you want to see a line in sigma-units, separated by ';'
552    // example: sigmas = "2; 4; 6;"  at Begin_Latex 2 #sigma End_Latex, Begin_Latex 4 #sigma End_Latex and Begin_Latex 6 #sigma End_Latex  a line is drawn.
553    // "plotMean", "plotMedian" and "plotLTM": what kind of lines do you want to see?
554    // 
555    if (sector >= 0 && sector < 72) {
556       char sectorChr[3];
557       sprintf(sectorChr, "%i", sector);
558       return DrawHisto1D(drawCommand, sectorChr, cuts, sigmas, plotMean, plotMedian, plotLTM);
559    }
560    Error("DrawHisto1D","The TPC contains only sectors between 0 and 71.");
561    return -1;
562 }   
563
564
565 Int_t  AliTPCCalibViewer::DrawHisto1D(const char* drawCommand, const char* sector, const char* cuts, const char *sigmas, Bool_t plotMean, Bool_t plotMedian, Bool_t plotLTM) const {
566    // 
567    // Easy drawing of data, in principle the same as EasyDraw1D
568    // Difference: A line for the mean / median / LTM is drawn 
569    // in 'sigmas' you can specify in which distance to the mean/median/LTM you want to see a line in sigma-units, separated by ';'
570    // example: sigmas = "2; 4; 6;"  at Begin_Latex 2 #sigma End_Latex, Begin_Latex 4 #sigma End_Latex and Begin_Latex 6 #sigma End_Latex  a line is drawn.
571    // "plotMean", "plotMedian" and "plotLTM": what kind of lines do you want to see?
572    // 
573    Int_t oldOptStat = gStyle->GetOptStat();
574    gStyle->SetOptStat(0000000);
575    Double_t ltmFraction = 0.8;
576    
577    TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");  
578    TVectorF nsigma(sigmasTokens->GetEntriesFast());
579    for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
580       TString str(((TObjString*)sigmasTokens->At(i))->GetString());
581       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
582       nsigma[i] = sig;
583    }
584    
585    TString drawStr(drawCommand);
586    Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
587    if (dangerousToDraw) {
588       Warning("DrawHisto1D", "The draw string must not contain ':' or '>>'.");
589       return -1;
590    }
591    drawStr += " >> tempHist";
592    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts);
593    TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
594    // FIXME is this histogram deleted automatically?
595    Double_t *values = fTree->GetV1();  // value is the array containing 'entries' numbers
596    
597    Double_t mean = TMath::Mean(entries, values);
598    Double_t median = TMath::Median(entries, values);
599    Double_t sigma = TMath::RMS(entries, values);
600    Double_t maxY = htemp->GetMaximum();
601    
602    char c[500];
603    TLegend * legend = new TLegend(.7,.7, .99, .99, "Statistical information");
604 //    sprintf(c, "%s, sector: %i", type, sector);
605    //fListOfObjectsToBeDeleted->Add(legend);
606
607    if (plotMean) {
608       // draw Mean
609       TLine* line = new TLine(mean, 0, mean, maxY);
610       //fListOfObjectsToBeDeleted->Add(line);
611       line->SetLineColor(kRed);
612       line->SetLineWidth(2);
613       line->SetLineStyle(1);
614       line->Draw();
615       sprintf(c, "Mean: %f", mean);
616       legend->AddEntry(line, c, "l");
617       // draw sigma lines
618       for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
619          TLine* linePlusSigma = new TLine(mean + nsigma[i] * sigma, 0, mean + nsigma[i] * sigma, maxY);
620          //fListOfObjectsToBeDeleted->Add(linePlusSigma);
621          linePlusSigma->SetLineColor(kRed);
622          linePlusSigma->SetLineStyle(2 + i);
623          linePlusSigma->Draw();
624          TLine* lineMinusSigma = new TLine(mean - nsigma[i] * sigma, 0, mean - nsigma[i] * sigma, maxY);
625          //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
626          lineMinusSigma->SetLineColor(kRed);
627          lineMinusSigma->SetLineStyle(2 + i);
628          lineMinusSigma->Draw();
629          sprintf(c, "%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma));
630          legend->AddEntry(lineMinusSigma, c, "l");
631       }
632    }
633    if (plotMedian) {
634       // draw median
635       TLine* line = new TLine(median, 0, median, maxY);
636       //fListOfObjectsToBeDeleted->Add(line);
637       line->SetLineColor(kBlue);
638       line->SetLineWidth(2);
639       line->SetLineStyle(1);
640       line->Draw();
641       sprintf(c, "Median: %f", median);
642       legend->AddEntry(line, c, "l");
643       // draw sigma lines
644       for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
645          TLine* linePlusSigma = new TLine(median + nsigma[i] * sigma, 0, median + nsigma[i]*sigma, maxY);
646          //fListOfObjectsToBeDeleted->Add(linePlusSigma);
647          linePlusSigma->SetLineColor(kBlue);
648          linePlusSigma->SetLineStyle(2 + i);
649          linePlusSigma->Draw();
650          TLine* lineMinusSigma = new TLine(median - nsigma[i] * sigma, 0, median - nsigma[i]*sigma, maxY);
651          //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
652          lineMinusSigma->SetLineColor(kBlue);
653          lineMinusSigma->SetLineStyle(2 + i);
654          lineMinusSigma->Draw();
655          sprintf(c, "%i #sigma = %f",(Int_t)(nsigma[i]), (Float_t)(nsigma[i] * sigma));
656          legend->AddEntry(lineMinusSigma, c, "l");
657       }
658    }
659    if (plotLTM) {
660       // draw LTM
661       Double_t ltmRms = 0;
662       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
663       TLine* line = new TLine(ltm, 0, ltm, maxY);
664       //fListOfObjectsToBeDeleted->Add(line);
665       line->SetLineColor(kGreen+2);
666       line->SetLineWidth(2);
667       line->SetLineStyle(1);
668       line->Draw();
669       sprintf(c, "LTM: %f", ltm);
670       legend->AddEntry(line, c, "l");
671       // draw sigma lines
672       for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
673          TLine* linePlusSigma = new TLine(ltm + nsigma[i] * ltmRms, 0, ltm + nsigma[i] * ltmRms, maxY);
674          //fListOfObjectsToBeDeleted->Add(linePlusSigma);
675          linePlusSigma->SetLineColor(kGreen+2);
676          linePlusSigma->SetLineStyle(2+i);
677          linePlusSigma->Draw();
678    
679          TLine* lineMinusSigma = new TLine(ltm - nsigma[i] * ltmRms, 0, ltm - nsigma[i] * ltmRms, maxY);
680          //fListOfObjectsToBeDeleted->Add(lineMinusSigma);
681          lineMinusSigma->SetLineColor(kGreen+2);
682          lineMinusSigma->SetLineStyle(2+i);
683          lineMinusSigma->Draw();
684          sprintf(c, "%i #sigma = %f", (Int_t)(nsigma[i]), (Float_t)(nsigma[i] * ltmRms));
685          legend->AddEntry(lineMinusSigma, c, "l");
686       }
687    }
688    if (!plotMean && !plotMedian && !plotLTM) return -1;
689    legend->Draw();
690    gStyle->SetOptStat(oldOptStat);
691    return 1;
692 }
693
694
695 Int_t AliTPCCalibViewer::SigmaCut(const char* drawCommand, Int_t 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 {
696    //
697    // 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
698    // The data of the distribution Begin_Latex f(x, #mu, #sigma) End_Latex are given in 'array', 'n' specifies the length of the array
699    // 'mean' and 'sigma' are Begin_Latex #mu End_Latex and  Begin_Latex #sigma End_Latex of the distribution in 'array', to be specified by the user
700    // 'nbins': number of bins, 'binLow': first bin, 'binUp': last bin
701    // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma, Begin_Latex t #sigma End_Latex)
702    // sigmaStep: the binsize of the generated histogram
703    // Begin_Latex 
704    // 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 }
705    // End_Latex
706    // 
707    //
708    // Creates a histogram, where you can see, how much of the data are inside sigma-intervals 
709    // around the mean/median/LTM
710    // with drawCommand, sector and cuts you specify your input data, see EasyDraw
711    // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma)
712    // sigmaStep: the binsize of the generated histogram
713    // plotMean/plotMedian/plotLTM: specifies where to put the center
714    //
715    if (sector >= 0 && sector < 72) {
716       char sectorChr[3];
717       sprintf(sectorChr, "%i", sector);
718       return SigmaCut(drawCommand, sectorChr, cuts, sigmaMax, plotMean, plotMedian, plotLTM, pm, sigmas, sigmaStep);
719    }
720    Error("SigmaCut","The TPC contains only sectors between 0 and 71.");
721    return -1;
722 }
723
724
725 Int_t AliTPCCalibViewer::SigmaCut(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 {
726    //
727    // Creates a histogram, where you can see, how much of the data are inside sigma-intervals 
728    // around the mean/median/LTM
729    // with drawCommand, sector and cuts you specify your input data, see EasyDraw
730    // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma)
731    // sigmaStep: the binsize of the generated histogram
732    // plotMean/plotMedian/plotLTM: specifies where to put the center
733    //
734   
735    Double_t ltmFraction = 0.8;
736    
737    TString drawStr(drawCommand);
738    Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
739    if (dangerousToDraw) {
740       Warning("SigmaCut", "The draw string must not contain ':' or '>>'.");
741       return -1;
742    }
743    drawStr += " >> tempHist";
744    
745    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
746    TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
747    // FIXME is this histogram deleted automatically?
748    Double_t *values = fTree->GetV1();  // value is the array containing 'entries' numbers
749    
750    Double_t mean = TMath::Mean(entries, values);
751    Double_t median = TMath::Median(entries, values);
752    Double_t sigma = TMath::RMS(entries, values);
753    
754    TLegend * legend = new TLegend(.7,.7, .99, .99, "Cumulative");
755    //fListOfObjectsToBeDeleted->Add(legend);
756    TH1F *cutHistoMean = 0;
757    TH1F *cutHistoMedian = 0;
758    TH1F *cutHistoLTM = 0;
759    
760    TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");  
761    TVectorF nsigma(sigmasTokens->GetEntriesFast());
762    for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
763       TString str(((TObjString*)sigmasTokens->At(i))->GetString());
764       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
765       nsigma[i] = sig;
766    }
767   
768    if (plotMean) {
769       cutHistoMean = AliTPCCalibViewer::SigmaCut(htemp, mean, sigma, sigmaMax, sigmaStep, pm);
770       if (cutHistoMean) {
771          //fListOfObjectsToBeDeleted->Add(cutHistoMean);
772          cutHistoMean->SetLineColor(kRed);
773          legend->AddEntry(cutHistoMean, "Mean", "l");
774          cutHistoMean->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
775          cutHistoMean->Draw();
776          DrawLines(cutHistoMean, nsigma, legend, kRed, pm);
777       } // if (cutHistoMean)
778        
779    }
780    if (plotMedian) {
781       cutHistoMedian = AliTPCCalibViewer::SigmaCut(htemp, median, sigma, sigmaMax, sigmaStep, pm);
782       if (cutHistoMedian) {
783          //fListOfObjectsToBeDeleted->Add(cutHistoMedian);
784          cutHistoMedian->SetLineColor(kBlue);
785          legend->AddEntry(cutHistoMedian, "Median", "l");
786          cutHistoMedian->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
787          if (plotMean && cutHistoMean) cutHistoMedian->Draw("same");
788             else cutHistoMedian->Draw();
789          DrawLines(cutHistoMedian, nsigma, legend, kBlue, pm);
790       }  // if (cutHistoMedian)
791    }
792    if (plotLTM) {
793       Double_t ltmRms = 0;
794       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
795       cutHistoLTM = AliTPCCalibViewer::SigmaCut(htemp, ltm, ltmRms, sigmaMax, sigmaStep, pm);
796       if (cutHistoLTM) {
797          //fListOfObjectsToBeDeleted->Add(cutHistoLTM);
798          cutHistoLTM->SetLineColor(kGreen+2);
799          legend->AddEntry(cutHistoLTM, "LTM", "l");
800          cutHistoLTM->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
801          if ((plotMean && cutHistoMean) || (plotMedian && cutHistoMedian)) cutHistoLTM->Draw("same");
802             else cutHistoLTM->Draw();
803          DrawLines(cutHistoLTM, nsigma, legend, kGreen+2, pm);
804       }
805    }
806    if (!plotMean && !plotMedian && !plotLTM) return -1;
807    legend->Draw();
808    return 1;
809 }
810
811
812 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 {
813    //
814    // Creates a histogram, where you can see, how much of the data are inside sigma-intervals 
815    // around the mean/median/LTM
816    // with drawCommand, sector and cuts you specify your input data, see EasyDraw
817    // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma)
818    // sigmaStep: the binsize of the generated histogram
819    // plotMean/plotMedian/plotLTM: specifies where to put the center
820    //
821   
822    // Double_t ltmFraction = 0.8;  //unused
823    // avoid compiler warnings:
824    sigmaMax = sigmaMax;
825    pm = pm;
826    sigmaStep = sigmaStep;
827    
828    TString drawStr(drawCommand);
829    drawStr += " >> tempHist";
830    
831    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
832    TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
833    TGraph *cutGraphMean   = 0;
834    // TGraph *cutGraphMedian = 0;
835    // TGraph *cutGraphLTM    = 0;
836    Double_t *values = fTree->GetV1();  // value is the array containing 'entries' numbers
837    Int_t    *index  = new Int_t[entries];
838    Float_t  *xarray = new Float_t[entries];
839    Float_t  *yarray = new Float_t[entries];
840    TMath::Sort(entries, values, index, kFALSE);
841    
842    Double_t mean = TMath::Mean(entries, values);
843    // Double_t median = TMath::Median(entries, values);
844    Double_t sigma = TMath::RMS(entries, values);
845    
846    TLegend * legend = new TLegend(.7,.7, .99, .99, "Cumulative");
847    //fListOfObjectsToBeDeleted->Add(legend);
848    
849    // parse sigmas string
850    TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");  
851    TVectorF nsigma(sigmasTokens->GetEntriesFast());
852    for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
853       TString str(((TObjString*)sigmasTokens->At(i))->GetString());
854       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
855       nsigma[i] = sig;
856    }
857    
858    if (plotMean) {
859       for (Int_t i = 0; i < entries; i++) {
860          xarray[i] = TMath::Abs(values[index[i]] - mean) / sigma; 
861          yarray[i] = float(i) / float(entries);
862       }
863       cutGraphMean = new TGraph(entries, xarray, yarray);
864       if (cutGraphMean) {
865          //fListOfObjectsToBeDeleted->Add(cutGraphMean);
866          cutGraphMean->SetLineColor(kRed);
867          legend->AddEntry(cutGraphMean, "Mean", "l");
868          cutGraphMean->SetTitle(Form("%s, Cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
869          cutGraphMean->Draw("alu");
870          DrawLines(cutGraphMean, nsigma, legend, kRed, kTRUE);
871       }
872    }
873    /*
874    if (plotMedian) {
875       cutHistoMedian = AliTPCCalibViewer::SigmaCut(htemp, median, sigma, sigmaMax, sigmaStep, pm);
876       if (cutHistoMedian) {
877          fListOfObjectsToBeDeleted->Add(cutHistoMedian);
878          cutHistoMedian->SetLineColor(kBlue);
879          legend->AddEntry(cutHistoMedian, "Median", "l");
880          cutHistoMedian->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
881          if (plotMean && cutHistoMean) cutHistoMedian->Draw("same");
882             else cutHistoMedian->Draw();
883          DrawLines(cutHistoMedian, nsigma, legend, kBlue, pm);
884       }  // if (cutHistoMedian)
885    }
886    if (plotLTM) {
887       Double_t ltmRms = 0;
888       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
889       cutHistoLTM = AliTPCCalibViewer::SigmaCut(htemp, ltm, ltmRms, sigmaMax, sigmaStep, pm);
890       if (cutHistoLTM) {
891          fListOfObjectsToBeDeleted->Add(cutHistoLTM);
892          cutHistoLTM->SetLineColor(kGreen+2);
893          legend->AddEntry(cutHistoLTM, "LTM", "l");
894          cutHistoLTM->SetTitle(Form("%s, cumulative; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
895          if (plotMean && cutHistoMean || plotMedian && cutHistoMedian) cutHistoLTM->Draw("same");
896             else cutHistoLTM->Draw();
897          DrawLines(cutHistoLTM, nsigma, legend, kGreen+2, pm);
898       }
899    }*/
900    if (!plotMean && !plotMedian && !plotLTM) return -1;
901    legend->Draw();
902    return 1;
903 }
904
905
906 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 {
907    //
908    // 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"   
909    // "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
910    // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate 
911    // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
912    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
913    // The actual work is done on the array.
914    /* Begin_Latex 
915          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 }
916       End_Latex  
917    */
918    if (sector >= 0 && sector < 72) {
919       char sectorChr[3];
920       sprintf(sectorChr, "%i", sector);
921       return Integrate(drawCommand, sectorChr, cuts, sigmaMax, plotMean, plotMedian, plotLTM, sigmas, sigmaStep);
922    }
923    Error("Integrate","The TPC contains only sectors between 0 and 71.");
924    return -1;
925    
926 }
927
928
929 Int_t AliTPCCalibViewer::IntegrateOld(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 {
930    //
931    // 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"   
932    // "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
933    // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate 
934    // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
935    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
936    // The actual work is done on the array.
937    /* Begin_Latex 
938          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 }
939       End_Latex  
940    */
941    
942    Double_t ltmFraction = 0.8;
943    
944    TString drawStr(drawCommand);
945    drawStr += " >> tempHist";
946    
947    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
948    TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
949    // FIXME is this histogram deleted automatically?
950    Double_t *values = fTree->GetV1();  // value is the array containing 'entries' numbers
951    
952    Double_t mean = TMath::Mean(entries, values);
953    Double_t median = TMath::Median(entries, values);
954    Double_t sigma = TMath::RMS(entries, values);
955     
956    TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");  
957    TVectorF nsigma(sigmasTokens->GetEntriesFast());
958    for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
959       TString str(((TObjString*)sigmasTokens->At(i))->GetString());
960       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
961       nsigma[i] = sig;
962    }
963   
964    TLegend * legend = new TLegend(.7,.7, .99, .99, "Integrated histogram");
965    //fListOfObjectsToBeDeleted->Add(legend);
966    TH1F *integralHistoMean = 0;
967    TH1F *integralHistoMedian = 0;
968    TH1F *integralHistoLTM = 0;
969   
970    if (plotMean) {
971       integralHistoMean = AliTPCCalibViewer::Integrate(htemp, mean, sigma, sigmaMax, sigmaStep);
972       if (integralHistoMean) {
973          //fListOfObjectsToBeDeleted->Add(integralHistoMean);
974          integralHistoMean->SetLineColor(kRed);
975          legend->AddEntry(integralHistoMean, "Mean", "l");
976          integralHistoMean->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
977          integralHistoMean->Draw();
978          DrawLines(integralHistoMean, nsigma, legend, kRed, kTRUE);
979       }
980    }
981    if (plotMedian) {
982       integralHistoMedian = AliTPCCalibViewer::Integrate(htemp, median, sigma, sigmaMax, sigmaStep);
983       if (integralHistoMedian) {
984          //fListOfObjectsToBeDeleted->Add(integralHistoMedian);
985          integralHistoMedian->SetLineColor(kBlue);
986          legend->AddEntry(integralHistoMedian, "Median", "l");
987          integralHistoMedian->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
988          if (plotMean && integralHistoMean) integralHistoMedian->Draw("same");
989             else integralHistoMedian->Draw();
990          DrawLines(integralHistoMedian, nsigma, legend, kBlue, kTRUE);
991       }
992    }
993    if (plotLTM) {
994       Double_t ltmRms = 0;
995       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
996       integralHistoLTM = AliTPCCalibViewer::Integrate(htemp, ltm, ltmRms, sigmaMax, sigmaStep);
997       if (integralHistoLTM) {
998          //fListOfObjectsToBeDeleted->Add(integralHistoLTM);
999          integralHistoLTM->SetLineColor(kGreen+2);
1000          legend->AddEntry(integralHistoLTM, "LTM", "l");
1001          integralHistoLTM->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
1002          if ((plotMean && integralHistoMean) || (plotMedian && integralHistoMedian)) integralHistoLTM->Draw("same");
1003             else integralHistoLTM->Draw();
1004          DrawLines(integralHistoLTM, nsigma, legend, kGreen+2, kTRUE);
1005       }
1006    }
1007    if (!plotMean && !plotMedian && !plotLTM) return -1;
1008    legend->Draw();
1009    return 1;
1010 }
1011
1012
1013 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 {
1014    //
1015    // 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"   
1016    // "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
1017    // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate 
1018    // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
1019    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
1020    // The actual work is done on the array.
1021    /* Begin_Latex 
1022          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 }
1023       End_Latex  
1024    */
1025    
1026    Double_t ltmFraction = 0.8;
1027    // avoid compiler warnings:
1028    sigmaMax = sigmaMax;
1029    sigmaStep = sigmaStep;
1030    
1031    TString drawStr(drawCommand);
1032    Bool_t dangerousToDraw = drawStr.Contains(":") || drawStr.Contains(">>");
1033    if (dangerousToDraw) {
1034       Warning("Integrate", "The draw string must not contain ':' or '>>'.");
1035       return -1;
1036    }
1037    drawStr += " >> tempHist";
1038    
1039    Int_t entries = EasyDraw1D(drawStr.Data(), sector, cuts, "goff");
1040    TH1F *htemp = (TH1F*)gDirectory->Get("tempHist");
1041    TGraph *integralGraphMean   = 0;
1042    TGraph *integralGraphMedian = 0;
1043    TGraph *integralGraphLTM    = 0;
1044    Double_t *values = fTree->GetV1();  // value is the array containing 'entries' numbers
1045    Int_t    *index  = new Int_t[entries];
1046    Float_t  *xarray = new Float_t[entries];
1047    Float_t  *yarray = new Float_t[entries];
1048    TMath::Sort(entries, values, index, kFALSE);
1049    
1050    Double_t mean = TMath::Mean(entries, values);
1051    Double_t median = TMath::Median(entries, values);
1052    Double_t sigma = TMath::RMS(entries, values);
1053    
1054    // parse sigmas string
1055    TObjArray *sigmasTokens = TString(sigmas).Tokenize(";");  
1056    TVectorF nsigma(sigmasTokens->GetEntriesFast());
1057    for (Int_t i = 0; i < sigmasTokens->GetEntriesFast(); i++) {
1058       TString str(((TObjString*)sigmasTokens->At(i))->GetString());
1059       Double_t sig = (str.IsFloat()) ? str.Atof() : 0;
1060       nsigma[i] = sig;
1061    }
1062   
1063    TLegend * legend = new TLegend(.7,.7, .99, .99, "Integrated histogram");
1064    //fListOfObjectsToBeDeleted->Add(legend);
1065   
1066    if (plotMean) {
1067       for (Int_t i = 0; i < entries; i++) {
1068          xarray[i] = (values[index[i]] - mean) / sigma; 
1069          yarray[i] = float(i) / float(entries);
1070       }
1071       integralGraphMean = new TGraph(entries, xarray, yarray);
1072       if (integralGraphMean) {
1073          //fListOfObjectsToBeDeleted->Add(integralGraphMean);
1074          integralGraphMean->SetLineColor(kRed);
1075          legend->AddEntry(integralGraphMean, "Mean", "l");
1076          integralGraphMean->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
1077          integralGraphMean->Draw("alu");
1078          DrawLines(integralGraphMean, nsigma, legend, kRed, kTRUE);
1079       }
1080    }
1081    if (plotMedian) {
1082       for (Int_t i = 0; i < entries; i++) {
1083          xarray[i] = (values[index[i]] - median) / sigma; 
1084          yarray[i] = float(i) / float(entries);
1085       }
1086       integralGraphMedian = new TGraph(entries, xarray, yarray);
1087       if (integralGraphMedian) {
1088          //fListOfObjectsToBeDeleted->Add(integralGraphMedian);
1089          integralGraphMedian->SetLineColor(kBlue);
1090          legend->AddEntry(integralGraphMedian, "Median", "l");
1091          integralGraphMedian->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
1092          if (plotMean && integralGraphMean) integralGraphMedian->Draw("samelu");
1093             else integralGraphMedian->Draw("alu");
1094          DrawLines(integralGraphMedian, nsigma, legend, kBlue, kTRUE);
1095       }
1096    }
1097    if (plotLTM) {
1098       Double_t ltmRms = 0;
1099       Double_t ltm = GetLTM(entries, values, &ltmRms, ltmFraction);
1100       for (Int_t i = 0; i < entries; i++) {
1101          xarray[i] = (values[index[i]] - ltm) / ltmRms; 
1102          yarray[i] = float(i) / float(entries);
1103       }
1104       integralGraphLTM = new TGraph(entries, xarray, yarray);
1105       if (integralGraphLTM) {
1106          //fListOfObjectsToBeDeleted->Add(integralGraphLTM);
1107          integralGraphLTM->SetLineColor(kGreen+2);
1108          legend->AddEntry(integralGraphLTM, "LTM", "l");
1109          integralGraphLTM->SetTitle(Form("%s, integrated; Multiples of #sigma; Fraction of included data", htemp->GetTitle()));
1110          if ((plotMean && integralGraphMean) || (plotMedian && integralGraphMedian)) integralGraphLTM->Draw("samelu");
1111             else integralGraphLTM->Draw("alu");
1112          DrawLines(integralGraphLTM, nsigma, legend, kGreen+2, kTRUE);
1113       }
1114    }
1115    if (!plotMean && !plotMedian && !plotLTM) return -1;
1116    legend->Draw();
1117    return entries;
1118 }
1119
1120
1121 void AliTPCCalibViewer::DrawLines(TH1F *histogram, TVectorF nsigma, TLegend *legend, Int_t color, Bool_t pm) const {
1122    // 
1123    // Private function for SigmaCut(...) and Integrate(...)
1124    // Draws lines into the given histogram, specified by "nsigma", the lines are addeed to the legend
1125    // 
1126    
1127    // start to draw the lines, loop over requested sigmas
1128    char c[500];
1129    for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
1130       if (!pm) { 
1131          Int_t bin = histogram->GetXaxis()->FindBin(nsigma[i]);
1132          TLine* lineUp = new TLine(nsigma[i], 0, nsigma[i], histogram->GetBinContent(bin));
1133          //fListOfObjectsToBeDeleted->Add(lineUp);
1134          lineUp->SetLineColor(color);
1135          lineUp->SetLineStyle(2 + i);
1136          lineUp->Draw();
1137          TLine* lineLeft = new TLine(nsigma[i], histogram->GetBinContent(bin), 0, histogram->GetBinContent(bin));
1138          //fListOfObjectsToBeDeleted->Add(lineLeft);
1139          lineLeft->SetLineColor(color);
1140          lineLeft->SetLineStyle(2 + i);
1141          lineLeft->Draw();
1142          sprintf(c, "Fraction(%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin));
1143          legend->AddEntry(lineLeft, c, "l");
1144       }
1145       else { // if (pm)
1146          Int_t bin = histogram->GetXaxis()->FindBin(nsigma[i]);
1147          TLine* lineUp1 = new TLine(nsigma[i], 0, nsigma[i], histogram->GetBinContent(bin));
1148          //fListOfObjectsToBeDeleted->Add(lineUp1);
1149          lineUp1->SetLineColor(color);
1150          lineUp1->SetLineStyle(2 + i);
1151          lineUp1->Draw();
1152          TLine* lineLeft1 = new TLine(nsigma[i], histogram->GetBinContent(bin), histogram->GetBinLowEdge(0)+histogram->GetBinWidth(0), histogram->GetBinContent(bin));
1153          //fListOfObjectsToBeDeleted->Add(lineLeft1);
1154          lineLeft1->SetLineColor(color);
1155          lineLeft1->SetLineStyle(2 + i);
1156          lineLeft1->Draw();
1157          sprintf(c, "Fraction(+%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin));
1158          legend->AddEntry(lineLeft1, c, "l");
1159          bin = histogram->GetXaxis()->FindBin(-nsigma[i]);
1160          TLine* lineUp2 = new TLine(-nsigma[i], 0, -nsigma[i], histogram->GetBinContent(bin));
1161          //fListOfObjectsToBeDeleted->Add(lineUp2);
1162          lineUp2->SetLineColor(color);
1163          lineUp2->SetLineStyle(2 + i);
1164          lineUp2->Draw();
1165          TLine* lineLeft2 = new TLine(-nsigma[i], histogram->GetBinContent(bin), histogram->GetBinLowEdge(0)+histogram->GetBinWidth(0), histogram->GetBinContent(bin));
1166          //fListOfObjectsToBeDeleted->Add(lineLeft2);
1167          lineLeft2->SetLineColor(color);
1168          lineLeft2->SetLineStyle(2 + i);
1169          lineLeft2->Draw();
1170          sprintf(c, "Fraction(-%f #sigma) = %f",nsigma[i], histogram->GetBinContent(bin));
1171          legend->AddEntry(lineLeft2, c, "l");
1172       }
1173    }  // for (Int_t i = 0; i < nsigma.GetNoElements(); i++)   
1174 }
1175
1176
1177 void AliTPCCalibViewer::DrawLines(TGraph *graph, TVectorF nsigma, TLegend *legend, Int_t color, Bool_t pm) const {
1178    // 
1179    // Private function for SigmaCut(...) and Integrate(...)
1180    // Draws lines into the given histogram, specified by "nsigma", the lines are addeed to the legend
1181    // 
1182    
1183    // start to draw the lines, loop over requested sigmas
1184    char c[500];
1185    for (Int_t i = 0; i < nsigma.GetNoElements(); i++) {
1186       if (!pm) { 
1187          TLine* lineUp = new TLine(nsigma[i], 0, nsigma[i], graph->Eval(nsigma[i]));
1188          //fListOfObjectsToBeDeleted->Add(lineUp);
1189          lineUp->SetLineColor(color);
1190          lineUp->SetLineStyle(2 + i);
1191          lineUp->Draw();
1192          TLine* lineLeft = new TLine(nsigma[i], graph->Eval(nsigma[i]), 0, graph->Eval(nsigma[i]));
1193          //fListOfObjectsToBeDeleted->Add(lineLeft);
1194          lineLeft->SetLineColor(color);
1195          lineLeft->SetLineStyle(2 + i);
1196          lineLeft->Draw();
1197          sprintf(c, "Fraction(%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i]));
1198          legend->AddEntry(lineLeft, c, "l");
1199       }
1200       else { // if (pm)
1201          TLine* lineUp1 = new TLine(nsigma[i], 0, nsigma[i], graph->Eval(nsigma[i]));
1202          //fListOfObjectsToBeDeleted->Add(lineUp1);
1203          lineUp1->SetLineColor(color);
1204          lineUp1->SetLineStyle(2 + i);
1205          lineUp1->Draw();
1206          TLine* lineLeft1 = new TLine(nsigma[i], graph->Eval(nsigma[i]), graph->GetHistogram()->GetXaxis()->GetBinLowEdge(0), graph->Eval(nsigma[i]));
1207          //fListOfObjectsToBeDeleted->Add(lineLeft1);
1208          lineLeft1->SetLineColor(color);
1209          lineLeft1->SetLineStyle(2 + i);
1210          lineLeft1->Draw();
1211          sprintf(c, "Fraction(+%f #sigma) = %f",nsigma[i], graph->Eval(nsigma[i]));
1212          legend->AddEntry(lineLeft1, c, "l");
1213          TLine* lineUp2 = new TLine(-nsigma[i], 0, -nsigma[i], graph->Eval(-nsigma[i]));
1214          //fListOfObjectsToBeDeleted->Add(lineUp2);
1215          lineUp2->SetLineColor(color);
1216          lineUp2->SetLineStyle(2 + i);
1217          lineUp2->Draw();
1218          TLine* lineLeft2 = new TLine(-nsigma[i], graph->Eval(-nsigma[i]), graph->GetHistogram()->GetXaxis()->GetBinLowEdge(0), graph->Eval(-nsigma[i]));
1219          //fListOfObjectsToBeDeleted->Add(lineLeft2);
1220          lineLeft2->SetLineColor(color);
1221          lineLeft2->SetLineStyle(2 + i);
1222          lineLeft2->Draw();
1223          sprintf(c, "Fraction(-%f #sigma) = %f",nsigma[i], graph->Eval(-nsigma[i]));
1224          legend->AddEntry(lineLeft2, c, "l");
1225       }
1226    }  // for (Int_t i = 0; i < nsigma.GetNoElements(); i++)   
1227 }
1228
1229
1230
1231
1232
1233 /////////////////
1234 // Array tools //
1235 /////////////////
1236
1237
1238 Int_t AliTPCCalibViewer::GetBin(Float_t value, Int_t nbins, Double_t binLow, Double_t binUp){
1239    // Returns the 'bin' for 'value'
1240    // The interval between 'binLow' and 'binUp' is divided into 'nbins' equidistant bins
1241    // avoid index out of bounds error: 'if (bin < binLow) bin = binLow' and vice versa
1242    /* Begin_Latex
1243          GetBin(value) = #frac{nbins - 1}{binUp - binLow} #upoint (value - binLow) +1
1244       End_Latex
1245    */
1246    
1247    Int_t bin =  TMath::Nint( (Float_t)(value - binLow) / (Float_t)(binUp - binLow) * (nbins-1) ) + 1;
1248    // avoid index out of bounds:   
1249    if (value < binLow) bin = 0;
1250    if (value > binUp)  bin = nbins + 1;
1251    return bin;
1252    
1253 }   
1254
1255
1256 Double_t AliTPCCalibViewer::GetLTM(Int_t n, const Double_t *const array, Double_t *const sigma, Double_t fraction){
1257    //
1258    //  returns the LTM and sigma
1259    //
1260    Double_t *ddata = new Double_t[n];
1261    Double_t mean = 0, lsigma = 0;
1262    UInt_t nPoints = 0;
1263    for (UInt_t i = 0; i < (UInt_t)n; i++) {
1264          ddata[nPoints]= array[nPoints];
1265          nPoints++;
1266    }
1267    Int_t hh = TMath::Min(TMath::Nint(fraction * nPoints), Int_t(n));
1268    AliMathBase::EvaluateUni(nPoints, ddata, mean, lsigma, hh);
1269    if (sigma) *sigma = lsigma;
1270    delete [] ddata;
1271    return mean;
1272 }
1273
1274
1275 TH1F* AliTPCCalibViewer::SigmaCut(TH1F *const histogram, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep, Bool_t pm) {
1276    //
1277    // 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
1278    // The data of the distribution Begin_Latex f(x, #mu, #sigma) End_Latex are given in 'histogram'
1279    // '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
1280    // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma, Begin_Latex t #sigma End_Latex)
1281    // sigmaStep: the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
1282    // pm: Decide weather Begin_Latex t > 0 End_Latex (first case) or Begin_Latex t End_Latex arbitrary (secound case)
1283    // The actual work is done on the array.
1284    /* Begin_Latex 
1285          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    
1286          or      
1287          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 }
1288       End_Latex  
1289       begin_macro(source)
1290       {
1291          Float_t mean = 0;
1292          Float_t sigma = 1.5;
1293          Float_t sigmaMax = 4;
1294          gROOT->SetStyle("Plain");
1295          TH1F *distribution = new TH1F("Distribution1", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
1296          TRandom rand(23);
1297          for (Int_t i = 0; i <50000;i++) distribution->Fill(rand.Gaus(mean, sigma));
1298          Float_t *ar = distribution->GetArray();
1299          
1300          TCanvas* macro_example_canvas = new TCanvas("macro_example_canvas_SigmaCut", "", 350, 350);
1301          macro_example_canvas->Divide(0,3);
1302          TVirtualPad *pad1 = macro_example_canvas->cd(1);
1303          pad1->SetGridy();
1304          pad1->SetGridx();
1305          distribution->Draw();
1306          TVirtualPad *pad2 = macro_example_canvas->cd(2);
1307          pad2->SetGridy();
1308          pad2->SetGridx();
1309          
1310          TH1F *shist = AliTPCCalibViewer::SigmaCut(distribution, mean, sigma, sigmaMax);
1311          shist->SetNameTitle("Cumulative","Cumulative S(t, #mu, #sigma)");
1312          shist->Draw();  
1313          TVirtualPad *pad3 = macro_example_canvas->cd(3);
1314          pad3->SetGridy();
1315          pad3->SetGridx();
1316          TH1F *shistPM = AliTPCCalibViewer::SigmaCut(distribution, mean, sigma, sigmaMax, -1, kTRUE);
1317          shistPM->Draw();   
1318          return macro_example_canvas;
1319       }  
1320       end_macro
1321    */ 
1322    
1323    Float_t *array = histogram->GetArray();
1324    Int_t    nbins = histogram->GetXaxis()->GetNbins();
1325    Float_t binLow = histogram->GetXaxis()->GetXmin();
1326    Float_t binUp  = histogram->GetXaxis()->GetXmax();
1327    return AliTPCCalibViewer::SigmaCut(nbins, array, mean, sigma, nbins, binLow, binUp, sigmaMax, sigmaStep, pm);
1328 }   
1329    
1330
1331 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){
1332    //
1333    // 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
1334    // The data of the distribution Begin_Latex f(x, #mu, #sigma) End_Latex are given in 'array', 'n' specifies the length of the array
1335    // 'mean' and 'sigma' are Begin_Latex #mu End_Latex and  Begin_Latex #sigma End_Latex of the distribution in 'array', to be specified by the user
1336    // 'nbins': number of bins, 'binLow': first bin, 'binUp': last bin
1337    // sigmaMax: up to which sigma around the mean/median/LTM the histogram is generated (in units of sigma, Begin_Latex t #sigma End_Latex)
1338    // sigmaStep: the binsize of the generated histogram
1339    // Here the actual work is done.
1340    
1341    if (sigma == 0) return 0;
1342    Float_t binWidth = (binUp-binLow)/(nbins - 1);
1343    if (sigmaStep <= 0) sigmaStep = binWidth;
1344    Int_t kbins = (Int_t)(sigmaMax * sigma / sigmaStep) + 1; // + 1  due to overflow bin in histograms
1345    if (pm) kbins = 2 * (Int_t)(sigmaMax * sigma / sigmaStep) + 1;
1346    Float_t kbinLow = !pm ? 0 : -sigmaMax;
1347    Float_t kbinUp  = sigmaMax;
1348    TH1F *hist = new TH1F("sigmaCutHisto","Cumulative; Multiples of #sigma; Fraction of included data", kbins, kbinLow, kbinUp); 
1349    hist->SetDirectory(0);
1350    hist->Reset();
1351    
1352    // calculate normalization
1353    Double_t normalization = 0;
1354    for (Int_t i = 0; i <= n; i++) {
1355         normalization += array[i];
1356    }
1357    
1358    // given units: units from given histogram
1359    // sigma units: in units of sigma
1360    // iDelta: integrate in interval (mean +- iDelta), given units
1361    // x:      ofset from mean for integration, given units
1362    // hist:   needs 
1363    
1364 //    printf("nbins: %i, binLow: %f, binUp: %f \n", nbins, binLow, binUp);
1365    // fill histogram
1366    for (Float_t iDelta = 0; iDelta <= sigmaMax * sigma; iDelta += sigmaStep) {
1367       // integrate array
1368       Double_t valueP = array[GetBin(mean, nbins, binLow, binUp)];
1369       Double_t valueM = array[GetBin(mean-binWidth, nbins, binLow, binUp)];
1370       // add bin of mean value only once to the histogram
1371 //       printf("++ adding bins: ");
1372       for (Float_t x = binWidth; x <= iDelta; x += binWidth) {
1373          valueP += (mean + x <= binUp)  ? array[GetBin(mean + x, nbins, binLow, binUp)] : 0;
1374          valueM += (mean-binWidth - x >= binLow) ? array[GetBin(mean-binWidth - x, nbins, binLow, binUp)] : 0; 
1375 //          printf("%i, ", GetBin(mean + x, nbins, binLow, binUp));        
1376       }
1377 //       printf("\n");
1378       if (valueP / normalization > 100) printf("+++ Error, value to big: %f, normalization with %f will fail  +++ \n", valueP, normalization);
1379       if (valueP / normalization > 100) return hist;
1380       if (valueM / normalization > 100) printf("+++ Error, value to big: %f, normalization with %f will fail  +++ \n", valueM, normalization);
1381       if (valueM / normalization > 100) return hist;
1382       valueP = (valueP / normalization);
1383       valueM = (valueM / normalization);
1384       if (pm) {
1385          Int_t bin = GetBin(iDelta/sigma, kbins, kbinLow, kbinUp);
1386          hist->SetBinContent(bin, valueP);
1387          bin = GetBin(-iDelta/sigma, kbins, kbinLow, kbinUp);
1388          hist->SetBinContent(bin, valueM);
1389       }
1390       else { // if (!pm)
1391          Int_t bin = GetBin(iDelta/sigma, kbins, kbinLow, kbinUp);
1392          hist->SetBinContent(bin, valueP + valueM);
1393 //          printf("  first integration bin: %i, last integration bin in + direction: %i \n", GetBin(mean+binWidth, nbins, binLow, binUp), GetBin(iDelta, nbins, binLow, binUp));
1394 //          printf("  first integration bin: %i, last integration bin in - direction: %i \n", GetBin(mean+binWidth, nbins, binLow, binUp), GetBin(-iDelta, nbins, binLow, binUp));
1395 //          printf("  value: %f, normalization: %f, iDelta: %f, Bin: %i \n", valueP+valueM, normalization, iDelta, bin);
1396       }
1397    }
1398    //hist->SetMaximum(0.7);
1399    if (!pm) hist->SetMaximum(1.2);
1400    return hist;
1401 }
1402
1403
1404 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){
1405    // 
1406    // SigmaCut for variable binsize
1407    // NOT YET IMPLEMENTED !!!
1408    // 
1409    printf("SigmaCut with variable binsize, Not yet implemented\n");
1410    // avoid compiler warnings:
1411    n=n;
1412    mean=mean;
1413    sigma=sigma;
1414    nbins=nbins;
1415    sigmaMax=sigmaMax;
1416    array=array;
1417    xbins=xbins;
1418    
1419    return 0;
1420 }   
1421
1422
1423 TH1F* AliTPCCalibViewer::Integrate(TH1F *const histogram, Float_t mean, Float_t sigma, Float_t sigmaMax, Float_t sigmaStep){
1424    //
1425    // 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"   
1426    // "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
1427    // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate 
1428    // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
1429    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
1430    // The actual work is done on the array.
1431    /* Begin_Latex 
1432          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 }
1433       End_Latex  
1434       begin_macro(source)
1435       {
1436          Float_t mean = 0;
1437          Float_t sigma = 1.5;
1438          Float_t sigmaMax = 4;
1439          gROOT->SetStyle("Plain");
1440          TH1F *distribution = new TH1F("Distribution2", "Distribution f(x, #mu, #sigma)", 1000,-5,5);
1441          TRandom rand(23);
1442          for (Int_t i = 0; i <50000;i++) distribution->Fill(rand.Gaus(mean, sigma));
1443          Float_t *ar = distribution->GetArray();
1444          
1445          TCanvas* macro_example_canvas = new TCanvas("macro_example_canvas_Integrate", "", 350, 350);
1446          macro_example_canvas->Divide(0,2);
1447          TVirtualPad *pad1 = macro_example_canvas->cd(1);
1448          pad1->SetGridy();
1449          pad1->SetGridx();
1450          distribution->Draw();
1451          TVirtualPad *pad2 = macro_example_canvas->cd(2);
1452          pad2->SetGridy();
1453          pad2->SetGridx();
1454          TH1F *shist = AliTPCCalibViewer::Integrate(distribution, mean, sigma, sigmaMax);
1455          shist->SetNameTitle("Cumulative","Cumulative S(t, #mu, #sigma)");
1456          shist->Draw();  
1457          
1458          return macro_example_canvas_Integrate;
1459       }  
1460       end_macro
1461    */ 
1462
1463    
1464    Float_t *array = histogram->GetArray();
1465    Int_t    nbins = histogram->GetXaxis()->GetNbins();
1466    Float_t binLow = histogram->GetXaxis()->GetXmin();
1467    Float_t binUp  = histogram->GetXaxis()->GetXmax();
1468    return AliTPCCalibViewer::Integrate(nbins, array, nbins, binLow, binUp, mean, sigma, sigmaMax, sigmaStep);
1469 }   
1470
1471
1472 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){
1473    // 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"   
1474    // "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
1475    // sigmaMax: up to which sigma around the mean/median/LTM you want to integrate 
1476    // if "igma == 0" and "sigmaMax == 0" the whole histogram is integrated
1477    // "sigmaStep": the binsize of the generated histogram, -1 means, that the maximal reasonable stepsize is used
1478    // Here the actual work is done.
1479       
1480    Bool_t givenUnits = kTRUE;
1481    if (sigma != 0 && sigmaMax != 0) givenUnits = kFALSE;
1482    if (givenUnits) {
1483       sigma = 1;
1484       sigmaMax = (binUp - binLow) / 2.;
1485    }
1486    
1487    Float_t binWidth = (binUp-binLow)/(nbins - 1);
1488    if (sigmaStep <= 0) sigmaStep = binWidth;
1489    Int_t kbins =  (Int_t)(sigmaMax * sigma / sigmaStep) + 1;  // + 1  due to overflow bin in histograms
1490    Float_t kbinLow = givenUnits ? binLow : -sigmaMax;
1491    Float_t kbinUp  = givenUnits ? binUp  : sigmaMax;
1492    TH1F *hist = 0; 
1493    if (givenUnits)  hist = new TH1F("integratedHisto","Integrated Histogram; Given x; Fraction of included data", kbins, kbinLow, kbinUp); 
1494    if (!givenUnits) hist = new TH1F("integratedHisto","Integrated Histogram; Multiples of #sigma; Fraction of included data", kbins, kbinLow, kbinUp); 
1495    hist->SetDirectory(0);
1496    hist->Reset();
1497    
1498    // calculate normalization
1499  //  printf("calculating normalization, integrating from bin 1 to %i \n", n);
1500    Double_t normalization = 0;
1501    for (Int_t i = 1; i <= n; i++) {
1502         normalization += array[i];
1503    }
1504  //  printf("normalization: %f \n", normalization);
1505    
1506    // given units: units from given histogram
1507    // sigma units: in units of sigma
1508    // iDelta: integrate in interval (mean +- iDelta), given units
1509    // x:      ofset from mean for integration, given units
1510    // hist:   needs 
1511    
1512    // fill histogram
1513    for (Float_t iDelta = mean - sigmaMax * sigma; iDelta <= mean + sigmaMax * sigma; iDelta += sigmaStep) {
1514       // integrate array
1515       Double_t value = 0;
1516       for (Float_t x = mean - sigmaMax * sigma; x <= iDelta; x += binWidth) {
1517          value += (x <= binUp && x >= binLow)  ? array[GetBin(x, nbins, binLow, binUp)] : 0;
1518       }
1519       if (value / normalization > 100) printf("+++ Error, value to big: %f, normalization with %f will fail  +++ \n", value, normalization);
1520       if (value / normalization > 100) return hist;
1521       Int_t bin = GetBin(iDelta/sigma, kbins, kbinLow, kbinUp);
1522     //  printf("first integration bin: %i, last integration bin: %i \n", GetBin(mean - sigmaMax * sigma, nbins, binLow, binUp), GetBin(iDelta, nbins, binLow, binUp));
1523     //  printf("value: %f, normalization: %f, normalized value: %f, iDelta: %f, Bin: %i \n", value, normalization, value/normalization, iDelta, bin);
1524       value = (value / normalization);
1525       hist->SetBinContent(bin, value);
1526    }
1527    return hist;
1528 }
1529
1530
1531
1532
1533
1534 ////////////////////////
1535 // end of Array tools //
1536 ////////////////////////
1537
1538
1539
1540 //_____________________________________________________________________________
1541 AliTPCCalPad* AliTPCCalibViewer::GetCalPadOld(const char* desiredData, const char* cuts, const char* calPadName) const {
1542   //
1543   // creates a AliTPCCalPad out of the 'desiredData'
1544   // the functionality of EasyDraw1D is used
1545   // calPadName specifies the name of the created AliTPCCalPad
1546   //  - this takes a while -
1547   //
1548    TString drawStr(desiredData);
1549    drawStr.Append(":channel");
1550    drawStr.Append(fAbbreviation);
1551    AliTPCCalPad * createdCalPad = new AliTPCCalPad(calPadName, calPadName);
1552    Int_t entries = 0;
1553    for (Int_t sec = 0; sec < 72; sec++) {
1554      AliTPCCalROC * roc = createdCalPad->GetCalROC(sec);
1555       entries = EasyDraw1D(drawStr.Data(), (Int_t)sec, cuts, "goff");
1556       if (entries == -1) return 0;
1557       const Double_t *pchannel = fTree->GetV2();
1558       const Double_t *pvalue   = fTree->GetV1();
1559       for (Int_t i = 0; i < entries; i++) 
1560          roc->SetValue((UInt_t)(pchannel[i]), (Float_t)(pvalue[i]));
1561    }
1562    return createdCalPad;   
1563 }
1564
1565
1566 //_____________________________________________________________________________
1567 AliTPCCalPad* AliTPCCalibViewer::GetCalPad(const char* desiredData, const char* cuts, const char* calPadName) const {
1568   //
1569   // creates a AliTPCCalPad out of the 'desiredData'
1570   // the functionality of EasyDraw1D is used
1571   // calPadName specifies the name of the created AliTPCCalPad
1572   //  - this takes a while -
1573   //
1574    TString drawStr(desiredData);
1575    drawStr.Append(":channel.fElements:sector");
1576    AliTPCCalPad * createdCalPad = new AliTPCCalPad(calPadName, calPadName);
1577    //
1578    Int_t entries = fTree->Draw(drawStr, cuts,"goff");
1579    const Double_t *pvalue   = fTree->GetV1();
1580    const Double_t *pchannel = fTree->GetV2();
1581    const Double_t *psector  = fTree->GetV3();
1582
1583    for (Int_t ientry=0; ientry<entries; ientry++){
1584      Int_t sector= TMath::Nint(psector[ientry]);
1585      AliTPCCalROC * roc = createdCalPad->GetCalROC(sector);
1586      if (roc) roc->SetValue((UInt_t)(pchannel[ientry]), (Float_t)(pvalue[ientry]));
1587    }
1588
1589   //  for (Int_t sec = 0; sec < 72; sec++) {
1590 //      AliTPCCalROC * roc = createdCalPad->GetCalROC(sec);
1591 //       entries = EasyDraw1D(drawStr.Data(), (Int_t)sec, cuts, "goff");
1592 //       if (entries == -1) return 0;
1593 //       for (Int_t i = 0; i < entries; i++) 
1594 //          roc->SetValue((UInt_t)(pchannel[i]), (Float_t)(pvalue[i]));
1595 //    }
1596    return createdCalPad;   
1597 }
1598
1599 //_____________________________________________________________________________
1600 AliTPCCalROC* AliTPCCalibViewer::GetCalROC(const char* desiredData, UInt_t sector, const char* cuts) const {
1601   //
1602   // creates a AliTPCCalROC out of the desiredData
1603   // the functionality of EasyDraw1D is used
1604   // sector specifies the sector of the created AliTPCCalROC
1605   //
1606    TString drawStr(desiredData);
1607    drawStr.Append(":channel");
1608    drawStr.Append(fAbbreviation);
1609    Int_t entries = EasyDraw1D(drawStr.Data(), (Int_t)sector, cuts, "goff");
1610    if (entries == -1) return 0;
1611    AliTPCCalROC * createdROC = new AliTPCCalROC(sector);
1612    for (Int_t i = 0; i < entries; i++) 
1613       createdROC->SetValue((UInt_t)(fTree->GetV2()[i]), fTree->GetV1()[i]);
1614    return createdROC;
1615 }
1616
1617
1618 TObjArray* AliTPCCalibViewer::GetListOfVariables(Bool_t printList) {
1619   //
1620   // scan the tree  - produces a list of available variables in the tree
1621   // printList: print the list to the screen, after the scan is done
1622   //
1623    TObjArray* arr = new TObjArray();
1624    TObjString* str = 0;
1625    if (!fTree) return 0;
1626    Int_t nentries = fTree->GetListOfBranches()->GetEntries();
1627    for (Int_t i = 0; i < nentries; i++) {
1628       str = new TObjString(fTree->GetListOfBranches()->At(i)->GetName());
1629       str->String().ReplaceAll("_Median", "");
1630       str->String().ReplaceAll("_Mean", "");
1631       str->String().ReplaceAll("_RMS", "");
1632       str->String().ReplaceAll("_LTM", "");
1633       str->String().ReplaceAll("_OutlierCutted", "");
1634       str->String().ReplaceAll(".", "");
1635       if (!arr->FindObject(str) && 
1636           !(str->String() == "channel" || str->String() == "gx" || str->String() == "gy" || 
1637             str->String() == "lx" || str->String() == "ly" || str->String() == "pad" || 
1638             str->String() == "row" || str->String() == "rpad" || str->String() == "sector"  ))
1639          arr->Add(str);
1640    }
1641    
1642    // loop over all friends (if there are some) and add them to the list
1643    if (fTree->GetListOfFriends()) {
1644       for (Int_t ifriend = 0; ifriend < fTree->GetListOfFriends()->GetEntries(); ifriend++){
1645          // printf("iterating through friendlist, currently at %i\n", ifriend);
1646          // printf("working with %s\n", fTree->GetListOfFriends()->At(ifriend)->ClassName());
1647          if (TString(fTree->GetListOfFriends()->At(ifriend)->ClassName()) != "TFriendElement") continue; // no friendElement found
1648          TFriendElement *friendElement = (TFriendElement*)fTree->GetListOfFriends()->At(ifriend);
1649          if (friendElement->GetTree() == 0) continue; // no tree found in friendElement
1650          // printf("friend found \n");
1651          for (Int_t i = 0; i < friendElement->GetTree()->GetListOfBranches()->GetEntries(); i++) {
1652             // printf("iterating through friendelement entries, currently at %i\n", i);
1653             str = new TObjString(friendElement->GetTree()->GetListOfBranches()->At(i)->GetName());
1654             str->String().ReplaceAll("_Median", "");
1655             str->String().ReplaceAll("_Mean", "");
1656             str->String().ReplaceAll("_RMS", "");
1657             str->String().ReplaceAll("_LTM", "");
1658             str->String().ReplaceAll("_OutlierCutted", "");
1659             str->String().ReplaceAll(".", "");
1660             if (!(str->String() == "channel" || str->String() == "gx" || str->String() == "gy" || 
1661                   str->String() == "lx" || str->String() == "ly" || str->String() == "pad" || 
1662                   str->String() == "row" || str->String() == "rpad" || str->String() == "sector"  )){
1663                // insert "<friendName>." at the beginning: (<friendName> is per default "R")
1664                str->String().Insert(0, ".");
1665                str->String().Insert(0, friendElement->GetName());
1666                if (!arr->FindObject(str)) arr->Add(str);
1667                // printf("added string %s \n", str->String().Data());
1668             }
1669          }
1670       }
1671    } // if (fTree->GetListOfFriends())
1672    
1673    arr->Sort();
1674 //   ((TFriendElement*)gui->GetViewer()->GetTree()->GetListOfFriends()->At(0))->GetTree()->GetListOfBranches()->At(0)->GetName()
1675 // ((TFriendElement*)gui->GetViewer()->GetTree()->GetListOfFriends()->At(0))->GetTree()->GetListOfBranches()
1676
1677
1678    if (printList) {
1679       TIterator* iter = arr->MakeIterator();
1680       iter->Reset();
1681       TObjString* currentStr = 0;
1682       while ( (currentStr = (TObjString*)(iter->Next())) ) {
1683          std::cout << currentStr->GetString().Data() << std::endl;
1684       }
1685       delete iter;
1686    }
1687    return arr;
1688 }
1689
1690
1691 TObjArray* AliTPCCalibViewer::GetListOfNormalizationVariables(Bool_t printList) const{
1692   //
1693   // produces a list of available variables for normalization in the tree
1694   // printList: print the list to the screen, after the scan is done
1695   //
1696    TObjArray* arr = new TObjArray();
1697    arr->Add(new TObjString("_Mean"));
1698    arr->Add(new TObjString("_Mean_OutlierCutted"));
1699    arr->Add(new TObjString("_Median"));
1700    arr->Add(new TObjString("_Median_OutlierCutted"));
1701    arr->Add(new TObjString("_LTM"));
1702    arr->Add(new TObjString("_LTM_OutlierCutted"));
1703    arr->Add(new TObjString(Form("LFitIntern_4_8%s", fAppendString.Data())));
1704    arr->Add(new TObjString(Form("GFitIntern_Lin%s", fAppendString.Data())));
1705    arr->Add(new TObjString(Form("GFitIntern_Par%s", fAppendString.Data())));
1706    arr->Add(new TObjString("FitLinLocal"));
1707    arr->Add(new TObjString("FitLinGlobal"));
1708    arr->Add(new TObjString("FitParLocal"));
1709    arr->Add(new TObjString("FitParGlobal"));
1710
1711    if (printList) {
1712       TIterator* iter = arr->MakeIterator();
1713       iter->Reset();
1714       TObjString* currentStr = 0;
1715       while ((currentStr = (TObjString*)(iter->Next()))) {
1716          std::cout << currentStr->GetString().Data() << std::endl;
1717       }
1718       delete iter;
1719    }
1720    return arr;
1721 }
1722
1723
1724 TFriendElement* AliTPCCalibViewer::AddReferenceTree(const char* filename, const char* treename, const char* refname){
1725   //
1726   // add a reference tree to the current tree
1727   // by default the treename is 'calPads' and the reference treename is 'R'
1728   //
1729    TFile *file = new TFile(filename);
1730    fListOfObjectsToBeDeleted->Add(file);
1731    TTree * tree = (TTree*)file->Get(treename);
1732    return AddFriend(tree, refname);
1733 }
1734
1735
1736 TObjArray* AliTPCCalibViewer::GetArrayOfCalPads(){
1737   //
1738   // Returns a TObjArray with all AliTPCCalPads that are stored in the tree
1739   //  - this takes a while - 
1740   //
1741    TObjArray *listOfCalPads = GetListOfVariables();
1742    TObjArray *calPadsArray = new TObjArray();
1743    Int_t numberOfCalPads = listOfCalPads->GetEntries();
1744    for (Int_t i = 0; i < numberOfCalPads; i++) {
1745       std::cout << "Creating calPad " << (i+1) << " of " << numberOfCalPads << "\r" << std::flush;
1746       char* calPadName = (char*)((TObjString*)(listOfCalPads->At(i)))->GetString().Data();
1747       TString drawCommand = ((TObjString*)(listOfCalPads->At(i)))->GetString();
1748       drawCommand.Append(fAbbreviation.Data());
1749       AliTPCCalPad* calPad = GetCalPad(drawCommand.Data(), "", calPadName); 
1750       calPadsArray->Add(calPad); 
1751    }
1752    std::cout << std::endl;
1753    listOfCalPads->Delete();
1754    delete listOfCalPads;
1755    return calPadsArray;
1756 }
1757
1758
1759 TString* AliTPCCalibViewer::Fit(const char* drawCommand, const char* formula, const char* cuts, Double_t & chi2, TVectorD &fitParam, TMatrixD &covMatrix){
1760    //
1761    // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
1762    // returns chi2, fitParam and covMatrix
1763    // returns TString with fitted formula
1764    //
1765    
1766    TString formulaStr(formula); 
1767    TString drawStr(drawCommand);
1768    TString cutStr(cuts);
1769    
1770    // abbreviations:
1771    drawStr.ReplaceAll(fAbbreviation, fAppendString);
1772    cutStr.ReplaceAll(fAbbreviation, fAppendString);
1773    formulaStr.ReplaceAll(fAbbreviation, fAppendString);
1774    
1775    formulaStr.ReplaceAll("++", fAbbreviation);
1776    TObjArray* formulaTokens = formulaStr.Tokenize(fAbbreviation.Data()); 
1777    Int_t dim = formulaTokens->GetEntriesFast();
1778    
1779    fitParam.ResizeTo(dim);
1780    covMatrix.ResizeTo(dim,dim);
1781    
1782    TLinearFitter* fitter = new TLinearFitter(dim+1, Form("hyp%d",dim));
1783    fitter->StoreData(kTRUE);   
1784    fitter->ClearPoints();
1785    
1786    Int_t entries = Draw(drawStr.Data(), cutStr.Data(), "goff");
1787    if (entries == -1) return new TString("An ERROR has occured during fitting!");
1788    Double_t **values = new Double_t*[dim+1] ; 
1789    
1790    for (Int_t i = 0; i < dim + 1; i++){
1791       Int_t centries = 0;
1792       if (i < dim) centries = fTree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff");
1793       else  centries = fTree->Draw(drawStr.Data(), cutStr.Data(), "goff");
1794       
1795       if (entries != centries) return new TString("An ERROR has occured during fitting!");
1796       values[i] = new Double_t[entries];
1797       memcpy(values[i],  fTree->GetV1(), entries*sizeof(Double_t)); 
1798    }
1799    
1800    // add points to the fitter
1801    for (Int_t i = 0; i < entries; i++){
1802       Double_t x[1000];
1803       for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
1804       fitter->AddPoint(x, values[dim][i], 1);
1805    }
1806
1807    fitter->Eval();
1808    fitter->GetParameters(fitParam);
1809    fitter->GetCovarianceMatrix(covMatrix);
1810    chi2 = fitter->GetChisquare();
1811    chi2 = chi2;
1812    
1813    TString *preturnFormula = new TString(Form("( %e+",fitParam[0])), &returnFormula = *preturnFormula;
1814    
1815    for (Int_t iparam = 0; iparam < dim; iparam++) {
1816      returnFormula.Append(Form("%s*(%e)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
1817      if (iparam < dim-1) returnFormula.Append("+");
1818    }
1819    returnFormula.Append(" )");
1820    delete formulaTokens;
1821    delete fitter;
1822    delete[] values;
1823    return preturnFormula;
1824 }
1825
1826
1827 void AliTPCCalibViewer::MakeTreeWithObjects(const char *fileName, const TObjArray *const array, const char * mapFileName) {
1828   //
1829   // Write tree with all available information
1830   // im mapFileName is speciefied, the Map information are also written to the tree
1831   // AliTPCCalPad-Objects are written directly to the tree, so that they can be accessd later on
1832   // (does not work!!!)
1833   //
1834    AliTPCROC* tpcROCinstance = AliTPCROC::Instance();
1835
1836    TObjArray* mapIROCs = 0;
1837    TObjArray* mapOROCs = 0;
1838    TVectorF *mapIROCArray = 0;
1839    TVectorF *mapOROCArray = 0;
1840    Int_t mapEntries = 0;
1841    TString* mapNames = 0;
1842    
1843    if (mapFileName) {
1844       TFile mapFile(mapFileName, "read");
1845       
1846       TList* listOfROCs = mapFile.GetListOfKeys();
1847       mapEntries = listOfROCs->GetEntries()/2;
1848       mapIROCs = new TObjArray(mapEntries*2);
1849       mapOROCs = new TObjArray(mapEntries*2);
1850       mapIROCArray = new TVectorF[mapEntries];
1851       mapOROCArray = new TVectorF[mapEntries];
1852       
1853       mapNames = new TString[mapEntries];
1854       for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
1855          TString rocName(((TKey*)(listOfROCs->At(ivalue*2)))->GetName());
1856          rocName.Remove(rocName.Length()-4, 4);
1857          mapIROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "IROC").Data()), ivalue);
1858          mapOROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "OROC").Data()), ivalue);
1859          mapNames[ivalue].Append(rocName);
1860       }
1861       
1862       for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
1863          mapIROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(0));
1864          mapOROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(36));
1865       
1866          for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(0); ichannel++)
1867             (mapIROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapIROCs->At(ivalue)))->GetValue(ichannel);
1868          for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(36); ichannel++)
1869             (mapOROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapOROCs->At(ivalue)))->GetValue(ichannel);
1870       }
1871
1872    } //  if (mapFileName)
1873   
1874    TTreeSRedirector cstream(fileName);
1875    Int_t arrayEntries = array->GetEntries();
1876    
1877    // Read names of AliTPCCalPads and save them in names[]
1878    TString* names = new TString[arrayEntries];
1879    for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
1880       names[ivalue].Append(((AliTPCCalPad*)array->At(ivalue))->GetName());
1881
1882    for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++) {
1883       
1884       TVectorF *vectorArray = new TVectorF[arrayEntries];
1885       for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
1886          vectorArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
1887             
1888       
1889       //
1890       // fill vectors of variable per pad
1891       //
1892       TVectorF *posArray = new TVectorF[8];
1893       for (Int_t ivalue = 0; ivalue < 8; ivalue++)
1894          posArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
1895
1896       Float_t posG[3] = {0};
1897       Float_t posL[3] = {0};
1898       Int_t ichannel = 0;
1899       for (UInt_t irow = 0; irow < tpcROCinstance->GetNRows(isector); irow++) {
1900          for (UInt_t ipad = 0; ipad < tpcROCinstance->GetNPads(isector, irow); ipad++) {
1901             tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
1902             tpcROCinstance->GetPositionGlobal(isector, irow, ipad, posG);
1903             posArray[0][ichannel] = irow;
1904             posArray[1][ichannel] = ipad;
1905             posArray[2][ichannel] = posL[0];
1906             posArray[3][ichannel] = posL[1];
1907             posArray[4][ichannel] = posG[0];
1908             posArray[5][ichannel] = posG[1];
1909             posArray[6][ichannel] = (Int_t)(ipad - (Double_t)(tpcROCinstance->GetNPads(isector, irow))/2);
1910             posArray[7][ichannel] = ichannel;
1911             
1912             // loop over array containing AliTPCCalPads
1913             for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
1914                AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
1915                AliTPCCalROC* calROC = calPad->GetCalROC(isector);
1916                if (calROC)
1917                   (vectorArray[ivalue])[ichannel] = calROC->GetValue(irow, ipad);
1918                else
1919                   (vectorArray[ivalue])[ichannel] = 0;
1920             }
1921             ichannel++;
1922          }
1923       }
1924       AliTPCCalROC dummyROC(0);
1925       for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
1926          AliTPCCalROC *roc = ((AliTPCCalPad*)array->At(ivalue))->GetCalROC(isector);
1927          if (!roc) roc = &dummyROC;
1928          cstream << "calPads" <<
1929             (Char_t*)((names[ivalue] + ".=").Data()) << &vectorArray[ivalue];
1930          cstream << "calPads" << 
1931             (Char_t*)((names[ivalue] + "Pad.=").Data()) << roc;
1932       }
1933
1934       if (mapFileName) {
1935          for  (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
1936             if (isector < 36)
1937                cstream << "calPads" <<
1938                   (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapIROCArray[ivalue];
1939             else
1940                cstream << "calPads" <<
1941                   (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapOROCArray[ivalue];
1942          }
1943       }
1944       
1945       cstream << "calPads" <<
1946          "sector=" << isector;
1947
1948       cstream << "calPads" <<
1949          "row.=" << &posArray[0] <<
1950          "pad.=" << &posArray[1] <<
1951          "lx.=" << &posArray[2] <<
1952          "ly.=" << &posArray[3] <<
1953          "gx.=" << &posArray[4] <<
1954          "gy.=" << &posArray[5] <<
1955          "rpad.=" << &posArray[6] <<
1956          "channel.=" << &posArray[7];
1957
1958       cstream << "calPads" <<
1959          "\n";
1960
1961       delete[] posArray;
1962       delete[] vectorArray;
1963    } //for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++)
1964
1965    delete[] names;
1966    if (mapFileName) {
1967       delete mapIROCs;
1968       delete mapOROCs;
1969       delete[] mapIROCArray;
1970       delete[] mapOROCArray;
1971       delete[] mapNames;
1972    }
1973 }
1974
1975
1976 void AliTPCCalibViewer::MakeTree(const char * fileName, TObjArray * array, const char * mapFileName, AliTPCCalPad *const outlierPad, Float_t ltmFraction) {
1977   //
1978   // Write a tree with all available information
1979   // if mapFileName is speciefied, the Map information are also written to the tree
1980   // pads specified in outlierPad are not used for calculating statistics
1981   // The following statistical information on the basis of a ROC are calculated: 
1982   // "_Median", "_Mean", "_LTM", "_RMS_LTM"
1983   // "_Median_OutlierCutted", "_Mean_OutlierCutted", "_RMS_OutlierCutted", "_LTM_OutlierCutted", "_RMS_LTM_OutlierCutted"
1984   // The following position variables are available:
1985   // "row", "pad", "lx", "ly", "gx", "gy", "rpad", "channel"
1986   // 
1987   // The tree out of this function is the basis for the AliTPCCalibViewer and the AliTPCCalibViewerGUI.
1988    
1989   AliTPCROC* tpcROCinstance = AliTPCROC::Instance();
1990
1991   TObjArray* mapIROCs = 0;
1992   TObjArray* mapOROCs = 0;
1993   TVectorF *mapIROCArray = 0;
1994   TVectorF *mapOROCArray = 0;
1995   Int_t mapEntries = 0;
1996   TString* mapNames = 0;
1997   
1998   if (mapFileName) {
1999     TFile mapFile(mapFileName, "read");
2000     
2001     TList* listOfROCs = mapFile.GetListOfKeys();
2002     mapEntries = listOfROCs->GetEntries()/2;
2003     mapIROCs = new TObjArray(mapEntries*2);
2004     mapOROCs = new TObjArray(mapEntries*2);
2005     mapIROCArray = new TVectorF[mapEntries];
2006     mapOROCArray = new TVectorF[mapEntries];
2007     
2008     mapNames = new TString[mapEntries];
2009     for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
2010       TString rocName(((TKey*)(listOfROCs->At(ivalue*2)))->GetName());
2011       rocName.Remove(rocName.Length()-4, 4);
2012       mapIROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "IROC").Data()), ivalue);
2013       mapOROCs->AddAt((AliTPCCalROC*)mapFile.Get((rocName + "OROC").Data()), ivalue);
2014       mapNames[ivalue].Append(rocName);
2015     }
2016     
2017     for (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
2018       mapIROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(0));
2019       mapOROCArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(36));
2020       
2021       for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(0); ichannel++)
2022         (mapIROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapIROCs->At(ivalue)))->GetValue(ichannel);
2023       for (UInt_t ichannel = 0; ichannel < tpcROCinstance->GetNChannels(36); ichannel++)
2024         (mapOROCArray[ivalue])[ichannel] = ((AliTPCCalROC*)(mapOROCs->At(ivalue)))->GetValue(ichannel);
2025     }
2026     
2027   } //  if (mapFileName)
2028   
2029   TTreeSRedirector cstream(fileName);
2030   Int_t arrayEntries = 0;
2031   if (array) arrayEntries = array->GetEntries();
2032   
2033   TString* names = new TString[arrayEntries];
2034   for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++)
2035     names[ivalue].Append(((AliTPCCalPad*)array->At(ivalue))->GetName());
2036   
2037   for (UInt_t isector = 0; isector < tpcROCinstance->GetNSectors(); isector++) {
2038       //
2039       // get statistic for given sector
2040       //
2041     TVectorF median(arrayEntries);
2042     TVectorF mean(arrayEntries);
2043     TVectorF rms(arrayEntries);
2044     TVectorF ltm(arrayEntries);
2045     TVectorF ltmrms(arrayEntries);
2046     TVectorF medianWithOut(arrayEntries);
2047     TVectorF meanWithOut(arrayEntries);
2048     TVectorF rmsWithOut(arrayEntries);
2049     TVectorF ltmWithOut(arrayEntries);
2050     TVectorF ltmrmsWithOut(arrayEntries);
2051     
2052     TVectorF *vectorArray = new TVectorF[arrayEntries];
2053     for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++){
2054       vectorArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
2055       vectorArray[ivalue].SetUniqueID(array->UncheckedAt(ivalue)->GetUniqueID());
2056 //       printf("UniqueID: %d\n",vectorArray[ivalue].GetUniqueID());
2057     }
2058     
2059     for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
2060       AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
2061       AliTPCCalROC* calROC = calPad->GetCalROC(isector);
2062       AliTPCCalROC* outlierROC = 0;
2063       if (outlierPad) outlierROC = outlierPad->GetCalROC(isector);
2064       if (calROC) {
2065         median[ivalue] = calROC->GetMedian();
2066         mean[ivalue] = calROC->GetMean();
2067         rms[ivalue] = calROC->GetRMS();
2068         Double_t ltmrmsValue = 0;
2069         ltm[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction);
2070         ltmrms[ivalue] = ltmrmsValue;
2071         if (outlierROC) {
2072           medianWithOut[ivalue] = calROC->GetMedian(outlierROC);
2073           meanWithOut[ivalue] = calROC->GetMean(outlierROC);
2074           rmsWithOut[ivalue] = calROC->GetRMS(outlierROC);
2075           ltmrmsValue = 0;
2076           ltmWithOut[ivalue] = calROC->GetLTM(&ltmrmsValue, ltmFraction, outlierROC);
2077           ltmrmsWithOut[ivalue] = ltmrmsValue;
2078         }
2079       }
2080       else {
2081         median[ivalue] = 0.;
2082         mean[ivalue] = 0.;
2083         rms[ivalue] = 0.;
2084         ltm[ivalue] = 0.;
2085         ltmrms[ivalue] = 0.;
2086         medianWithOut[ivalue] = 0.;
2087         meanWithOut[ivalue] = 0.;
2088         rmsWithOut[ivalue] = 0.;
2089         ltmWithOut[ivalue] = 0.;
2090         ltmrmsWithOut[ivalue] = 0.;
2091       }
2092     }
2093     
2094       //
2095       // fill vectors of variable per pad
2096       //
2097     TVectorF *posArray = new TVectorF[8];
2098     for (Int_t ivalue = 0; ivalue < 8; ivalue++)
2099       posArray[ivalue].ResizeTo(tpcROCinstance->GetNChannels(isector));
2100     
2101     Float_t posG[3] = {0};
2102     Float_t posL[3] = {0};
2103     Int_t ichannel = 0;
2104     for (UInt_t irow = 0; irow < tpcROCinstance->GetNRows(isector); irow++) {
2105       for (UInt_t ipad = 0; ipad < tpcROCinstance->GetNPads(isector, irow); ipad++) {
2106         tpcROCinstance->GetPositionLocal(isector, irow, ipad, posL);
2107         tpcROCinstance->GetPositionGlobal(isector, irow, ipad, posG);
2108         posArray[0][ichannel] = irow;
2109         posArray[1][ichannel] = ipad;
2110         posArray[2][ichannel] = posL[0];
2111         posArray[3][ichannel] = posL[1];
2112         posArray[4][ichannel] = posG[0];
2113         posArray[5][ichannel] = posG[1];
2114         posArray[6][ichannel] = (Int_t)(ipad - (Double_t)(tpcROCinstance->GetNPads(isector, irow))/2);
2115         posArray[7][ichannel] = ichannel;
2116         
2117             // loop over array containing AliTPCCalPads
2118         for (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
2119           AliTPCCalPad* calPad = (AliTPCCalPad*) array->At(ivalue);
2120           AliTPCCalROC* calROC = calPad->GetCalROC(isector);
2121                 if (calROC)
2122                   (vectorArray[ivalue])[ichannel] = calROC->GetValue(irow, ipad);
2123           else
2124             (vectorArray[ivalue])[ichannel] = 0;
2125         }
2126         ichannel++;
2127       }
2128     }
2129     
2130     cstream << "calPads" <<
2131       "sector=" << isector;
2132     
2133     for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
2134       cstream << "calPads" <<
2135         (Char_t*)((names[ivalue] + "_Median=").Data()) << median[ivalue] <<
2136         (Char_t*)((names[ivalue] + "_Mean=").Data()) << mean[ivalue] <<
2137         (Char_t*)((names[ivalue] + "_RMS=").Data()) << rms[ivalue] <<
2138         (Char_t*)((names[ivalue] + "_LTM=").Data()) << ltm[ivalue] <<
2139         (Char_t*)((names[ivalue] + "_RMS_LTM=").Data()) << ltmrms[ivalue];
2140       if (outlierPad) {
2141         cstream << "calPads" <<
2142           (Char_t*)((names[ivalue] + "_Median_OutlierCutted=").Data()) << medianWithOut[ivalue] <<
2143           (Char_t*)((names[ivalue] + "_Mean_OutlierCutted=").Data()) << meanWithOut[ivalue] <<
2144           (Char_t*)((names[ivalue] + "_RMS_OutlierCutted=").Data()) << rmsWithOut[ivalue] <<
2145           (Char_t*)((names[ivalue] + "_LTM_OutlierCutted=").Data()) << ltmWithOut[ivalue] <<
2146           (Char_t*)((names[ivalue] + "_RMS_LTM_OutlierCutted=").Data()) << ltmrmsWithOut[ivalue];
2147       }
2148         //timestamp and run, if given in title
2149 /*      TString title(((AliTPCCalPad*) array->At(ivalue))->GetTitle());
2150       TObjArray *arrtitle=title.Tokenize(",");
2151       Int_t run=-1;
2152       UInt_t time=0;
2153       TIter next(arrtitle);
2154       TObject *o=0;
2155       while ( (o=next()) ){
2156         TString &entry=((TObjString*)o)->GetString();
2157         entry.Remove(TString::kBoth,' ');
2158         entry.Remove(TString::kBoth,'\t');
2159         if (entry.BeginsWith("Run:")) {
2160           run=entry(4,entry.Length()).Atoi();
2161         } else if (entry.BeginsWith("Time:")) {
2162           time=entry(6,entry.Length()).Atoi();
2163         }
2164       }
2165       delete arrtitle;*/
2166       
2167     }
2168     
2169     for  (Int_t ivalue = 0; ivalue < arrayEntries; ivalue++) {
2170       cstream << "calPads" <<
2171         (Char_t*)((names[ivalue] + ".=").Data()) << &vectorArray[ivalue];
2172     }
2173     
2174     if (mapFileName) {
2175           for  (Int_t ivalue = 0; ivalue < mapEntries; ivalue++) {
2176             if (isector < 36)
2177               cstream << "calPads" <<
2178               (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapIROCArray[ivalue];
2179             else
2180                 cstream << "calPads" <<
2181               (Char_t*)((mapNames[ivalue] + ".=").Data()) << &mapOROCArray[ivalue];
2182          }
2183     }
2184
2185     cstream << "calPads" <<
2186       "row.=" << &posArray[0] <<
2187       "pad.=" << &posArray[1] <<
2188       "lx.=" << &posArray[2] <<
2189       "ly.=" << &posArray[3] <<
2190       "gx.=" << &posArray[4] <<
2191       "gy.=" << &posArray[5] <<
2192       "rpad.=" << &posArray[6] <<
2193       "channel.=" << &posArray[7];
2194     
2195     cstream << "calPads" <<
2196       "\n";
2197     
2198     delete[] posArray;
2199     delete[] vectorArray;
2200   }
2201   
2202   
2203   delete[] names;
2204   if (mapFileName) {
2205     delete mapIROCs;
2206     delete mapOROCs;
2207     delete[] mapIROCArray;
2208     delete[] mapOROCArray;
2209     delete[] mapNames;
2210   }
2211 }
2212
2213
2214 void AliTPCCalibViewer::MakeTree(const char *outPutFileName, const Char_t *inputFileName, AliTPCCalPad *outlierPad, Float_t ltmFraction, const char *mapFileName ){
2215    // 
2216    // Function to create a calibration Tree with all available information.
2217    // See also documentation to MakeTree   
2218    // the file "inputFileName" must be in the following format (see also CreateObjectList):
2219    // (each colum separated by tabs, "dependingOnType" can have 2 or 3 colums)
2220    // 
2221    // type      path    dependingOnType
2222    // 
2223    // type == "CE":
2224    // dependingOnType = CETmean CEQmean CETrms
2225    // 
2226    // type == "Pulser":
2227    // dependingOnType = PulserTmean     PulsterQmean    PulserTrms
2228    // 
2229    // type == "Pedestals":
2230    // dependingOnType = Pedestals       Noise
2231    // 
2232    // type == "CalPad":
2233    // dependingOnType = NameInFile      NameToWriteToFile
2234    // 
2235    // 
2236    TObjArray objArray;
2237    CreateObjectList(inputFileName, &objArray);
2238    MakeTree(outPutFileName, &objArray, mapFileName, outlierPad, ltmFraction);   
2239 }
2240
2241
2242 void AliTPCCalibViewer::CreateObjectList(const Char_t *filename, TObjArray *calibObjects){
2243    // 
2244    // Function to create a TObjArray out of a given file
2245    // the file must be in the following format:
2246    // (each colum separated by tabs, "dependingOnType" can have 2 or 3 colums)
2247    // 
2248    // 
2249    // type      path    dependingOnType
2250    // 
2251    // type == "CE":
2252    // dependingOnType = CETmean CEQmean CETrms
2253    // 
2254    // type == "Pulser":
2255    // dependingOnType = PulserTmean     PulsterQmean    PulserTrms
2256    // 
2257    // type == "Pedestals":
2258    // dependingOnType = Pedestals       Noise
2259    // 
2260    // type == "CalPad":
2261    // dependingOnType = NameInFile      NameToWriteToFile
2262    // 
2263    // 
2264    // 
2265    if ( calibObjects == 0x0 ) return;
2266    ifstream in;
2267    in.open(filename);
2268    if ( !in.is_open() ){
2269       fprintf(stderr,"Error: cannot open list file '%s'", filename);
2270       return;
2271    }
2272    
2273    AliTPCCalPad *calPad=0x0;
2274    
2275    TString sFile;
2276    sFile.ReadFile(in);
2277    in.close();
2278    
2279    TObjArray *arrFileLine = sFile.Tokenize("\n");
2280    TIter nextLine(arrFileLine);
2281    
2282    TObjString *sObjLine = 0x0;
2283    while ( (sObjLine = (TObjString*)nextLine()) ){
2284       TString sLine(sObjLine->GetString());
2285       
2286       TObjArray *arrCol = sLine.Tokenize("\t");
2287       Int_t nCols = arrCol->GetEntriesFast();
2288       
2289       TObjString *sObjType     = (TObjString*)(arrCol->At(0));
2290       TObjString *sObjFileName = (TObjString*)(arrCol->At(1));
2291       TObjString *sObjName = 0x0;
2292       
2293       if ( !sObjType || !sObjFileName ) continue;
2294       TString sType(sObjType->GetString());
2295       TString sFileName(sObjFileName->GetString());
2296       printf("Type %s, opening %s \n", sType.Data(), sFileName.Data());
2297       TFile *fIn = TFile::Open(sFileName);
2298       if ( !fIn ){
2299          fprintf(stderr,"File not found: '%s'", sFileName.Data());
2300          continue;
2301       }
2302       
2303       if ( sType == "CE" ){  // next three colums are the names for CETmean, CEQmean and CETrms
2304          AliTPCCalibCE *ce = (AliTPCCalibCE*)fIn->Get("AliTPCCalibCE");
2305          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadT0());         
2306          if (nCols > 2) {  // check, if the name is provided
2307             sObjName = (TObjString*)(arrCol->At(2));
2308             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2309          }
2310          else calPad->SetNameTitle("CETmean","CETmean");
2311          calibObjects->Add(calPad);
2312          
2313          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadQ());         
2314          if (nCols > 3) {  // check, if the name is provided
2315             sObjName = (TObjString*)(arrCol->At(3));
2316             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2317          }
2318          else calPad->SetNameTitle("CEQmean","CEQmean");
2319          calibObjects->Add(calPad);        
2320          
2321          calPad = new AliTPCCalPad((TObjArray*)ce->GetCalPadRMS());
2322          if (nCols > 4) {  // check, if the name is provided
2323             sObjName = (TObjString*)(arrCol->At(4));
2324             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2325          }
2326          else calPad->SetNameTitle("CETrms","CETrms");
2327          calibObjects->Add(calPad);         
2328                   
2329       } else if ( sType == "Pulser") {
2330          AliTPCCalibPulser *sig = (AliTPCCalibPulser*)fIn->Get("AliTPCCalibPulser");
2331          
2332          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadT0());         
2333          if (nCols > 2) {
2334             sObjName = (TObjString*)(arrCol->At(2));
2335             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2336          }
2337          else calPad->SetNameTitle("PulserTmean","PulserTmean");
2338          calibObjects->Add(calPad);
2339          
2340          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadQ());         
2341          if (nCols > 3) {
2342             sObjName = (TObjString*)(arrCol->At(3));
2343             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2344          }
2345          else calPad->SetNameTitle("PulserQmean","PulserQmean");
2346          calibObjects->Add(calPad);        
2347          
2348          calPad = new AliTPCCalPad((TObjArray*)sig->GetCalPadRMS());
2349          if (nCols > 4) {
2350             sObjName = (TObjString*)(arrCol->At(4));
2351             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2352          }
2353          else calPad->SetNameTitle("PulserTrms","PulserTrms");
2354          calibObjects->Add(calPad);         
2355       
2356       } else if ( sType == "Pedestals") {
2357          AliTPCCalibPedestal *ped = (AliTPCCalibPedestal*)fIn->Get("AliTPCCalibPedestal");
2358          
2359          calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadPedestal());         
2360          if (nCols > 2) {
2361             sObjName = (TObjString*)(arrCol->At(2));
2362             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2363          }
2364          else calPad->SetNameTitle("Pedestals","Pedestals");
2365          calibObjects->Add(calPad);
2366          
2367          calPad = new AliTPCCalPad((TObjArray*)ped->GetCalPadRMS());         
2368          if (nCols > 3) {
2369             sObjName = (TObjString*)(arrCol->At(3));
2370             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2371          }
2372          else calPad->SetNameTitle("Noise","Noise");
2373          calibObjects->Add(calPad);        
2374      
2375       } else if ( sType == "CalPad") {
2376          if (nCols > 2) sObjName = (TObjString*)(arrCol->At(2));
2377          else continue;
2378          calPad = new AliTPCCalPad(*(AliTPCCalPad*)fIn->Get(sObjName->GetString().Data()));
2379          if (nCols > 3) {
2380             sObjName = (TObjString*)(arrCol->At(3));
2381             calPad->SetNameTitle(sObjName->GetString().Data(), sObjName->GetString().Data());
2382          }
2383          calibObjects->Add(calPad);
2384       } else {
2385          fprintf(stderr,"Undefined Type: '%s'",sType.Data());
2386       }
2387       delete fIn;
2388    }
2389 }
2390
2391
2392