]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliCalibViewerGUItime.cxx
Removing self-assignments
[u/mrichter/AliRoot.git] / STEER / CDB / AliCalibViewerGUItime.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 //  GUI for displaying calibration entries over time                         //
20 //                                                                           //
21 //  Authors:     Marian Ivanov (Marian.Ivanov@cern.ch)                       //
22 //               Jens Wiechula (Jens.Wiechula@cern.ch)                       //
23 //               Ionut Arsene  (iarsene@cern.ch)                             //
24 //                                                                           //
25 //  Usage:   AliCalibViewerGUItime::ShowGUI(TChain* chain)                   //
26 //           AliCalibViewerGUItime::ShowGUI()                                //
27 ///////////////////////////////////////////////////////////////////////////////
28
29
30 #include <iostream>
31 //Root includes
32 #include <TROOT.h>
33 #include <TDirectory.h>
34 #include <TStyle.h>
35 #include <TCanvas.h>
36 #include <TPad.h>
37 #include <TVirtualPad.h>
38 #include <TObject.h>
39 #include <TObjArray.h>
40 #include <TObjString.h>
41 #include <TSystem.h>
42 #include <TVector.h>
43 #include <TH1.h>
44 #include <TCut.h>
45 #include <TFile.h>
46 #include <TTree.h>
47 #include <TChain.h>
48 #include <TBranch.h>
49 #include <TIterator.h>
50 #include <TGraph.h>
51 #include <TAxis.h>
52 #include <TTimeStamp.h>
53 #include <TMath.h>
54 #include <TMap.h>
55 //
56 #include <TGFileDialog.h>
57 #include <TGInputDialog.h>
58 //
59 #include <TGButton.h>
60 #include <TGListBox.h>
61 #include <TGComboBox.h>
62 #include <TGNumberEntry.h>
63 #include <TGLayout.h>
64 #include <TRootEmbeddedCanvas.h>
65 #include <TGSplitter.h>
66 #include <TGButtonGroup.h>
67 #include <TGLabel.h>
68 #include <TGTab.h>
69 #include <TGString.h>
70
71 //AliRoot includes
72 #include <AliLog.h>
73 #include "AliBaseCalibViewer.h"
74 #include "AliBaseCalibViewerGUI.h"
75 #include "AliCalibViewerGUItime.h"
76
77
78 ClassImp(AliCalibViewerGUItime)
79
80 //_______________________________________________________________________________________________________________
81 AliCalibViewerGUItime::AliCalibViewerGUItime(const TGWindow *p, UInt_t w, UInt_t h, const Char_t* det) :
82 TGCompositeFrame(p,w,h),
83   fDetector(det),
84   fFile(0x0),
85   fTree(0x0),
86   fCalibViewerGUI(0x0),
87   fCalibViewerGUItab(0x0),
88   fCurrentHist(0x0),
89   fCurrentGraph(0x0),
90   fCurrentRunDetails(-1),
91   fOutputCacheDir("/tmp"),
92   fDrawString(""),
93   fIsCustomDraw(kFALSE),
94   fRunNumbers(10),
95   fTimeStamps(10),
96   fValuesX(10),
97   fValuesY(10),
98   fNoGraph(kFALSE),
99   fGraphLimitEntries(10000),
100   fMapRefTrees(new TMap),
101   //GUI elements
102   //main canvas Top part, bottom part
103   fContTopBottom(0x0),
104   //top left, centre, right
105   fContLCR(0x0),
106   //content left
107   fContLeft(0x0),
108   fContDrawOpt(0x0),
109   fChkDrawOptSame(0x0),
110   fComboAddDrawOpt(0x0),
111   fContDrawSel(0x0),
112   fContDrawSelSubRunTime(0x0),
113   fRadioXhist(0x0),
114   fRadioXrun(0x0),
115   fRadioXtime(0x0),
116   fListVariables(0x0),
117   fComboRunType(0x0),
118   fLblRunType(0x0),
119   fNmbPar(0x0),
120   fLblPar(0x0),
121   fListCalibType(0x0),
122   fContCalibType(0x0),
123   //content centre
124   fContCenter(0x0),
125   fCanvMain(0x0),
126   //content right
127   fContRight(0x0),
128   fContValues(0x0),
129   fLblRunNumber(0x0),
130   fLblRunTime(0x0),
131   fLblValueX(0x0),
132   fLblValueY(0x0),
133   fLblRunNumberVal(0x0),
134   fLblRunTimeVal(0x0),
135   fLblValueXVal(0x0),
136   fLblValueYVal(0x0),
137   fBtnDumpRuns(0x0),
138   fBtnSave(0x0),
139   //load a file
140   fContLoad(0x0),
141   fContFilename(0x0),
142   fContConfigFile(0x0),
143   fContTreeName(0x0),
144   fLblTreeName(0x0),
145   fTxtFilename(0x0),
146   fTxtConfigFile(0x0),
147   fTxtTreeName(0x0),
148   fBtnLoadFile(0x0),
149   // extract information from OCDB
150   //content bottom
151   fContCustom(0x0),
152   fContCustomCuts(0x0),
153   fLblCustomDraw(0x0),
154   fLblCustomCuts(0x0),
155   fComboCustomDraw(0x0),
156   fComboCustomCuts(0x0),
157   fTrashBox(new TObjArray)
158 {
159   //
160   // ctor
161   //
162   fDetector.ToUpper();
163   fMapRefTrees->SetOwnerKeyValue();
164   fTrashBox->SetOwner();
165   DrawGUI(p,w,h);
166   gStyle->SetMarkerStyle(20);
167   gStyle->SetMarkerSize(0.5);
168   SetInitialValues();
169 }
170
171 //______________________________________________________________________________
172 AliCalibViewerGUItime::~AliCalibViewerGUItime(){
173   //
174   // dtor
175   //
176   //  delete fConfigParser;
177   delete fTrashBox;
178   delete fMapRefTrees;
179 }
180
181 //______________________________________________________________________________
182 void AliCalibViewerGUItime::DrawGUI(const TGWindow */*p*/, UInt_t w, UInt_t h) {
183    //
184    // draw the GUI
185    //
186    // ======================================================================
187    // ************************* Display everything *************************
188    // ======================================================================
189   
190   SetCleanup(kDeepCleanup);
191   
192    // *****************************************************************************
193    // ************************* content of this MainFrame *************************
194    // *****************************************************************************
195    // top level container with horizontal layout
196   fContTopBottom = new TGCompositeFrame(this, w, h, kVerticalFrame | kFixedWidth | kFixedHeight);
197   AddFrame(fContTopBottom, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
198   
199   fContLCR = new TGCompositeFrame(fContTopBottom, w, h, kHorizontalFrame | kFixedWidth | kFixedHeight);
200   fContTopBottom->AddFrame(fContLCR, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
201   
202    // ***********************************************************************
203    // ************************* content of fContLCR *************************
204    // ***********************************************************************
205    // left container
206   fContLeft = new TGCompositeFrame(fContLCR, 200, 200, kVerticalFrame | kFixedWidth | kFitHeight);
207   fContLCR->AddFrame(fContLeft, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandY, 5, 3, 3, 3));
208   
209    // left vertical splitter
210   TGVSplitter *splitLeft = new TGVSplitter(fContLCR);
211   splitLeft->SetFrame(fContLeft, kTRUE);
212   fContLCR->AddFrame(splitLeft, new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 0, 0, 0, 0));
213   
214    // right container
215   fContRight = new TGCompositeFrame(fContLCR, 200, 200, kVerticalFrame | kFixedWidth | kFitHeight);
216   fContLCR->AddFrame(fContRight, new TGLayoutHints(kLHintsTop | kLHintsRight | kLHintsExpandY, 3, 5, 3, 3));
217   
218    // center container
219   fContCenter = new TGCompositeFrame(fContLCR, 200, 200, kVerticalFrame | kFixedWidth | kFitHeight);
220   fContLCR->AddFrame(fContCenter, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
221   
222    // right vertical splitter
223   TGVSplitter *splitRight = new TGVSplitter(fContLCR);
224   splitRight->SetFrame(fContRight, kFALSE);
225   fContLCR->AddFrame(splitRight, new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 0, 0, 0, 0));
226   
227   
228    // ========================================================================
229    // ************************* content of fContLeft *************************
230    // ========================================================================
231    // --- draw button and tabLeft ---
232   // draw options
233   fContDrawOpt = new TGGroupFrame(fContLeft, "Draw options", kVerticalFrame | kFitWidth | kFitHeight);
234   fContLeft->AddFrame(fContDrawOpt, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
235   fChkDrawOptSame = new TGCheckButton(fContDrawOpt, "Same");
236   fContDrawOpt->AddFrame(fChkDrawOptSame, new TGLayoutHints(kLHintsNormal, 0, 2, 0, 0));
237   fChkDrawOptSame->SetToolTipText("Add draw option 'same'");
238   // additional draw options combo box
239   fComboAddDrawOpt = new TGComboBox(fContDrawOpt);
240   fComboAddDrawOpt->Resize(0, 22);
241   fComboAddDrawOpt->EnableTextInput(kTRUE);
242   fContDrawOpt->AddFrame(fComboAddDrawOpt, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
243     
244   // draw selection group
245   fContDrawSel = new TGGroupFrame(fContLeft, "Draw selection", kVerticalFrame | kFitWidth | kFitHeight);
246   fContLeft->AddFrame(fContDrawSel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 10, 0));
247   //x-axis variables selection, Run of Time
248   fContDrawSelSubRunTime = new TGCompositeFrame(fContDrawSel, 200, 23, kHorizontalFrame | kFitWidth | kFixedHeight);
249   fContDrawSel->AddFrame(fContDrawSelSubRunTime, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX , 0, 0, 0, 0));
250   
251   // ------------------------- content of fContDrawOpt -------------------------
252   // 
253   // Run radio button
254     // Time radio button
255   fRadioXhist = new TGRadioButton(fContDrawSelSubRunTime, "hist", kRadioXhist);
256   fContDrawSelSubRunTime->AddFrame(fRadioXhist, new TGLayoutHints(kLHintsNormal, 0, 2, 0, 0));
257   fRadioXhist->Connect("Clicked()", "AliCalibViewerGUItime", this, "HandleButtonsDrawSel()");
258   fRadioXhist->SetToolTipText("Draw the distribution of the variable");
259   
260   fRadioXrun = new TGRadioButton(fContDrawSelSubRunTime, ":Run", kRadioXrun);
261   fContDrawSelSubRunTime->AddFrame(fRadioXrun, new TGLayoutHints(kLHintsNormal, 2, 2, 0, 0));
262   fRadioXrun->Connect("Clicked()", "AliCalibViewerGUItime", this, "HandleButtonsDrawSel()");
263   fRadioXrun->SetToolTipText("Use run number as x-value");
264   
265   // Time radio button
266   fRadioXtime = new TGRadioButton(fContDrawSelSubRunTime, ":Time", kRadioXtime);
267   fContDrawSelSubRunTime->AddFrame(fRadioXtime, new TGLayoutHints(kLHintsNormal, 2, 2, 0, 0));
268   fRadioXtime->Connect("Clicked()", "AliCalibViewerGUItime", this, "HandleButtonsDrawSel()");
269   fRadioXtime->SetToolTipText("Use time stamp number as x-value");
270   
271   
272   // list of variables
273   fListVariables = new TGListBox(fContDrawSel);
274   fContDrawSel->AddFrame(fListVariables, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
275   fListVariables->Connect("Selected(Int_t)", "AliCalibViewerGUItime", this, "DoNewSelection()");
276
277   
278 //-------------------- run type selection ------------------------
279   // Parameter label
280   fLblRunType = new TGLabel(fContDrawSel, "Run Type:");
281   fLblRunType->SetTextJustify(kTextLeft);
282   fContDrawSel->AddFrame(fLblRunType, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
283   
284   fComboRunType = new TGComboBox(fContDrawSel);
285   fComboRunType->EnableTextInput(kFALSE);
286   fContDrawSel->AddFrame(fComboRunType, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
287   fComboRunType->Connect("Selected(Int_t)", "AliCalibViewerGUItime", this, "DoDraw()");
288   fComboRunType->Resize(0, 22);
289   
290   //-------------------- parameter selection ------------------------
291   // Parameter label
292   fLblPar = new TGLabel(fContDrawSel, "Parameter:");
293   fLblPar->SetTextJustify(kTextLeft);
294   fContDrawSel->AddFrame(fLblPar, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
295   
296   fNmbPar = new TGNumberEntry(fContDrawSel, 0, 1, -1, TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative, TGNumberFormat::kNELLimitMinMax, 0, 71);
297   fContDrawSel->AddFrame(fNmbPar, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
298   fNmbPar->Connect("ValueSet(Long_t)", "AliCalibViewerGUItime", this, "DoParLimitChange()");
299   fNmbPar->SetState(kFALSE);
300   
301   //-------------------- calibration type selection ------------------------
302   // label
303   // draw selection group
304   fContCalibType = new TGGroupFrame(fContLeft, "Calib type selection", kVerticalFrame | kFitWidth | kFitHeight);
305   fContLeft->AddFrame(fContCalibType, new TGLayoutHints(kLHintsExpandX , 0, 0, 10, 0));
306     
307     // list of variables
308   fListCalibType = new TGListBox(fContCalibType);
309   fContCalibType->AddFrame(fListCalibType, new TGLayoutHints(kLHintsNormal | kLHintsExpandX , 0, 0, 0, 0));
310   fListCalibType->Connect("Selected(Int_t)", "AliCalibViewerGUItime", this, "DoChangeSelectionList()");
311   fListCalibType->Resize(0,88);
312   fListCalibType->SetMultipleSelections();
313   
314   
315      // ==========================================================================
316    // ************************* content of fContCenter *************************
317    // ========================================================================
318    // main drawing canvas
319   fCanvMain = new TRootEmbeddedCanvas("GUItime_Canvas", fContCenter, 200, 200, kFitWidth | kFitHeight);
320   fContCenter->AddFrame(fCanvMain, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
321   fCanvMain->GetCanvas()->Connect("ProcessedEvent(Int_t, Int_t, Int_t, TObject*)", "AliCalibViewerGUItime", this, "MouseMove(Int_t, Int_t, Int_t, TObject*)");
322   fCanvMain->GetCanvas()->SetToolTipText("The Main_Canvas, here your plots are displayed.");
323   fCanvMain->GetCanvas()->SetRightMargin(0.062);
324   fCanvMain->GetCanvas()->SetLeftMargin(0.15);
325   
326    // =========================================================================
327    // ************************* content of fContRight *************************
328    // ========================================================================
329   //group frame for value information
330   fContValues = new TGGroupFrame(fContRight, "Data point info", kVerticalFrame | kFitWidth | kFitHeight);
331   fContRight->AddFrame(fContValues, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
332   //set layout manager
333   fContValues->SetLayoutManager(new TGMatrixLayout(fContValues, 0, 2, 4));
334   //value information labels
335
336   //run number label
337   fLblRunNumber = new TGLabel(fContValues, "Run:");
338   fLblRunNumber->SetTextJustify(kTextLeft);
339   fContValues->AddFrame(fLblRunNumber, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
340   //run number
341   fLblRunNumberVal = new TGLabel(fContValues, "0000000");
342   fLblRunNumberVal->SetTextJustify(kTextLeft);
343   fContValues->AddFrame(fLblRunNumberVal, new TGLayoutHints(kLHintsLeft | kLHintsExpandX, 0, 0, 0, 0));
344   //time stamp label
345   fLblRunTime = new TGLabel(fContValues, "Time:");
346   fLblRunTime->SetTextJustify(kTextLeft);
347   fContValues->AddFrame(fLblRunTime, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
348   //run number
349   fLblRunTimeVal = new TGLabel(fContValues, "00.00.0000\n00:00:00");
350   fLblRunTimeVal->SetTextJustify(kTextLeft);
351   fContValues->AddFrame(fLblRunTimeVal, new TGLayoutHints(kLHintsLeft | kLHintsExpandX, 0, 0, 0, 0));
352   //value label x
353   fLblValueX = new TGLabel(fContValues, "x-Value:");
354   fLblValueX->SetTextJustify(kTextLeft);
355   fContValues->AddFrame(fLblValueX, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
356   //value x
357   fLblValueXVal = new TGLabel(fContValues, "00.000e+00");
358   fLblValueXVal->SetTextJustify(kTextRight);
359   fContValues->AddFrame(fLblValueXVal, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
360   //value label y
361   fLblValueY = new TGLabel(fContValues, "y-Value:");
362   fLblValueY->SetTextJustify(kTextLeft);
363   fContValues->AddFrame(fLblValueY, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0));
364   //value y
365   fLblValueYVal = new TGLabel(fContValues, "00.000e+00");
366   fLblValueYVal->SetTextJustify(kTextRight);
367   fContValues->AddFrame(fLblValueYVal, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
368    // draw button
369   fBtnDumpRuns = new TGTextButton(fContRight, "&Dump runs");
370   fContRight->AddFrame(fBtnDumpRuns, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
371   fBtnDumpRuns->Connect("Clicked()", "AliCalibViewerGUItime", this, "DoDumpRuns()");
372   fBtnDumpRuns->SetToolTipText("Press to dump the run numbers of the current selection.");
373   // save button
374   fBtnSave = new TGTextButton(fContRight, "&Save picture");
375   fContRight->AddFrame(fBtnSave, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
376   fBtnSave->SetToolTipText("Press to save the current picture in the canvas");
377   fBtnSave->Connect("Clicked()", "AliCalibViewerGUItime", this, "SavePicture()");
378
379   //group frame for loading a file
380   fContLoad = new TGGroupFrame(fContRight, "Load file", kVerticalFrame | kFitWidth | kFitHeight);
381   fContRight->AddFrame(fContLoad, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
382
383     fContFilename = new TGCompositeFrame(fContLoad, 200, 200, kHorizontalFrame | kFitWidth | kFitHeight);
384     fContLoad->AddFrame(fContFilename, new TGLayoutHints(kLHintsExpandX, 0, 0, 2, 0));
385
386       fTxtFilename = new TGTextEntry(fContFilename, "Input file", 100);
387       fContFilename->AddFrame(fTxtFilename, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
388       fTxtFilename->Connect("DoubleClicked()", "AliCalibViewerGUItime", this, "HandleLoadRunTextEntry()");
389      
390     fContTreeName = new TGCompositeFrame(fContLoad, 200, 200, kHorizontalFrame | kFitWidth | kFitHeight);
391     fContLoad->AddFrame(fContTreeName, new TGLayoutHints(kLHintsExpandX, 0, 0, 2, 0));
392
393       fLblTreeName = new TGLabel(fContTreeName, "Tree Name:");
394       fLblTreeName->SetTextJustify(kTextLeft);
395       fContTreeName->AddFrame(fLblTreeName, new TGLayoutHints(kLHintsNormal | kLHintsCenterY, 0, 1, 0, 0));
396
397       fTxtTreeName = new TGTextEntry(fContTreeName, "trdTree");
398       if(fDetector.Contains("TPC")) fTxtTreeName->SetText("dcs");
399       fContTreeName->AddFrame(fTxtTreeName, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
400
401     fBtnLoadFile = new TGTextButton(fContLoad, "Load file", 100);
402     fContLoad->AddFrame(fBtnLoadFile, new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 0));
403     fBtnLoadFile->SetToolTipText("Load a file into viewer");
404     fBtnLoadFile->Connect("Clicked()", "AliCalibViewerGUItime", this, "HandleLoadRunButtons()");
405
406    // =========================================================================
407    // ****************** bottom content of fContTopBottom *********************
408    // =========================================================================
409   
410   // custom options container
411   // --- fComboCustom --- the custom draw line on the very low
412   fContCustom = new TGCompositeFrame(fContTopBottom, 200, 200, kHorizontalFrame | kFitWidth | kFitHeight);
413   fContTopBottom->AddFrame(fContCustom, new TGLayoutHints(kLHintsExpandX, 10, 0, 0, 0));
414
415          // ------------------------- content of fContCustom -------------------------
416   fLblCustomDraw = new TGLabel(fContCustom, "Custom draw: ");
417   fLblCustomDraw->SetTextJustify(kTextLeft);
418   fContCustom->AddFrame(fLblCustomDraw, new TGLayoutHints(kLHintsNormal, 5, 0, 0, 0));
419          // text field for custom draw command
420   fComboCustomDraw = new TGComboBox(fContCustom);
421   fComboCustomDraw->Resize(0, 22);
422   fComboCustomDraw->EnableTextInput(kTRUE);
423   fContCustom->AddFrame(fComboCustomDraw, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
424   fComboCustomDraw->Connect("ReturnPressed()", "AliCalibViewerGUItime", this, "DoCustomDraw()");
425   fComboCustomDraw->Connect("Selected(Int_t)", "AliCalibViewerGUItime", this, "DoCustomDraw()");
426   
427   
428       // additional cuts container
429   fContCustomCuts = new TGCompositeFrame(fContTopBottom, 200, 200, kHorizontalFrame | kFitWidth | kFitHeight);
430   fContTopBottom->AddFrame(fContCustomCuts, new TGLayoutHints(kLHintsExpandX, 10, 0, 0, 0));
431   
432          // ------------------------- content of fContCustomCuts -------------------------
433   fLblCustomCuts = new TGLabel(fContCustomCuts, "Custom cuts:  ");
434   fLblCustomCuts->SetTextJustify(kTextLeft);
435   fContCustomCuts->AddFrame(fLblCustomCuts, new TGLayoutHints(kLHintsNormal, 5, 0, 0, 0));
436          // combo text field for additional cuts
437   fComboCustomCuts = new TGComboBox(fContCustomCuts);
438 //   fComboCustomCuts->Resize(0, fLblValueY->GetDefaultHeight());
439   fComboCustomCuts->Resize(0, 22);
440   fComboCustomCuts->EnableTextInput(kTRUE);
441   fContCustomCuts->AddFrame(fComboCustomCuts, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 0, 0, 0, 0));
442   fComboCustomCuts->Connect("ReturnPressed()", "AliCalibViewerGUItime", this, "DoCustomCutsDraw()");
443   fComboCustomCuts->Connect("Selected(Int_t)", "AliCalibViewerGUItime", this, "DoCustomCutsDraw()");
444
445   if(fDetector.Contains("TPC")) SetWindowName("AliTPCCalibViewer GUI - Time");
446   else if (fDetector.Contains("TRD")) SetWindowName("AliTRDCalibViewer GUI - Time");
447   else SetWindowName("Unknown detector");
448   MapSubwindows();
449   Resize(GetDefaultSize());
450   MapWindow();
451 }
452
453 //______________________________________________________________________________
454 void AliCalibViewerGUItime::SetInitialValues(){
455   //
456   // Set inital selections of the gui
457   //
458   fRadioXrun->SetState(kButtonDown);
459   fRadioXtime->SetState(kButtonUp);
460 }
461
462 //______________________________________________________________________________
463 void AliCalibViewerGUItime::UseFile(const char* fileName, const char* treeName) {
464   //
465   // retrieve tree from file
466   //
467   TString s(fileName);
468   TObjArray *arr=s.Tokenize(" ");
469   TIter next(arr);
470   TObject *o=0;
471   if (fTree) delete fTree;
472   fTree=new TChain(treeName);
473   while ( (o=next()) ){
474     TString tmpString(o->GetName());
475     std::cout << "Adding " << tmpString.Data() << " to the chain" << std::endl;
476     if(tmpString.Contains(".root"))
477       fTree->AddFile(tmpString.Data());
478     else {
479       tmpString += "/*.root";
480       fTree->Add(tmpString.Data());
481     }
482   }
483   delete arr;
484   if (!CheckChain())
485     return;
486   //  UseConfigFile(fConfigFile.Data());
487   FillCalibTypes();
488   Reload();
489 }
490
491 //______________________________________________________________________________
492 void AliCalibViewerGUItime::UseChain(TChain* chain)
493 {
494   //
495   // load a chain
496   //
497   fTree=chain;
498   if (!CheckChain()) {
499     std::cout << "AliCalibViewerGUItime::UseChain !checkchain OUT" << std::endl;
500     return;
501   }
502   //set configuration file
503   //  UseConfigFile(fConfigFile.Data());
504   FillCalibTypes();
505   Reload();
506 }
507
508 //______________________________________________________________________________
509 Bool_t AliCalibViewerGUItime::CheckChain()
510 {
511   //
512   // check whether cahin has entries
513   // decide whether to draw graphs in 2D
514   //
515   if (!fTree) {
516     return kFALSE;
517   }
518   fTree->Lookup();
519   Long64_t entries=fTree->GetEntries();
520   if (entries==0){
521     AliError("No entries found in chain");
522     return kFALSE;
523   }
524   //check whether to draw graphs
525   CheckDrawGraph();
526   return kTRUE;
527 }
528
529 //______________________________________________________________________________
530 void AliCalibViewerGUItime::FillRunTypes()
531 {
532   //
533   //Loop over the tree entries and fill the run types
534   //
535   if (!fTree) {
536     return;
537   }
538   Int_t id=0;
539   fComboRunType->RemoveAll();
540   fComboRunType->AddEntry("ALL",id++);
541   fComboRunType->Select(0,kFALSE);
542   if (!fTree->GetBranch("runType.")) {
543     return;
544   }
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 //______________________________________________________________________________
560 void AliCalibViewerGUItime::FillCalibTypes()
561 {
562   //
563   // loop over configuration and fill calibration types
564   //
565   Int_t id=0;
566   fListCalibType->RemoveAll();
567   //  TObject *o=0x0;
568   //  fConfigParser->ResetIter();
569   TString type="UNSPECIFIED";
570   fListCalibType->AddEntry(type.Data(),id);
571   fListCalibType->Select(id++);
572 }
573
574 //______________________________________________________________________________
575 void AliCalibViewerGUItime::CheckDrawGraph()
576 {
577   //
578   // Check whether to draw graphs in 2D mode based on the number of entries in the chain
579   // GetEstimate() returns the maximum size of the arrays stored in GetV1()...
580   //
581   if (!fTree) {
582     return;
583   }
584   fNoGraph=kTRUE;
585   if (fTree->GetEntries()<fTree->GetEstimate()) fNoGraph=kFALSE;
586 }
587
588 //______________________________________________________________________________
589 void AliCalibViewerGUItime::Reload(Int_t /*first*/)
590 {
591   //
592   // reload the gui contents, this is needed after the input tree has changed
593   //
594   if ( !fTree ) {
595     return;
596   }
597   
598   FillRunTypes();
599   
600   //activate all branches
601   fTree->SetBranchStatus("*",1);
602   //reset variables list
603   fListVariables->RemoveAll();
604   //get selected calibration types
605   TList calibTypes;
606   fListCalibType->GetSelectedEntries(&calibTypes);
607     
608   TObjArray *branchList = fTree->GetListOfBranches();
609   if ( !branchList ) return;
610   TIter nextBranch(branchList);
611   Int_t idCount=0,id=0;
612   TObject *objBranch=0;
613   while ( (objBranch=nextBranch()) ){
614     TString branchName(objBranch->GetName());
615     TString branchTitle(objBranch->GetName());
616     if (branchName == "run" || branchName == "time" || branchName == "runType.") continue;
617     Bool_t active=kTRUE;
618     TString calibType="UNSPECIFIED";
619    
620     //check if branch is in selected calibration types
621     //if not, don't show it in the list and deactivate the branch.
622     Bool_t calibActive=kFALSE;
623     TIter nextCalib(&calibTypes);
624     TObject *objCalib=0;
625     while ( (objCalib=nextCalib()) ) {
626       if (calibType==objCalib->GetTitle()) calibActive=kTRUE;
627     }
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->Select(id);
637     ++idCount;
638   }
639   //trick to display modifications
640   fListVariables->Resize(fListVariables->GetWidth()-1, fListVariables->GetHeight());
641   fListCalibType->Resize(fListCalibType->GetWidth()+1, fListCalibType->GetHeight());
642 }
643
644 //______________________________________________________________________________
645 void AliCalibViewerGUItime::AddReferenceTree(const char* treeFileName, const char* refName)
646 {
647   //
648   // map of reference trees that should always be attached to the CalibViewerGUI
649   //
650   fMapRefTrees->Add(new TObjString(refName), new TObjString(treeFileName));
651 }
652
653 //______________________________________________________________________________
654 const char* AliCalibViewerGUItime::GetDrawString(){
655   //
656   // create draw string for ttree by combining the user requestsa
657   //
658   
659   TString selectedVariable="";
660   Int_t id=-1;
661   if (!fListVariables->GetSelectedEntry()) {
662     return "";
663   }
664   selectedVariable = fListVariables->GetSelectedEntry()->GetTitle();
665   id=fListVariables->GetSelectedEntry()->EntryId();
666   TString branchName=selectedVariable;
667   //  const TObject *key=(*fConfigParser)(id);
668   //  if (key) branchName=(*fConfigParser)(id)->GetName();
669   //treat case of TVector
670   if (branchName.EndsWith(".")){
671     Int_t par = (Int_t)(fNmbPar->GetNumber());
672     branchName.Append(Form("fElements[%d]",par));
673   }
674
675   return branchName.Data();
676 }
677 //______________________________________________________________________________
678 const char* AliCalibViewerGUItime::GetDrawOption() const {
679   //
680   // get user selected draw options
681   //
682   TString drawOpt;
683   if (fComboAddDrawOpt->GetSelectedEntry()) drawOpt=fComboAddDrawOpt->GetSelectedEntry()->GetTitle();
684   if (fChkDrawOptSame->GetState()==kButtonDown && !drawOpt.Contains("same",TString::kIgnoreCase))
685     drawOpt+="same";
686   return drawOpt.Data();
687 }
688 //______________________________________________________________________________
689 void AliCalibViewerGUItime::GetCutString(TString &cutStr){
690   //
691   // create cut string
692   //
693   TCut cuts(fComboCustomCuts->GetTextEntry()->GetText());
694   TString runType="";
695   if (fComboRunType->GetSelectedEntry()) runType=fComboRunType->GetSelectedEntry()->GetTitle();
696   if (runType!="ALL"&&!runType.IsNull()) cuts+=Form("runType.String().Data()==\"%s\"",runType.Data());
697   cutStr=cuts.GetTitle();
698 }
699 //______________________________________________________________________________
700 void AliCalibViewerGUItime::UpdateValueArrays(Bool_t withGraph)
701 {
702   //
703   // Update arrays of runs
704   //
705   if (!withGraph){
706     fValuesX.ResizeTo(1);
707     fValuesY.ResizeTo(1);
708     fRunNumbers.ResizeTo(1);
709     fTimeStamps.ResizeTo(1);
710   } else {
711     fValuesX.ResizeTo(fTree->GetSelectedRows());
712     fValuesY.ResizeTo(fTree->GetSelectedRows());
713     fRunNumbers.ResizeTo(fTree->GetSelectedRows());
714     fTimeStamps.ResizeTo(fTree->GetSelectedRows());
715     fValuesY.SetElements(fTree->GetV3());
716     fRunNumbers.SetElements(fTree->GetV1());
717     fTimeStamps.SetElements(fTree->GetV2());
718   }
719 }
720
721 //______________________________________________________________________________
722 void AliCalibViewerGUItime::GetHistogramTitle(TString &title)
723 {
724   //
725   // Create string for histogram title
726   //
727
728   title=fDrawString;
729   Int_t pos=title.First(">>");
730   if (pos>0) title=title(0,pos);
731   if (!fIsCustomDraw){
732     if (fRadioXrun->GetState()==kButtonDown){
733       title+=":Run";
734     } else if (fRadioXtime->GetState()==kButtonDown){
735       title+=":Date";
736     }
737   }
738   TString cuts;
739   GetCutString(cuts);
740   TObjArray *arr=title.Tokenize(":");
741   TObject *o=0x0;
742   title+=" {";
743   title+=cuts;
744   title+="}";
745   TIter next(arr,kIterBackward);
746   while ( (o=next()) ){
747     TString varName=o->GetName();
748     title+=";";
749     title+=varName;
750   }
751   delete arr;
752 }
753
754 //______________________________________________________________________________
755 void AliCalibViewerGUItime::AdjustYRange()
756 {
757   //
758   // adjust the range of the Y axis
759   //
760   TIter nextGraphicObject(fTrashBox);
761   TObject *o=0x0;
762   Float_t min=0,max=0;
763   while ( (o=nextGraphicObject()) ){
764     if (o->IsA()==TGraph::Class()){
765       TGraph *gr=(TGraph*)o;
766       if (min==max) {
767         min=TMath::MinElement(gr->GetN(),gr->GetY());
768         max=TMath::MaxElement(gr->GetN(),gr->GetY());
769       } else {
770         Float_t currmin=TMath::MinElement(gr->GetN(),gr->GetY());
771         Float_t currmax=TMath::MaxElement(gr->GetN(),gr->GetY());
772         if (currmax>max) max=currmax;
773         if (currmin<min) min=currmin;
774       }
775     }
776   }
777   if (min!=max){
778     if (min!=0) min=min-(max-min)/10;
779     if (max!=0) max=max+(max-min)/10;
780     fCurrentHist->SetMinimum(min);
781     fCurrentHist->SetMaximum(max);
782   }
783 }
784 //______________________________________________________________________________
785 void AliCalibViewerGUItime::DoDraw() {
786   //
787   // Draw graphics
788   //
789   TString drawString=fDrawString;
790   TString cutString;
791   GetCutString(cutString);
792   TString optString  = GetDrawOption();
793   Bool_t graphOutput=!fNoGraph;  //ttree buffer for V1, V2... too small
794   graphOutput&=(drawString.First(">>")<0); //histogram output in custom draw
795   graphOutput&=fRadioXhist->GetState()!=kButtonDown; //histogram drawing selected
796   graphOutput&=!(fIsCustomDraw&&!fDrawString.Contains(":")); //custom draw 1D
797   Bool_t drawSame=optString.Contains("same",TString::kIgnoreCase);
798 //   optString+="goff";
799   if (graphOutput) {
800     drawString.Prepend("run:time:");
801     optString="goff";
802   }else{
803     if (!fIsCustomDraw){
804       if (fRadioXrun->GetState()==kButtonDown){
805         drawString+=":run";
806       } else if (fRadioXtime->GetState()==kButtonDown){
807         drawString+=":time";
808       }
809     }
810   }
811   TVirtualPad *padsave=gPad;
812   fCanvMain->GetCanvas()->cd();
813   //delete old histograms and graphs
814   if (!drawSame){
815     fTrashBox->Delete();
816     fCurrentGraph=0x0;
817     fCurrentHist=0x0;
818   }
819
820   //select data
821   fTree->Draw(drawString.Data(),cutString.Data(),optString.Data());
822   if (fTree->GetSelectedRows()==-1) {
823     return;
824   }
825   UpdateValueArrays(graphOutput);
826   TString title;
827   GetHistogramTitle(title);
828   Bool_t drawGraph=kFALSE;
829   if (graphOutput){
830     if (fIsCustomDraw){
831       if (fDrawString.Contains(":")){
832         fValuesX.SetElements(fTree->GetV4());
833         drawGraph=kTRUE;
834       } else {
835         drawGraph=kFALSE;
836       }
837     }else{
838       drawGraph=kTRUE;
839       if (fRadioXrun->GetState()==kButtonDown){
840         fValuesX.SetElements(fTree->GetV1());
841       } else if (fRadioXtime->GetState()==kButtonDown){
842         fValuesX.SetElements(fTree->GetV2());
843       } else {
844         drawGraph=kFALSE;
845       }
846     }
847   }//create graph according to selection
848   if (drawGraph){
849     TGraph *graph=new TGraph(fValuesX,fValuesY);
850     TString grDraw="p";
851     if (!drawSame) grDraw+="a";
852     if (!fIsCustomDraw) grDraw+="l";
853     graph->Draw(grDraw.Data());
854     graph->SetEditable(kFALSE);
855     TH1 *hist=graph->GetHistogram();
856     hist->SetTitle(title.Data());
857     fTrashBox->Add(graph);
858     graph->SetLineColor(fTrashBox->GetEntries());
859     graph->SetMarkerColor(fTrashBox->GetEntries());
860     if (!drawSame) {
861       fCurrentGraph=graph;
862       fCurrentHist=hist;
863     }
864   } else {
865     TH1 *hist=fTree->GetHistogram();
866     hist->SetTitle(title.Data());
867     fTrashBox->Add(hist);
868     hist->SetLineColor(fTrashBox->GetEntries());
869     hist->SetMarkerColor(fTrashBox->GetEntries());
870     if (!drawSame) fCurrentHist=hist;
871   }
872   
873   //Set time axis if choosen as x-variables
874   if (fRadioXtime->GetState()==kButtonDown&&!fIsCustomDraw&&!drawSame){
875     TAxis *xaxis=fCurrentHist->GetXaxis();
876     xaxis->SetTimeFormat("#splitline{%d.%m}{%H:%M}");
877     xaxis->SetTimeDisplay(1);
878     xaxis->SetLabelOffset(xaxis->GetLabelOffset()*3);
879     xaxis->SetLabelSize(xaxis->GetLabelSize()/1.3);
880   }
881   if (!drawSame) {
882   //Set title offset
883     fCurrentHist->GetYaxis()->SetTitleOffset(1.5);
884   } else {
885     //adjust y-range
886     AdjustYRange();
887   }
888   gPad->Modified();
889   gPad->Update();
890   padsave->cd();
891 }
892
893 //______________________________________________________________________________
894 void AliCalibViewerGUItime::DoDumpRuns()
895 {
896   //
897   // Dump the current run numbers to stdout
898   //
899   Int_t npoints=fRunNumbers.GetNrows();
900   Int_t    *sortIndex = new Int_t[npoints];
901   TMath::Sort(npoints,fRunNumbers.GetMatrixArray(),sortIndex,kFALSE);
902   Int_t run=0, prevRun=-1;
903   
904   for (Int_t irun=0;irun<npoints;++irun){
905     run=(Int_t)fRunNumbers.GetMatrixArray()[sortIndex[irun]];
906     if (run!=prevRun) std::cout << Form("%d",run) << std::endl;
907     prevRun=run;
908   }
909   delete [] sortIndex;
910 }
911
912 //______________________________________________________________________________
913 void AliCalibViewerGUItime::DoParLimitChange()
914 {
915   //
916   // DoParLimitChange()
917   //
918   UpdateParName();
919   DoDraw();
920 }
921
922 //______________________________________________________________________________
923 void AliCalibViewerGUItime::DoNewSelection() {
924   //
925    // decides whether to redraw if user makes another selection
926    //
927   UpdateParLimits();
928   fDrawString=GetDrawString();
929   fIsCustomDraw=kFALSE;
930   DoDraw();
931 }
932
933 //______________________________________________________________________________
934 void AliCalibViewerGUItime::DoCustomDraw()
935 {
936   //
937   // Custom draw (TTree::Draw syntax)
938   //
939   fDrawString=fComboCustomDraw->GetTextEntry()->GetText();
940   fNmbPar->SetState(kFALSE);
941   fIsCustomDraw=kTRUE;
942   DoDraw();
943 }
944
945 //______________________________________________________________________________
946 void AliCalibViewerGUItime::DoCustomCutsDraw()
947 {
948   //
949   // Custom cuts (TTree::Draw syntax)
950   //
951   if (fIsCustomDraw) DoCustomDraw();
952   else {
953     fDrawString=GetDrawString();
954     fIsCustomDraw=kFALSE;
955     DoDraw();
956   }
957 }
958
959 //______________________________________________________________________________
960 void AliCalibViewerGUItime::HandleButtonsDrawSel(Int_t id)
961 {
962   //
963   // Draw selection button handling (x-variable)
964   //
965
966   if (id == -1) {
967     TGButton *btn = (TGButton *) gTQSender;
968     id = btn->WidgetId();
969   }
970   
971   Bool_t doDraw=kFALSE;
972   switch (id) {
973   case (kRadioXhist):
974     doDraw=(fRadioXtime->GetState()==kButtonDown||fRadioXrun->GetState()==kButtonDown);
975     if (doDraw){
976       fRadioXrun->SetState(kButtonUp);
977       fRadioXtime->SetState(kButtonUp);
978     }
979     break;
980   case (kRadioXrun):
981     doDraw=(fRadioXtime->GetState()==kButtonDown||fRadioXhist->GetState()==kButtonDown);
982     if (doDraw){
983       fRadioXhist->SetState(kButtonUp);
984       fRadioXtime->SetState(kButtonUp);
985     }
986     break;
987   case (kRadioXtime):
988     doDraw=(fRadioXhist->GetState()==kButtonDown||fRadioXrun->GetState()==kButtonDown);
989     if (doDraw){
990       fRadioXrun->SetState(kButtonUp);
991       fRadioXhist->SetState(kButtonUp);
992     }
993     break;
994   }
995   if (doDraw) DoCustomCutsDraw();
996 }
997 //______________________________________________________________________________
998 void AliCalibViewerGUItime::UpdateParName()
999 {
1000   //
1001   // change parameter name
1002   //
1003   
1004   Int_t par = (Int_t)(fNmbPar->GetNumber());
1005   TString parName="";
1006   parName.Form("%d",par);
1007   fLblPar->SetText(Form("Parameter: %s",parName.Data()));
1008   fDrawString=GetDrawString();
1009   fIsCustomDraw=kFALSE;
1010 }
1011
1012 //______________________________________________________________________________
1013 void AliCalibViewerGUItime::UpdateParLimits()
1014 {
1015   //
1016   // Adjust limits for TVectorT based variables
1017   //
1018   if (!fTree) return;
1019   TString selectedVariableTitle="";
1020   Int_t id=-1;
1021   if (!fListVariables->GetSelectedEntry()) return;
1022   selectedVariableTitle = fListVariables->GetSelectedEntry()->GetTitle();
1023   id=fListVariables->GetSelectedEntry()->EntryId();
1024   TString selectedVariable=selectedVariableTitle;
1025   //  const TObject *key=(*fConfigParser)(id);
1026   //  if (key) selectedVariable=(*fConfigParser)(id)->GetName();
1027   
1028   if (selectedVariable.IsNull()||!selectedVariable.EndsWith(".")) {
1029     fNmbPar->SetState(kFALSE);
1030     fLblPar->SetText("Parameter: none");
1031     return;
1032   }
1033   TVectorD *vD=0x0;
1034   TVectorF *vF=0x0;
1035   Int_t maxPar=0;
1036   fTree->GetEntry(1);
1037   TBranch *branch=fTree->GetTree()->GetBranch(selectedVariable.Data());
1038   TString branchClass=branch->GetClassName();
1039   Int_t event=0;
1040   if (branchClass=="TVectorT<double>"){
1041 //     branch->SetAddress(&vD);
1042     fTree->SetBranchAddress(selectedVariable.Data(),&vD);
1043     while (maxPar<2&&event<fTree->GetEntries()){
1044       fTree->GetEntry(event++);
1045       maxPar=vD->GetNrows();
1046     }
1047   } else if (branchClass=="TVectorT<float>"){
1048 //     branch->SetAddress(&vF);
1049     fTree->SetBranchAddress(selectedVariable.Data(),&vF);
1050     while (maxPar<2&&event<fTree->GetEntries()){
1051       fTree->GetEntry(event++);
1052       maxPar=vF->GetNrows();
1053     }
1054   } else {
1055     //class not known
1056     fNmbPar->SetState(kFALSE);
1057     return;
1058   }
1059   fTree->SetBranchAddress(selectedVariable.Data(),0x0);
1060   if (fNmbPar->GetNumMax()!=maxPar-1) fNmbPar->SetNumber(0);
1061   fNmbPar->SetLimitValues(0,maxPar-1);
1062   fNmbPar->SetState(kTRUE);
1063   UpdateParName();
1064 }
1065 //______________________________________________________________________________
1066 void AliCalibViewerGUItime::MouseMove(Int_t event, Int_t x, Int_t y, TObject */*selected*/)
1067 {
1068   //
1069   // handle mouse events in the draw canvas
1070   //
1071   UInt_t dd=0,mm=0,yy=0,HH=0,MM=0,SS=0,run=0;
1072   Double_t valx=0.,valy=0.;
1073   if (!fCurrentGraph) {
1074     fLblRunNumberVal->SetText(Form("%07u",run));
1075     fLblRunTimeVal->SetText(Form("%02u.%02u.%04u\n%02u:%02u:%02u",dd,mm,yy,HH,MM,SS));
1076     fLblValueXVal->SetText(Form("%.3e", valx));
1077     fLblValueYVal->SetText(Form("%.3e", valy));
1078     return;
1079   }
1080   TVirtualPad *padsave=gPad;
1081   fCanvMain->GetCanvas()->cd();
1082   Int_t n=fValuesY.GetNrows();
1083   Double_t *arr=0x0;
1084   arr=fValuesX.GetMatrixArray();
1085
1086   Int_t minDist=1000000;
1087   Int_t minPoint=-1;
1088   for (Int_t i=0;i<n;++i){
1089     Int_t pxp = gPad->XtoAbsPixel(gPad->XtoPad(arr[i]));
1090     Int_t pyp = gPad->YtoAbsPixel(gPad->YtoPad(fValuesY[i]));
1091     Int_t d   = TMath::Nint(TMath::Sqrt(TMath::Abs(pxp-x) + TMath::Abs(pyp-y)));
1092     if (d < minDist) {
1093       minDist  = d;
1094       minPoint = i;
1095     }
1096   }
1097   if (minDist<2){
1098     TTimeStamp t((Int_t)fTimeStamps[minPoint]);
1099     t.GetDate(kTRUE,0,&yy,&mm,&dd);
1100     t.GetTime(kTRUE,0,&HH,&MM,&SS);
1101     run=(UInt_t)fRunNumbers[minPoint];
1102     valx=fValuesX[minPoint];
1103     valy=fValuesY[minPoint];
1104   } else {
1105     dd=0;mm=0;yy=0;HH=0;MM=0;SS=0;run=0;
1106     valx=0.;
1107     valy=0.;
1108   }
1109   fLblRunNumberVal->SetText(Form("%07u",run));
1110   fLblRunTimeVal->SetText(Form("%02u.%02u.%04u\n%02u.%02u.%02u",dd,mm,yy,HH,MM,SS));
1111   if (fIsCustomDraw){
1112     fLblValueXVal->SetText(Form("%.3e", valx));
1113   }else{
1114     if (fRadioXrun->GetState()==kButtonDown){
1115       fLblValueXVal->SetText("Run");
1116     } else if (fRadioXtime->GetState()==kButtonDown){
1117       fLblValueXVal->SetText("Time");
1118     }
1119   }
1120   fLblValueYVal->SetText(Form("%.3e", valy));
1121   padsave->cd();
1122   if (run==0) {
1123     return;
1124   }
1125   if (event == kButton1Double ){
1126     SetGuiTree(run);
1127   }
1128   //find closes point of current selection
1129 }
1130
1131 //______________________________________________________________________________
1132 void AliCalibViewerGUItime::SetGuiTree(Int_t run)
1133 {
1134   //
1135   // create the AliCalibViewerGUI tree for run
1136   // cache tree in directory fOutputCacheDir
1137   // retrieve file from this directory if it already exists
1138   //
1139
1140   //try to find file for run in fOutputCacheDir
1141   TString fileName=fOutputCacheDir;
1142   if (!fileName.EndsWith("/")) fileName+="/";
1143   if(fDetector.Contains("TPC")) fileName+=Form("guiTreeRun_%d.root",run);
1144   else if(fDetector.Contains("TRD")) fileName+=Form("trdOCDBDetails_run%d.root", run);
1145   else return;
1146   Bool_t load=kTRUE;
1147   TFile f(fileName.Data());
1148   if (!f.IsOpen()){
1149     if(fDetector.Contains("TRD")) 
1150       load = fCalibViewerGUI->CreateDetailsTree(run, fileName.Data(), "nothing");
1151     if(fDetector.Contains("TPC")) 
1152       load = fCalibViewerGUI->CreateDetailsTree(run, fileName.Data(), "nothing");
1153
1154     if (!load){
1155       fCalibViewerGUI->Reset();
1156       if (fCalibViewerGUItab) fCalibViewerGUItab->SetText(new TGString(Form("Detail - XXXXX")));
1157       return;
1158     }
1159   }
1160   f.Close();
1161   if(fDetector.Contains("TPC")) fCalibViewerGUI->Initialize(fileName.Data(), "calPads");
1162   else fCalibViewerGUI->Initialize(fileName.Data(), "TRDcalibDetails");
1163   if (fCalibViewerGUItab) fCalibViewerGUItab->SetText(new TGString(Form("Detail - %07d",run)));
1164   TIter nextRefTree(fMapRefTrees);
1165   TObject *o=0x0;
1166   while ( (o=nextRefTree()) ){
1167     if(fDetector.Contains("TPC"))
1168       fCalibViewerGUI->GetViewer()->AddReferenceTree(fMapRefTrees->GetValue(o)->GetName(),"calPads",o->GetName());
1169     else
1170       fCalibViewerGUI->GetViewer()->AddReferenceTree(fMapRefTrees->GetValue(o)->GetName(),"TRDcalibDetails",o->GetName());
1171   }
1172   //if(fDetector.Contains("TPC")) ((AliTPCCalibViewerGUI_new*)fCalibViewerGUI)->Reload();
1173   //else ((AliTRDCalibViewerGUI*)fCalibViewerGUI)->Reload();
1174   fCalibViewerGUI->Reload();
1175 }
1176
1177 //______________________________________________________________________________
1178 const char* AliCalibViewerGUItime::SubstituteUnderscores(const char* in)
1179 {
1180   //
1181   // Substitute underscores from the branch names
1182   //
1183   TString s(in);
1184   s.ReplaceAll("_{","|{");
1185   s.ReplaceAll("_"," ");
1186   s.ReplaceAll("|{","_{");
1187   return s.Data();
1188 }
1189
1190 //______________________________________________________________________________
1191 void AliCalibViewerGUItime::SavePicture() {
1192    // 
1193    // saves the current picture
1194    // 
1195    
1196   const char *kSaveAsTypes[] = {
1197     "Postscript",  "*.ps",
1198     "Encapsulated Postscript",   "*.eps",
1199     "PDF",   "*.pdf",
1200     "JPEG",   "*.jpg",
1201     "PNG",   "*.png",
1202     "TIFF",   "*.tiff",
1203     "GIF",   "*.gif",
1204     "XPM",   "*.xpm",
1205     "SVG",   "*.svg",
1206     "XML",   "*.xml",
1207     "C++ macro",   "*.cxx",
1208     "Macro file",  "*.C",
1209     "ROOT file",   "*.root",
1210     "All file",    "*",
1211     0,              0
1212   };
1213   TString dir(".");
1214   TGFileInfo fi;
1215   fi.fFileTypes = kSaveAsTypes;
1216   // fi.fIniDir    = StrDup(dir);
1217   fi.fOverwrite = kFALSE;
1218   new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDSave, &fi);
1219   if (fi.fFilename && strlen(fi.fFilename)) {
1220     fCanvMain->GetCanvas()->Print(fi.fFilename);
1221   }  
1222 }
1223
1224 //______________________________________________________________________________
1225 void AliCalibViewerGUItime::HandleLoadRunButtons() {
1226   //
1227   //  Handle the buttons
1228   //
1229   Int_t id = ((TGTextButton*)gTQSender)->WidgetId();
1230   if(id==100) {
1231     UseFile(fTxtFilename->GetText(), fTxtTreeName->GetText());
1232     return;
1233   }
1234   else
1235     return;
1236 }
1237
1238 //______________________________________________________________________________
1239 void AliCalibViewerGUItime::HandleLoadRunTextEntry() {
1240   //
1241   //  Handle the text entries
1242   //
1243   // buttons ID
1244   // 100 - fTxtFilename
1245   // 101 - fTxtConfigFile
1246   
1247   Int_t id = ((TGTextEntry*)gTQSender)->WidgetId();
1248   if(id>=100 && id<=103) {
1249     const char *kTypes[] = {
1250       "All files",    "*",
1251       0,              0
1252     };
1253     TString dir(".");
1254     TGFileInfo fi;
1255     fi.fFileTypes = kTypes;
1256     fi.fOverwrite = kFALSE;
1257     new TGFileDialog(gClient->GetRoot(), gClient->GetRoot(), kFDOpen, &fi);
1258     if(fi.fFilename && strlen(fi.fFilename)) {
1259       if(id==100) {
1260         fTxtFilename->SetText(fi.fFilename);
1261       }
1262       if(id==101) {
1263         //        fTxtConfigFile->SetText(fi.fFilename);
1264         //        fConfigFile=fi.fFilename;
1265       }
1266       if(id==102) {
1267         //fTxtRunList->SetText(fi.fFilename);
1268       }
1269       if(id==103) {
1270         //fTxtOutputOCDB->SetText(fi.fFilename);
1271       }
1272     }
1273     return;
1274   }
1275   else {
1276     return;
1277   }
1278 }
1279
1280 //__________________________________________________________________
1281 TObjArray* AliCalibViewerGUItime::ShowGUI(TChain* chain) {
1282   //
1283   //  Launch the time trending GUI. Load the TChain chain into the viewer
1284   //
1285   TGMainFrame* frmMain = new TGMainFrame(gClient->GetRoot(), 1000, 650);
1286   frmMain->SetWindowName("Calib Viewer - time trend");
1287   frmMain->SetCleanup(kDeepCleanup);
1288
1289   TGTab* tabMain = new TGTab(frmMain, 1000, 600);
1290   frmMain->AddFrame(tabMain, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1291
1292   TGCompositeFrame* tabCont1 = tabMain->AddTab("Time");
1293
1294   AliCalibViewerGUItime* calibViewerTime = new AliCalibViewerGUItime(tabCont1, 1000, 650);
1295   tabCont1->AddFrame(calibViewerTime, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1296   calibViewerTime->UseChain(chain);
1297
1298   TObjArray *guiArray = new TObjArray();
1299   guiArray->Add(calibViewerTime);
1300
1301   frmMain->MapSubwindows();
1302   frmMain->Resize();
1303   frmMain->MapWindow();
1304   return guiArray;
1305 }
1306
1307 //__________________________________________________________________
1308 TObjArray* AliCalibViewerGUItime::ShowGUI() {
1309   //
1310   //  Launch the time trending GUI. The GUI will be empty but trees can be loaded by using the GUI interface
1311   //
1312   TGMainFrame* frmMain = new TGMainFrame(gClient->GetRoot(), 1000, 650);
1313   frmMain->SetWindowName("Calib Viewer - time trend");
1314   frmMain->SetCleanup(kDeepCleanup);
1315
1316   TGTab* tabMain = new TGTab(frmMain, 1000, 600);
1317   frmMain->AddFrame(tabMain, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1318
1319   TGCompositeFrame* tabCont1 = tabMain->AddTab("Time");
1320
1321   AliCalibViewerGUItime* calibViewerTime = new AliCalibViewerGUItime(tabCont1, 1000, 650);
1322   tabCont1->AddFrame(calibViewerTime, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0, 0, 0, 0));
1323   
1324   TObjArray *guiArray = new TObjArray();
1325   guiArray->Add(calibViewerTime);
1326
1327   frmMain->MapSubwindows();
1328   frmMain->Resize();
1329   frmMain->MapWindow();
1330   return guiArray;
1331 }