]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveDet/AliEveTRDTrackListEditor.cxx
there have been some changes in the on-line tracklet and GTU track
[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   if (selIterator != 0) delete selIterator;
336   selIterator = 0;  
337   if (procIterator != 0)  delete procIterator;  
338   procIterator = 0;  
339   
340   if (!success)
341   {
342     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
343                  "AliEveTRDTrackList::ApplyProcessMacros experienced an error (cf. CINT-output)!", 
344                  kMBIconExclamation, kMBOk);  
345   }
346 }
347
348 //______________________________________________________
349 void AliEveTRDTrackListEditor::NewMacros()
350 {
351   // Start the macro creation wizard.
352   // thanks to Jacek Otwinowski<J.Otwinowski@GSI.DE> for this suggestion
353
354   AliEveTRDMacroWizard *wizz = new AliEveTRDMacroWizard();
355   wizz->Connect("Create(Char_t*)", "AliEveTRDTrackListEditor", this, "AddMacro(Char_t*)");
356 }
357
358
359 //______________________________________________________
360 void AliEveTRDTrackListEditor::BrowseMacros()
361 {
362   // Creates a file-dialog. The selected files will be added to the macro list
363   // via AddMacro(...).
364
365   new TGFileDialog(gClient->GetRoot(), GetMainFrame(), kFDOpen, fFileInfo);
366   
367   if (fFileInfo->fIniDir != 0 && fFileInfo->fFileNamesList != 0)
368   {       
369     // Extract filenames
370     TObject* iter = fFileInfo->fFileNamesList->First();
371  
372     Char_t* name = 0;
373
374     while (iter != 0)
375     {
376       // NOTE: fileInfo->fFileNamesList will be changed by that, too!
377       name = (Char_t*)strrchr(iter->GetName(), '/');
378       // Delete '"' at the end
379       name[strlen(name)] = '\0';
380               
381       AddMacro(name + 1, fFileInfo->fIniDir); 
382       iter = (TObjString*)fFileInfo->fFileNamesList->After(iter);
383     }
384   }
385
386   // -> The following problem has been fixed (trunk -> Changes according to 03 September 2008):
387   // Some error occurs, when one ends the filedialog with "cancel": fileInfo->fFileNamesList is set to 0x0, but
388   // in the next launch no new memory is allocated. So do this manually.
389   //if (fileInfo->fFileNamesList == 0)  fileInfo->fFileNamesList = new TList();
390 }
391
392 //______________________________________________________
393 void AliEveTRDTrackListEditor::CloseTabs()
394 {
395   // Closes + deletes the tabs created by this object
396
397   if (fHistoCanvas != 0)
398   {
399     // Close the created tab, if it exists
400     if (fHistoCanvasName != 0)
401     {
402       if (gEve->GetBrowser()->GetTab(1)->SetTab(fHistoCanvasName->GetString()))
403       {
404         // Now the created tab is the current one and can be deleted
405         gEve->GetBrowser()->GetTab(1)->RemoveTab();
406       }
407     }
408     // With the tab removal, the canvas will be deleted automatically!
409     fHistoCanvas = 0;
410   }
411 }
412
413 //______________________________________________________
414 void AliEveTRDTrackListEditor::DrawHistos()
415 {
416   // Accesses the temporary data file created by the last call of ApplyMacros() and draws
417   // histograms according to the selection in the "Histograms"-tab.
418  
419   Int_t nHistograms = GetNSelectedHistograms();
420   if (nHistograms <= 0)
421   {
422     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
423                  "No data selected. Please select the data you want to plot!", kMBIconExclamation, kMBOk);
424     return;
425   }
426   if (nHistograms > 3)
427   {
428     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), 
429                  "Error", "Only histograms with up to 3 dimensions supported. Please select 1,2 or 3 data macros!",
430                  kMBIconExclamation, kMBOk);
431     return;
432   }
433
434   // Check, if a histo macro shall be drawn
435   Int_t indexOfHistoMacro = -1;
436   Int_t selectedChecked = 0;
437   for (Int_t j = 0; j < fM->fDataFromMacroList->GetEntries(); j++)
438   {
439     if (fCheckButtons[j]->TGButton::GetState() == kButtonDown)
440     {
441       selectedChecked++;
442
443       // Histo macro? -> To check this, look for the substring "(histo macro)"
444       if (strstr(fM->fDataFromMacroList->At(j)->GetName(), "(histo macro)") != 0)
445       {
446         // Is also another macro selected?
447         if (nHistograms > 1)
448         {
449           // Histo macros cannot(!) be correlated!
450           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
451                        "Histo macros (return value \"TH1*\") cannot be combined with other macros", 
452                        kMBIconExclamation, kMBOk);
453           return;        
454         }
455
456         // Mark this histo macro for drawing
457         indexOfHistoMacro = j;
458
459         // Have all selected macros been checked? -> If yes, we are done with this
460         if (selectedChecked == nHistograms)  break;
461       }
462     }
463   }
464
465   TFile* file = new TFile(Form("/tmp/TRD.TrackListMacroData_%s.root", gSystem->Getenv("USER")), "READ");
466   if (!file)  
467   {
468     Error("Draw histograms", Form("Cannot open file \"/tmp/TRD.TrackListMacroData_%s.root\"", 
469                                   gSystem->Getenv("USER")));
470     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
471                  Form("Cannot open file \"/tmp/TRD.TrackListMacroData_%s.root\"", gSystem->Getenv("USER")),
472                  kMBIconExclamation, kMBOk);
473     return;
474   }
475   
476   TTree* t = 0;
477   TTree* tFriend1 = 0;
478   TTree* tFriend2 = 0;
479
480   Int_t indexOfMacro1 = 0;
481   Int_t indexOfMacro2 = 0;
482   Int_t indexOfMacro3 = 0;
483
484   // Variable for the loop below -> Will be set to aborting value, if a histo macro is drawn
485   Int_t i = 0;
486   
487   // Draw histo macro?
488   if (indexOfHistoMacro >= 0)
489   {
490     if ((t = (TTree*)file->Get(Form("TrackData%d", indexOfHistoMacro))))
491     {
492       SetDrawingToHistoCanvasTab();
493  
494       TH1* myHist = 0;
495       t->SetBranchAddress(Form("Macro%d", indexOfHistoMacro), &myHist);
496       t->GetEntry(0);
497       if (myHist != 0)  myHist->Draw();
498       else
499       {
500         Error("Draw histograms", Form("No histogram for histo macro \"%s\" found!", 
501                                       fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName()));
502         new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
503                      Form("No histogram for histo macro \"%s\" found!", 
504                           fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName()), kMBIconExclamation, kMBOk);
505                
506       }
507
508       UpdateHistoCanvasTab();    
509     }
510     else
511     {
512       Error("Draw histograms", Form("No data for histo macro \"%s\" found!\nMaybe no tracks have been selected.", 
513                                     fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName()));
514       new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
515                    Form("No data for histo macro \"%s\" found!\nMaybe no tracks have been selected.", 
516                         fM->fDataFromMacroList->At(indexOfHistoMacro)->GetName()), kMBIconExclamation, kMBOk);
517     }
518
519     // Skip the loop below
520     i = fM->fDataFromMacroList->GetEntries();
521   }
522
523   // Load the trees in succession and remember the entries -> Plot the analyse macros
524   for ( ; i < fM->fDataFromMacroList->GetEntries(); i++)
525   {
526     if (fCheckButtons[i]->TGButton::GetState() == kButtonDown)
527     {
528       if (t == 0)
529       {
530         indexOfMacro1 = i;
531         if (!(t = (TTree*)file->Get(Form("TrackData%d", i))))
532         { 
533           Error("Draw histograms", Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
534                                         fM->fDataFromMacroList->At(i)->GetName()));
535           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
536                        Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
537                             fM->fDataFromMacroList->At(i)->GetName()), kMBIconExclamation, kMBOk);
538           break;   
539         }
540
541         // 1d histogram
542         if (nHistograms == 1) 
543         {
544           SetDrawingToHistoCanvasTab();
545       
546           t->Draw(Form("Macro%d", indexOfMacro1), "1");
547           ((TH1*)gPad->GetPrimitive("htemp"))->SetTitle(Form("%s;%s",
548             fM->fDataFromMacroList->At(indexOfMacro1)->GetName(),
549             fM->fDataFromMacroList->At(indexOfMacro1)->GetName()));
550           UpdateHistoCanvasTab();        
551
552           break;     
553         }
554       }
555       else if (tFriend1 == 0)
556       {
557         indexOfMacro2 = i;
558         if (!(tFriend1 = (TTree*)file->Get(Form("TrackData%d", i))))
559         { 
560           Error("Draw histograms", Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
561                                         fM->fDataFromMacroList->At(i)->GetName()));
562           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
563                        Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
564                             fM->fDataFromMacroList->At(i)->GetName()),
565                             kMBIconExclamation, kMBOk);
566           break;   
567         }
568         
569         // 2d histogram
570         if (nHistograms == 2) 
571         {
572           SetDrawingToHistoCanvasTab();
573
574           t->AddFriend(tFriend1);
575           t->Draw(Form("Macro%d:Macro%d", indexOfMacro1, indexOfMacro2), "1");
576           ((TH1*)gPad->GetPrimitive("htemp"))->SetTitle(Form("%s - %s;%s;%s",
577             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
578             fM->fDataFromMacroList->At(indexOfMacro1)->GetName(),
579             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
580             fM->fDataFromMacroList->At(indexOfMacro1)->GetName()));
581
582           UpdateHistoCanvasTab();
583  
584           break;     
585         }
586       }    
587       // 3d histogram
588       else
589       {
590         indexOfMacro3 = i;
591         if (!(tFriend2 = (TTree*)file->Get(Form("TrackData%d", i))))
592         { 
593           Error("Draw histograms", Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
594                                         fM->fDataFromMacroList->At(i)->GetName()));
595           new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error - Draw histograms", 
596                        Form("No data for macro \"%s\" found!\nMaybe no tracks have been selected.", 
597                             fM->fDataFromMacroList->At(i)->GetName()), kMBIconExclamation, kMBOk);
598           break;   
599         }
600
601         SetDrawingToHistoCanvasTab();
602
603         t->AddFriend(tFriend1);
604         t->AddFriend(tFriend2);
605         t->Draw(Form("Macro%d:Macro%d:Macro%d", indexOfMacro1, indexOfMacro2, indexOfMacro3), "1");
606         ((TH1*)gPad->GetPrimitive("htemp"))->SetTitle(Form("%s - %s - %s;%s;%s;%s",
607             fM->fDataFromMacroList->At(indexOfMacro3)->GetName(),
608             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
609             fM->fDataFromMacroList->At(indexOfMacro1)->GetName(),
610             fM->fDataFromMacroList->At(indexOfMacro3)->GetName(),
611             fM->fDataFromMacroList->At(indexOfMacro2)->GetName(),
612             fM->fDataFromMacroList->At(indexOfMacro1)->GetName()));
613         
614         UpdateHistoCanvasTab();
615  
616         break;     
617       }
618     }
619   }
620
621   if (t != 0) delete t;
622   t = 0;
623   if (tFriend1 != 0)  delete tFriend1;
624   tFriend1 = 0;
625   if (tFriend2 != 0)  delete tFriend2;
626   tFriend2 = 0;
627
628   file->Close("R");
629   delete file;
630   file = 0;
631 }
632
633 //______________________________________________________
634 Int_t AliEveTRDTrackListEditor::GetNSelectedHistograms() const
635 {
636   // Returns the number of selected macros (or rather: of their selected data) in the "Histograms"-tab
637
638   Int_t count = 0;
639   
640   for (Int_t i = 0; i < fM->fDataFromMacroList->GetEntries(); i++)
641   {
642     if (fCheckButtons[i]->TGButton::GetState() == kButtonDown)  count++;
643   }
644
645   return count;
646 }
647
648 //______________________________________________________
649 void AliEveTRDTrackListEditor::HandleMacroPathSet()
650 {
651   // Takes the input of the text field (adding a macro), checks if the macro can be
652   // accessed (and that it exists) and adds the macro to the macro list via AddMacro(...).
653   // You can use environment variables in the text field, e.g. "$ALICE_ROOT/Eve/alice-macro/myMacro.C".
654
655   if (strlen(fteField->GetText()) != 0)
656   {  
657     // Expand the pathname
658     Char_t* systemPath = gSystem->ExpandPathName(fteField->GetText());
659     fteField->SetText(systemPath);
660     delete systemPath;
661     systemPath = 0;
662                         
663     // Check if file exists
664     FILE* fp = NULL;
665
666     fp = fopen(fteField->GetText(), "rb");
667     if (fp != NULL)
668     {
669       fclose(fp);
670
671       // Extract filename
672       Char_t* name = (Char_t*)strrchr(fteField->GetText(), '/');
673
674       // Current path
675       if (name == NULL)
676       {
677         name = new Char_t[AliEveTRDTrackList::fkMaxMacroNameLength];
678         memset(name, '\0', sizeof(Char_t) * AliEveTRDTrackList::fkMaxMacroNameLength);
679         sprintf(name, "%s", fteField->GetText());
680
681         // Add path to textfield -> Path is "./" -> Use length for the name + 2
682         Char_t pathname[AliEveTRDTrackList::fkMaxMacroNameLength + 2];
683         memset(pathname, '\0', sizeof(Char_t) * (AliEveTRDTrackList::fkMaxMacroNameLength + 2));
684         sprintf(pathname, "./%s", fteField->GetText());
685         fteField->SetText(pathname);
686
687         AddMacro(name);  
688         if (name != 0)  delete name;
689         name = 0;
690       }
691       // Different path
692       else
693       {
694         // Extract path
695         Char_t* path = new Char_t[AliEveTRDTrackList::fkMaxMacroPathLength];
696         memset(path, '\0', sizeof(Char_t) * AliEveTRDTrackList::fkMaxMacroPathLength);
697         strncpy(path, fteField->GetText(), strlen(fteField->GetText()) - strlen(name));
698         
699         // Ignore the slash "/" in name
700         AddMacro(name + 1, path);  
701   
702         if (path != 0)  delete path;
703         path = 0;
704       }       
705     }
706     else
707     {
708       new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
709                    "File does not exist or you do not have read permission!", kMBIconExclamation, kMBOk);
710     }
711   }
712 }
713
714 //______________________________________________________
715 void AliEveTRDTrackListEditor::HandleNewEventLoaded()
716 {
717   // Closes the tabs created by this object and sets a flag that will
718   // cause the function SetModel() to inherit the macro lists + style
719   // for the next AliEveTRDTrackList from the current one.
720
721   // Inherit the macro list and track style for the next track list!
722   fInheritSettings = kTRUE;
723
724   // Close the tabs
725   CloseTabs();
726 }
727
728 //______________________________________________________
729 void AliEveTRDTrackListEditor::HandleTabChangedToIndex(Int_t index)
730 {
731   // Saves the current tab in the current AliEveTRDTrackList.
732
733   fM->SetSelectedTab(index);
734 }
735
736 //______________________________________________________
737 void AliEveTRDTrackListEditor::InheritMacroList()
738 {
739   // The old macro list is possibly stored in the corresponding interior map. This function will 
740   // use this interior map to move the data from the interior map to the newly loaded AliEveTRDTrackList. 
741   // Then the interior map will be cleaned up. With this, the settings will be inherited from the previously 
742   // loaded AliEveTRDTrackList.
743
744   if (fInheritedMacroList == 0)  return;
745
746   // Clear list  
747   fM->fMacroList->Delete();
748
749   // Store data from interior list in the track list's map
750   TMapIter* iter = (TMapIter*)fInheritedMacroList->MakeIterator();
751   
752   TObject* key = 0;
753   TMacroData* macro = 0;
754   
755   while ((key = iter->Next()) != 0)
756   {
757     macro = (TMacroData*)fInheritedMacroList->GetValue(key);
758     if (macro != 0)  fM->fMacroList->Add(new TObjString(key->GetName()), 
759                                          new TMacroData(macro->GetName(), macro->GetPath(), macro->GetType()));
760     else
761     {
762       Error("AliEveTRDTrackListEditor::InheritMacroList", Form("Failed to inherit the macro \"%s\"!", key->GetName()));
763     }
764   }
765   
766   fInheritedMacroList->Delete();
767   delete fInheritedMacroList;
768   fInheritedMacroList = 0;
769 }
770
771 //______________________________________________________
772 void AliEveTRDTrackListEditor::InheritStyle()
773 {
774   // The old styles are stored in the corresponding button groups. This function will replace
775   // the style settings of the newly loaded AliEveTRDTrackList with the old styles. With this, the settings
776   // will be inherited from the previously loaded AliEveTRDTrackList.
777
778   for (Int_t ind = 0; ind < 3; ind++)
779   {
780     if (fbgStyleTrack->GetButton(ind)->IsOn())
781     {
782       SetTrackModel(ind);
783       break;
784     }
785   }
786   for (Int_t ind = 0; ind < 3; ind++)
787   {
788     if (fbgStyleColor->GetButton(ind)->IsOn())
789     {
790       SetTrackColor(ind);
791       break;
792     }
793   }
794 }
795
796 //______________________________________________________
797 void AliEveTRDTrackListEditor::RemoveMacros()
798 {
799   // Removes the selected macros from the corresponding list.
800
801   TList* iterator = new TList();
802   
803   ftlMacroList->GetSelectedEntries(iterator);
804   fM->RemoveSelectedMacros(iterator);
805
806   if (iterator != 0)  delete iterator;
807
808   iterator = new TList();
809   ftlMacroSelList->GetSelectedEntries(iterator);
810   fM->RemoveSelectedMacros(iterator);
811
812   // Selected macros are deleted from the list -> No selected entries left
813   fM->fMacroListSelected = 0;
814
815   UpdateMacroList();
816
817   if (iterator != 0)  delete iterator;
818   iterator = 0;
819 }
820
821 //______________________________________________________
822 void AliEveTRDTrackListEditor::SaveMacroList(TMap* list)
823 {
824   // Saves the provided macro list in an interior list. This list will be used by
825   // InheritMacroList() to restore the data in "list". With this method one is able
826   // to inherit the macro list from track list to track list (i.e. from event to event).
827
828   if (fInheritedMacroList != 0)
829   {
830     fInheritedMacroList->Delete();
831     delete fInheritedMacroList;
832   }
833   fInheritedMacroList = new TMap();
834   fInheritedMacroList->SetOwnerKeyValue(kTRUE, kTRUE);
835
836   TMapIter* iter = (TMapIter*)list->MakeIterator();
837   TObject* key = 0;
838   TMacroData* macro = 0;
839   
840   while ((key = iter->Next()) != 0)
841   {
842     macro = (TMacroData*)fM->fMacroList->GetValue(key);
843     if (macro != 0) fInheritedMacroList->Add(new TObjString(key->GetName()), 
844                                              new TMacroData(macro->GetName(), macro->GetPath(), macro->GetType()));
845     else
846     {
847       Error("AliEveTRDTrackListEditor::SaveMacroList", Form("Failed to inherit the macro \"%s\"!", key->GetName()));
848     }
849   }
850 }
851
852 //______________________________________________________
853 void AliEveTRDTrackListEditor::SetDrawingToHistoCanvasTab()
854 {
855   // Sets gPad to the tab with the name of the current AliEveTRDTrackList. If this tab does
856   // not exist, it will be created. Otherwise, it is re-used.
857
858   // If the tab with the canvas has been closed, the canvas will be deleted.
859   // So, if there is no tab, set the canvas pointer to zero and recreate it in a new tab.
860   if (fHistoCanvas != 0) 
861   {
862     if (gEve->GetBrowser()->GetTab(1)->SetTab(fHistoCanvasName->GetString()) == 0)
863     {
864       fHistoCanvas = 0;
865     }
866   }
867
868   if (!fHistoCanvas)
869   {
870     fHistoCanvas = gEve->AddCanvasTab(fM->GetName());     
871   }
872                            
873   gPad = fHistoCanvas;
874 }
875
876 //______________________________________________________
877 void AliEveTRDTrackListEditor::SetModel(TObject* obj)
878 {  
879   // Sets the model object, updates the related data in the GUI and
880   // inherits settings (cf. Inherit*(...)), if the flag fInheritSettings is set to kTRUE.
881
882   fM = dynamic_cast<AliEveTRDTrackList*>(obj);
883
884   if (fM == 0) 
885   {
886     Error("SetModel", "Parameter is zero pointer");
887     return;
888   }
889
890   // Provide a pointer to this editor
891   fM->fEditor = this;
892
893   // If macro list + track style shall be inherited from previously loaded track list, do so
894   if (fInheritSettings)
895   {
896     InheritMacroList();
897     InheritStyle();
898
899     fInheritSettings = kFALSE;
900   }
901
902   // Select the correct styles
903   Int_t b = 0;
904   UChar_t style = fM->GetSelectedTrackStyle();
905   if (TESTBIT(style, AliEveTRDTrack::kSource)) b = 2;
906   else 
907   {
908     if (TESTBIT(style, AliEveTRDTrack::kPID)) b = 1;
909     else b = 0;
910   } 
911   fbgStyleColor->SetButton(b, kTRUE);
912
913
914   if (TESTBIT(style, AliEveTRDTrack::kTrackCosmics)) b = 2;
915   else
916   {
917     if (TESTBIT(style, AliEveTRDTrack::kTrackModel)) b = 1;
918     else b = 0;
919   }
920   fbgStyleTrack->SetButton(b, kTRUE);
921   
922   UpdateMacroList();
923   UpdateHistoList(); 
924
925   // View correct tab
926   GetGedEditor()->GetTab()->SetTab(fM->GetSelectedTab()); 
927 }
928
929 //______________________________________________________
930 void AliEveTRDTrackListEditor::SetTrackColor(Int_t ind)
931 {
932   // Sets the color model for the tracks, updates the tracks with this model and
933   // redraws the scene.
934
935   switch(ind)
936   { 
937     case AliTRDpidUtil::kLQ:
938       fM->UpdateTrackStyle(AliEveTRDTrack::kPID, AliTRDpidUtil::kLQ);
939       break;
940     case AliTRDpidUtil::kNN:
941       fM->UpdateTrackStyle(AliEveTRDTrack::kPID, AliTRDpidUtil::kNN);
942       break;
943     default:
944       fM->UpdateTrackStyle(AliEveTRDTrack::kSource);
945       break;
946   }
947
948   gEve->Redraw3D();
949 }
950
951 //______________________________________________________
952 void AliEveTRDTrackListEditor::SetTrackModel(Int_t ind)
953 {
954   // Sets the track model for the tracks, updates the tracks with this model and
955   // redraws the scene.
956
957   switch(ind)
958   { 
959     case AliEveTRDTrack::kRieman:
960       fM->UpdateTrackStyle(AliEveTRDTrack::kTrackModel, AliEveTRDTrack::kRieman);
961       break;
962     case AliEveTRDTrack::kKalman:
963       fM->UpdateTrackStyle(AliEveTRDTrack::kTrackModel, AliEveTRDTrack::kKalman);
964       break;
965     default:
966       fM->UpdateTrackStyle(AliEveTRDTrack::kTrackCosmics);
967       break;
968   }
969
970   gEve->Redraw3D();
971 }
972
973 //______________________________________________________
974 void AliEveTRDTrackListEditor::UpdateDataFromMacroListSelection()
975 {
976   // Saves the current selection in the "Histograms"-tab to the current
977   // AliEveTRDTrackList. This means that the selection is updated and won't
978   // get lost, if another editor is loaded in Eve.
979
980   for (Int_t i = 0; i < fM->fDataFromMacroList->GetEntries(); i++)
981   {
982     fM->SetHistoDataSelection(i, fCheckButtons[i]->IsOn());
983   }
984 }
985
986 //______________________________________________________
987 void AliEveTRDTrackListEditor::UpdateHistoCanvasTab()
988 {
989   // Updates the histogram and the corresponding tab (including titles).
990
991   // Update name of the tab (tab has been set to current tab!)
992   fHistoCanvasName->SetString(fM->GetName());  
993
994   // Use a copy of fHistoCanvasName!! -> If the user closes a tab manually, the TGString
995   // will be deleted -> Error might occur, when accessing the pointer   
996   gEve->GetBrowser()->GetTab(1)->GetCurrentTab()->SetText(new TGString(fHistoCanvasName));
997
998   // Switch tabs to force redrawing
999   gEve->GetBrowser()->GetTab(1)->SetTab(0);
1000   gEve->GetBrowser()->GetTab(1)->SetTab(fHistoCanvasName->GetString());
1001   fHistoCanvas->Update();
1002 }
1003
1004 //______________________________________________________
1005 void AliEveTRDTrackListEditor::UpdateHistoList()
1006 {
1007   // Reloads (updates) the buttons in the "Histograms"-tab via
1008   // the current AliEveTRDTrackList (data).
1009
1010   fHistoSubFrame->TGCompositeFrame::Cleanup();
1011   
1012   // Set buttons for histograms
1013   if (fCheckButtons != 0) delete fCheckButtons;
1014   fCheckButtons = new TGCheckButton*[fM->fDataFromMacroList->GetEntries()];
1015   
1016   TObjString* iter = (TObjString*)fM->fDataFromMacroList->First();
1017   for (Int_t i = 0; i < fM->fDataFromMacroList->GetEntries() && iter != 0; i++)
1018   {
1019     fCheckButtons[i] = new TGCheckButton(fHistoSubFrame, iter->GetName());
1020     fHistoSubFrame->AddFrame(fCheckButtons[i]);
1021     
1022     fCheckButtons[i]->SetState(kButtonUp, kFALSE);
1023     fCheckButtons[i]->MapRaised();
1024     fCheckButtons[i]->SetOn(fM->HistoDataIsSelected(i));
1025     fCheckButtons[i]->Connect("Clicked()", "AliEveTRDTrackListEditor", this, "UpdateDataFromMacroListSelection()");
1026             
1027     iter = (TObjString*)fM->fDataFromMacroList->After(iter);
1028   }  
1029 }
1030
1031 //______________________________________________________
1032 void AliEveTRDTrackListEditor::UpdateMacroList()
1033 {
1034   // Reloads (updates) the macro list (selection AND process macros) via
1035   // the current AliEveTRDTrackList (data).
1036
1037   ftlMacroList->RemoveAll();
1038   ftlMacroSelList->RemoveAll();
1039    
1040   TMapIter* iter = (TMapIter*)fM->fMacroList->MakeIterator();
1041   TObject* key = 0;
1042   TMacroData* macro = 0;
1043
1044   Int_t ind = 0;
1045   while ((key = iter->Next()) != 0)
1046   {
1047     macro = (TMacroData*)fM->fMacroList->GetValue(key);
1048     if (macro != 0)
1049     {
1050       if (macro->IsProcessMacro())
1051       {
1052         ftlMacroList->AddEntry(macro->GetName(), ind);
1053         // Select, what has been selected before
1054         ftlMacroList->Select(ind, fM->MacroListIsSelected(ind));
1055         ind++;
1056       }
1057       else if (macro->IsSelectionMacro())
1058       {
1059         ftlMacroSelList->AddEntry(macro->GetName(), ind);
1060         // Select, what has been selected before
1061         ftlMacroSelList->Select(ind, fM->MacroListIsSelected(ind));
1062         ind++;
1063       }
1064       else
1065       {
1066         Error("AliEveTRDTrackListEditor::UpdateMacroList()", 
1067               Form("Macro \"%s/%s.C\" is neither a selection macro nor a process macro!",
1068                    macro->GetPath(), macro->GetName()));                                        
1069       }
1070     }
1071     else
1072     {
1073       Error("AliEveTRDTrackListEditor::UpdateMacroList()", 
1074               Form("Macro list is corrupted: Macro \"%s\" not found!", key->GetName()));            
1075     }     
1076   }
1077
1078   ftlMacroList->SortByName(); 
1079   ftlMacroSelList->SortByName(); 
1080 }
1081
1082 //______________________________________________________
1083 void AliEveTRDTrackListEditor::UpdateMacroListSelection(Int_t ind)
1084 {
1085   // Saves the current selection in the macro listS to the current
1086   // AliEveTRDTrackList. This means that the selection is updated and won't
1087   // get lost, if another editor is loaded in Eve.
1088   // NOTE: The indices in BOTH lists will be unique!
1089
1090   // Toggle selected item
1091   fM->SetMacroListSelection(ind, !fM->MacroListIsSelected(ind));
1092 }
1093
1094
1095 //______________________________________________________
1096 //______________________________________________________
1097 //______________________________________________________
1098
1099
1100 /////////////////////////////////////////////////
1101 ClassImp(AliEveTRDMacroWizard)
1102 /////////////////////////////////////////////////
1103
1104 //______________________________________________________
1105 AliEveTRDMacroWizard::AliEveTRDMacroWizard(const TGWindow* p)
1106   :TGMainFrame(p ? p : gClient->GetRoot(), 10, 10, kMainFrame | kVerticalFrame)
1107   ,fText(0x0)
1108   ,fCombo(0x0)
1109   ,fTextEdit(0x0)
1110   ,fbCreate(0x0)
1111   ,fbCancel(0x0)
1112 {
1113   // Creates the macro wizard.
1114
1115   const Int_t width = 300;
1116
1117   // horizontal frame
1118   TGHorizontalFrame *fFrameName = new TGHorizontalFrame(this, 10, 10, kHorizontalFrame);
1119   TGLabel *fLabel = new TGLabel(fFrameName, "Name*");
1120   fLabel->SetTextJustify(36);
1121   fLabel->SetMargins(0,0,0,0);
1122   fLabel->SetWrapLength(-1);
1123   fFrameName->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1124
1125   fText = new TGTextEntry(fFrameName);
1126   fText->SetMaxLength(255);
1127   fText->SetAlignment(kTextLeft);
1128   fText->SetText("");
1129   fText->Resize(width, fText->GetDefaultHeight());
1130   fFrameName->AddFrame(fText, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2));
1131
1132
1133   // horizontal frame
1134   TGHorizontalFrame *fFrameComment = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1135   fLabel = new TGLabel(fFrameComment, "Comment");
1136   fLabel->SetTextJustify(36);
1137   fLabel->SetMargins(0,0,0,0);
1138   fLabel->SetWrapLength(-1);
1139   fFrameComment->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1140
1141   fTextEdit = new TGTextEdit(fFrameComment, width, 5*fText->GetDefaultHeight());
1142   fFrameComment->AddFrame(fTextEdit, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2));
1143
1144   // horizontal frame
1145   TGHorizontalFrame *fFrameType = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1146   fLabel = new TGLabel(fFrameType, "Type*");
1147   fLabel->SetTextJustify(36);
1148   fLabel->SetMargins(0,0,0,0);
1149   fLabel->SetWrapLength(-1);
1150   fFrameType->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1151
1152   fCombo = new TGComboBox(fFrameType, -1, kHorizontalFrame | kSunkenFrame | kDoubleBorder | kOwnBackground);
1153   fCombo->AddEntry("Single Track Selection", AliEveTRDTrackList::kSingleTrackSelect);
1154   fCombo->AddEntry("Pair Tracks Selection", AliEveTRDTrackList::kCorrelTrackSelect);
1155   fCombo->AddEntry("Single Track Analyse", AliEveTRDTrackList::kSingleTrackAnalyse);
1156   fCombo->AddEntry("Single Track Histo", AliEveTRDTrackList::kSingleTrackHisto);
1157   fCombo->AddEntry("Pair Tracks Analyse", AliEveTRDTrackList::kCorrelTrackAnalyse);
1158   fCombo->AddEntry("Pair Tracks Histo", AliEveTRDTrackList::kCorrelTrackHisto);
1159   fCombo->Select(-1);
1160   fCombo->Resize(width, fText->GetDefaultHeight());
1161   fFrameType->AddFrame(fCombo, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2));
1162
1163   // horizontal frame
1164   TGHorizontalFrame *fFrameAction = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1165   fbCancel = new TGTextButton(fFrameAction, "Cancel");
1166   fbCancel->SetToolTipText("Exit macro creation wizard");
1167   fFrameAction->AddFrame(fbCancel, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2)); 
1168   fbCreate = new TGTextButton(fFrameAction, "Done");
1169   fbCreate->SetToolTipText("Use settings to create the macro");
1170   fFrameAction->AddFrame(fbCreate, new TGLayoutHints(kLHintsRight | kLHintsTop,2,2,2,2)); 
1171
1172
1173   // horizontal frame
1174   TGHorizontalFrame *fFrameText = new TGHorizontalFrame(this,10,10,kHorizontalFrame);
1175   fLabel = new TGLabel(fFrameText, "(*) Mandatory fields");
1176   fLabel->SetTextJustify(36);
1177   fLabel->SetMargins(0,0,0,0);
1178   fLabel->SetWrapLength(-1);
1179   fFrameText->AddFrame(fLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
1180
1181   // put things together  
1182   AddFrame(fFrameName, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1183   AddFrame(fFrameComment, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1184   AddFrame(fFrameType, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1185   AddFrame(fFrameAction, new TGLayoutHints(kLHintsRight | kLHintsTop | kLHintsExpandX,2,2,2,2));
1186
1187   TGHorizontal3DLine *fLine = new TGHorizontal3DLine(this, 281, 2);
1188   AddFrame(fLine, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1189   AddFrame(fFrameText, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
1190
1191
1192   SetWindowName("TRD Macro Wizard");
1193   SetMWMHints(kMWMDecorAll,
1194               kMWMFuncAll,
1195               kMWMInputModeless);
1196   MapSubwindows();
1197
1198   Resize(GetDefaultSize());
1199   MapWindow();
1200
1201   // Do the linking
1202   //fCombo->Connect("Selected(Int_t)", "AliEveTRDMacroWizard", this, "Create(Int_t)");
1203   fbCreate->Connect("Clicked()", "AliEveTRDMacroWizard", this, "HandleCreate()");
1204   fbCancel->Connect("Clicked()", "AliEveTRDMacroWizard", this, "CloseWindow()");
1205
1206   // Standard choice
1207   fCombo->Select(1, kFALSE);
1208 }  
1209
1210 const Char_t *fIncludes = 
1211 "#if !defined(__CINT__) || defined(__MAKECINT__)\n"
1212 "#include <TROOT.h>\n"
1213 "#include <TH1.h>\n"
1214 "#include <TRD/AliTRDgeometry.h>\n"
1215 "#include <TRD/AliTRDcluster.h>\n"
1216 "#include <TRD/AliTRDseedV1.h>\n"
1217 "#include <TRD/AliTRDtrackV1.h>\n"
1218 "#endif\n";
1219
1220 const Char_t *fMacroTemplate[7] = {
1221 ""
1222 ,"  if (!track) return kFALSE;\n"
1223
1224 ,"  n = 0;\n"
1225 "  r = 0x0;\n"
1226 "  if (!track) return;\n"
1227
1228 ,"  if (!track) return 0x0;\n"
1229 "  TH1* h = 0x0;\n\n"
1230 "// Set bins, xmin and xmax here\n"
1231 "  Int_t n = 1;\n"
1232 "  Double_t xmin = 0;\n"
1233 "  Double_t xmax = 100;\n\n" 
1234 "  if(!(h = (TH1*)gROOT->FindObject(\"h\"))){\n"
1235 "    h = new TH1(\"h\", \"Title\", n, xmin, xmax);\n"
1236 "    h->GetXaxis()->SetTitle("");\n"
1237 "    h->GetYaxis()->SetTitle("");\n"
1238 "  } else h->Reset();\n"
1239
1240 ,"  if (!track) return kFALSE;\n"
1241 "  if (!track2) return kFALSE;\n"
1242
1243 ,"  n = 0;\n"
1244 "  r = 0x0;\n"
1245 "  if (!track) return;\n"
1246 "  if (!track2) return;\n"
1247
1248 ,"  if (!track) return 0x0;\n"
1249 "  if (!track2) return 0x0;\n"
1250 "  TH1* h = 0x0;\n\n"
1251 "// Set bins, xmin and xmax here\n"
1252 "  Int_t n = 1;\n"
1253 "  Double_t xmin = 0;\n"
1254 "  Double_t xmax = 100;\n\n" 
1255 "  if(!(h = (TH1*)gROOT->FindObject(\"h\"))){\n"
1256 "    h = new TH1(\"h\", \"Title\", n, xmin, xmax);\n"
1257 "    h->GetXaxis()->SetTitle("");\n"
1258 "    h->GetYaxis()->SetTitle("");\n"
1259 "  } else h->Reset();\n"
1260 };
1261 //______________________________________________________
1262 void AliEveTRDMacroWizard::Create(Int_t type)
1263 {
1264   // Creates the macro with the selected type (combo box).
1265
1266   const Char_t *name = fText->GetText();
1267   if(strcmp(name,"")==0){
1268     AliInfo("Please specify a name for your macro.");
1269     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1270                  "Please specify a name for your macro.", kMBIconExclamation, kMBOk);
1271     //fCombo->Select(-1);
1272     return;
1273   }
1274
1275   // Note: gSystem->AccessPathName(...) returns kTRUE, if the access FAILED!
1276   if(!gSystem->AccessPathName(Form("./%s.C", name))){
1277     // If there is already a file with this name -> Error
1278     AliInfo(Form("A macro \"%s.C\" already exists in the current directory!\nPlease choose another name!", name));
1279     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1280                  Form("A macro \"%s.C\" already exists in the current directory!\nPlease choose another name!", name), kMBIconExclamation, kMBOk);
1281     //fCombo->Select(-1);
1282     return;
1283   }
1284
1285   FILE* fp = 0x0;
1286   if(!(fp = fopen(Form("%s.C", name), "wt"))){
1287     AliInfo("Couldn't create macro file.");
1288     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1289                  "Couldn't create macro file.", kMBIconExclamation, kMBOk);
1290     //fCombo->Select(-1);
1291     return;
1292   }
1293
1294   TGText *comment = fTextEdit->GetText();
1295   Char_t *line = 0x0; Int_t iline = 0;
1296   while((line = comment->GetLine(TGLongPosition(0,iline++), 200))) fprintf(fp, "// %s\n", line);
1297
1298   fprintf(fp, "\n%s\n", fIncludes);
1299
1300   switch(type){
1301   case AliEveTRDTrackList::kSingleTrackSelect:
1302     fprintf(fp, "Bool_t %s(const AliTRDtrackV1 *track)\n", name);
1303     break;
1304   case AliEveTRDTrackList::kCorrelTrackSelect:
1305     fprintf(fp, "Bool_t %s(const AliTRDtrackV1 *track, const AliTRDtrackV1 *track2)\n", name);
1306     break;
1307   case AliEveTRDTrackList::kSingleTrackAnalyse:
1308     fprintf(fp, "void %s(const AliTRDtrackV1 *track, Double_t*& r, Int_t& n)\n", name);
1309     break;
1310   case AliEveTRDTrackList::kSingleTrackHisto:
1311     fprintf(fp, "TH1* %s(const AliTRDtrackV1 *track)\n", name);
1312     break;
1313   case AliEveTRDTrackList::kCorrelTrackAnalyse:
1314     fprintf(fp, "void %s(const AliTRDtrackV1 *track, const AliTRDtrackV1 *track2, Double_t*& r, Int_t& n)\n", name);
1315     break;
1316   case AliEveTRDTrackList::kCorrelTrackHisto:
1317     fprintf(fp, "TH1* %s(const AliTRDtrackV1 *track, const AliTRDtrackV1 *track2)\n", name);
1318     break;
1319   default:
1320     AliInfo(Form("Unknown type[%d]", type));
1321     new TGMsgBox(gClient->GetRoot(), GetMainFrame(), "Error", 
1322                  Form("Unknown type[%d]", type), kMBIconExclamation, kMBOk);
1323     fclose(fp);
1324     gSystem->Exec(Form("rm -f %s.C", name));
1325     //fCombo->Select(-1);
1326     return;
1327   }
1328   
1329   fprintf(fp, "{\n%s\n", fMacroTemplate[type]);
1330   fprintf(fp, "// add your own code here\n\n\n}\n");
1331   fclose(fp);
1332
1333   Emit("Create(Int_t)", type);
1334   Create((Char_t*)name);
1335   CloseWindow();
1336 }
1337
1338 //______________________________________________________
1339 void AliEveTRDMacroWizard::Create(Char_t *name)
1340 {
1341   // Emits the creation signal.
1342
1343   Emit("Create(Char_t*)", Form("%s.C", name));
1344 }
1345
1346 //______________________________________________________
1347 void AliEveTRDMacroWizard::HandleCreate()
1348 {
1349   // Handles the signal, when the creation button is pressed.
1350
1351   Create(fCombo->GetSelected());
1352 }