]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveDet/AliEveTRDTrackList.cxx
more user friendly interface (Ben)
[u/mrichter/AliRoot.git] / EVE / EveDet / AliEveTRDTrackList.cxx
1 // Uncomment to display debugging infos
2 //#define ALIEVETRDTRACKLIST_DEBUG
3
4 #include "AliEveTRDTrackList.h"
5
6 #include <AliTRDtrackV1.h>
7 #include <TFile.h>
8 #include <TFunction.h>
9 #include <TList.h>
10 #include <TObjString.h>
11 #include <TROOT.h>
12 #include <TSystem.h>
13 #include <TTree.h>
14 #include <TTreeStream.h>
15 #include <EveDet/AliEveTRDData.h>
16
17 ClassImp(AliEveTRDTrackList)
18
19 ///////////////////////////////////////////////////////////
20 /////////////   AliEveTRDTrackList ////////////////////////
21 ///////////////////////////////////////////////////////////
22 AliEveTRDTrackList::AliEveTRDTrackList(const Text_t* n, const Text_t* t, Bool_t doColor):
23   TEveElementList(n, t, doColor),
24   fMacroList(0),
25   fMacroSelList(0),
26   fDataFromMacroList(0),
27   fDataTree(0)
28 {
29   // Only accept childs of type AliEveTRDTrack
30   SetChildClass(AliEveTRDTrack::Class());
31
32   fMacroList = new TList();
33   fMacroSelList = new TList();
34   fDataFromMacroList = new TList();
35
36   fDataTree = new TTreeSRedirector("TRD.TrackListMacroData.root");
37
38   AddStandardMacros();
39 }
40
41 //______________________________________________________
42 AliEveTRDTrackList::~AliEveTRDTrackList()
43 {
44   if (fMacroList != 0)
45   {
46     fMacroList->Clear();
47     delete fMacroList;
48     fMacroList = 0;
49   }
50   if (fMacroSelList != 0)
51   {
52     fMacroSelList->Clear();
53     delete fMacroSelList;
54     fMacroSelList = 0;
55   } 
56   if (fDataFromMacroList != 0)
57   {
58     fDataFromMacroList->Clear();
59     delete fDataFromMacroList;
60     fDataFromMacroList = 0;
61   } 
62   if (fDataTree != 0)
63   {
64     delete fDataTree;
65     fDataTree = 0;
66   } 
67 }
68
69 //______________________________________________________
70 Int_t AliEveTRDTrackList::AddMacro(const Char_t* path, const Char_t* nameC)
71 {
72   // First check the type of the macro:
73   // If it has the signature of a selection macro:
74   // Bool_t MacroName(AliTRDtrackV1*)
75   // it is assumed to be a selection macro.
76   // If it has the signature of a process macro:
77   // void MacroName(AliTRDtrackV1*, Double_t*&, Int_t&)
78   // it is assumed to be a process macro.
79   // In all other cases: Macro is rejected
80   Bool_t isSelectionMacro = kFALSE;
81   Bool_t hasCorrectSignature = kFALSE;
82   
83
84   Char_t* entryName = MakeMacroEntry(path, nameC);
85
86   Char_t* pathname = new Char_t[300];
87   memset(pathname, '\0', sizeof(Char_t) * 300);
88
89   // Expand the path and create the pathname
90   Char_t* systemPath = gSystem->ExpandPathName(path);
91   sprintf(pathname, "%s/%s", systemPath, nameC);
92   delete systemPath;
93   systemPath = 0;
94
95   // Delete ".C" from filename
96   Char_t* name = new Char_t[strlen(nameC)];
97   memset(name, '\0', sizeof(Char_t) * strlen(nameC));
98   strncpy(name, nameC, strlen(nameC) - 2);
99
100   // Check, if files exists
101   FILE* fp = 0;
102
103   fp = fopen(pathname, "rb");
104   if (fp != 0)
105   {
106     fclose(fp);
107     fp = 0;
108   }
109   else
110   {
111     if (name != 0)  delete name;
112     name = 0;
113     if (pathname != 0)  delete pathname;
114     pathname = 0;
115     if (entryName != 0)  delete entryName;
116     entryName = 0;
117
118     return NOT_EXIST_ERROR;
119   }
120
121   // Clean up root, load the desired macro and then check the type of the macro
122   //gROOT->Reset("a");
123   gROOT->Reset();
124   gROOT->ProcessLineSync(Form(".L %s", pathname));
125
126   // Selection macro?
127   TFunction* f = gROOT->GetGlobalFunctionWithPrototype(name, "AliTRDtrackV1*", kTRUE);
128   if (f != 0x0)
129   {
130     if (!strcmp(f->GetReturnTypeName(), "Bool_t")) 
131     {
132       // Some additional check (is the parameter EXACTLY of the desired type?)
133       if (strstr(f->GetMangledName(), "AliTRDtrackV1mUsP") != 0x0)
134       {
135         hasCorrectSignature = kTRUE;
136         isSelectionMacro = kTRUE;
137       }
138     }
139   }
140   // Process macro?
141   else
142   {
143     f = gROOT->GetGlobalFunctionWithPrototype(name, "AliTRDtrackV1*, Double_t*&, Int_t&", kTRUE);
144     if (f != 0x0)
145     {
146       if (!strcmp(f->GetReturnTypeName(), "void"))
147       {
148         // Some additional check (are the parameters EXACTLY of the desired type?)
149         if (strstr(f->GetMangledName(), "AliTRDtrackV1mUsP") != 0x0 &&
150             strstr(f->GetMangledName(), "Double_tmUaNsP") != 0x0 &&
151             strstr(f->GetMangledName(), "Int_taNsP") != 0x0)
152         {
153           hasCorrectSignature = kTRUE;
154           isSelectionMacro = kFALSE;
155         }
156       }
157     }
158   }
159
160   // Clean up again / unload this function
161   gROOT->ProcessLineSync(Form(".U %s", pathname));
162   //gROOT->Reset("a");
163   gROOT->Reset();
164   if (name != 0)  delete name;
165   name = 0;
166   if (pathname != 0)  delete pathname;
167   pathname = 0;
168
169   // Has not the correct signature!
170   if (!hasCorrectSignature) 
171   {
172     if (entryName != 0)  delete entryName;
173     entryName = 0;
174     return SIGNATURE_ERROR;
175   }
176
177   Int_t returnValue = WARNING;
178
179   // Only add macro, if it is not already in the list
180   if (!isSelectionMacro && fMacroList->FindObject(entryName) == 0)
181   {
182     fMacroList->Add(new TObjString(entryName));
183     fMacroList->Sort();
184
185     returnValue = SUCCESS;
186   }
187   else if (isSelectionMacro && fMacroSelList->FindObject(entryName) == 0)
188   {
189     fMacroSelList->Add(new TObjString(entryName));
190     fMacroSelList->Sort();
191     
192     returnValue = SUCCESS;
193   }
194   else  returnValue = WARNING;
195
196   if (entryName != 0)  delete entryName;
197   entryName = 0;
198
199   return returnValue;
200 }
201
202 //______________________________________________________
203 void AliEveTRDTrackList::AddMacroFast(const Char_t* path, const Char_t* name, Bool_t toSelectionList)
204 {
205   Char_t* entry = MakeMacroEntry(path, name);
206   if (entry != 0)
207   {
208     if (toSelectionList)  fMacroSelList->Add(new TObjString(entry));
209     else                  fMacroList->Add(new TObjString(entry));
210     
211     delete entry;
212     entry = 0;
213
214 #ifdef ALIEVETRDTRACKLIST_DEBUG
215     // Successfull add will only be displayed in debug mode
216     printf("#AliEveTRDTrackList: Standard macros: Added macro %s/%s to %s list\n", path, name, 
217            (toSelectionList ? "selection" : "process"));
218 #endif
219   }
220   else
221   {
222     // Error will always be displayed
223     printf("#AliEveTRDTrackList: Standard macros: ERROR: Could not add macro %s/%s to %s list\n", path, name, 
224            (toSelectionList ? "selection" : "process"));
225   } 
226 }
227
228 //______________________________________________________
229 void AliEveTRDTrackList::AddStandardMacros()
230 {
231   // Add your standard macros here, e.g.: 
232   // To add a macro without any checks (very fast, but unsafe):
233   // AddMacroFast("$(ALICE_ROOT)/myFolder", "myMacroName.C", isSelMacro);
234   // To add a macro with checks (slower, but safe):
235   // AddMacro("$(ALICE_ROOT)/myFolder", "myMacroName.C");
236   // -> If the file does not exist, nothing happens. So if you want to handle this,
237   // use the return value of AddMacro (NOT_EXIST_ERROR is returned, if file does not exist)
238   AddMacro("$(ALICE_ROOT)/TRD/qaRec/macros", "clusterSelection.C");
239   AddMacro("$(ALICE_ROOT)/TRD/qaRec/macros", "chargeDistr.C");
240 }
241
242 //______________________________________________________
243 void AliEveTRDTrackList::ApplyProcessMacros(TList* iterator)
244 {
245   if (iterator->GetEntries() <= 0)  return;
246
247   Char_t name[100];
248   Char_t path[300];
249   Char_t pathname[400];
250   Char_t** cmds = new Char_t*[iterator->GetEntries()];
251
252   AliEveTRDTrack* track = 0;
253   AliTRDtrackV1 *trackv1 = 0;
254
255   // Clear root
256   gROOT->Reset();
257   
258   // Clear old data and re-allocate
259   if (fDataFromMacroList != 0) delete fDataFromMacroList;
260   fDataFromMacroList = new TList();
261
262   if (fDataTree == 0) fDataTree = new TTreeSRedirector("TRD.TrackListMacroData.root");
263
264   // Collect the commands for each macro and add them to "data-from-list"
265   for (Int_t i = 0; i < iterator->GetEntries(); i++)
266   {
267     memset(name, '\0', sizeof(Char_t) * 100);
268     memset(path, '\0', sizeof(Char_t) * 300);
269     memset(pathname, '\0', sizeof(Char_t) * 400);
270
271     cmds[i] = new Char_t[430];
272     memset(cmds[i], '\0', sizeof(Char_t) * 430);
273
274 #ifdef ALIEVETRDTRACKLIST_DEBUG
275     printf("AliEveTRDTrackList: Applying process macro: %s\n", iterator->At(i)->GetTitle());
276 #endif
277  
278     // Extract path and name -> Make pathname
279     sscanf(iterator->At(i)->GetTitle(), "%s (Path: %s)", name, path);
280    
281     // Add to "data-from-list"
282     fDataFromMacroList->Add(new TObjString(name));
283
284     // Delete ")" at the end of path
285     path[strlen(path)] = '\0';
286     path[strlen(path) - 1] = '\0';
287     sprintf(pathname, "%s/%s", path, name);
288     sprintf(cmds[i], ".x %s(automaticTrackV1, results, n);", pathname);    
289   }  
290   
291   // Walk through the list of tracks     
292   for (TEveElement::List_i iter = this->BeginChildren(); iter != this->EndChildren(); ++iter)
293   {
294     track = dynamic_cast<AliEveTRDTrack*>(*iter);
295
296     if (!track)  continue;
297       
298     // Skip tracks that have not been selected
299     if (!track->GetRnrState())  continue;
300       
301     trackv1 = (AliTRDtrackV1*)track->GetUserData();
302
303     track->ExportToCINT((Text_t*)"automaticTrack");
304     // Cast to AliTRDtrackV1
305     gROOT->ProcessLineSync("AliTRDtrackV1* automaticTrackV1 = (AliTRDtrackV1*)automaticTrack->GetUserData();");
306
307     // Collect data for each macro
308     for (Int_t i = 0; i < iterator->GetEntries(); i++)
309     {
310       // Create data pointers in CINT, execute the macro and get the data
311       gROOT->ProcessLineSync("Double_t* results = 0;");
312       gROOT->ProcessLineSync("Int_t n = 0;");
313       gROOT->ProcessLineSync(cmds[i]);
314       Double_t* results = (Double_t*)gROOT->ProcessLineSync("results;");
315       Int_t nResults = (Int_t)gROOT->ProcessLineSync("n;");
316       
317       if (results == 0)
318       {
319         Error("Apply macros", Form("Error reading data from macro \"%s\"", iterator->At(i)->GetTitle()));
320         continue;
321       }
322       for (Int_t resInd = 0; resInd < nResults; resInd++)
323       {
324         (*fDataTree) << Form("TrackData%d", i) << Form("Macro%d=", i) << results[resInd] << (Char_t*)"\n";   
325       }
326
327       delete results;
328       results = 0;
329     }
330   }    
331
332   delete fDataTree;
333   fDataTree = 0;
334
335   if (cmds != 0)  delete [] cmds;
336
337   // Clear root
338   gROOT->Reset();
339   
340   // Now the data is stored in "TRD.TrackListMacroData.root"
341   // The editor will access this file to display the data
342 }
343
344 //______________________________________________________
345 void AliEveTRDTrackList::ApplySelectionMacros(TList* iterator)
346 {
347   if (iterator->GetEntries() <= 0)  return;
348
349   Char_t name[100];
350   Char_t path[300];
351   Char_t pathname[400];
352   Char_t cmd[430];
353
354   AliEveTRDTrack* track = 0;
355   AliTRDtrackV1 *trackv1 = 0;
356   Bool_t selectedByMacro = kFALSE;
357
358   // Clear root
359   gROOT->Reset();
360
361   for (Int_t i = 0; i < iterator->GetEntries(); i++)
362   {
363     memset(name, '\0', sizeof(Char_t) * 100);
364     memset(path, '\0', sizeof(Char_t) * 300);
365     memset(pathname, '\0', sizeof(Char_t) * 400);
366     memset(cmd, '\0', sizeof(Char_t) * 430);
367
368 #ifdef ALIEVETRDTRACKLIST_DEBUG
369     printf("AliEveTRDTrackList: Applying selection macro: %s\n", iterator->At(i)->GetTitle());
370 #endif
371       
372     // Extract path and name -> Make pathname
373     sscanf(iterator->At(i)->GetTitle(), "%s (Path: %s)", name, path);
374     // Delete ")" at the end of path.
375     path[strlen(path)] = '\0';
376     path[strlen(path) - 1] = '\0';
377     
378     sprintf(pathname, "%s/%s", path, name);
379     sprintf(cmd, ".x %s(automaticTrackV1);", pathname);
380
381     // Walk through the list of tracks
382     for (TEveElement::List_i iter = this->BeginChildren(); iter != this->EndChildren(); ++iter)
383     {
384       track = dynamic_cast<AliEveTRDTrack*>(*iter);
385
386       if (!track) continue;
387       
388       trackv1 = (AliTRDtrackV1*)track->GetUserData();
389
390       track->ExportToCINT((Text_t*)"automaticTrack");
391       // Cast to AliTRDtrackV1
392       gROOT->ProcessLineSync("AliTRDtrackV1* automaticTrackV1 = (AliTRDtrackV1*)automaticTrack->GetUserData();");
393       selectedByMacro = (Bool_t)gROOT->ProcessLineSync(cmd);
394       track->SetRnrState(selectedByMacro);         
395     }
396   }
397
398   // Clear root
399   gROOT->Reset();  
400 }
401
402 //______________________________________________________
403 Char_t* AliEveTRDTrackList::MakeMacroEntry(const Char_t* path, const Char_t* name)
404 {
405   Char_t* entry = new Char_t[400];
406   memset(entry, '\0', sizeof(Char_t) * 400);
407
408   Char_t* systemPath = gSystem->ExpandPathName(path);
409   sprintf(entry, "%s (Path: %s)", name, systemPath);
410   delete systemPath;
411   systemPath = 0;
412
413   return entry;
414 }
415
416 //______________________________________________________
417 void AliEveTRDTrackList::RemoveProcessMacros(TList* iterator) 
418 {
419   for (Int_t i = 0; i < iterator->GetEntries(); i++)
420   {
421     fMacroList->Remove(fMacroList->FindObject(iterator->At(i)->GetTitle()));
422   }
423 }
424
425 //______________________________________________________
426 void AliEveTRDTrackList::RemoveSelectionMacros(TList* iterator) 
427 {
428   for (Int_t i = 0; i < iterator->GetEntries(); i++)
429   {
430     fMacroSelList->Remove(fMacroSelList->FindObject(iterator->At(i)->GetTitle()));
431   }
432 }