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