1 // Uncomment to display debugging infos
2 //#define ALIEVETRDTRACKLIST_DEBUG
4 #include "AliEveTRDTrackList.h"
6 #include <AliTRDtrackV1.h>
11 #include <TObjString.h>
15 #include <TTreeStream.h>
16 #include <EveDet/AliEveTRDData.h>
18 ClassImp(AliEveTRDTrackList)
20 ///////////////////////////////////////////////////////////
21 ///////////// AliEveTRDTrackList ////////////////////////
22 ///////////////////////////////////////////////////////////
23 AliEveTRDTrackList::AliEveTRDTrackList(const Text_t* n, const Text_t* t, Bool_t doColor):
24 TEveElementList(n, t, doColor),
27 fDataFromMacroList(0),
29 fHistoDataSelected(0),
30 fMacroListSelected(0),
31 fMacroSelListSelected(0),
32 fSelectedTab(1) // Standard tab: "Apply macros" (index 1)
34 // Only accept childs of type AliEveTRDTrack
35 SetChildClass(AliEveTRDTrack::Class());
37 fMacroList = new TList();
38 fMacroSelList = new TList();
39 fDataFromMacroList = new TList();
41 //fDataTree = new TTreeSRedirector("TRD.TrackListMacroData.root");
46 //______________________________________________________
47 AliEveTRDTrackList::~AliEveTRDTrackList()
55 if (fMacroSelList != 0)
57 fMacroSelList->Delete();
61 if (fDataFromMacroList != 0)
63 fDataFromMacroList->Delete();
64 delete fDataFromMacroList;
65 fDataFromMacroList = 0;
74 //______________________________________________________
75 Int_t AliEveTRDTrackList::AddMacro(const Char_t* path, const Char_t* nameC, Bool_t forceReload)
77 // First check the type of the macro:
78 // If it has the signature of a selection macro:
79 // Bool_t MacroName(AliTRDtrackV1*)
80 // it is assumed to be a selection macro.
81 // If it has the signature of a process macro:
82 // void MacroName(AliTRDtrackV1*, Double_t*&, Int_t&)
83 // it is assumed to be a process macro.
84 // In all other cases: Macro is rejected
85 Bool_t isHistoMacro = kFALSE;
86 Bool_t isSelectionMacro = kFALSE;
87 Bool_t hasCorrectSignature = kFALSE;
90 Char_t* entryName = MakeMacroEntry(path, nameC);
92 Char_t pathname[fkMaxMacroPathNameLength];
93 memset(pathname, '\0', sizeof(Char_t) * fkMaxMacroPathNameLength);
95 // Expand the path and create the pathname
96 Char_t* systemPath = gSystem->ExpandPathName(path);
97 sprintf(pathname, "%s/%s", systemPath, nameC);
101 // Delete ".C" from filename
102 Char_t name[fkMaxMacroNameLength];
103 memset(name, '\0', sizeof(Char_t) * fkMaxMacroNameLength);
105 for (UInt_t ind = 0; ind < fkMaxMacroNameLength && ind < strlen(nameC) - 2; ind++) name[ind] = nameC[ind];
107 // Check, if files exists
110 fp = fopen(pathname, "rb");
118 if (entryName != 0) delete entryName;
121 return NOT_EXIST_ERROR;
124 // Clean up root, load the desired macro and then check the type of the macro
128 if (forceReload) gROOT->ProcessLineSync(Form(".L %s++", pathname));
129 else gROOT->ProcessLineSync(Form(".L %s+", pathname));
131 // Selection macro or process macro of type 2 (histo)?
132 TFunction* f = gROOT->GetGlobalFunctionWithPrototype(name, "const AliTRDtrackV1*", kTRUE);
135 // Some additional check (is the parameter EXACTLY of the desired type?)
136 if (strstr(f->GetMangledName(), "oPconstsPAliTRDtrackV1mUsP") != 0x0)
139 if (!strcmp(f->GetReturnTypeName(), "Bool_t"))
141 hasCorrectSignature = kTRUE;
142 isSelectionMacro = kTRUE;
143 isHistoMacro = kFALSE;
145 // Process macro of type 2 (histo)?
146 else if (!strcmp(f->GetReturnTypeName(), "TH1*"))
148 hasCorrectSignature = kTRUE;
149 isSelectionMacro = kFALSE;
150 isHistoMacro = kTRUE;
154 // Process macro of type 1?
155 else if ((f = gROOT->GetGlobalFunctionWithPrototype(name, "const AliTRDtrackV1*, Double_t*&, Int_t&", kTRUE)) != 0x0)
157 if (!strcmp(f->GetReturnTypeName(), "void"))
159 // Some additional check (are the parameters EXACTLY of the desired type?)
160 if (strstr(f->GetMangledName(), "oPconstsPAliTRDtrackV1mUsP") != 0x0 &&
161 strstr(f->GetMangledName(), "cODouble_tmUaNsP") != 0x0 &&
162 strstr(f->GetMangledName(), "cOInt_taNsP") != 0x0)
164 hasCorrectSignature = kTRUE;
165 isSelectionMacro = kFALSE;
166 isHistoMacro = kFALSE;
171 //// Clean up again / unload this function
172 //gROOT->ProcessLineSync(Form(".U %s", pathname));
177 // Has not the correct signature!
178 if (!hasCorrectSignature)
180 if (entryName != 0) delete entryName;
182 return SIGNATURE_ERROR;
185 Int_t returnValue = WARNING;
187 // Only add macro, if it is not already in the list
188 if (!isSelectionMacro && fMacroList->FindObject(entryName) == 0)
190 fMacroList->Add(new TObjString(entryName));
193 // We do not know, where the element has been inserted - deselect this list
194 fMacroListSelected = 0;
196 returnValue = SUCCESS;
198 else if (isSelectionMacro && fMacroSelList->FindObject(entryName) == 0)
200 fMacroSelList->Add(new TObjString(entryName));
201 fMacroSelList->Sort();
203 // We do not know, where the element has been inserted - deselect this list
204 fMacroSelListSelected = 0;
206 returnValue = SUCCESS;
208 else returnValue = WARNING;
210 if (entryName != 0) delete entryName;
216 //______________________________________________________
217 void AliEveTRDTrackList::AddMacroFast(const Char_t* entry, Bool_t toSelectionList)
221 fMacroSelList->Add(new TObjString(entry));
222 fMacroSelList->Sort();
224 // We do not know, where the element has been inserted - deselect this list
225 fMacroSelListSelected = 0;
229 fMacroList->Add(new TObjString(entry));
232 // We do not know, where the element has been inserted - deselect this list
233 fMacroListSelected = 0;
237 //______________________________________________________
238 void AliEveTRDTrackList::AddMacroFast(const Char_t* path, const Char_t* name, Bool_t toSelectionList)
240 Char_t* entry = MakeMacroEntry(path, name);
243 AddMacroFast(entry, toSelectionList);
245 #ifdef ALIEVETRDTRACKLIST_DEBUG
246 // Successfull add will only be displayed in debug mode
247 printf("#AliEveTRDTrackList::AddMacroFast: Added macro \"%s/%s\" to %s list\n", path, name,
248 (toSelectionList ? "selection" : "process"));
256 // Error will always be displayed
257 printf("#AliEveTRDTrackList::AddMacroFast: ERROR: Could not add macro \"%s/%s\" to %s list\n", path, name,
258 (toSelectionList ? "selection" : "process"));
262 //______________________________________________________
263 void AliEveTRDTrackList::AddStandardMacros()
265 // Add your standard macros here, e.g.:
266 // To add a macro without any checks (very fast, but unsafe):
267 // AddMacroFast("$(ALICE_ROOT)/myFolder", "myMacroName.C", isSelMacro);
268 // To add a macro with checks (slower, but safe):
269 // AddMacro("$(ALICE_ROOT)/myFolder", "myMacroName.C");
270 // -> If the file does not exist, nothing happens. So if you want to handle this,
271 // use the return value of AddMacro (NOT_EXIST_ERROR is returned, if file does not exist)
272 // (-> You can also check for other return values (see AddMacro(...)))
273 AddMacro("$(ALICE_ROOT)/TRD/qaRec/macros", "clusterSelection.C");
274 AddMacro("$(ALICE_ROOT)/TRD/qaRec/macros", "chargeDistr.C");
275 AddMacro("$(ALICE_ROOT)/TRD/qaRec/macros", "clusterResiduals.C");
276 AddMacro("$(ALICE_ROOT)/TRD/qaRec/macros", "PH.C");
279 //______________________________________________________
280 void AliEveTRDTrackList::ApplyProcessMacros(TList* iterator)
282 if (iterator->GetEntries() <= 0) return;
284 Char_t name[fkMaxMacroNameLength];
285 Char_t** cmds = new Char_t*[iterator->GetEntries()];
286 Bool_t* isHistoMacro = new Bool_t[iterator->GetEntries()];
288 Int_t numHistoMacros = 0;
291 AliEveTRDTrack* track = 0;
292 AliTRDtrackV1 *trackv1 = 0;
293 TH1* returnedHist = 0x0;
298 // Clear old data and re-allocate
299 if (fDataFromMacroList != 0) delete fDataFromMacroList;
300 fDataFromMacroList = new TList();
302 fHistoDataSelected = 0;
304 if (fDataTree == 0) fDataTree = new TTreeSRedirector("TRD.TrackListMacroData.root");
306 // Collect the commands for each macro and add them to "data-from-list"
307 for (Int_t i = 0; i < iterator->GetEntries(); i++)
309 memset(name, '\0', sizeof(Char_t) * fkMaxMacroNameLength);
311 cmds[i] = new Char_t[(fkMaxMacroPathNameLength + fkMaxApplyCommandLength)];
312 memset(cmds[i], '\0', sizeof(Char_t) * (fkMaxMacroNameLength + fkMaxApplyCommandLength));
314 #ifdef ALIEVETRDTRACKLIST_DEBUG
315 printf("AliEveTRDTrackList: Applying process macro: %s\n", iterator->At(i)->GetTitle());
319 sscanf(iterator->At(i)->GetTitle(), "%s (Path: %*s)", name);
321 // Delete ".C" at the end
322 // -> Note: Physical address pointer, do NOT delete. / Changes "name" as well!
323 Char_t* dotC = (Char_t*)strrchr(name, '.');
331 // Add to "data-from-list"
332 fDataFromMacroList->Add(new TObjString(name));
334 // Find the type of the process macro
335 if (!IsHistogramMacro(name))
338 isHistoMacro[i] = kFALSE;
339 // Create the command
340 sprintf(cmds[i], "%s(automaticTrackV1, results, n);", name);
345 isHistoMacro[i] = kTRUE;
347 // Create the command
348 sprintf(cmds[i], "%s(automaticTrackV1);", name);
352 // Allocate memory for the histograms
353 if (numHistoMacros > 0) histos = new TH1*[numHistoMacros];
354 for (Int_t i = 0; i < numHistoMacros; i++) histos[i] = 0;
356 // Walk through the list of tracks
357 for (TEveElement::List_i iter = this->BeginChildren(); iter != this->EndChildren(); ++iter)
359 track = dynamic_cast<AliEveTRDTrack*>(*iter);
361 if (!track) continue;
363 // Skip tracks that have not been selected
364 if (!track->GetRnrState()) continue;
366 trackv1 = (AliTRDtrackV1*)track->GetUserData();
368 track->ExportToCINT((Text_t*)"automaticTrack");
369 // Cast to AliTRDtrackV1
370 gROOT->ProcessLineSync("AliTRDtrackV1* automaticTrackV1 = (AliTRDtrackV1*)automaticTrack->GetUserData();");
372 // Collect data for each macro
373 for (Int_t i = 0, histoIndex = 0; i < iterator->GetEntries(); i++)
375 // Process for macro type 2 (histo)
378 returnedHist = (TH1*)gROOT->ProcessLineSync(cmds[i]);
379 if (returnedHist != 0x0)
381 if (histos[histoIndex] == 0) histos[histoIndex] = (TH1*)gROOT->ProcessLineSync(cmds[i]);
382 else histos[histoIndex]->Add((const TH1*)gROOT->ProcessLineSync(cmds[i]));
389 // Process for macro type 1
392 // Create data pointers in CINT, execute the macro and get the data
393 gROOT->ProcessLineSync("Double_t* results = 0;");
394 gROOT->ProcessLineSync("Int_t n = 0;");
395 gROOT->ProcessLineSync(cmds[i]);
396 Double_t* results = (Double_t*)gROOT->ProcessLineSync("results;");
397 Int_t nResults = (Int_t)gROOT->ProcessLineSync("n;");
401 Error("Apply macros", Form("Error reading data from macro \"%s\"", iterator->At(i)->GetTitle()));
404 for (Int_t resInd = 0; resInd < nResults; resInd++)
406 (*fDataTree) << Form("TrackData%d", i) << Form("Macro%d=", i) << results[resInd] << (Char_t*)"\n";
415 for (Int_t i = 0, histoIndex = 0; i < iterator->GetEntries() && histoIndex < numHistoMacros; i++)
419 // Might be empty (e.g. no tracks have been selected)!
420 if (histos[histoIndex] != 0)
422 (*fDataTree) << Form("TrackData%d", i) << Form("Macro%d=", i) << histos[histoIndex] << (Char_t*)"\n";
428 if (fDataTree != 0) delete fDataTree;
431 if (cmds != 0) delete [] cmds;
432 if (isHistoMacro != 0) delete isHistoMacro;
435 if (histos != 0) delete [] histos;
441 // If there is data, select the first data set
442 if (iterator->GetEntries() > 0) SETBIT(fHistoDataSelected, 0);
444 // Now the data is stored in "TRD.TrackListMacroData.root"
445 // The editor will access this file to display the data
448 //______________________________________________________
449 void AliEveTRDTrackList::ApplySelectionMacros(TList* iterator)
451 Char_t name[fkMaxMacroNameLength];
452 Char_t cmd[(fkMaxMacroNameLength + fkMaxApplyCommandLength)];
454 AliEveTRDTrack* track = 0;
455 AliTRDtrackV1 *trackv1 = 0;
456 Bool_t selectedByMacro = kFALSE;
461 // Select all tracks at first. A track is then deselect, if at least one selection macro
462 // returns kFALSE for this track
463 // Enable all tracks (Note: EnableListElements(..) will call "ElementChanged", which will cause unforseen behaviour!)
464 for (TEveElement::List_i iter = this->BeginChildren(); iter != this->EndChildren(); ++iter)
466 ((TEveElement*)(*iter))->SetRnrState(kTRUE);
470 for (Int_t i = 0; i < iterator->GetEntries(); i++)
473 memset(name, '\0', sizeof(Char_t) * fkMaxMacroNameLength);
474 memset(cmd, '\0', sizeof(Char_t) * (fkMaxMacroNameLength + fkMaxApplyCommandLength));
476 #ifdef ALIEVETRDTRACKLIST_DEBUG
477 printf("AliEveTRDTrackList: Applying selection macro: %s\n", iterator->At(i)->GetTitle());
481 sscanf(iterator->At(i)->GetTitle(), "%s (Path: %*s)", name);
482 // Delete ".C" at the end
483 // -> Note: Physical address pointer, do NOT delete. / Changes "name" as well!
484 Char_t* dotC = (Char_t*)strrchr(name, '.');
492 // Create the command
493 sprintf(cmd, "%s(automaticTrackV1);", name);
495 // Walk through the list of tracks
496 for (TEveElement::List_i iter = this->BeginChildren(); iter != this->EndChildren(); ++iter)
498 track = dynamic_cast<AliEveTRDTrack*>(*iter);
500 if (!track) continue;
502 trackv1 = (AliTRDtrackV1*)track->GetUserData();
504 track->ExportToCINT((Text_t*)"automaticTrack");
505 // Cast to AliTRDtrackV1
506 gROOT->ProcessLineSync("AliTRDtrackV1* automaticTrackV1 = (AliTRDtrackV1*)automaticTrack->GetUserData();");
507 selectedByMacro = (Bool_t)gROOT->ProcessLineSync(cmd);
508 track->SetRnrState(selectedByMacro && track->GetRnrState());
516 //______________________________________________________
517 Char_t* AliEveTRDTrackList::MakeMacroEntry(const Char_t* path, const Char_t* name)
519 Char_t* entry = new Char_t[(fkMaxMacroPathNameLength + 30)];
520 memset(entry, '\0', sizeof(Char_t) * (fkMaxMacroPathNameLength + 30));
522 Char_t* systemPath = gSystem->ExpandPathName(path);
523 sprintf(entry, "%s (Path: %s)", name, systemPath);
530 //______________________________________________________
531 Bool_t AliEveTRDTrackList::IsHistogramMacro(const Char_t* name)
534 if ((f = gROOT->GetGlobalFunctionWithPrototype(name, "const AliTRDtrackV1*", kTRUE)) != 0x0)
535 if (strcmp(f->GetReturnTypeName(), "TH1*") == 0) return kTRUE;
540 //______________________________________________________
541 void AliEveTRDTrackList::RemoveProcessMacros(TList* iterator)
544 for (Int_t i = 0; i < iterator->GetEntries(); i++)
546 obj = (TObjString*)fMacroList->Remove(fMacroList->FindObject(iterator->At(i)->GetTitle()));
548 if (obj != 0) delete obj;
553 //______________________________________________________
554 void AliEveTRDTrackList::RemoveSelectionMacros(TList* iterator)
557 for (Int_t i = 0; i < iterator->GetEntries(); i++)
559 obj = (TObjString*)fMacroSelList->Remove(fMacroSelList->FindObject(iterator->At(i)->GetTitle()));
560 if (obj != 0) delete obj;