]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCCalibViewerGUItime.cxx
ATO-98 -adding additonal distortion/correction to the list of primitive/derivative...
[u/mrichter/AliRoot.git] / TPC / AliTPCCalibViewerGUItime.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 //                                                                           //
20 ///////////////////////////////////////////////////////////////////////////////
21
22
23 #include <iostream>
24 //Root includes
25 #include <TROOT.h>
26 #include <TDirectory.h>
27 #include <TStyle.h>
28 #include <TCanvas.h>
29 #include <TPad.h>
30 #include <TVirtualPad.h>
31 #include <TObject.h>
32 #include <TObjArray.h>
33 #include <TObjString.h>
34 #include <TSystem.h>
35 #include <TVector.h>
36 #include <TH1.h>
37 #include <TCut.h>
38 #include <TFile.h>
39 #include <TTree.h>
40 #include <TChain.h>
41 #include <TBranch.h>
42 #include <TIterator.h>
43 #include <TGraph.h>
44 #include <TAxis.h>
45 #include <TTimeStamp.h>
46 #include <TMath.h>
47 #include <TMap.h>
48 //
49 #include <TGFileDialog.h>
50 #include <TGInputDialog.h>
51 //
52 #include <TGButton.h>
53 #include <TGListBox.h>
54 #include <TGComboBox.h>
55 #include <TGNumberEntry.h>
56 #include <TGLayout.h>
57 #include <TRootEmbeddedCanvas.h>
58 #include <TGSplitter.h>
59 #include <TGButtonGroup.h>
60 #include <TGLabel.h>
61 #include <TGTab.h>
62 #include <TGString.h>
63
64 //AliRoot includes
65 #include <AliLog.h>
66 #include "AliTPCCalibViewerGUI.h"
67 #include "AliTPCCalibViewer.h"
68 #include "AliTPCcalibDB.h"
69 #include "AliTPCcalibDButil.h"
70 #include "AliTPCConfigParser.h"
71
72 #include "AliTPCCalibViewerGUItime.h"
73
74 ClassImp(AliTPCCalibViewerGUItime)
75
76 AliTPCCalibViewerGUItime::AliTPCCalibViewerGUItime(const TGWindow *p, UInt_t w, UInt_t h) :
77 TGCompositeFrame(p,w,h),
78   fFile(0x0),
79   fTree(0x0),
80   fCalibViewerGUI(0x0),
81   fCalibViewerGUItab(0x0),
82   fCurrentHist(0x0),
83   fCurrentGraph(0x0),
84   fCurrentRunDetails(-1),
85   fOutputCacheDir("/tmp"),
86   fDrawString(""),
87   fConfigFile(""),
88   fConfigParser(new AliTPCConfigParser),
89   fIsCustomDraw(kFALSE),
90   fRunNumbers(10),
91   fTimeStamps(10),
92   fValuesX(10),
93   fValuesY(10),
94   fNoGraph(kFALSE),
95   fGraphLimitEntries(10000),
96   fMapRefTrees(new TMap),
97   //GUI elements
98   //main canvas Top part, bottom part
99   fContTopBottom(0x0),
100   //top left, centre, right
101   fContLCR(0x0),
102   //content left
103   fContLeft(0x0),
104   fContDrawOpt(0x0),
105   fChkDrawOptSame(0x0),
106   fChkDrawOptSparse(0x0),
107   fComboAddDrawOpt(0x0),
108   fContDrawSel(0x0),
109   fContDrawSelSubRunTime(0x0),
110   fRadioXhist(0x0),
111   fRadioXrun(0x0),
112   fRadioXtime(0x0),
113   fListVariables(0x0),
114   fComboRunType(0x0),
115   fLblRunType(0x0),
116   fNmbPar(0x0),
117   fLblPar(0x0),
118   fListCalibType(0x0),
119   fContCalibType(0x0),
120   //content centre
121   fContCenter(0x0),
122   fCanvMain(0x0),
123   //content right
124   fContRight(0x0),
125   fContValues(0x0),
126   fLblRunNumber(0x0),
127   fLblRunTime(0x0),
128   fLblValueX(0x0),
129   fLblValueY(0x0),
130   fLblRunNumberVal(0x0),
131   fLblRunTimeVal(0x0),
132   fLblValueXVal(0x0),
133   fLblValueYVal(0x0),
134   fBtnDumpRuns(0x0),
135   fContAliases(0x0),
136   fListAliases(0x0),
137   //content bottom
138   fContCustom(0x0),
139   fContCustomCuts(0x0),
140   fLblCustomDraw(0x0),
141   fLblCustomCuts(0x0),
142   fComboCustomDraw(0x0),
143   fComboCustomCuts(0x0),
144   fTrashBox(new TObjArray)
145 {
146   //
147   // ctor
148   //
149   fMapRefTrees->SetOwnerKeyValue();
150   fTrashBox->SetOwner();
151   DrawGUI(p,w,h);
152   gStyle->SetMarkerStyle(20);
153   gStyle->SetMarkerSize(0.5);
154   SetInitialValues();
155 }
156 //______________________________________________________________________________
157 AliTPCCalibViewerGUItime::~AliTPCCalibViewerGUItime(){
158   //
159   // dtor
160   //
161   delete fConfigParser;
162   delete fTrashBox;
163   delete fMapRefTrees;
164 }
165 //______________________________________________________________________________
166 void AliTPCCalibViewerGUItime::DrawGUI(const TGWindow */*p*/, UInt_t w, UInt_t h) {
167    //
168    // draw the GUI
169    //
170    // ======================================================================
171    // ************************* Display everything *************************
172    // ======================================================================
173   
174   SetCleanup(kDeepCleanup);
175   
176    // *****************************************************************************
177    // ************************* content of this MainFrame *************************
178    // *****************************************************************************
179    // top level container with horizontal layout
180   fContTopBottom = new TGCompositeFrame(this, w, h, kVerticalFrame | kFixedWidth | kFixedHeight);
181   AddFrame(fContTopBottom, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
182   
183   fContLCR = new TGCompositeFrame(fContTopBottom, w, h, kHorizontalFrame | kFixedWidth | kFixedHeight);
184   fContTopBottom->AddFrame(fContLCR, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
185   
186    // ***********************************************************************
187    // ************************* content of fContLCR *************************
188    // ***********************************************************************
189    // left container
190   fContLeft = new TGCompositeFrame(fContLCR, 200, 200, kVerticalFrame | kFixedWidth | kFitHeight);
191   fContLCR->AddFrame(fContLeft, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandY, 5, 3, 3, 3));
192   
193    // left vertical splitter
194   TGVSplitter *splitLeft = new TGVSplitter(fContLCR);
195   splitLeft->SetFrame(fContLeft, kTRUE);
196   fContLCR->AddFrame(splitLeft, new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 0, 0, 0, 0));
197   
198    // right container
199   fContRight = new TGCompositeFrame(fContLCR, 150, 200, kVerticalFrame | kFixedWidth | kFitHeight);
200   fContLCR->AddFrame(fContRight, new TGLayoutHints(kLHintsTop | kLHintsRight | kLHintsExpandY, 3, 5, 3, 3));
201   
202    // center container
203   fContCenter = new TGCompositeFrame(fContLCR, 200, 200, kVerticalFrame | kFixedWidth | kFitHeight);
204   fContLCR->AddFrame(fContCenter, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
205   
206    // right vertical splitter
207   TGVSplitter *splitRight = new TGVSplitter(fContLCR);
208   splitRight->SetFrame(fContRight, kFALSE);
209   fContLCR->AddFrame(splitRight, new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 0, 0, 0, 0));
210   
211   
212    // ========================================================================
213    // ************************* content of fContLeft *************************
214    // ========================================================================
215    // --- draw button and tabLeft ---
216   // draw options
217   fContDrawOpt = new TGGroupFrame(fContLeft, "Draw options", kVerticalFrame | kFitWidth | kFitHeight);
218   fContLeft->AddFrame(fContDrawOpt, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
219
220   TGCompositeFrame *cfr = new TGCompositeFrame(fContDrawOpt, 200, 23, kHorizontalFrame | kFitWidth | kFixedHeight);
221   fContDrawOpt->AddFrame(cfr, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX , 0, 0, 0, 0));
222   
223   fChkDrawOptSame = new TGCheckButton(cfr, "Same");
224   cfr->AddFrame(fChkDrawOptSame, new TGLayoutHints(kLHintsNormal, 0, 2, 0, 0));
225   fChkDrawOptSame->SetToolTipText("Add draw option 'same'");
226
227   fChkDrawOptSparse = new TGCheckButton(cfr, "Sparse");
228   cfr->AddFrame(fChkDrawOptSparse, new TGLayoutHints(kLHintsNormal, 0, 2, 0, 0));
229   fChkDrawOptSparse->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "DoNewSelection()");
230   fChkDrawOptSparse->SetToolTipText("In case of run trending only plot runs with information");
231   
232   // additional draw options combo box
233   fComboAddDrawOpt = new TGComboBox(fContDrawOpt);
234   fComboAddDrawOpt->Resize(0, 22);
235   fComboAddDrawOpt->EnableTextInput(kTRUE);
236   fContDrawOpt->AddFrame(fComboAddDrawOpt, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
237 //   fComboAddDrawOpt->Connect("ReturnPressed()", "AliTPCCalibViewerGUI", this, "HandleButtonsGeneral(=14)");
238 //   fComboAddDrawOpt->Connect("Selected(Int_t)", "AliTPCCalibViewerGUI", this, "DoNewSelection()");
239   fComboAddDrawOpt->GetTextEntry()->SetText("",kFALSE);
240   
241   // draw selection group
242   fContDrawSel = new TGGroupFrame(fContLeft, "Draw selection", kVerticalFrame | kFitWidth | kFitHeight);
243   fContLeft->AddFrame(fContDrawSel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 10, 0));
244   //x-axis variables selection, Run of Time
245   fContDrawSelSubRunTime = new TGCompositeFrame(fContDrawSel, 200, 23, kHorizontalFrame | kFitWidth | kFixedHeight);
246   fContDrawSel->AddFrame(fContDrawSelSubRunTime, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX , 0, 0, 0, 0));
247   
248   // ------------------------- content of fContDrawOpt -------------------------
249   // 
250   // Run radio button
251     // Time radio button
252   fRadioXhist = new TGRadioButton(fContDrawSelSubRunTime, "hist", kRadioXhist);
253   fContDrawSelSubRunTime->AddFrame(fRadioXhist, new TGLayoutHints(kLHintsNormal, 0, 2, 0, 0));
254   fRadioXhist->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "HandleButtonsDrawSel()");
255   fRadioXhist->SetToolTipText("Draw the distribution of the variable");
256   
257   fRadioXrun = new TGRadioButton(fContDrawSelSubRunTime, ":Run", kRadioXrun);
258   fContDrawSelSubRunTime->AddFrame(fRadioXrun, new TGLayoutHints(kLHintsNormal, 2, 2, 0, 0));
259   fRadioXrun->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "HandleButtonsDrawSel()");
260   fRadioXrun->SetToolTipText("Use run number as x-value");
261   
262   // Time radio button
263   fRadioXtime = new TGRadioButton(fContDrawSelSubRunTime, ":Time", kRadioXtime);
264   fContDrawSelSubRunTime->AddFrame(fRadioXtime, new TGLayoutHints(kLHintsNormal, 2, 2, 0, 0));
265   fRadioXtime->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "HandleButtonsDrawSel()");
266   fRadioXtime->SetToolTipText("Use time stamp number as x-value");
267   
268   
269   // list of variables
270   fListVariables = new TGListBox(fContDrawSel);
271   fContDrawSel->AddFrame(fListVariables, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
272   fListVariables->Connect("Selected(Int_t)", "AliTPCCalibViewerGUItime", this, "DoNewSelection()");
273
274   
275 //-------------------- run type selection ------------------------
276   // Parameter label
277   fLblRunType = new TGLabel(fContDrawSel, "Run Type:");
278   fLblRunType->SetTextJustify(kTextLeft);
279   fContDrawSel->AddFrame(fLblRunType, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
280   
281   fComboRunType = new TGComboBox(fContDrawSel);
282   fComboRunType->EnableTextInput(kFALSE);
283   fContDrawSel->AddFrame(fComboRunType, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
284   fComboRunType->Connect("Selected(Int_t)", "AliTPCCalibViewerGUItime", this, "DoDraw()");
285 //   fComboRunType->SetEnabled(kFALSE);
286   fComboRunType->Resize(0, 22);
287   
288   //-------------------- parameter selection ------------------------
289   // Parameter label
290   fLblPar = new TGLabel(fContDrawSel, "Parameter:");
291   fLblPar->SetTextJustify(kTextLeft);
292   fContDrawSel->AddFrame(fLblPar, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
293   
294   fNmbPar = new TGNumberEntry(fContDrawSel, 0, 1, -1, TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative, TGNumberFormat::kNELLimitMinMax, 0, 71);
295   fContDrawSel->AddFrame(fNmbPar, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
296   fNmbPar->Connect("ValueSet(Long_t)", "AliTPCCalibViewerGUItime", this, "DoParLimitChange()");
297   fNmbPar->SetState(kFALSE);
298   
299   //-------------------- calibration type selection ------------------------
300   // label
301   // draw selection group
302   fContCalibType = new TGGroupFrame(fContLeft, "Calib type selection", kVerticalFrame | kFitWidth | kFitHeight);
303   fContLeft->AddFrame(fContCalibType, new TGLayoutHints(kLHintsExpandX , 0, 0, 10, 0));
304     
305     // list of variables
306   fListCalibType = new TGListBox(fContCalibType);
307   fContCalibType->AddFrame(fListCalibType, new TGLayoutHints(kLHintsNormal | kLHintsExpandX , 0, 0, 0, 0));
308   fListCalibType->Connect("Selected(Int_t)", "AliTPCCalibViewerGUItime", this, "DoChangeSelectionList()");
309   fListCalibType->Resize(0,88);
310   fListCalibType->SetMultipleSelections();
311   
312   
313      // ==========================================================================
314    // ************************* content of fContCenter *************************
315    // ========================================================================
316    // main drawing canvas
317   fCanvMain = new TRootEmbeddedCanvas("GUItime_Canvas", fContCenter, 200, 200, kFitWidth | kFitHeight);
318   fContCenter->AddFrame(fCanvMain, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
319   fCanvMain->GetCanvas()->Connect("ProcessedEvent(Int_t, Int_t, Int_t, TObject*)", "AliTPCCalibViewerGUItime", this, "MouseMove(Int_t, Int_t, Int_t, TObject*)");
320 //   fCanvMain->GetCanvas()->Connect("RangeAxisChanged()", "AliTPCCalibViewerGUItime", this, "GetMinMax()");
321   fCanvMain->GetCanvas()->SetToolTipText("The Main_Canvas, here your plots are displayed.");
322   fCanvMain->GetCanvas()->SetRightMargin(0.062);
323   fCanvMain->GetCanvas()->SetLeftMargin(0.15);
324   
325    // =========================================================================
326    // ************************* content of fContRight *************************
327    // ========================================================================
328   //group frame for value information
329   fContValues = new TGGroupFrame(fContRight, "Data point info", kVerticalFrame | kFitWidth | kFitHeight);
330   fContRight->AddFrame(fContValues, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
331   //set layout manager
332   fContValues->SetLayoutManager(new TGMatrixLayout(fContValues, 0, 2, 5));
333   //value information labels
334
335   //run number label
336   fLblRunNumber = new TGLabel(fContValues, "Run:");
337   fLblRunNumber->SetTextJustify(kTextLeft);
338   fContValues->AddFrame(fLblRunNumber, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
339   //run number
340   fLblRunNumberVal = new TGLabel(fContValues, "000000");
341   fLblRunNumberVal->SetTextJustify(kTextLeft);
342   fContValues->AddFrame(fLblRunNumberVal, new TGLayoutHints(kLHintsLeft | kLHintsExpandX, 0, 0, 0, 0));
343   //time stamp label
344   fLblRunTime = new TGLabel(fContValues, "Time:");
345   fLblRunTime->SetTextJustify(kTextLeft);
346   fContValues->AddFrame(fLblRunTime, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
347   //run number
348   fLblRunTimeVal = new TGLabel(fContValues, "00.00.0000\n00:00:00");
349   fLblRunTimeVal->SetTextJustify(kTextLeft);
350   fContValues->AddFrame(fLblRunTimeVal, new TGLayoutHints(kLHintsLeft | kLHintsExpandX, 0, 0, 0, 0));
351   //value label x
352   fLblValueX = new TGLabel(fContValues, "x-Value:");
353   fLblValueX->SetTextJustify(kTextLeft);
354   fContValues->AddFrame(fLblValueX, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
355   //value x
356   fLblValueXVal = new TGLabel(fContValues, "00.000e+00");
357   fLblValueXVal->SetTextJustify(kTextRight);
358   fContValues->AddFrame(fLblValueXVal, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
359   //value label y
360   fLblValueY = new TGLabel(fContValues, "y-Value:");
361   fLblValueY->SetTextJustify(kTextLeft);
362   fContValues->AddFrame(fLblValueY, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
363   //value y
364   fLblValueYVal = new TGLabel(fContValues, "00.000e+00");
365   fLblValueYVal->SetTextJustify(kTextRight);
366   fContValues->AddFrame(fLblValueYVal, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
367    // draw button
368   fBtnDumpRuns = new TGTextButton(fContRight, "&Dump runs");
369   fContRight->AddFrame(fBtnDumpRuns, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
370   fBtnDumpRuns->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "DoDumpRuns()");
371   fBtnDumpRuns->SetToolTipText("Press to dump the run numbers of the current selection.");
372   //group frame for value information
373   fContAliases = new TGGroupFrame(fContRight, "Aliases", kVerticalFrame | kFitWidth | kFitHeight);
374   fContRight->AddFrame(fContAliases, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 10, 0));
375   // list of aliases
376   fListAliases = new TGListBox(fContAliases);
377   fContAliases->AddFrame(fListAliases, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
378   fListAliases->Connect("Selected(Int_t)", "AliTPCCalibViewerGUItime", this, "DoNewSelectionAliases()");
379 //   fListAliases->Resize(0,88);
380   //buttons
381   TGCompositeFrame *frame1 = new TGCompositeFrame(fContAliases, 200, 23, kHorizontalFrame | kFitWidth | kFitHeight);
382   fContAliases->AddFrame(frame1, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX , 0, 0, 0, 0));
383   // add button
384   TGTextButton *btnAdd = new TGTextButton(frame1, "&Add");
385   frame1->AddFrame(btnAdd, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
386   btnAdd->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "DoAddAlias()");
387   btnAdd->SetToolTipText("Press to add an alias.");
388   btnAdd->Resize(0,22);
389   // del button
390   TGTextButton *btnDel = new TGTextButton(frame1, "&Del");
391   frame1->AddFrame(btnDel, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
392   btnDel->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "DoDelAlias()");
393   btnDel->SetToolTipText("Press to delete the selected alias.");
394   btnDel->Resize(0,22);
395   // update button
396   TGTextButton *btnUp = new TGTextButton(frame1, "&Upd");
397   frame1->AddFrame(btnUp, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
398   btnUp->Connect("Clicked()", "AliTPCCalibViewerGUItime", this, "UpdateAliasList()");
399   btnUp->SetToolTipText("Press to update the alias list.");
400   btnUp->Resize(0,22);
401   
402
403   
404    // =========================================================================
405    // ****************** bottom content of fContTopBottom *********************
406    // =========================================================================
407   
408   // custom options container
409   // --- fComboCustom --- the custom draw line on the very low
410   fContCustom = new TGCompositeFrame(fContTopBottom, 200, 200, kHorizontalFrame | kFitWidth | kFitHeight);
411   fContTopBottom->AddFrame(fContCustom, new TGLayoutHints(kLHintsExpandX, 10, 0, 0, 0));
412   
413          // ------------------------- content of fContCustom -------------------------
414   fLblCustomDraw = new TGLabel(fContCustom, "Custom draw: ");
415   fLblCustomDraw->SetTextJustify(kTextLeft);
416   fContCustom->AddFrame(fLblCustomDraw, new TGLayoutHints(kLHintsNormal, 5, 0, 0, 0));
417          // text field for custom draw command
418   fComboCustomDraw = new TGComboBox(fContCustom);
419 //   fComboCustomDraw->Resize(0, fLblValueY->GetDefaultHeight());
420   fComboCustomDraw->Resize(0, 22);
421   fComboCustomDraw->EnableTextInput(kTRUE);
422   fContCustom->AddFrame(fComboCustomDraw, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
423   fComboCustomDraw->Connect("ReturnPressed()", "AliTPCCalibViewerGUItime", this, "DoCustomDraw()");
424   fComboCustomDraw->Connect("Selected(Int_t)", "AliTPCCalibViewerGUItime", this, "DoCustomDraw()");
425   fComboCustomDraw->GetTextEntry()->SetText("",kFALSE);
426   
427       // additional cuts container
428   fContCustomCuts = new TGCompositeFrame(fContTopBottom, 200, 200, kHorizontalFrame | kFitWidth | kFitHeight);
429   fContTopBottom->AddFrame(fContCustomCuts, new TGLayoutHints(kLHintsExpandX, 10, 0, 0, 0));
430   
431          // ------------------------- content of fContCustomCuts -------------------------
432   fLblCustomCuts = new TGLabel(fContCustomCuts, "Custom cuts:  ");
433   fLblCustomCuts->SetTextJustify(kTextLeft);
434   fContCustomCuts->AddFrame(fLblCustomCuts, new TGLayoutHints(kLHintsNormal, 5, 0, 0, 0));
435          // combo text field for additional cuts
436   fComboCustomCuts = new TGComboBox(fContCustomCuts);
437 //   fComboCustomCuts->Resize(0, fLblValueY->GetDefaultHeight());
438   fComboCustomCuts->Resize(0, 22);
439   fComboCustomCuts->EnableTextInput(kTRUE);
440   fContCustomCuts->AddFrame(fComboCustomCuts, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
441   fComboCustomCuts->Connect("ReturnPressed()", "AliTPCCalibViewerGUItime", this, "DoCustomCutsDraw()");
442   fComboCustomCuts->Connect("Selected(Int_t)", "AliTPCCalibViewerGUItime", this, "DoCustomCutsDraw()");
443   fComboCustomCuts->GetTextEntry()->SetText("",kFALSE);
444   
445   SetWindowName("AliTPCCalibViewer GUI - Time");
446   MapSubwindows();
447   Resize(GetDefaultSize());
448   MapWindow();
449 }
450 //______________________________________________________________________________
451 void AliTPCCalibViewerGUItime::SetInitialValues(){
452   //
453   // Set inital selections of the gui
454   //
455   fRadioXrun->SetState(kButtonDown);
456   fRadioXtime->SetState(kButtonUp);
457 }
458
459 //______________________________________________________________________________
460 void AliTPCCalibViewerGUItime::UseFile(const char* fileName, const char* treeName) {
461   //
462   // retrieve tree from file
463   //
464   TObjArray *arr=0x0;
465   TString file(fileName);
466   if (file.Contains("://")) {
467     if (file.Contains(";")) {
468       arr=file.Tokenize(";");
469     } else {
470       arr=new TObjArray;
471       arr->Add(new TObjString(fileName));
472     }
473   } else {
474     TString s=gSystem->GetFromPipe(Form("ls %s",fileName));
475     arr=s.Tokenize("\n");
476   }
477
478   if (!arr) return;
479   TIter next(arr);
480   TObject *o=0;
481   if (fTree) delete fTree;
482   fTree=new TChain(treeName);
483   while ( (o=next()) ){
484     fTree->AddFile(o->GetName());
485   }
486   arr->SetOwner();
487   delete arr;
488   if (!CheckChain()) return;
489   UseConfigFile(fConfigFile.Data());
490   Reload();
491   
492 }
493 //______________________________________________________________________________
494 void AliTPCCalibViewerGUItime::UseChain(TChain *const chain  = 0)
495 {
496   //
497   //
498   //
499   fTree=chain;
500   if (!CheckChain()) return;
501   //set configuration file
502   UseConfigFile(fConfigFile.Data());
503   Reload();
504 }
505 //______________________________________________________________________________
506 Bool_t AliTPCCalibViewerGUItime::CheckChain()
507 {
508   //
509   // check whether cahin has entries
510   // decide whether to draw graphs in 2D
511   //
512   if (!fTree) return kFALSE;
513   fTree->Lookup();
514   Long64_t entries=fTree->GetEntries();
515   if (entries==0){
516     AliError("No entries found in chain");
517     return kFALSE;
518   }
519   //check whether to draw graphs
520   CheckDrawGraph();
521   return kTRUE;
522 }
523 //______________________________________________________________________________
524 void AliTPCCalibViewerGUItime::UseConfigFile(const char* file)
525 {
526   //
527   // Use 'file' as configuration file
528   //
529   fConfigFile=file;
530   fConfigParser->ParseConfigFileTxt(fConfigFile.Data());
531   FillCalibTypes();
532 }
533 //______________________________________________________________________________
534 void AliTPCCalibViewerGUItime::FillRunTypes()
535 {
536   //
537   //Loop over the tree entries and fill the run types
538   //
539   if (!fTree) return;
540   Int_t id=0;
541   fComboRunType->RemoveAll();
542   fComboRunType->AddEntry("ALL",id++);
543   fComboRunType->Select(0,kFALSE);
544   if (!fTree->GetBranch("runType.")) return;
545   TObjString *runType=0x0;
546   Int_t nevets=fTree->GetEntries();
547   fTree->SetBranchStatus("*",0);
548   fTree->SetBranchStatus("runType.*",1);
549   fTree->SetBranchAddress("runType.",&runType);
550   for (Int_t iev=0;iev<nevets;++iev){
551     fTree->GetEntry(iev);
552     TString type=runType->String();
553     if (!type.IsNull()&&!fComboRunType->FindEntry(type)) fComboRunType->AddEntry(type,id++);
554   }
555   fTree->ResetBranchAddresses();
556   fTree->SetBranchStatus("*",1);
557 }
558 //______________________________________________________________________________
559 void AliTPCCalibViewerGUItime::FillCalibTypes()
560 {
561   //
562   // loop over configuration and fill calibration types
563   //
564   Int_t id=0;
565   fListCalibType->RemoveAll();
566   TObject *o=0x0;
567   fConfigParser->ResetIter();
568   TString type;
569   while ( (o=fConfigParser->NextKey()) ){
570     type=fConfigParser->GetData(o,kCalibType);
571     //remove whitespcaces
572     type.Remove(TString::kBoth,' ');
573     type.Remove(TString::kBoth,'\t');
574     if (type.IsNull()) type="UNSPECIFIED";
575 //     printf("CalibType: '%s'\n",type.Data());
576     if (!fListCalibType->FindEntry(type.Data())) {
577       fListCalibType->AddEntry(type.Data(),id);
578       fListCalibType->Select(id++);
579     }
580   }
581   //add type for unspecified calibration type
582   type="UNSPECIFIED";
583   if (!fListCalibType->FindEntry(type.Data())) {
584     fListCalibType->AddEntry(type.Data(),id);
585     fListCalibType->Select(id++);
586   }
587 }
588 //______________________________________________________________________________
589 void AliTPCCalibViewerGUItime::CheckDrawGraph()
590 {
591   //
592   // Check whether to draw graphs in 2D mode based on the number of entries in the chain
593   // GetEstimate() returns the maximum size of the arrays stored in GetV1()...
594   //
595   if (!fTree) return;
596   fNoGraph=kTRUE;
597   if (fTree->GetEntries()<fTree->GetEstimate()) fNoGraph=kFALSE;
598 }
599 //______________________________________________________________________________
600 void AliTPCCalibViewerGUItime::Reload(Int_t first)
601 {
602   //
603   // reload the gui contents, this is needed after the input tree has changed
604   //
605
606   if ( !fTree ) return;
607   //in case of the first call create run type and calibration type entries
608   if (first){
609     FillRunTypes();
610     FillCalibTypes();
611   }
612   //activate all branches
613   fTree->SetBranchStatus("*",1);
614   //reset variables list
615   fListVariables->RemoveAll();
616   //get selected calibration types
617   TList calibTypes;
618   fListCalibType->GetSelectedEntries(&calibTypes);
619   
620   TObjArray *branchList = fTree->GetListOfBranches();
621   if ( !branchList ) return;
622   TIter nextBranch(branchList);
623   Int_t idCount=0,id=0;
624   TObject *objBranch=0;
625   while ( (objBranch=nextBranch()) ){
626     TString branchName(objBranch->GetName());
627     TString branchTitle(objBranch->GetName());
628     if (branchName == "run" || branchName == "time" || branchName == "runType.") continue;
629     Bool_t active=kTRUE;
630     TString calibType="UNSPECIFIED";
631     if (fConfigParser){
632       const TObject *key=(*fConfigParser)(branchName.Data());
633       if (key){
634         //test if branch is active
635         active=fConfigParser->GetValue(branchName.Data(),kBranchOnOff);
636         id=(*fConfigParser)()->IndexOf(key);
637 //         branchTitle=fConfigParser->GetData(key,kBranchTitle);
638         calibType=fConfigParser->GetData(key,kCalibType);
639       }
640       else{
641         id=1000+idCount;
642       }
643     } else {
644       id=idCount;
645     }
646     if (calibType.IsNull()) calibType="UNSPECIFIED";
647     //check if branch is in selected calibration types
648     //if not, don't show it in the list and deactivate the branch.
649     Bool_t calibActive=kFALSE;
650     TIter nextCalib(&calibTypes);
651     TObject *objCalib=0;
652     while ( (objCalib=nextCalib()) )
653       if (calibType==objCalib->GetTitle()) calibActive=kTRUE;
654     active&=calibActive;
655     if (!active){
656       TString s=branchName;
657       if (branchName.EndsWith(".")) s+="*";
658 //       fTree->SetBranchStatus(s.Data(),0);
659       continue;
660     }
661 //     fListVariables->AddEntry(SubstituteUnderscores(branchTitle.Data()),id);
662     fListVariables->AddEntry(branchTitle.Data(),id);
663     ++idCount;
664   }
665   //trick to display modifications
666   fListVariables->Resize(fListVariables->GetWidth()-1, fListVariables->GetHeight());
667   fListVariables->Resize(fListVariables->GetWidth()+1, fListVariables->GetHeight());
668 }
669 //______________________________________________________________________________
670 void AliTPCCalibViewerGUItime::AddReferenceTree(const char* treeFileName, const char* refName)
671 {
672   //
673   // map of reference trees that should always be attached to the CalibViewerGUI
674   //
675   fMapRefTrees->Add(new TObjString(refName), new TObjString(treeFileName));
676
677 }
678 //______________________________________________________________________________
679 const TString AliTPCCalibViewerGUItime::GetDrawString(){
680   //
681   // create draw string for ttree by combining the user requestsa
682   //
683   
684   TString selectedVariable="";
685   Int_t id=-1;
686   if (!fListVariables->GetSelectedEntry()) return "";
687   selectedVariable = fListVariables->GetSelectedEntry()->GetTitle();
688   id=fListVariables->GetSelectedEntry()->EntryId();
689 //   printf("id: %d\n",id);
690   TString branchName=selectedVariable;
691   if (fConfigParser){
692     const TObject *key=(*fConfigParser)(id);
693     if (key) branchName=(*fConfigParser)(id)->GetName();
694   }
695   //treat case of TVector
696   if (branchName.EndsWith(".")){
697     Int_t par = (Int_t)(fNmbPar->GetNumber());
698     branchName.Append(Form("fElements[%d]",par));
699   }
700 //   if (fRadioXrun->GetState()==kButtonDown)
701 //     selectedVariable.Append(":run");
702 //   if (fRadioXtime->GetState()==kButtonDown)
703 //     selectedVariable.Append(":time");
704   
705   return branchName;
706 }
707 //______________________________________________________________________________
708 const TString AliTPCCalibViewerGUItime::GetDrawOptionString(){
709   //
710   // get user selected draw options
711   //
712   TString drawOpt;
713   if (fComboAddDrawOpt->GetSelectedEntry()) drawOpt=fComboAddDrawOpt->GetSelectedEntry()->GetTitle();
714   if (fChkDrawOptSame->GetState()==kButtonDown && !drawOpt.Contains("same",TString::kIgnoreCase))
715     drawOpt+="same";
716   return drawOpt;
717 }
718 //______________________________________________________________________________
719 void AliTPCCalibViewerGUItime::GetCutString(TString &cutStr){
720   //
721   // create cut string
722   //
723   TCut cuts(fComboCustomCuts->GetTextEntry()->GetText());
724   TString runType="";
725   if (fComboRunType->GetSelectedEntry()) runType=fComboRunType->GetSelectedEntry()->GetTitle();
726   if (runType!="ALL"&&!runType.IsNull()) cuts+=Form("runType.String().Data()==\"%s\"",runType.Data());
727   cutStr=cuts.GetTitle();
728 }
729 //______________________________________________________________________________
730 void AliTPCCalibViewerGUItime::UpdateValueArrays(Bool_t withGraph, const Double_t *xArr)
731 {
732   //
733   //
734   //
735   if (!withGraph){
736     fValuesX.ResizeTo(1);
737     fValuesY.ResizeTo(1);
738     fRunNumbers.ResizeTo(1);
739     fTimeStamps.ResizeTo(1);
740   } else {
741     const Long64_t nrows=fTree->GetSelectedRows();
742     fValuesX.ResizeTo(nrows);
743     fValuesY.ResizeTo(nrows);
744     fRunNumbers.ResizeTo(nrows);
745     fTimeStamps.ResizeTo(nrows);
746     long long *index=new long long[nrows];
747
748     //sort data
749     Int_t nTime0=0;
750     for (Int_t i=0;i<fTree->GetSelectedRows();++i){
751       if (fTree->GetV2()[i]<1) ++nTime0;
752     }
753     
754     if (nTime0==fTree->GetSelectedRows()){
755         TMath::Sort(nrows,fTree->GetV1(),index,kFALSE);
756     } else {
757       TMath::Sort(nrows,fTree->GetV2(),index,kFALSE);
758     }
759
760     Double_t lastRun=-1.;
761     Int_t entries=0;
762     const Bool_t drawSparse=(fRadioXrun->GetState()==kButtonDown && fChkDrawOptSparse->GetState()==kButtonDown);
763     for (Long64_t i=0; i<nrows; ++i){
764       // in case of sparse drawing only use the first entry per run
765       Double_t run  = fTree->GetV1()[index[i]];
766       Double_t xval = xArr[index[i]];
767       
768       if (drawSparse){
769         if (TMath::Abs(lastRun-run)<.1) {
770           lastRun=run;
771           continue;
772         }
773         xval=entries+0.5;
774       }
775       fValuesX.GetMatrixArray()[entries]=xval;
776       fValuesY.GetMatrixArray()[entries]=fTree->GetV3()[index[i]];
777       fRunNumbers.GetMatrixArray()[entries]=run;
778       fTimeStamps.GetMatrixArray()[entries]=fTree->GetV2()[index[i]];
779       lastRun=run;
780       ++entries;
781     }
782     
783     if (drawSparse){
784       fValuesX.ResizeTo(entries);
785       fValuesY.ResizeTo(entries);
786       fRunNumbers.ResizeTo(entries);
787       fTimeStamps.ResizeTo(entries);
788       //      printf("entries: %d\n",entries);
789     }
790     
791     delete [] index;
792   }
793 }
794 //______________________________________________________________________________
795 void AliTPCCalibViewerGUItime::GetHistogramTitle(TString &title)
796 {
797   //
798   // Create string for histogram title
799   //
800
801   title=fDrawString;
802   Int_t pos=title.First(">>");
803   if (pos>0) title=title(0,pos);
804 //   if (!fIsCustomDraw){
805     if (fRadioXrun->GetState()==kButtonDown){
806       title+=":Run";
807     } else if (fRadioXtime->GetState()==kButtonDown){
808       title+=":Date";
809     }
810 //   }
811   TString cuts;
812   GetCutString(cuts);
813   TObjArray *arr=title.Tokenize(":");
814   TObject *o=0x0;
815   title+=" {";
816   title+=cuts;
817   title+="}";
818   TIter next(arr,kIterBackward);
819   while ( (o=next()) ){
820     TString varName=o->GetName();
821     title+=";";
822     //substitue variable names with names in configuration file if available
823     if ((*fConfigParser)()->GetEntries()){
824       TString branchName=varName;
825       Int_t par=0;
826       if (branchName.Contains('.')) branchName=branchName(0,branchName.First('.')+1);
827       //chek if a configuration for that branch is available
828       const TObject *oBranch=(*fConfigParser)(branchName.Data());
829       if (oBranch){
830         TString branchTitle=fConfigParser->GetData(oBranch,kBranchTitle);
831         if (!branchTitle.IsNull()){
832           //check for TVectorT type branch
833           //add parameter name if available
834           if (varName.Contains("fElements")){
835             TString parStr=varName(varName.First('[')+1,varName.Length()-varName.First('[')-2);
836             par=parStr.Atoi();
837             branchTitle+=": ";
838             TString yparname=fConfigParser->GetData(oBranch,par+kParamNames);
839             if (!yparname.IsNull()){
840               branchTitle+=yparname;
841             } else {
842               branchTitle+="[";
843               branchTitle+=par;
844               branchTitle+="]";
845             }
846           }
847         }
848         varName=branchTitle;
849         SubstituteUnderscores(varName);
850       }
851     }
852     title+=varName;
853   }
854   delete arr;
855 }
856 //______________________________________________________________________________
857 void AliTPCCalibViewerGUItime::AdjustYRange()
858 {
859   //
860   //
861   //
862   TIter nextGraphicObject(fTrashBox);
863   TObject *o=0x0;
864   Float_t min=1,max=0;
865   while ( (o=nextGraphicObject()) ){
866     if (o->IsA()==TGraph::Class()){
867       TGraph *gr=(TGraph*)o;
868       if (min>max) {
869         min=TMath::MinElement(gr->GetN(),gr->GetY());
870         max=TMath::MaxElement(gr->GetN(),gr->GetY());
871       } else {
872         Float_t currmin=TMath::MinElement(gr->GetN(),gr->GetY());
873         Float_t currmax=TMath::MaxElement(gr->GetN(),gr->GetY());
874         if (currmax>max) max=currmax;
875         if (currmin<min) min=currmin;
876       }
877     }
878   }
879   if (min!=max){
880     if (min!=0) min=min-(max-min)/10;
881     if (max!=0) max=max+(max-min)/10;
882     fCurrentHist->SetMinimum(min);
883     fCurrentHist->SetMaximum(max);
884   }
885 }
886 //______________________________________________________________________________
887 void AliTPCCalibViewerGUItime::DoDraw() {
888   //
889   // Draw graphics
890   //
891   TString drawString=fDrawString;
892   TString cutString;
893   GetCutString(cutString);
894   TString optString  = GetDrawOptionString();
895   Bool_t graphOutput=!fNoGraph;  //ttree buffer for V1, V2... too small
896   graphOutput&=(drawString.First(">>")<0); //histogram output in custom draw
897   graphOutput&=fRadioXhist->GetState()!=kButtonDown; //histogram drawing selected
898 //   graphOutput&=!(fIsCustomDraw&&!fDrawString.Contains(":")); //custom draw 1D
899 //   graphOutput&=fDrawString.CountChar(':')<2; //custom draw 1D
900   if (fIsCustomDraw&&fDrawString.Contains(":")) HandleButtonsDrawSel(-kRadioXhist);
901   if (fIsCustomDraw&&fDrawString.Contains(":")) HandleButtonsDrawSel(-kRadioXhist);
902   Bool_t drawSame=optString.Contains("same",TString::kIgnoreCase);
903 //   optString+="goff";
904   if (graphOutput) {
905     drawString.Prepend("run:time:");
906     optString="goff";
907   }else{
908 //     if (!fIsCustomDraw){
909       if (fRadioXrun->GetState()==kButtonDown){
910         drawString+=":run";
911       } else if (fRadioXtime->GetState()==kButtonDown){
912         drawString+=":time";
913       }
914 //     }
915   }
916   TVirtualPad *padsave=gPad;
917   fCanvMain->GetCanvas()->cd();
918   //delete old histograms and graphs
919   if (!drawSame){
920     fTrashBox->Delete();
921     fCurrentGraph=0x0;
922     fCurrentHist=0x0;
923   }
924 //   printf("%s (%s) [%s]\n",drawString.Data(), cutString.Data(), optString.Data());
925   //select data
926   fTree->Draw(drawString.Data(),cutString.Data(),optString.Data());
927   if (fTree->GetSelectedRows()==-1) return;
928   
929   TString title;
930   GetHistogramTitle(title);
931   Bool_t drawGraph=kFALSE;
932   Double_t *xArr=0;
933   if (graphOutput){
934     drawGraph=kTRUE;
935     if (fIsCustomDraw&&fDrawString.Contains(":")){
936 //       fValuesX.SetElements(fTree->GetV4());
937       xArr=fTree->GetV4();
938     }else{
939       if (fRadioXrun->GetState()==kButtonDown){
940 //         fValuesX.SetElements(fTree->GetV1());
941         xArr=fTree->GetV1();
942       } else if (fRadioXtime->GetState()==kButtonDown){
943 //         fValuesX.SetElements(fTree->GetV2());
944         xArr=fTree->GetV2();
945       } else {
946         drawGraph=kFALSE;
947       }
948     }
949   }
950   if (xArr) UpdateValueArrays(graphOutput, xArr);
951 //   if (graphOutput){
952 //     if (fIsCustomDraw){
953 //       if (fDrawString.Contains(":")){
954 //         fValuesX.SetElements(fTree->GetV4());
955 //         drawGraph=kTRUE;
956 //       } else {
957 //         drawGraph=kFALSE;
958 //       }
959 //     }else{
960 //       drawGraph=kTRUE;
961 //       if (fRadioXrun->GetState()==kButtonDown){
962 //         fValuesX.SetElements(fTree->GetV1());
963 //       } else if (fRadioXtime->GetState()==kButtonDown){
964 //         fValuesX.SetElements(fTree->GetV2());
965 //       } else {
966 //         drawGraph=kFALSE;
967 //       }
968 //     }
969 //   }
970 //create graph according to selection
971   if (drawGraph){
972     TGraph *graph=new TGraph(fValuesX,fValuesY);
973 //     graph->Sort();
974     TString grDraw="p";
975     if (!drawSame) grDraw+="a";
976     if (!fIsCustomDraw) grDraw+="l";
977     // sparse drawing, set bin labels
978     if (fRadioXrun->GetState()==kButtonDown && fChkDrawOptSparse->GetState()==kButtonDown){
979       Int_t nrows=fValuesX.GetNrows();
980       Double_t *newBins = new Double_t[nrows+1];
981       for (Int_t ibin=0; ibin<nrows+1; ++ibin) newBins[ibin]=ibin;
982       graph->GetXaxis()->Set(nrows,newBins);
983       graph->GetXaxis()->LabelsOption("v");
984       for (Int_t i=0; i<nrows;++i)
985         graph->GetXaxis()->SetBinLabel(i+1,Form("%.0f",fRunNumbers.GetMatrixArray()[i]));
986       delete [] newBins;
987     }
988     graph->Draw(grDraw.Data());
989     graph->SetEditable(kFALSE);
990     TH1 *hist=graph->GetHistogram();
991     hist->SetTitle(title.Data());
992     fTrashBox->Add(graph);
993     graph->SetLineColor(fTrashBox->GetEntries());
994     graph->SetMarkerColor(fTrashBox->GetEntries());
995     if (!drawSame) {
996       fCurrentGraph=graph;
997       fCurrentHist=hist;
998     }
999   } else {
1000     TH1 *hist=fTree->GetHistogram();
1001     hist->SetTitle(title.Data());
1002 //     hist->Draw(optString.Data());
1003     fTrashBox->Add(hist);
1004     hist->SetLineColor(fTrashBox->GetEntries());
1005     hist->SetMarkerColor(fTrashBox->GetEntries());
1006     if (!drawSame) fCurrentHist=hist;
1007   }
1008   
1009   //Set time axis if choosen as x-variables
1010 //   if (fRadioXtime->GetState()==kButtonDown&&!fIsCustomDraw&&!drawSame){
1011   if (fRadioXtime->GetState()==kButtonDown&&!drawSame){
1012     TAxis *xaxis=fCurrentHist->GetXaxis();
1013     xaxis->SetTimeFormat("#splitline{%d.%m}{%H:%M}");
1014     xaxis->SetTimeDisplay(1);
1015     xaxis->SetLabelOffset(xaxis->GetLabelOffset()*3);
1016     xaxis->SetLabelSize(xaxis->GetLabelSize()/1.3);
1017   }
1018   if (!drawSame) {
1019   //Set title offset
1020     fCurrentHist->GetYaxis()->SetTitleOffset(1.5);
1021   } else {
1022     //adjust y-range
1023     AdjustYRange();
1024   }
1025   gPad->Modified();
1026   gPad->Update();
1027   padsave->cd();
1028 }
1029
1030 //______________________________________________________________________________
1031 void AliTPCCalibViewerGUItime::DoDumpRuns()
1032 {
1033   //
1034   // Dump the current run numbers to stdout
1035   //
1036   Int_t npoints=fRunNumbers.GetNrows();
1037   Int_t    *sortIndex = new Int_t[npoints];
1038   TMath::Sort(npoints,fRunNumbers.GetMatrixArray(),sortIndex,kFALSE);
1039   Int_t run=0, prevRun=-1;
1040   
1041   for (Int_t irun=0;irun<npoints;++irun){
1042     run=(Int_t)fRunNumbers.GetMatrixArray()[sortIndex[irun]];
1043     if (run!=prevRun) std::cout << Form("%d",run) << std::endl;
1044     prevRun=run;
1045   }
1046   delete [] sortIndex;
1047 }
1048 //______________________________________________________________________________
1049 void AliTPCCalibViewerGUItime::DoParLimitChange()
1050 {
1051   //
1052   //
1053   //
1054   UpdateParName();
1055   DoDraw();
1056 }
1057 //______________________________________________________________________________
1058 void AliTPCCalibViewerGUItime::DoNewSelection() {
1059    //
1060    // decides whether to redraw if user makes another selection
1061    //
1062   UpdateParLimits();
1063   fDrawString=GetDrawString();
1064   fIsCustomDraw=kFALSE;
1065   DoDraw();
1066 }
1067 //______________________________________________________________________________
1068 void AliTPCCalibViewerGUItime::DoCustomDraw()
1069 {
1070   //
1071   //
1072   //
1073   fDrawString=fComboCustomDraw->GetTextEntry()->GetText();
1074 //   if (fDrawString.Contains(">>")){
1075 //     Warning("DoCustomDraw","Currently no user defined histograms allowed!");
1076 //     return;
1077 //   }
1078   fNmbPar->SetState(kFALSE);
1079   fIsCustomDraw=kTRUE;
1080   DoDraw();
1081 }
1082 //______________________________________________________________________________
1083 void AliTPCCalibViewerGUItime::DoCustomCutsDraw()
1084 {
1085   //
1086   //
1087   //
1088   if (fIsCustomDraw) DoCustomDraw();
1089   else {
1090     fDrawString=GetDrawString();
1091     fIsCustomDraw=kFALSE;
1092     DoDraw();
1093   }
1094 }
1095 //______________________________________________________________________________
1096 void AliTPCCalibViewerGUItime::HandleButtonsDrawSel(Int_t id)
1097 {
1098   //
1099   // Draw selection button handling (x-variable)
1100   //
1101
1102   if (id == -1) {
1103     TGButton *btn = (TGButton *) gTQSender;
1104     id = btn->WidgetId();
1105   }
1106   
1107   Bool_t doDraw=kFALSE;
1108   Bool_t noDraw=kFALSE;
1109   if (id<-1){
1110     noDraw=kTRUE;
1111     id=TMath::Abs(id);
1112   }
1113   switch (id) {
1114   case (kRadioXhist):
1115     doDraw=(fRadioXtime->GetState()==kButtonDown||fRadioXrun->GetState()==kButtonDown);
1116     if (doDraw){
1117       fRadioXrun->SetState(kButtonUp);
1118       fRadioXtime->SetState(kButtonUp);
1119       fRadioXhist->SetState(kButtonDown);
1120     }
1121     break;
1122   case (kRadioXrun):
1123     doDraw=(fRadioXtime->GetState()==kButtonDown||fRadioXhist->GetState()==kButtonDown);
1124     if (doDraw){
1125       fRadioXhist->SetState(kButtonUp);
1126       fRadioXtime->SetState(kButtonUp);
1127       fRadioXrun->SetState(kButtonDown);
1128     }
1129     break;
1130   case (kRadioXtime):
1131     doDraw=(fRadioXhist->GetState()==kButtonDown||fRadioXrun->GetState()==kButtonDown);
1132     if (doDraw){
1133       fRadioXrun->SetState(kButtonUp);
1134       fRadioXhist->SetState(kButtonUp);
1135       fRadioXtime->SetState(kButtonDown);
1136     }
1137     break;
1138   }
1139   if (doDraw&&!noDraw) DoCustomCutsDraw();
1140 }
1141 //______________________________________________________________________________
1142 void AliTPCCalibViewerGUItime::UpdateParName()
1143 {
1144   //
1145   // change parameter name
1146   //
1147   
1148   Int_t par = (Int_t)(fNmbPar->GetNumber());
1149   TString parName="";
1150   Int_t id=fListVariables->GetSelectedEntry()->EntryId();
1151   if (fConfigParser && (*fConfigParser)(id)) parName=fConfigParser->GetData((*fConfigParser)(id),par+kParamNames);
1152   if (parName=="") parName.Form("%d",par);
1153   fLblPar->SetText(Form("Parameter: %s",parName.Data()));
1154   fDrawString=GetDrawString();
1155   fIsCustomDraw=kFALSE;
1156 }
1157
1158 //______________________________________________________________________________
1159 void AliTPCCalibViewerGUItime::UpdateParLimits()
1160 {
1161   //
1162   // Adjust limits for TVectorT based variables
1163   //
1164   if (!fTree) return;
1165   TString selectedVariableTitle="";
1166   Int_t id=-1;
1167   if (!fListVariables->GetSelectedEntry()) return;
1168   selectedVariableTitle = fListVariables->GetSelectedEntry()->GetTitle();
1169   id=fListVariables->GetSelectedEntry()->EntryId();
1170 //   printf("id: %d\n",id);
1171   TString selectedVariable=selectedVariableTitle;
1172   const TObject *key=(*fConfigParser)(id);
1173   if (key) selectedVariable=(*fConfigParser)(id)->GetName();
1174   
1175   if (selectedVariable.IsNull()||!selectedVariable.EndsWith(".")) {
1176     fNmbPar->SetState(kFALSE);
1177     fLblPar->SetText("Parameter: none");
1178     return;
1179   }
1180   TVectorD *vD=0x0;
1181   TVectorF *vF=0x0;
1182   Int_t maxPar=0;
1183   fTree->GetEntry(1);
1184   TBranch *branch=fTree->GetTree()->GetBranch(selectedVariable.Data());
1185   TString branchClass=branch->GetClassName();
1186   Int_t event=0;
1187   if (branchClass=="TVectorT<double>"){
1188 //     branch->SetAddress(&vD);
1189     fTree->SetBranchAddress(selectedVariable.Data(),&vD);
1190     while (maxPar<2&&event<fTree->GetEntries()){
1191       fTree->GetEntry(event++);
1192       maxPar=vD->GetNrows();
1193     }
1194   } else if (branchClass=="TVectorT<float>"){
1195 //     branch->SetAddress(&vF);
1196     fTree->SetBranchAddress(selectedVariable.Data(),&vF);
1197     while (maxPar<2&&event<fTree->GetEntries()){
1198       fTree->GetEntry(event++);
1199       maxPar=vF->GetNrows();
1200     }
1201   } else {
1202     //class not known
1203     fNmbPar->SetState(kFALSE);
1204     return;
1205   }
1206 //   branch->ResetAddress();
1207   fTree->SetBranchAddress(selectedVariable.Data(),0x0);
1208   if (fNmbPar->GetNumMax()!=maxPar-1) fNmbPar->SetNumber(0);
1209   fNmbPar->SetLimitValues(0,maxPar-1);
1210   fNmbPar->SetState(kTRUE);
1211   UpdateParName();
1212 }
1213 //______________________________________________________________________________
1214 void AliTPCCalibViewerGUItime::MouseMove(Int_t event, Int_t x, Int_t y, TObject */*selected*/)
1215 {
1216   //
1217   // handle mouse events in the draw canvas
1218   //
1219   UInt_t dd=0,mm=0,yy=0,HH=0,MM=0,SS=0,run=0;
1220   Double_t valx=0.,valy=0.;
1221   if (!fCurrentGraph) {
1222     fLblRunNumberVal->SetText(Form("%06u",run));
1223     fLblRunTimeVal->SetText(Form("%02u.%02u.%04u\n%02u:%02u:%02u",dd,mm,yy,HH,MM,SS));
1224     fLblValueXVal->SetText(Form("%.3e", valx));
1225     fLblValueYVal->SetText(Form("%.3e", valy));
1226     return;
1227   }
1228   TVirtualPad *padsave=gPad;
1229   fCanvMain->GetCanvas()->cd();
1230   Int_t n=fValuesY.GetNrows();
1231   Double_t *arr=0x0;
1232   arr=fValuesX.GetMatrixArray();
1233
1234   Int_t minDist=1000000;
1235   Int_t minPoint=-1;
1236   for (Int_t i=0;i<n;++i){
1237     Int_t pxp = gPad->XtoAbsPixel(gPad->XtoPad(arr[i]));
1238     Int_t pyp = gPad->YtoAbsPixel(gPad->YtoPad(fValuesY[i]));
1239     Int_t d   = TMath::Nint(TMath::Sqrt(TMath::Abs(pxp-x) + TMath::Abs(pyp-y)));
1240     if (d < minDist) {
1241       minDist  = d;
1242       minPoint = i;
1243     }
1244   }
1245   if (minDist<2){
1246     TTimeStamp t((Int_t)fTimeStamps[minPoint]);
1247     t.GetDate(kTRUE,0,&yy,&mm,&dd);
1248     t.GetTime(kTRUE,0,&HH,&MM,&SS);
1249     run=(UInt_t)fRunNumbers[minPoint];
1250     valx=fValuesX[minPoint];
1251     valy=fValuesY[minPoint];
1252   } else {
1253     dd=0;mm=0;yy=0;HH=0;MM=0;SS=0;run=0;
1254     valx=0.;
1255     valy=0.;
1256   }
1257   fLblRunNumberVal->SetText(Form("%06u",run));
1258   fLblRunTimeVal->SetText(Form("%02u.%02u.%04u\n%02u.%02u.%02u",dd,mm,yy,HH,MM,SS));
1259   if (fIsCustomDraw){
1260     fLblValueXVal->SetText(Form("%.3e", valx));
1261   }else{
1262     if (fRadioXrun->GetState()==kButtonDown){
1263       fLblValueXVal->SetText("Run");
1264     } else if (fRadioXtime->GetState()==kButtonDown){
1265       fLblValueXVal->SetText("Time");
1266     }
1267   }
1268   fLblValueYVal->SetText(Form("%.3e", valy));
1269   padsave->cd();
1270   if (run==0) return;
1271   if (event == kButton1Double ){
1272     SetGuiTree(run);
1273   }
1274   //find closes point of current selection
1275 }
1276 //______________________________________________________________________________
1277 void AliTPCCalibViewerGUItime::SetGuiTree(Int_t run)
1278 {
1279   //
1280   // create the AliTPCCalibViewerGUI tree for run
1281   // cache tree in directory fOutputCacheDir
1282   // retrieve file from this directory if it already exists
1283   //
1284
1285   //
1286   //Create and set GUI tree  
1287   //
1288   //try to find file for run in fOutputCacheDir
1289   TString fileName=fOutputCacheDir;
1290   if (!fileName.EndsWith("/")) fileName+="/";
1291   fileName+=Form("guiTreeRun_%d.root",run);
1292   Bool_t load=kTRUE;
1293   if (gSystem->AccessPathName(fileName.Data())){
1294     load=AliTPCcalibDB::CreateGUITree(run,fileName.Data());
1295     if (!load){
1296       fCalibViewerGUI->Reset();
1297       if (fCalibViewerGUItab) fCalibViewerGUItab->SetText(new TGString(Form("Detail - XXXXX")));
1298       return;
1299     }
1300   }
1301   fCalibViewerGUI->Initialize(fileName.Data());
1302   if (fCalibViewerGUItab) fCalibViewerGUItab->SetText(new TGString(Form("Detail - %05d",run)));
1303   
1304   //
1305   //Create and set Reference GUI tree
1306   //
1307   AliTPCcalibDButil util;
1308   util.SetReferenceRun(run);
1309   fileName=fOutputCacheDir;
1310   if (!fileName.EndsWith("/")) fileName+="/";
1311   fileName+=util.GetGUIRefTreeDefaultName();
1312   //only update if file does not exist
1313   if (gSystem->AccessPathName(fileName.Data())){
1314     util.UpdateRefDataFromOCDB();
1315     util.CreateGUIRefTree(fileName.Data());
1316   }
1317   
1318   fCalibViewerGUI->GetViewer()->AddReferenceTree(fileName.Data(),"calPads","Ref");
1319   
1320   //
1321   // Process additional reference trees
1322   //
1323   TIter nextRefTree(fMapRefTrees);
1324   TObject *o=0x0;
1325   //Set static reference data
1326   while ( (o=nextRefTree()) ){
1327     fCalibViewerGUI->GetViewer()->AddReferenceTree(fMapRefTrees->GetValue(o)->GetName(),"calPads",o->GetName());
1328   }
1329   fCalibViewerGUI->Reload();
1330 }
1331 //______________________________________________________________________________
1332 void AliTPCCalibViewerGUItime::SubstituteUnderscores(TString &s)
1333 {
1334   //
1335   //
1336   //
1337   s.ReplaceAll("_{","|{");
1338   s.ReplaceAll("_"," ");
1339   s.ReplaceAll("|{","_{");
1340 }
1341
1342 //______________________________________________________________________________
1343 void AliTPCCalibViewerGUItime::DoNewSelectionAliases()
1344 {
1345   //
1346   //
1347   //
1348   if (!fTree) return;
1349   TList *l=fTree->GetListOfAliases();
1350   if (!l) return;
1351   TString selectedVariable="";
1352   if (!fListAliases->GetSelectedEntry()) return;
1353   selectedVariable = fListAliases->GetSelectedEntry()->GetTitle();
1354   fDrawString=selectedVariable;
1355   fIsCustomDraw=kFALSE;
1356   DoDraw();
1357 }
1358 //______________________________________________________________________________
1359 void AliTPCCalibViewerGUItime::DoAddAlias()
1360 {
1361   //
1362   //
1363   //
1364   new AliTPCCalibViewerGUItimeAddAliasFrame(gClient->GetRoot(), fContTopBottom, 400, 200, kVerticalFrame, this);
1365 }
1366 //______________________________________________________________________________
1367 void AliTPCCalibViewerGUItime::DoDelAlias()
1368 {
1369   //
1370   //
1371   //
1372   if (!fTree) return;
1373   TList *l=fTree->GetListOfAliases();
1374   if (!l) return;
1375   TString selectedVariable="";
1376   if (!fListAliases->GetSelectedEntry()) return;
1377   selectedVariable = fListAliases->GetSelectedEntry()->GetTitle();
1378   l->Remove(l->FindObject(selectedVariable.Data()));
1379   UpdateAliasList();
1380 }
1381
1382 //______________________________________________________________________________
1383 void AliTPCCalibViewerGUItime::UpdateAliasList()
1384 {
1385   //
1386   //
1387   //
1388   printf("UpdateAliasList\n");
1389   if (!fTree) return;
1390   TList *l=fTree->GetListOfAliases();
1391   if (!l) return;
1392   TIter nextAlias(l);
1393   TObject *o;
1394   fListAliases->RemoveAll();
1395   Int_t id=0;
1396   while( (o=nextAlias()) ){
1397     fListAliases->AddEntry(o->GetName(),id++);
1398   }
1399   fListAliases->Resize(fListAliases->GetWidth()-1, fListAliases->GetHeight());
1400   fListAliases->Resize(fListAliases->GetWidth()+1, fListAliases->GetHeight());
1401 }
1402 //______________________________________________________________________________
1403 TObjArray* AliTPCCalibViewerGUItime::ShowGUI(const char* fileName, const char* treeName) {
1404    //
1405    // Initialize and show GUI for presentation for demonstration purposes
1406    // or for fast standalone use
1407    //
1408   TGMainFrame* frmMain = new TGMainFrame(gClient->GetRoot(), 1000, 600);
1409   frmMain->SetWindowName("AliTPCCalibViewer GUItime");
1410   frmMain->SetCleanup(kDeepCleanup);
1411   
1412   TGTab* tabMain = new TGTab(frmMain, 1000, 600);
1413   frmMain->AddFrame(tabMain, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1414   
1415   TGCompositeFrame* tabCont1 = tabMain->AddTab("Time");
1416   TGCompositeFrame* tabCont2 = tabMain->AddTab("Detail - XXXXX");
1417   
1418   AliTPCCalibViewerGUItime* calibViewerTime = new AliTPCCalibViewerGUItime(tabCont1, 1000, 600);
1419   tabCont1->AddFrame(calibViewerTime, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1420   calibViewerTime->SetConfigFileName("$ALICE_ROOT/TPC/CalibMacros/calibVarDescription.txt");
1421   calibViewerTime->UseFile(fileName, treeName);
1422   
1423   AliTPCCalibViewerGUI* calibViewer = new AliTPCCalibViewerGUI(tabCont2, 1000, 600, 0);
1424   tabCont2->AddFrame(calibViewer, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1425   calibViewerTime->SetCalibViewerGUI(calibViewer);
1426   calibViewerTime->SetCalibViewerGUItab(tabMain->GetTabTab(1));
1427   
1428   
1429   TObjArray *guiArray = new TObjArray();
1430   guiArray->Add(calibViewerTime);
1431   guiArray->Add(calibViewer);
1432   
1433   frmMain->MapSubwindows();
1434   frmMain->Resize();
1435   frmMain->MapWindow();
1436   
1437   return guiArray;
1438 }
1439
1440 //______________________________________________________________________________
1441 TObjArray* AliTPCCalibViewerGUItime::ShowGUI(TChain *chain) {
1442    //
1443    // Initialize and show GUI for presentation for demonstration purposes
1444    // or for fast standalone use
1445    //
1446   TGMainFrame* frmMain = new TGMainFrame(gClient->GetRoot(), 1000, 600);
1447   frmMain->SetWindowName("AliTPCCalibViewer GUItime");
1448   frmMain->SetCleanup(kDeepCleanup);
1449
1450   TGTab* tabMain = new TGTab(frmMain, 1000, 600);
1451   frmMain->AddFrame(tabMain, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1452
1453   TGCompositeFrame* tabCont1 = tabMain->AddTab("Time");
1454
1455   AliTPCCalibViewerGUItime* calibViewerTime = new AliTPCCalibViewerGUItime(tabCont1, 1000, 600);
1456   tabCont1->AddFrame(calibViewerTime, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1457   calibViewerTime->UseChain(chain);
1458
1459   TObjArray *guiArray = new TObjArray();
1460   guiArray->Add(calibViewerTime);
1461
1462   frmMain->MapSubwindows();
1463   frmMain->Resize();
1464   frmMain->MapWindow();
1465
1466   return guiArray;
1467 }
1468
1469
1470 ////////////////////////////////////////////////////////////////////////
1471 //
1472 //   GUI Alias frame
1473 //
1474 ////////////////////////////////////////////////////////////////////////
1475
1476
1477 ClassImp(AliTPCCalibViewerGUItimeAddAliasFrame)
1478
1479 AliTPCCalibViewerGUItimeAddAliasFrame::AliTPCCalibViewerGUItimeAddAliasFrame(const TGWindow *p, const TGWindow *main,
1480                                                                              UInt_t w, UInt_t h, UInt_t options,
1481                                                                              AliTPCCalibViewerGUItime *gui, TString strAlias) :
1482   fMain(0x0),
1483   fTxt1(0x0),
1484   fTxt2(0x0),
1485   fGUI(0x0)
1486 {
1487   fMain = new TGTransientFrame(p, main, w, h, options);
1488   fMain->Connect("CloseWindow()", "AliTPCCalibViewerGUItimeAddAliasFrame", this, "DoCancel()");
1489   fMain->DontCallClose(); // to avoid double deletions.
1490   
1491    // use hierarchical cleaning
1492   fMain->SetCleanup(kDeepCleanup);
1493
1494   //layout
1495   TGLayoutHints *l1 = new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 2, 2, 2, 2);
1496   TGLayoutHints *l2 = new TGLayoutHints(kLHintsTop | kLHintsRight | kLHintsExpandX, 2, 2, 0, 5);
1497 //   TGLayoutHints *l3 = new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5);
1498   
1499   //input fields
1500   TGCompositeFrame *f1 = new TGCompositeFrame(fMain, 60, 20, kVerticalFrame);
1501   fMain->AddFrame(f1, l1);
1502   TGCompositeFrame *frameName    = new TGCompositeFrame(f1);
1503   TGCompositeFrame *frameFormula = new TGCompositeFrame(f1);
1504   f1->AddFrame(frameName,l2);
1505   f1->AddFrame(frameFormula,l2);
1506   TGLabel *lblTxt1   = new TGLabel(frameName, "Name:");
1507   TGLabel *lblTxt2   = new TGLabel(frameFormula, "Formula:");
1508   fTxt1 = new TGTextEntry(frameName, new TGTextBuffer(1000));
1509   fTxt2 = new TGTextEntry(frameFormula, new TGTextBuffer(1000));
1510   
1511   frameName->AddFrame(lblTxt1, l2);
1512   frameName->AddFrame(fTxt1, l2);
1513   frameFormula->AddFrame(lblTxt2, l2);
1514   frameFormula->AddFrame(fTxt2, l2);
1515   
1516   fTxt1->Resize(350, fTxt1->GetDefaultHeight());
1517   fTxt2->Resize(350, fTxt2->GetDefaultHeight());
1518
1519   //ok and cancel buttons
1520   TGHorizontalFrame *frame = new TGHorizontalFrame(fMain, 60, 20, kFixedWidth);
1521   
1522   TGTextButton *okButton = new TGTextButton(frame, "&Ok", 1);
1523   okButton->Connect("Clicked()", "AliTPCCalibViewerGUItimeAddAliasFrame", this, "DoOK()");
1524   TGTextButton *cancelButton = new TGTextButton(frame, "&Cancel", 2);
1525   cancelButton->Connect("Clicked()", "AliTPCCalibViewerGUItimeAddAliasFrame", this, "DoCancel()");
1526   
1527   
1528   frame->AddFrame(okButton, l1);
1529   frame->AddFrame(cancelButton, l1);
1530   
1531   frame->Resize(150, okButton->GetDefaultHeight());
1532   
1533   fMain->AddFrame(frame, new TGLayoutHints(kLHintsBottom | kLHintsRight, 2, 2, 5, 1));
1534   
1535   fGUI=gui;
1536   TString aliasName, alias;
1537   if (!strAlias.IsNull()){
1538     TChain *c=fGUI->GetChain();
1539     if (c){
1540       TList *l=c->GetListOfAliases();
1541       if (l){
1542         TNamed *d=(TNamed*)l->FindObject(strAlias);
1543         if (d){
1544           aliasName=d->GetName();
1545           alias=d->GetTitle();
1546         }
1547       }
1548     }
1549   }else{
1550     alias=fGUI->GetCustomDrawString();
1551   }
1552   fTxt1->SetText(aliasName.Data(),kFALSE);
1553   fTxt2->SetText(alias.Data(),kFALSE);
1554
1555   fMain->MapSubwindows();
1556   fMain->Resize();
1557   
1558    // position relative to the parent's window
1559   fMain->CenterOnParent();
1560   
1561   fMain->SetWindowName("Alias Editor");
1562   
1563   fMain->MapWindow();
1564   
1565 }
1566 //______________________________________________________________________________
1567 AliTPCCalibViewerGUItimeAddAliasFrame::~AliTPCCalibViewerGUItimeAddAliasFrame()
1568 {
1569   //
1570   //
1571   //
1572   fMain->DeleteWindow();  // deletes fMain
1573 }
1574 //______________________________________________________________________________
1575 void AliTPCCalibViewerGUItimeAddAliasFrame::DoOK()
1576 {
1577   //
1578   //
1579   //
1580   TString aliasName=fTxt1->GetText();
1581   TString alias=fTxt2->GetText();
1582   if (!aliasName.IsNull()&&!alias.IsNull()){
1583     TChain *c=fGUI->GetChain();
1584     if (c){
1585       c->SetAlias(aliasName.Data(),alias.Data());
1586     }
1587   }
1588   fGUI->UpdateAliasList();
1589   delete this;
1590 }
1591 //______________________________________________________________________________
1592 void AliTPCCalibViewerGUItimeAddAliasFrame::DoCancel()
1593 {
1594   //
1595   //
1596   //
1597   delete this;
1598 }
1599