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