]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveDet/AliEveTRDTrackListEditor.cxx
Moving PWG1 to PWGPP
[u/mrichter/AliRoot.git] / EVE / EveDet / AliEveTRDTrackListEditor.cxx
1 // Author: Benjamin Hess   29/01/2010
2
3 /*************************************************************************
4  * Copyright (C) 2009-2010, Alexandru Bercuci, Benjamin Hess.            *
5  * All rights reserved.                                                  *
6  *************************************************************************/
7
8 //////////////////////////////////////////////////////////////////////////
9 //                                                                      //
10 // AliEveTRDTrackListEditor                                             //
11 //                                                                      //
12 // The AliEveTRDTrackListEditor provides the graphical functionality    //
13 // for the AliEveTRDTrackList. It creates the tabs and canvases, when   //
14 // they are needed and, as well, frees allocated memory on destruction  //
15 // (or if new events are loaded and thus some tabs are closed).         //
16 // The function DrawHistos() accesses the temporary file created by the //
17 // AliEveTRDTrackList and draws the desired data (the file will be      //
18 // created within the call of ApplyMacros()). Have a look at this       //
19 // function to learn more about the structure of the file and how to    //
20 // access the data.                                                     //
21 //////////////////////////////////////////////////////////////////////////
22
23 #include <EveDet/AliEveTRDData.h>
24 #include <EveDet/AliEveTRDTrackList.h>
25 #include "AliEveTRDTrackListEditor.h"
26
27 #include <EveBase/AliEveEventManager.h>
28 #include <AliTRDReconstructor.h>
29 #include <AliTRDtrackV1.h>
30 #include <TGButton.h>
31 #include <TCanvas.h>     
32 #include <TEveBrowser.h>
33 #include <TEveGedEditor.h> 
34 #include <TEveMacro.h>
35 #include <TEveManager.h>
36 #include <TFile.h>
37 #include <TG3DLine.h>
38 #include <TGButtonGroup.h>
39 #include <TGFileDialog.h>
40 #include <TGLabel.h>
41 #include <TGListBox.h>
42 #include <TGMsgBox.h>
43 #include <TGTab.h>
44 #include <TMap.h>
45 #include <TObjString.h>
46 #include <TROOT.h>
47 #include <TSystem.h>
48 #include <TGTextEntry.h>
49 #include <TGTextEdit.h>
50 #include <TGComboBox.h>
51 #include <TGTextView.h>
52 #include <TH1.h>
53 #include <TTreeStream.h>
54
55
56 ClassImp(AliEveTRDTrackListEditor)
57
58 ///////////////////////////////////////////////////////////
59 /////////////   AliEveTRDTrackListEditor //////////////////
60 ///////////////////////////////////////////////////////////
61 AliEveTRDTrackListEditor::AliEveTRDTrackListEditor(const TGWindow* p, Int_t width, Int_t height,
62                                                    UInt_t options, Pixel_t back) :
63   TGedFrame(p, width, height, options, back),
64   fM(0),
65   fHistoCanvas(0),
66   fHistoCanvasName(0),
67   fInheritedMacroList(0),
68   fInheritSettings(kFALSE),
69   fStyleFrame(0),
70   fMainFrame(0),
71   fHistoFrame(0),
72   fHistoSubFrame(0),
73   fBrowseFrame(0),
74   fbgStyleColor(0),
75   fbgStyleTrack(0),
76   frbColor(new TGRadioButton*[3]),
77   frbTrack(new TGRadioButton*[3]),
78   fbBrowse(0),
79   fbNew(0),
80   fbApplyMacros(0),
81   fbRemoveMacros(0),
82   fbDrawHisto(0),
83   fteField(0),
84   ftlMacroList(0),
85   ftlMacroSelList(0),
86   fFileInfo(0),
87   fFileTypes(0),
88   fLabel1(0), fLabel2(0), fLabel3(0), fLabel4(0),
89   fLine1(0), fLine2(0), fLine3(0), fLine4(0), fLine5(0),
90   fCheckButtons(0)
91 {
92   // Creates the AliEveTRDTrackListEditor.
93
94   // Style stuff
95   fLine5 = new TGHorizontal3DLine(this, 194, 8);
96   AddFrame(fLine5, new TGLayoutHints(kLHintsLeft  | kLHintsTop, 2, 2, 8, 8));
97   fStyleFrame = new TGHorizontalFrame(this);
98   AddFrame(fStyleFrame);
99
100   // Style - Track model
101   fbgStyleTrack = new TGButtonGroup(fStyleFrame, "Track model");
102   fbgStyleTrack->SetMapSubwindows(kTRUE);
103   fbgStyleTrack->Resize(194, 200);
104   fStyleFrame->AddFrame(fbgStyleTrack);
105
106   frbTrack[0] = new TGRadioButton(fbgStyleTrack, "Rieman", 0);
107   frbTrack[0]->SetToolTipText("Set the track model to \"Rieman\" (i.e. the used fit method)");
108   frbTrack[1] = new TGRadioButton(fbgStyleTrack, "Kalman", 1);
109   frbTrack[1]->SetToolTipText("Set the track model to \"Kalman\" (i.e. the used fit method)");
110   frbTrack[2] = new TGRadioButton(fbgStyleTrack, "Line", 2);
111   frbTrack[2]->SetToolTipText("Set the track model to \"Line\" (i.e. the used fit method)");
112
113   // Style - Color model
114   fbgStyleColor = new TGButtonGroup(fStyleFrame, "Color model");
115   fbgStyleColor->SetMapSubwindows(kTRUE);
116   fbgStyleColor->Resize(194, 200);
117   fStyleFrame->AddFrame(fbgStyleColor);
118
119   frbColor[0] = new TGRadioButton(fbgStyleColor, "PID LQ", 0);
120   frbColor[0]->SetToolTipText("Set color model to \"PID LQ\" -> 2 dimensional likelihood particle identification");
121   frbColor[1] = new TGRadioButton(fbgStyleColor, "PID NN", 1);
122   frbColor[1]->SetToolTipText("Set color model to \"PID NN\" -> Neural network particle identification");
123   frbColor[2] = new TGRadioButton(fbgStyleColor, "ESD Source", 2);
124   frbColor[2]->SetToolTipText("Set color model to \"ESD Source\" -> By source (TPC track prolongation or TRD stand alone)");
125   
126   // Functionality for adding plugins  
127   fMainFrame = CreateEditorTabSubFrame("Process");
128    
129   fLabel1 = new TGLabel(fMainFrame,"Add plugin(s):");
130   fMainFrame->AddFrame(fLabel1);
131   fBrowseFrame = new TGHorizontalFrame(fMainFrame);
132
133   fteField = new TGTextEntry(fBrowseFrame);
134   fteField->SetToolTipText("Enter the pathname of the macro you want to add here and press \"Enter\"");
135   fteField->Connect("ReturnPressed()","AliEveTRDTrackListEditor", this, "HandleMacroPathSet()"); 
136   fBrowseFrame->AddFrame(fteField);
137   
138   fbBrowse = new TGTextButton(fBrowseFrame, "Browse");
139   fbBrowse->SetToolTipText("Browse the macro you want to add");
140   fbBrowse->Connect("Clicked()", "AliEveTRDTrackListEditor", this, "BrowseMacros()");
141   fBrowseFrame->AddFrame(fbBrowse);
142   
143   fbNew = new TGTextButton(fBrowseFrame, "New");
144   fbNew->SetToolTipText("Start macro creation wizard");
145   fbNew->Connect("Clicked()", "AliEveTRDTrackListEditor", this, "NewMacros()");
146   fBrowseFrame->AddFrame(fbNew);
147   fMainFrame->AddFrame(fBrowseFrame);
148
149   fLine1 = new TGHorizontal3DLine(fMainFrame, 194, 8);
150   fMainFrame->AddFrame(fLine1, new TGLayoutHints(kLHintsLeft  | kLHintsTop, 2, 2, 8, 2));
151   fLabel2 = new TGLabel(fMainFrame,"Selection plugins:");
152   fMainFrame->AddFrame(fLabel2);
153
154   ftlMacroSelList = new TGListBox(fMainFrame);
155   ftlMacroSelList->Resize(194, 94);
156   ftlMacroSelList->SetMultipleSelections(kTRUE);
157   fMainFrame->AddFrame(ftlMacroSelList);
158
159   fLine2 = new TGHorizontal3DLine(fMainFrame, 194, 8);
160   fMainFrame->AddFrame(fLine2, new TGLayoutHints(kLHintsLeft  | kLHintsTop, 2, 2, 8, 2));
161   fLabel3 = new TGLabel(fMainFrame,"Process plugins:");
162   fMainFrame->AddFrame(fLabel3);
163
164   ftlMacroList = new TGListBox(fMainFrame);
165   ftlMacroList->Resize(194, 94);
166   ftlMacroList->SetMultipleSelections(kTRUE);
167   fMainFrame->AddFrame(ftlMacroList);
168
169   fLine3 = new TGHorizontal3DLine(fMainFrame, 194, 8);
170   fMainFrame->AddFrame(fLine3, new TGLayoutHints(kLHintsLeft  | kLHintsTop, 2, 2, 8, 2));  
171
172   fbApplyMacros = new TGTextButton(fMainFrame, "Apply plugin(s)");
173   fbApplyMacros->SetToolTipText("Apply all selected macros/class functins to the list of tracks -> A data file will be generated");
174   fbApplyMacros->Connect("Clicked()", "AliEveTRDTrackListEditor", this, "ApplyMacros()");
175   fbApplyMacros->SetRightMargin(12);
176   fMainFrame->AddFrame(fbApplyMacros);
177
178   fbRemoveMacros = new TGTextButton(fMainFrame, "Remove plugin(s)");
179   fbRemoveMacros->SetToolTipText("Remove the selected macros/class functions from the list(s)");
180   fbRemoveMacros->Connect("Clicked()", "AliEveTRDTrackListEditor", this, "RemoveMacros()");
181   fMainFrame->AddFrame(fbRemoveMacros);
182
183   // Stuff for displaying histograms
184   fHistoFrame = CreateEditorTabSubFrame("Results");  
185   fHistoFrame->SetMapSubwindows(kTRUE);
186   fLabel4 = new TGLabel(fHistoFrame,"Data from plugins:");
187   fHistoFrame->AddFrame(fLabel4);
188
189   fHistoSubFrame = new TGVerticalFrame(fHistoFrame);
190   fHistoSubFrame->SetMapSubwindows(kTRUE);
191   fHistoSubFrame->Resize(194, 200);
192   fHistoFrame->AddFrame(fHistoSubFrame);
193
194   fLine4 = new TGHorizontal3DLine(fHistoFrame, 194, 8);
195   fHistoFrame->AddFrame(fLine4, new TGLayoutHints(kLHintsLeft  | kLHintsTop, 2, 2, 8, 2));  
196
197   fbDrawHisto = new TGTextButton(fHistoFrame, "Draw projections");
198   fbDrawHisto->SetToolTipText("Uses the data file created by the last \"Apply selected plugin(s)\".\nClick here to display the data histograms of the selected macros.\nSelect multiple macros to create multi-dimensional plots.\nHisto macros cannot be used for multi-dimensional plots!");
199   fbDrawHisto->Connect("Clicked()", "AliEveTRDTrackListEditor", this, "DrawHistos()");
200   fHistoFrame->AddFrame(fbDrawHisto);
201
202   // Set up file dialog
203   fFileInfo = new TGFileInfo();
204   fFileInfo->SetMultipleSelection(kTRUE);
205
206   fFileTypes = new Char_t*[6];
207   fFileTypes[0] = (Char_t*)"All files"; fFileTypes[1] = (Char_t*)"*";
208   fFileTypes[2] = (Char_t*)"ROOT macros"; fFileTypes[3] = (Char_t*)"*.C";
209   fFileTypes[4] = 0; fFileTypes[5] = 0;
210   fFileInfo->fFileTypes = (const Char_t**)fFileTypes;
211   fFileInfo->fFileTypeIdx = 2;
212   fFileInfo->fMultipleSelection = kTRUE;
213
214   fHistoCanvasName = new TGString("");
215
216   // Handle style changed signals:
217   fbgStyleTrack->Connect("Clicked(Int_t)", "AliEveTRDTrackListEditor", this, "SetTrackModel(Int_t)");
218   fbgStyleColor->Connect("Clicked(Int_t)", "AliEveTRDTrackListEditor", this, "SetTrackColor(Int_t)");
219
220   // Handle the signal "Selected(Int_t ind)"
221   ftlMacroList->Connect("Selected(Int_t)", "AliEveTRDTrackListEditor", this, "UpdateMacroListSelection(Int_t)");
222   ftlMacroSelList->Connect("Selected(Int_t)", "AliEveTRDTrackListEditor", this, "UpdateMacroListSelection(Int_t)");
223
224   // Handle the signal "NewEventLoaded"
225   AliEveEventManager::GetMaster()->Connect("NewEventLoaded()", "AliEveTRDTrackListEditor", this, "HandleNewEventLoaded()");
226
227   // Handle the signal "Selected" (another tab has been selected)
228   GetGedEditor()->GetTab()->Connect("Selected(Int_t)", "AliEveTRDTrackListEditor", this, "HandleTabChangedToIndex(Int_t)");
229 }
230
231 //______________________________________________________
232 AliEveTRDTrackListEditor::~AliEveTRDTrackListEditor()
233 {
234   // Destructor: Closes all tabs created by this object and
235   // frees the corresponding memory.
236
237   if (fFileTypes != 0)
238   {
239     delete [] fFileTypes;
240     fFileTypes = 0;
241   }
242
243   if (fFileInfo != 0)
244   {
245     delete fFileInfo; 
246     fFileInfo = 0;
247   }
248   // Close and delete all tabs that have been created by this class
249   CloseTabs();
250
251   if (fHistoCanvasName != 0)
252   {
253     delete fHistoCanvasName;
254     fHistoCanvasName = 0;
255   }
256   if (fInheritedMacroList != 0)
257   {
258     fInheritedMacroList->Delete();
259     delete fInheritedMacroList;
260     fInheritedMacroList = 0;
261   }
262 }
263
264 //______________________________________________________
265 void AliEveTRDTrackListEditor::AddMacro(const Char_t* name, const Char_t* path)
266 {
267   // Adds the macro path/name to the macro list. A warning is provided, if there is
268   // something wrong, e.g. if the macro does not have the correct signature.
269   Int_t result = fM->AddMacro(path, name);
270
271   switch (result)
272   {
273   case SUCCESS:
274     UpdateMacroList();
275     break;
276   case WARNING:
277     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Warning", "Macro is already in list (won't be added again)!",
278                  kMBIconExclamation, kMBOk);
279     break;
280   case ERROR:
281     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", "Failed to load the macro (check messages in the terminal)!",
282                  kMBIconExclamation, kMBOk);
283     break;
284   case SIGNATURE_ERROR:
285     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
286                  "Macro has not the signature of...\n...a single track selection macro: Bool_t YourMacro(const AliTRDtrackV1*)\n...a correlated tracks selection macro: Bool_t YourMacro(const AliTRDtrackV1*, const AliTRDtrackV1*)\n...a single track analyse macro: void YourMacro(const AliTRDtrackV1*, Double_t*&, Int_t&)\n...a correlated tracks analyse macro: void YourMacro(const AliTRDtrackV1*, const AliTRDtrackV1*, Double_t*&, Int_t&)\n...a single track histo macro: TH1* YourMacro(const AliTRDtrackV1*)\n...a correlated tracks histo macro: TH1* YourMacro(const AliTRDtrackV1*, const AliTRDtrackV1*)", 
287                  kMBIconExclamation, kMBOk);
288     break;               
289   case NOT_EXIST_ERROR:
290     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
291                  "File does not exist or you do not have read permission!", kMBIconExclamation, kMBOk);
292     break;
293   default:
294     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
295                  Form("AliEveTRDTrackList::AddMacro exited with unknown return value: %d", result),
296                  kMBIconExclamation, kMBOk);
297     break;
298   }
299 }
300
301 //______________________________________________________
302 void AliEveTRDTrackListEditor::ApplyMacros()
303 {
304   // Applies the selected macros and updates the view.
305
306   Bool_t success = kFALSE;
307
308   // First apply the single track selection macros
309   TList* selIterator = new TList();
310   ftlMacroSelList->GetSelectedEntries(selIterator);
311   fM->ApplySTSelectionMacros(selIterator);
312   
313   // Update view
314   gEve->Redraw3D();
315
316   // Now apply the process macros
317   TList* procIterator = new TList();
318   ftlMacroList->GetSelectedEntries(procIterator);
319   success = fM->ApplyProcessMacros(selIterator, procIterator);
320
321   // Update histogram tab (data has to be reloaded)
322   SetModel(fM);
323   Update();
324
325   // AlieveTRDTrackList::ApplyProcessMacros() automatically selects a macro -> Draw the histogram for it,
326   // if a process macro has been applied
327   if (success && procIterator->GetEntries() > 0) 
328   {
329     // Set focus on "Histograms" tab
330     GetGedEditor()->GetTab()->SetTab("Results");
331
332     DrawHistos();
333   }
334
335   delete selIterator;
336   delete procIterator;  
337   
338   if (!success) {
339     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
340                  "AliEveTRDTrackList::ApplyProcessMacros experienced an error (cf. CINT-output)!", 
341                  kMBIconExclamation, kMBOk);  
342   }
343 }
344
345 //______________________________________________________
346 void AliEveTRDTrackListEditor::NewMacros()
347 {
348   // Start the macro creation wizard.
349   // thanks to Jacek Otwinowski<J.Otwinowski@GSI.DE> for this suggestion
350
351   AliEveTRDMacroWizard *wizz = new AliEveTRDMacroWizard();
352   wizz->Connect("Create(Char_t*)", "AliEveTRDTrackListEditor", this, "AddMacro(Char_t*)");
353 }
354
355
356 //______________________________________________________
357 void AliEveTRDTrackListEditor::BrowseMacros()
358 {
359   // Creates a file-dialog. The selected files will be added to the macro list
360   // via AddMacro(...).
361
362   new TGFileDialog(gClient->GetRoot(), GetMainFrame(), kFDOpen, fFileInfo);
363   
364   if (fFileInfo->fIniDir != 0 && fFileInfo->fFileNamesList != 0)
365   {       
366     // Extract filenames
367     TObject* iter = fFileInfo->fFileNamesList->First();
368  
369     Char_t* name = 0;
370
371     while (iter != 0)
372     {
373       // NOTE: fileInfo->fFileNamesList will be changed by that, too!
374       name = (Char_t*)strrchr(iter->GetName(), '/');
375       // Delete '"' at the end
376       name[strlen(name)] = '\0';
377               
378       AddMacro(name + 1, fFileInfo->fIniDir); 
379       iter = (TObjString*)fFileInfo->fFileNamesList->After(iter);
380     }
381   }
382
383   // -> The following problem has been fixed (trunk -> Changes according to 03 September 2008):
384   // Some error occurs, when one ends the filedialog with "cancel": fileInfo->fFileNamesList is set to 0x0, but
385   // in the next launch no new memory is allocated. So do this manually.
386   //if (fileInfo->fFileNamesList == 0)  fileInfo->fFileNamesList = new TList();
387 }
388
389 //______________________________________________________
390 void AliEveTRDTrackListEditor::CloseTabs()
391 {
392   // Closes + deletes the tabs created by this object
393
394   if (fHistoCanvas != 0)
395   {
396     // Close the created tab, if it exists
397     if (fHistoCanvasName != 0)
398     {
399       if (gEve->GetBrowser()->GetTab(1)->SetTab(fHistoCanvasName->GetString()))
400       {
401         // Now the created tab is the current one and can be deleted
402         gEve->GetBrowser()->GetTab(1)->RemoveTab();
403       }
404     }
405     // With the tab removal, the canvas will be deleted automatically!
406     fHistoCanvas = 0;
407   }
408 }
409
410 //______________________________________________________
411 void AliEveTRDTrackListEditor::DrawHistos()
412 {
413   // Accesses the temporary data file created by the last call of ApplyMacros() and draws
414   // histograms according to the selection in the "Histograms"-tab.
415  
416   Int_t nHistograms = GetNSelectedHistograms();
417   if (nHistograms <= 0)
418   {
419     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
420                  "No data selected. Please select the data you want to plot!", kMBIconExclamation, kMBOk);
421     return;
422   }
423   if (nHistograms > 3)
424   {
425     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), 
426                  "Error", "Only histograms with up to 3 dimensions supported. Please select 1,2 or 3 data macros!",
427                  kMBIconExclamation, kMBOk);
428     return;
429   }
430
431   // Check, if a histo macro shall be drawn
432   Int_t indexOfHistoMacro = -1;
433   Int_t selectedChecked = 0;
434   for (Int_t j = 0; j < fM->fDataFromMacroList->GetEntries(); j++)
435   {
436     if (fCheckButtons[j]->TGButton::GetState() == kButtonDown)
437     {
438       selectedChecked++;
439
440       // Histo macro? -> To check this, look for the substring "(histo macro)"
441       if (strstr(fM->fDataFromMacroList->At(j)->GetName(), "(histo macro)") != 0)
442       {
443         // Is also another macro selected?
444         if (nHistograms > 1)
445         {
446           // Histo macros cannot(!) be correlated!
447           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
448                        "Histo macros (return value \"TH1*\") cannot be combined with other macros", 
449                        kMBIconExclamation, kMBOk);
450           return;        
451         }
452
453         // Mark this histo macro for drawing
454         indexOfHistoMacro = j;
455
456         // Have all selected macros been checked? -> If yes, we are done with this
457         if (selectedChecked == nHistograms)  break;
458       }
459     }
460   }
461
462   TFile* file = new TFile(Form("/tmp/TRD.TrackListMacroData_%s.root", gSystem->Getenv("USER")), "READ");
463   if (!file)  
464   {
465     Error("Draw histograms", "Cannot open file \"/tmp/TRD.TrackListMacroData_%s.root\"",
466                                   gSystem->Getenv("USER"));
467     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
468                  Form("Cannot open file \"/tmp/TRD.TrackListMacroData_%s.root\"", gSystem->Getenv("USER")),
469                  kMBIconExclamation, kMBOk);
470     return;
471   }
472   
473   TTree* t = 0;
474   TTree* tFriend1 = 0;
475   TTree* tFriend2 = 0;
476
477   Int_t indexOfMacro1 = 0;
478   Int_t indexOfMacro2 = 0;
479   Int_t indexOfMacro3 = 0;
480
481   // Variable for the loop below -> Will be set to aborting value, if a histo macro is drawn
482   Int_t i = 0;
483   
484   // Draw histo macro?
485   if (indexOfHistoMacro >= 0)
486   {
487     if ((t = (TTree*)file->Get(Form("TrackData%d", indexOfHistoMacro))))
488     {
489       SetDrawingToHistoCanvasTab();
490  
491       TH1* myHist = 0;
492       t->SetBranchAddress(Form("Macro%d", indexOfHistoMacro), &myHist);
493       t->GetEntry(0);
494       if (myHist != 0)  myHist->Draw();
495       else
496       {
497         Error("Draw histograms", "No histogram for histo macro \"%s\" found!",
498                                       fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName());
499         new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
500                      Form("No histogram for histo macro \"%s\" found!", 
501                           fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName()), kMBIconExclamation, kMBOk);
502                
503       }
504
505       UpdateHistoCanvasTab();    
506     }
507     else
508     {
509       Error("Draw histograms", "No data for histo macro \"%s\" found!\nMaybe no tracks have been selected.",
510                                     fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName());
511       new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
512                    Form("No data for histo macro \"%s\" found!\nMaybe no tracks have been selected.", 
513                         fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName()), kMBIconExclamation, kMBOk);
514     }
515
516     // Skip the loop below
517     i = fM->fDataFromMacroList->GetEntries();
518   }
519
520   // Load the trees in succession and remember the entries -> Plot the analyse macros
521   for ( ; i < fM->fDataFromMacroList->GetEntries(); i++)
522   {
523     if (fCheckButtons[i]->TGButton::GetState() == kButtonDown)
524     {
525       if (t == 0)
526       {
527         indexOfMacro1 = i;
528         if (!(t = (TTree*)file->Get(Form("TrackData%d", i))))
529         { 
530           Error("Draw histograms", "No data for macro \"%s\" found!\nMaybe no tracks have been selected.",
531                                         fM->fDataFromMacroList->At(i)->GetName());
532           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
533                        Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
534                             fM->fDataFromMacroList->At(i)->GetName()), kMBIconExclamation, kMBOk);
535           break;   
536         }
537
538         // 1d histogram
539         if (nHistograms == 1) 
540         {
541           SetDrawingToHistoCanvasTab();
542       
543           t->Draw(Form("Macro%d", indexOfMacro1), "1");
544           ((TH1*)gPad->GetPrimitive("htemp"))->SetTitle(Form("%s;%s",
545             fM->fDataFromMacroList->At(indexOfMacro1)->GetName(),
546             fM->fDataFromMacroList->At(indexOfMacro1)->GetName()));
547           UpdateHistoCanvasTab();        
548
549           break;     
550         }
551       }
552       else if (tFriend1 == 0)
553       {
554         indexOfMacro2 = i;
555         if (!(tFriend1 = (TTree*)file->Get(Form("TrackData%d", i))))
556         { 
557           Error("Draw histograms", "No data for macro \"%s\" found!\nMaybe no tracks have been selected.",
558                                         fM->fDataFromMacroList->At(i)->GetName());
559           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
560                        Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
561                             fM->fDataFromMacroList->At(i)->GetName()),
562                             kMBIconExclamation, kMBOk);
563           break;   
564         }
565         
566         // 2d histogram
567         if (nHistograms == 2) 
568         {
569           SetDrawingToHistoCanvasTab();
570
571           t->AddFriend(tFriend1);
572           t->Draw(Form("Macro%d:Macro%d", indexOfMacro1, indexOfMacro2), "1");
573           ((TH1*)gPad->GetPrimitive("htemp"))->SetTitle(Form("%s - %s;%s;%s",
574             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
575             fM->fDataFromMacroList->At(indexOfMacro1)->GetName(),
576             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
577             fM->fDataFromMacroList->At(indexOfMacro1)->GetName()));
578
579           UpdateHistoCanvasTab();
580  
581           break;     
582         }
583       }    
584       // 3d histogram
585       else
586       {
587         indexOfMacro3 = i;
588         if (!(tFriend2 = (TTree*)file->Get(Form("TrackData%d", i))))
589         { 
590           Error("Draw histograms", "No data for macro \"%s\" found!\nMaybe no tracks have been selected.",
591                                         fM->fDataFromMacroList->At(i)->GetName());
592           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
593                        Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
594                             fM->fDataFromMacroList->At(i)->GetName()), kMBIconExclamation, kMBOk);
595           break;   
596         }
597
598         SetDrawingToHistoCanvasTab();
599
600         t->AddFriend(tFriend1);
601         t->AddFriend(tFriend2);
602         t->Draw(Form("Macro%d:Macro%d:Macro%d", indexOfMacro1, indexOfMacro2, indexOfMacro3), "1");
603         ((TH1*)gPad->GetPrimitive("htemp"))->SetTitle(Form("%s - %s - %s;%s;%s;%s",
604             fM->fDataFromMacroList->At(indexOfMacro3)->GetName(),
605             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
606             fM->fDataFromMacroList->At(indexOfMacro1)->GetName(),
607             fM->fDataFromMacroList->At(indexOfMacro3)->GetName(),
608             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
609             fM->fDataFromMacroList->At(indexOfMacro1)->GetName()));
610         
611         UpdateHistoCanvasTab();
612  
613         break;     
614       }
615     }
616   }
617
618   if (t != 0) delete t;
619   t = 0;
620   if (tFriend1 != 0)  delete tFriend1;
621   tFriend1 = 0;
622   if (tFriend2 != 0)  delete tFriend2;
623   tFriend2 = 0;
624
625   file->Close("R");
626   delete file;
627   file = 0;
628 }
629
630 //______________________________________________________
631 Int_t AliEveTRDTrackListEditor::GetNSelectedHistograms() const
632 {
633   // Returns the number of selected macros (or rather: of their selected data) in the "Histograms"-tab
634
635   Int_t count = 0;
636   
637   for (Int_t i = 0; i < fM->fDataFromMacroList->GetEntries(); i++)
638   {
639     if (fCheckButtons[i]->TGButton::GetState() == kButtonDown)  count++;
640   }
641
642   return count;
643 }
644
645 //______________________________________________________
646 void AliEveTRDTrackListEditor::HandleMacroPathSet()
647 {
648   // Takes the input of the text field (adding a macro), checks if the macro can be
649   // accessed (and that it exists) and adds the macro to the macro list via AddMacro(...).
650   // You can use environment variables in the text field, e.g. "$ALICE_ROOT/Eve/alice-macro/myMacro.C".
651
652   Char_t bname[AliEveTRDTrackList::fkMaxMacroNameLength]; // allocate buffers
653
654   if (strlen(fteField->GetText()) != 0)
655   {  
656     // Expand the pathname
657     Char_t* systemPath = gSystem->ExpandPathName(fteField->GetText());
658     fteField->SetText(systemPath);
659     delete systemPath;
660     systemPath = 0;
661                         
662     // Check if file exists
663     FILE* fp = NULL;
664
665     fp = fopen(fteField->GetText(), "rb");
666     if (fp != NULL)
667     {
668       fclose(fp);
669
670       // Extract filename
671       Char_t* name = (Char_t*)strrchr(fteField->GetText(), '/');
672
673       // Current path
674       if (!name) {
675         name= bname;
676         memset(name, '\0', sizeof(Char_t) * AliEveTRDTrackList::fkMaxMacroNameLength);
677         snprintf(name, AliEveTRDTrackList::fkMaxMacroNameLength, "%s", fteField->GetText());
678
679         // Add path to textfield -> Path is "./" -> Use length for the name + 2
680         Char_t pathname[AliEveTRDTrackList::fkMaxMacroNameLength + 2];
681         memset(pathname, '\0', sizeof(Char_t) * (AliEveTRDTrackList::fkMaxMacroNameLength + 2));
682         snprintf(pathname, AliEveTRDTrackList::fkMaxMacroNameLength + 2, "./%s", fteField->GetText());
683         fteField->SetText(pathname);
684
685         AddMacro(name);  
686       }
687       // Different path
688       else
689       {
690         // Extract path
691         Char_t path[AliEveTRDTrackList::fkMaxMacroPathLength];
692         memset(path, '\0', sizeof(Char_t) * AliEveTRDTrackList::fkMaxMacroPathLength);
693         snprintf(path, strlen(fteField->GetText()) - strlen(name), "%s", fteField->GetText());
694         
695         // Ignore the slash "/" in name
696         AddMacro(name + 1, path);  
697       }
698     }
699     else
700     {
701       new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
702                    "File does not exist or you do not have read permission!", kMBIconExclamation, kMBOk);
703     }
704   }
705 }
706
707 //______________________________________________________
708 void AliEveTRDTrackListEditor::HandleNewEventLoaded()
709 {
710   // Closes the tabs created by this object and sets a flag that will
711   // cause the function SetModel() to inherit the macro lists + style
712   // for the next AliEveTRDTrackList from the current one.
713
714   // Inherit the macro list and track style for the next track list!
715   fInheritSettings = kTRUE;
716
717   // Close the tabs
718   CloseTabs();
719 }
720
721 //______________________________________________________
722 void AliEveTRDTrackListEditor::HandleTabChangedToIndex(Int_t index)
723 {
724   // Saves the current tab in the current AliEveTRDTrackList.
725
726   fM->SetSelectedTab(index);
727 }
728
729 //______________________________________________________
730 void AliEveTRDTrackListEditor::InheritMacroList()
731 {
732   // The old macro list is possibly stored in the corresponding interior map. This function will 
733   // use this interior map to move the data from the interior map to the newly loaded AliEveTRDTrackList. 
734   // Then the interior map will be cleaned up. With this, the settings will be inherited from the previously 
735   // loaded AliEveTRDTrackList.
736
737   if (fInheritedMacroList == 0)  return;
738
739   // Clear list  
740   fM->fMacroList->Delete();
741
742   // Store data from interior list in the track list's map
743   TMapIter* iter = (TMapIter*)fInheritedMacroList->MakeIterator();
744   
745   TObject* key = 0;
746   TMacroData* macro = 0;
747   
748   while ((key = iter->Next()) != 0)
749   {
750     macro = (TMacroData*)fInheritedMacroList->GetValue(key);
751     if (macro != 0)  fM->fMacroList->Add(new TObjString(key->GetName()), 
752                                          new TMacroData(macro->GetName(), macro->GetPath(), macro->GetType()));
753     else
754     {
755       Error("AliEveTRDTrackListEditor::InheritMacroList", "Failed to inherit the macro \"%s\"!", key->GetName());
756     }
757   }
758   
759   fInheritedMacroList->Delete();
760   delete fInheritedMacroList;
761   fInheritedMacroList = 0;
762 }
763
764 //______________________________________________________
765 void AliEveTRDTrackListEditor::InheritStyle()
766 {
767   // The old styles are stored in the corresponding button groups. This function will replace
768   // the style settings of the newly loaded AliEveTRDTrackList with the old styles. With this, the settings
769   // will be inherited from the previously loaded AliEveTRDTrackList.
770
771   for (Int_t ind = 0; ind < 3; ind++)
772   {
773     if (fbgStyleTrack->GetButton(ind)->IsOn())
774     {
775       SetTrackModel(ind);
776       break;
777     }
778   }
779   for (Int_t ind = 0; ind < 3; ind++)
780   {
781     if (fbgStyleColor->GetButton(ind)->IsOn())
782     {
783       SetTrackColor(ind);
784       break;
785     }
786   }
787 }
788
789 //______________________________________________________
790 void AliEveTRDTrackListEditor::RemoveMacros()
791 {
792   // Removes the selected macros from the corresponding list.
793
794   TList iterator;
795   ftlMacroList->GetSelectedEntries(&iterator);
796   fM->RemoveSelectedMacros(&iterator);
797
798
799   iterator.Clear();
800   ftlMacroSelList->GetSelectedEntries(&iterator);
801   fM->RemoveSelectedMacros(&iterator);
802
803   // Selected macros are deleted from the list -> No selected entries left
804   fM->fMacroListSelected = 0;
805   UpdateMacroList();
806 }
807
808 //______________________________________________________
809 void AliEveTRDTrackListEditor::SaveMacroList(TMap* list)
810 {
811   // Saves the provided macro list in an interior list. This list will be used by
812   // InheritMacroList() to restore the data in "list". With this method one is able
813   // to inherit the macro list from track list to track list (i.e. from event to event).
814
815   if (fInheritedMacroList != 0)
816   {
817     fInheritedMacroList->Delete();
818     delete fInheritedMacroList;
819   }
820   fInheritedMacroList = new TMap();
821   fInheritedMacroList->SetOwnerKeyValue(kTRUE, kTRUE);
822
823   TMapIter* iter = (TMapIter*)list->MakeIterator();
824   TObject* key = 0;
825   TMacroData* macro = 0;
826   
827   while ((key = iter->Next()) != 0)
828   {
829     macro = (TMacroData*)fM->fMacroList->GetValue(key);
830     if (macro != 0) fInheritedMacroList->Add(new TObjString(key->GetName()), 
831                                              new TMacroData(macro->GetName(), macro->GetPath(), macro->GetType()));
832     else
833     {
834       Error("AliEveTRDTrackListEditor::SaveMacroList", "Failed to inherit the macro \"%s\"!", key->GetName());
835     }
836   }
837 }
838
839 //______________________________________________________
840 void AliEveTRDTrackListEditor::SetDrawingToHistoCanvasTab()
841 {
842   // Sets gPad to the tab with the name of the current AliEveTRDTrackList. If this tab does
843   // not exist, it will be created. Otherwise, it is re-used.
844
845   // If the tab with the canvas has been closed, the canvas will be deleted.
846   // So, if there is no tab, set the canvas pointer to zero and recreate it in a new tab.
847   if (fHistoCanvas != 0) 
848   {
849     if (gEve->GetBrowser()->GetTab(1)->SetTab(fHistoCanvasName->GetString()) == 0)
850     {
851       fHistoCanvas = 0;
852     }
853   }
854
855   if (!fHistoCanvas)
856   {
857     fHistoCanvas = gEve->AddCanvasTab(fM->GetName());     
858   }
859                            
860   gPad = fHistoCanvas;
861 }
862
863 //______________________________________________________
864 void AliEveTRDTrackListEditor::SetModel(TObject* obj)
865 {  
866   // Sets the model object, updates the related data in the GUI and
867   // inherits settings (cf. Inherit*(...)), if the flag fInheritSettings is set to kTRUE.
868
869   fM = dynamic_cast<AliEveTRDTrackList*>(obj);
870
871   if (fM == 0) 
872   {
873     Error("SetModel", "Parameter is zero pointer");
874     return;
875   }
876
877   // Provide a pointer to this editor
878   fM->fEditor = this;
879
880   // If macro list + track style shall be inherited from previously loaded track list, do so
881   if (fInheritSettings)
882   {
883     InheritMacroList();
884     InheritStyle();
885
886     fInheritSettings = kFALSE;
887   }
888
889   // Select the correct styles
890   Int_t b = 0;
891   UChar_t style = fM->GetSelectedTrackStyle();
892   if (TESTBIT(style, AliEveTRDTrack::kSource)) b = 2;
893   else 
894   {
895     if (TESTBIT(style, AliEveTRDTrack::kPID)) b = 1;
896     else b = 0;
897   } 
898   fbgStyleColor->SetButton(b, kTRUE);
899
900
901   if (TESTBIT(style, AliEveTRDTrack::kTrackCosmics)) b = 2;
902   else
903   {
904     if (TESTBIT(style, AliEveTRDTrack::kTrackModel)) b = 1;
905     else b = 0;
906   }
907   fbgStyleTrack->SetButton(b, kTRUE);
908   
909   UpdateMacroList();
910   UpdateHistoList(); 
911
912   // View correct tab
913   GetGedEditor()->GetTab()->SetTab(fM->GetSelectedTab()); 
914 }
915
916 //______________________________________________________
917 void AliEveTRDTrackListEditor::SetTrackColor(Int_t ind)
918 {
919   // Sets the color model for the tracks, updates the tracks with this model and
920   // redraws the scene.
921
922   switch(ind)
923   { 
924     case AliTRDpidUtil::kLQ:
925       fM->UpdateTrackStyle(AliEveTRDTrack::kPID, AliTRDpidUtil::kLQ);
926       break;
927     case AliTRDpidUtil::kNN:
928       fM->UpdateTrackStyle(AliEveTRDTrack::kPID, AliTRDpidUtil::kNN);
929       break;
930     default:
931       fM->UpdateTrackStyle(AliEveTRDTrack::kSource);
932       break;
933   }
934
935   gEve->Redraw3D();
936 }
937
938 //______________________________________________________
939 void AliEveTRDTrackListEditor::SetTrackModel(Int_t ind)
940 {
941   // Sets the track model for the tracks, updates the tracks with this model and
942   // redraws the scene.
943
944   switch(ind)
945   { 
946     case AliEveTRDTrack::kRieman:
947       fM->UpdateTrackStyle(AliEveTRDTrack::kTrackModel, AliEveTRDTrack::kRieman);
948       break;
949     case AliEveTRDTrack::kKalman:
950       fM->UpdateTrackStyle(AliEveTRDTrack::kTrackModel, AliEveTRDTrack::kKalman);
951       break;
952     default:
953       fM->UpdateTrackStyle(AliEveTRDTrack::kTrackCosmics);
954       break;
955   }
956
957   gEve->Redraw3D();
958 }
959
960 //______________________________________________________
961 void AliEveTRDTrackListEditor::UpdateDataFromMacroListSelection()
962 {
963   // Saves the current selection in the "Histograms"-tab to the current
964   // AliEveTRDTrackList. This means that the selection is updated and won't
965   // get lost, if another editor is loaded in Eve.
966
967   for (Int_t i = 0; i < fM->fDataFromMacroList->GetEntries(); i++)
968   {
969     fM->SetHistoDataSelection(i, fCheckButtons[i]->IsOn());
970   }
971 }
972
973 //______________________________________________________
974 void AliEveTRDTrackListEditor::UpdateHistoCanvasTab()
975 {
976   // Updates the histogram and the corresponding tab (including titles).
977
978   // Update name of the tab (tab has been set to current tab!)
979   fHistoCanvasName->SetString(fM->GetName());  
980
981   // Use a copy of fHistoCanvasName!! -> If the user closes a tab manually, the TGString
982   // will be deleted -> Error might occur, when accessing the pointer   
983   gEve->GetBrowser()->GetTab(1)->GetCurrentTab()->SetText(new TGString(fHistoCanvasName));
984
985   // Switch tabs to force redrawing
986   gEve->GetBrowser()->GetTab(1)->SetTab(0);
987   gEve->GetBrowser()->GetTab(1)->SetTab(fHistoCanvasName->GetString());
988   fHistoCanvas->Update();
989 }
990
991 //______________________________________________________
992 void AliEveTRDTrackListEditor::UpdateHistoList()
993 {
994   // Reloads (updates) the buttons in the "Histograms"-tab via
995   // the current AliEveTRDTrackList (data).
996
997   fHistoSubFrame->TGCompositeFrame::Cleanup();
998   
999   // Set buttons for histograms
1000   if (fCheckButtons != 0) delete fCheckButtons;
1001   fCheckButtons = new TGCheckButton*[fM->fDataFromMacroList->GetEntries()];
1002   
1003   TObjString* iter = (TObjString*)fM->fDataFromMacroList->First();
1004   for (Int_t i = 0; i < fM->fDataFromMacroList->GetEntries() && iter != 0; i++)
1005   {
1006     fCheckButtons[i] = new TGCheckButton(fHistoSubFrame, iter->GetName());
1007     fHistoSubFrame->AddFrame(fCheckButtons[i]);
1008     
1009     fCheckButtons[i]->SetState(kButtonUp, kFALSE);
1010     fCheckButtons[i]->MapRaised();
1011     fCheckButtons[i]->SetOn(fM->HistoDataIsSelected(i));
1012     fCheckButtons[i]->Connect("Clicked()", "AliEveTRDTrackListEditor", this, "UpdateDataFromMacroListSelection()");
1013             
1014     iter = (TObjString*)fM->fDataFromMacroList->After(iter);
1015   }  
1016 }
1017
1018 //______________________________________________________
1019 void AliEveTRDTrackListEditor::UpdateMacroList()
1020 {
1021   // Reloads (updates) the macro list (selection AND process macros) via
1022   // the current AliEveTRDTrackList (data).
1023
1024   ftlMacroList->RemoveAll();
1025   ftlMacroSelList->RemoveAll();
1026    
1027   TMapIter* iter = (TMapIter*)fM->fMacroList->MakeIterator();
1028   TObject* key = 0;
1029   TMacroData* macro = 0;
1030
1031   Int_t ind = 0;
1032   while ((key = iter->Next()) != 0)
1033   {
1034     macro = (TMacroData*)fM->fMacroList->GetValue(key);
1035     if (macro != 0)
1036     {
1037       if (macro->IsProcessMacro())
1038       {
1039         ftlMacroList->AddEntry(macro->GetName(), ind);
1040         // Select, what has been selected before
1041         ftlMacroList->Select(ind, fM->MacroListIsSelected(ind));
1042         ind++;
1043       }
1044       else if (macro->IsSelectionMacro())
1045       {
1046         ftlMacroSelList->AddEntry(macro->GetName(), ind);
1047         // Select, what has been selected before
1048         ftlMacroSelList->Select(ind, fM->MacroListIsSelected(ind));
1049         ind++;
1050       }
1051       else
1052       {
1053         Error("AliEveTRDTrackListEditor::UpdateMacroList()", 
1054               "Macro \"%s/%s.C\" is neither a selection macro nor a process macro!",
1055                    macro->GetPath(), macro->GetName());
1056       }
1057     }
1058     else
1059     {
1060       Error("AliEveTRDTrackListEditor::UpdateMacroList()", 
1061               "Macro list is corrupted: Macro \"%s\" not found!", key->GetName());
1062     }
1063   }
1064
1065   ftlMacroList->SortByName(); 
1066   ftlMacroSelList->SortByName(); 
1067 }
1068
1069 //______________________________________________________
1070 void AliEveTRDTrackListEditor::UpdateMacroListSelection(Int_t ind)
1071 {
1072   // Saves the current selection in the macro listS to the current
1073   // AliEveTRDTrackList. This means that the selection is updated and won't
1074   // get lost, if another editor is loaded in Eve.
1075   // NOTE: The indices in BOTH lists will be unique!
1076
1077   // Toggle selected item
1078   fM->SetMacroListSelection(ind, !fM->MacroListIsSelected(ind));
1079 }
1080
1081
1082 //______________________________________________________
1083 //______________________________________________________
1084 //______________________________________________________
1085
1086
1087 /////////////////////////////////////////////////
1088 ClassImp(AliEveTRDMacroWizard)
1089 /////////////////////////////////////////////////
1090
1091 //______________________________________________________
1092 AliEveTRDMacroWizard::AliEveTRDMacroWizard(const TGWindow* p)
1093   :TGMainFrame(p ? p : gClient->GetRoot(), 10, 10, kMainFrame | kVerticalFrame)
1094   ,fText(0x0)
1095   ,fCombo(0x0)
1096   ,fTextEdit(0x0)
1097   ,fbCreate(0x0)
1098   ,fbCancel(0x0)
1099 {
1100   // Creates the macro wizard.
1101
1102   const Int_t width = 300;
1103
1104   // horizontal frame
1105   TGHorizontalFrame *fFrameName = new TGHorizontalFrame(this, 10, 10, kHorizontalFrame);
1106   TGLabel *fLabel = new TGLabel(fFrameName, "Name*");
1107   fLabel->SetTextJustify(36);
1108   fLabel->SetMargins(0,0,0,0);
1109   fLabel->SetWrapLength(-1);
1110   fFrameName->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1111
1112   fText = new TGTextEntry(fFrameName);
1113   fText->SetMaxLength(255);
1114   fText->SetAlignment(kTextLeft);
1115   fText->SetText("");
1116   fText->Resize(width, fText->GetDefaultHeight());
1117   fFrameName->AddFrame(fText, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2));
1118
1119
1120   // horizontal frame
1121   TGHorizontalFrame *fFrameComment = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1122   fLabel = new TGLabel(fFrameComment, "Comment");
1123   fLabel->SetTextJustify(36);
1124   fLabel->SetMargins(0,0,0,0);
1125   fLabel->SetWrapLength(-1);
1126   fFrameComment->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1127
1128   fTextEdit = new TGTextEdit(fFrameComment, width, 5*fText->GetDefaultHeight());
1129   fFrameComment->AddFrame(fTextEdit, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2));
1130
1131   // horizontal frame
1132   TGHorizontalFrame *fFrameType = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1133   fLabel = new TGLabel(fFrameType, "Type*");
1134   fLabel->SetTextJustify(36);
1135   fLabel->SetMargins(0,0,0,0);
1136   fLabel->SetWrapLength(-1);
1137   fFrameType->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1138
1139   fCombo = new TGComboBox(fFrameType, -1, kHorizontalFrame | kSunkenFrame | kDoubleBorder | kOwnBackground);
1140   fCombo->AddEntry("Single Track Selection", AliEveTRDTrackList::kSingleTrackSelect);
1141   fCombo->AddEntry("Pair Tracks Selection", AliEveTRDTrackList::kCorrelTrackSelect);
1142   fCombo->AddEntry("Single Track Analyse", AliEveTRDTrackList::kSingleTrackAnalyse);
1143   fCombo->AddEntry("Single Track Histo", AliEveTRDTrackList::kSingleTrackHisto);
1144   fCombo->AddEntry("Pair Tracks Analyse", AliEveTRDTrackList::kCorrelTrackAnalyse);
1145   fCombo->AddEntry("Pair Tracks Histo", AliEveTRDTrackList::kCorrelTrackHisto);
1146   fCombo->Select(-1);
1147   fCombo->Resize(width, fText->GetDefaultHeight());
1148   fFrameType->AddFrame(fCombo, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2));
1149
1150   // horizontal frame
1151   TGHorizontalFrame *fFrameAction = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1152   fbCancel = new TGTextButton(fFrameAction, "Cancel");
1153   fbCancel->SetToolTipText("Exit macro creation wizard");
1154   fFrameAction->AddFrame(fbCancel, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2)); 
1155   fbCreate = new TGTextButton(fFrameAction, "Done");
1156   fbCreate->SetToolTipText("Use settings to create the macro");
1157   fFrameAction->AddFrame(fbCreate, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2)); 
1158
1159
1160   // horizontal frame
1161   TGHorizontalFrame *fFrameText = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1162   fLabel = new TGLabel(fFrameText, "(*) Mandatory fields");
1163   fLabel->SetTextJustify(36);
1164   fLabel->SetMargins(0,0,0,0);
1165   fLabel->SetWrapLength(-1);
1166   fFrameText->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1167
1168   // put things together  
1169   AddFrame(fFrameName, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1170   AddFrame(fFrameComment, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1171   AddFrame(fFrameType, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1172   AddFrame(fFrameAction, new TGLayoutHints(kLHintsRight | kLHintsTop | kLHintsExpandX,2,2,2,2));
1173
1174   TGHorizontal3DLine *fLine = new TGHorizontal3DLine(this, 281, 2);
1175   AddFrame(fLine, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1176   AddFrame(fFrameText, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1177
1178
1179   SetWindowName("TRD Macro Wizard");
1180   SetMWMHints(kMWMDecorAll,
1181               kMWMFuncAll,
1182               kMWMInputModeless);
1183   MapSubwindows();
1184
1185   Resize(GetDefaultSize());
1186   MapWindow();
1187
1188   // Do the linking
1189   //fCombo->Connect("Selected(Int_t)", "AliEveTRDMacroWizard", this, "Create(Int_t)");
1190   fbCreate->Connect("Clicked()", "AliEveTRDMacroWizard", this, "HandleCreate()");
1191   fbCancel->Connect("Clicked()", "AliEveTRDMacroWizard", this, "CloseWindow()");
1192
1193   // Standard choice
1194   fCombo->Select(1, kFALSE);
1195 }  
1196
1197 const Char_t *fIncludes = 
1198 "#if !defined(__CINT__) || defined(__MAKECINT__)\n"
1199 "#include <TROOT.h>\n"
1200 "#include <TH1.h>\n"
1201 "#include <TRD/AliTRDgeometry.h>\n"
1202 "#include <TRD/AliTRDcluster.h>\n"
1203 "#include <TRD/AliTRDseedV1.h>\n"
1204 "#include <TRD/AliTRDtrackV1.h>\n"
1205 "#endif\n";
1206
1207 const Char_t *fMacroTemplate[7] = {
1208 ""
1209 ,"  if (!track) return kFALSE;\n"
1210
1211 ,"  n = 0;\n"
1212 "  r = 0x0;\n"
1213 "  if (!track) return;\n"
1214
1215 ,"  if (!track) return 0x0;\n"
1216 "  TH1* h = 0x0;\n\n"
1217 "// Set bins, xmin and xmax here\n"
1218 "  Int_t n = 1;\n"
1219 "  Double_t xmin = 0;\n"
1220 "  Double_t xmax = 100;\n\n" 
1221 "  if(!(h = (TH1*)gROOT->FindObject(\"h\"))){\n"
1222 "    h = new TH1(\"h\", \"Title\", n, xmin, xmax);\n"
1223 "    h->GetXaxis()->SetTitle("");\n"
1224 "    h->GetYaxis()->SetTitle("");\n"
1225 "  } else h->Reset();\n"
1226
1227 ,"  if (!track) return kFALSE;\n"
1228 "  if (!track2) return kFALSE;\n"
1229
1230 ,"  n = 0;\n"
1231 "  r = 0x0;\n"
1232 "  if (!track) return;\n"
1233 "  if (!track2) return;\n"
1234
1235 ,"  if (!track) return 0x0;\n"
1236 "  if (!track2) return 0x0;\n"
1237 "  TH1* h = 0x0;\n\n"
1238 "// Set bins, xmin and xmax here\n"
1239 "  Int_t n = 1;\n"
1240 "  Double_t xmin = 0;\n"
1241 "  Double_t xmax = 100;\n\n" 
1242 "  if(!(h = (TH1*)gROOT->FindObject(\"h\"))){\n"
1243 "    h = new TH1(\"h\", \"Title\", n, xmin, xmax);\n"
1244 "    h->GetXaxis()->SetTitle("");\n"
1245 "    h->GetYaxis()->SetTitle("");\n"
1246 "  } else h->Reset();\n"
1247 };
1248 //______________________________________________________
1249 void AliEveTRDMacroWizard::Create(Int_t type)
1250 {
1251   // Creates the macro with the selected type (combo box).
1252
1253   const Char_t *name = fText->GetText();
1254   if(strcmp(name,"")==0){
1255     AliInfo("Please specify a name for your macro.");
1256     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1257                  "Please specify a name for your macro.", kMBIconExclamation, kMBOk);
1258     //fCombo->Select(-1);
1259     return;
1260   }
1261
1262   // Note: gSystem->AccessPathName(...) returns kTRUE, if the access FAILED!
1263   if(!gSystem->AccessPathName(Form("./%s.C", name))){
1264     // If there is already a file with this name -> Error
1265     AliInfo(Form("A macro \"%s.C\" already exists in the current directory!\nPlease choose another name!", name));
1266     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1267                  Form("A macro \"%s.C\" already exists in the current directory!\nPlease choose another name!", name), kMBIconExclamation, kMBOk);
1268     //fCombo->Select(-1);
1269     return;
1270   }
1271
1272   FILE* fp = 0x0;
1273   if(!(fp = fopen(Form("%s.C", name), "wt"))){
1274     AliInfo("Couldn't create macro file.");
1275     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1276                  "Couldn't create macro file.", kMBIconExclamation, kMBOk);
1277     //fCombo->Select(-1);
1278     return;
1279   }
1280
1281   TGText *comment = fTextEdit->GetText();
1282   Char_t *line = 0x0; Int_t iline = 0;
1283   while((line = comment->GetLine(TGLongPosition(0,iline++), 200))) fprintf(fp, "// %s\n", line);
1284
1285   fprintf(fp, "\n%s\n", fIncludes);
1286
1287   switch(type){
1288   case AliEveTRDTrackList::kSingleTrackSelect:
1289     fprintf(fp, "Bool_t %s(const AliTRDtrackV1 *track)\n", name);
1290     break;
1291   case AliEveTRDTrackList::kCorrelTrackSelect:
1292     fprintf(fp, "Bool_t %s(const AliTRDtrackV1 *track, const AliTRDtrackV1 *track2)\n", name);
1293     break;
1294   case AliEveTRDTrackList::kSingleTrackAnalyse:
1295     fprintf(fp, "void %s(const AliTRDtrackV1 *track, Double_t*& r, Int_t& n)\n", name);
1296     break;
1297   case AliEveTRDTrackList::kSingleTrackHisto:
1298     fprintf(fp, "TH1* %s(const AliTRDtrackV1 *track)\n", name);
1299     break;
1300   case AliEveTRDTrackList::kCorrelTrackAnalyse:
1301     fprintf(fp, "void %s(const AliTRDtrackV1 *track, const AliTRDtrackV1 *track2, Double_t*& r, Int_t& n)\n", name);
1302     break;
1303   case AliEveTRDTrackList::kCorrelTrackHisto:
1304     fprintf(fp, "TH1* %s(const AliTRDtrackV1 *track, const AliTRDtrackV1 *track2)\n", name);
1305     break;
1306   default:
1307     AliInfo(Form("Unknown type[%d]", type));
1308     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1309                  Form("Unknown type[%d]", type), kMBIconExclamation, kMBOk);
1310     fclose(fp);
1311     gSystem->Exec(Form("rm -f %s.C", name));
1312     //fCombo->Select(-1);
1313     return;
1314   }
1315   
1316   fprintf(fp, "{\n%s\n", fMacroTemplate[type]);
1317   fprintf(fp, "// add your own code here\n\n\n}\n");
1318   fclose(fp);
1319
1320   Emit("Create(Int_t)", type);
1321   Create((Char_t*)name);
1322   CloseWindow();
1323 }
1324
1325 //______________________________________________________
1326 void AliEveTRDMacroWizard::Create(Char_t *name)
1327 {
1328   // Emits the creation signal.
1329
1330   Emit("Create(Char_t*)", Form("%s.C", name));
1331 }
1332
1333 //______________________________________________________
1334 void AliEveTRDMacroWizard::HandleCreate()
1335 {
1336   // Handles the signal, when the creation button is pressed.
1337
1338   Create(fCombo->GetSelected());
1339 }