Move contents of EVE/Alieve to EVE/EveDet as most code will remain there.
[u/mrichter/AliRoot.git] / EVE / EveDet / AliEveTRDLoader.cxx
1 // $Id$
2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
4 /**************************************************************************
5  * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6  * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for          *
7  * full copyright notice.                                                 *
8  **************************************************************************/
9 #include "AliEveTRDLoader.h"
10 #include "AliEveTRDModuleImp.h"
11
12 #include <TEveManager.h>
13 #include <TEveGValuators.h>
14
15 #include "TSystem.h"
16 #include "TFile.h"
17 #include "TTree.h"
18 #include "TString.h"
19 #include "TObjString.h"
20 #include "TObjArray.h"
21
22 #include <TGLabel.h>
23 #include <TGButton.h>
24 #include <TGTextEntry.h>
25 #include <TGNumberEntry.h>
26 #include <TGFileDialog.h>
27 #include <TGListTree.h>
28 #include <TGToolTip.h>
29
30 #include "AliLog.h"
31 #include "AliCDBManager.h"
32
33 #include "AliTRDv1.h"
34 #include "AliTRDhit.h"
35 #include "AliTRDcluster.h"
36 #include "AliTRDmcmTracklet.h"
37 #include "AliTRDdigitsManager.h"
38 #include "AliTRDgeometry.h"
39
40
41 #include <algorithm>
42
43 using namespace std;
44 class AliTRDdataArrayI;
45
46 ClassImp(AliEveTRDLoader)
47 ClassImp(AliEveTRDLoaderEditor)
48
49 ///////////////////////////////////////////////////////////
50 /////////////         AliEveTRDLoader       /////////////////////
51 ///////////////////////////////////////////////////////////
52
53
54 //________________________________________________________
55 AliEveTRDLoader::AliEveTRDLoader(const Text_t* n, const Text_t* t) : TEveElementList(n, t), fSM(-1), fStack(-1), fLy(-1), fEvent(0)
56 {
57         kLoadHits = kFALSE;
58         kLoadDigits = kFALSE;
59         kLoadClusters = kFALSE;
60         kLoadTracks = kFALSE;
61         fFilename = "";
62         fDir = ".";
63         fEvent  = -1;
64
65         fTRD           = 0x0;
66         fGeo = new AliTRDgeometry();
67
68         AliCDBManager *fCDBManager=AliCDBManager::Instance();
69         fCDBManager->SetDefaultStorage("local://$ALICE_ROOT");
70         fCDBManager->SetRun(0);
71 }
72
73 //________________________________________________________
74 AliEveTRDLoader::~AliEveTRDLoader()
75 {
76 //      if(fChambers) {fChambers->clear(); delete fChambers;}
77 }
78
79 //________________________________________________________
80 template<class T>
81 class ID
82 {
83 public:
84         ID( int value ) : id(value) {}
85         bool operator()(const T &t) const {
86                 return ((dynamic_cast<AliEveTRDModule*>(t))->GetID() == id);
87         }
88 private:
89         const int id;
90 };
91 void    AliEveTRDLoader::AddChambers(int sm, int stk, int ly)
92 {
93         Int_t ism_start = (sm == -1) ?  0 : sm;
94         Int_t ism_stop  = (sm == -1) ? 18 : sm+1;
95         Int_t istk_start= (stk == -1)?  0 : stk;
96         Int_t istk_stop = (stk == -1)?  5 : stk+1;
97         Int_t ily_start = (ly == -1) ?  0 : ly;
98         Int_t ily_stop  = (ly == -1) ?  6 : ly+1;
99
100         List_i ichmb;
101         ichmb = fChildren.begin();
102         while(ichmb != fChildren.end()){
103                 (*ichmb)->SetRnrSelf(kFALSE);
104                 ichmb++;
105         }
106
107         AliEveTRDNode *SM=0x0, *STK=0x0;
108         AliEveTRDChamber *CHMB = 0x0;
109         int det;
110         for(int ism=ism_start; ism<ism_stop; ism++){
111                 ichmb = find_if(fChildren.begin(), fChildren.end(), ID<TEveElement*>(ism));
112                 if(ichmb != fChildren.end()){
113                         SM = (AliEveTRDNode*)(*ichmb);
114                         SM->SetRnrSelf(kTRUE);
115                 }else{
116                   gEve->AddElement(SM = new AliEveTRDNode("SM", ism), this);
117                         SM->FindListTreeItem(gEve->GetListTree())->SetTipText(Form("Supermodule %2d", ism));
118                 }
119                 for(int istk=istk_start; istk<istk_stop; istk++){
120                         ichmb = find_if(SM->begin(), SM->end(), ID<TEveElement*>(istk));
121                         if(ichmb != SM->end()){
122                                 STK = (AliEveTRDNode*)(*ichmb);
123                                 STK->SetRnrSelf(kTRUE);
124                         }else{
125                           gEve->AddElement(STK = new AliEveTRDNode("Stack", istk), SM);
126                                 STK->FindListTreeItem(gEve->GetListTree())->SetTipText(Form("SM %2d Stack %1d", ism, istk));
127                         }
128                         for(int ily=ily_start; ily<ily_stop; ily++){
129                                 det = fGeo->GetDetector(ily, istk, ism);
130                                 ichmb = find_if(STK->begin(), STK->end(), ID<TEveElement*>(det));
131                                 if(ichmb != STK->end()) (*ichmb)->SetRnrSelf(kTRUE);
132                                 else{
133                                   gEve->AddElement(CHMB = new AliEveTRDChamber(det), STK);
134                                         CHMB->SetGeometry(fGeo);
135                                         CHMB->FindListTreeItem(gEve->GetListTree())->SetTipText(Form("SM %2d Stack %1d Layer %1d", ism, istk, ily));
136                                 }
137                         }
138                 }
139         }
140         gEve->Redraw3D();
141 }
142
143 //________________________________________________________
144 AliEveTRDChamber*       AliEveTRDLoader::GetChamber(int d)
145 {
146         List_i ism, istack, ichmb;
147
148         ism = find_if(fChildren.begin(), fChildren.end(), ID<TEveElement*>(fGeo->GetSector(d)));
149         if(ism == fChildren.end()) return 0x0;
150         istack = find_if(((AliEveTRDNode*)(*ism))->begin(), ((AliEveTRDNode*)(*ism))->end(), ID<TEveElement*>(fGeo->GetChamber(d)));
151         if(istack == ((AliEveTRDNode*)(*ism))->end()) return 0x0;
152         ichmb = find_if(((AliEveTRDNode*)(*istack))->begin(), ((AliEveTRDNode*)(*istack))->end(), ID<TEveElement*>(d));
153         if(ichmb == ((AliEveTRDNode*)(*istack))->end()) return 0x0;
154         return dynamic_cast<AliEveTRDChamber*>(*ichmb);
155 }
156
157 //________________________________________________________
158 Bool_t  AliEveTRDLoader::GoToEvent(int ev)
159 {
160         if(!fChildren.size()){
161                 AliWarning("Please select first the chamber that you want to monitor from \"Chamber(s) selector\".");
162                 return kFALSE;
163         }
164
165         fEvent = ev;
166
167         Unload();
168
169         TTree *t = 0x0;
170         TFile *f = new TFile(Form("%s/%s", fDir.Data(), fFilename.Data()));
171         if(! f->cd(Form("AliEveEventManager%d", ev))){
172                 AliError(Form("Couldn't find event %d in file \"%s/%s\".", ev, fDir.Data(), fFilename.Data()));
173                 f->Close(); delete f;
174                 return kFALSE;
175         }
176
177         if(kLoadDigits){
178                 t = (TTree*)gDirectory->Get("TreeD");
179                 if(!t) return kFALSE;
180                 if(!LoadDigits(t)) return kFALSE;
181         } else if(kLoadClusters){
182                 t = (TTree*)gDirectory->Get("TreeR");
183                 if(!t) return kFALSE;
184                 if(!LoadClusters(t)) return kFALSE;
185         } else if(kLoadTracks){
186                 t = (TTree*)gDirectory->Get("TreeT");
187                 if(!t) return kFALSE;
188                 if(!LoadTracklets(t)) return kFALSE;
189         } else AliWarning("Please select first the type of data that you want to monitor and then hit the \"Load\" button.");
190
191         f->Close(); delete f;
192
193         gEve->Redraw3D();
194
195         return kTRUE;
196 }
197
198
199 //________________________________________________________
200 Bool_t  AliEveTRDLoader::LoadClusters(TTree *tC)
201 {
202         AliInfo("Loading ...");
203         if(!fChildren.size()) return kTRUE;
204
205         TObjArray *clusters = new TObjArray();
206         tC->SetBranchAddress("TRDcluster", &clusters);
207
208         AliEveTRDChamber *chmb = 0x0;
209         AliTRDcluster *c=0x0;
210         for(int idet=0; idet<540; idet++){
211                 tC->GetEntry(idet);
212                 if(!clusters->GetEntriesFast()) continue;
213                 c = (AliTRDcluster*)clusters->UncheckedAt(0);
214                 if(!c) continue;
215                 if((chmb = GetChamber(c->GetDetector()))) chmb->LoadClusters(clusters);
216         }
217         return kTRUE;
218 }
219
220
221 //________________________________________________________
222 Bool_t  AliEveTRDLoader::LoadDigits(TTree *tD)
223 {
224         AliInfo("Loading ...");
225
226         if(!fChildren.size()) return kTRUE;
227
228         AliEveTRDChamber *chmb;
229         AliTRDdigitsManager dm;
230         dm.ReadDigits(tD);
231         for(int idet=0; idet<540; idet++){
232                 if(!(chmb=GetChamber(idet))) continue;
233 //              digits = dm.GetDigits(idet);
234 //              if(!digits) continue;
235 //              chmb->LoadDigits(digits);
236                 chmb->LoadDigits(&dm);
237         }
238         return kTRUE;
239 }
240
241
242 //________________________________________________________
243 Bool_t  AliEveTRDLoader::LoadTracklets(TTree *tT)
244 {
245         AliInfo("Loading ...");
246         if(!fChildren.size()) return kTRUE;
247
248         TObjArray *tracks = new TObjArray();
249         tT->SetBranchAddress("TRDmcmTracklet",&tracks);
250
251         AliEveTRDChamber *chmb = 0x0;
252         AliTRDmcmTracklet *trk=0x0;
253         for(int idet=0; idet<540; idet++){
254                 if(!tT->GetEntry(idet)) continue;
255                 if(tracks->GetEntriesFast()) trk = (AliTRDmcmTracklet*)tracks->UncheckedAt(0);
256                 if((chmb = GetChamber(trk->GetDetector()))) chmb->LoadTracklets(tracks);
257         }
258
259         return kTRUE;
260 }
261
262
263 //________________________________________________________
264 Bool_t  AliEveTRDLoader::Open(const char *filename, const char *dir)
265 {
266         fFilename = filename;
267         fDir = dir;
268         Int_t count = 0;
269         count += kLoadDigits ? 1 : 0;
270         count += kLoadClusters ? 1 : 0;
271         count += kLoadTracks ? 1 : 0;
272
273         TObjArray *so = fFilename.Tokenize(".");
274
275         if(((TObjString*)(*so)[0])->GetString().CompareTo("TRD") != 0){
276                 if(!count){
277                         AliWarning("Filename didn't fulfill naming conventions. No TRD data will be loaded.");
278                         return kFALSE;
279                 } else {
280                         Warning("Open()", "Filename didn't fulfill naming conventions.");
281                         return kTRUE;
282                 }
283         }
284         if(((TObjString*)(*so)[1])->GetString().CompareTo("Digits") == 0){
285                 if(!kLoadDigits) AliWarning("Data type set to DIGITS according to file name. Previous settings with SetDataType() will be discarded.");
286                 kLoadDigits = kTRUE;
287         } else if(((TObjString*)(*so)[1])->GetString().CompareTo("RecPoints") == 0){
288                 if(!kLoadClusters) AliWarning("Data type set to CLUSTERS according to file name. Previous settings with SetDataType() will be discarded.");
289                 kLoadClusters = kTRUE;
290         } else if(((TObjString*)(*so)[1])->GetString().CompareTo("Tracks") == 0){
291                 if(!kLoadTracks) AliWarning("Data type set to TRACKLETS according to file name. Previous settings with SetDataType() will be discarded.");
292                 kLoadTracks = kTRUE;
293         } else if(count){
294                 AliWarning("Filename didn't fulfill naming conventions.");
295                 return kTRUE;
296         } else {
297                 AliError("Filename didn't fulfill naming conventions. No data will be loaded.");
298                 return kFALSE;
299         }
300
301         return kTRUE;
302 }
303
304
305
306 //________________________________________________________
307 void AliEveTRDLoader::Paint(Option_t *option)
308 {
309         List_i ichmb = fChildren.begin();
310         while(ichmb != fChildren.end()){
311                 (dynamic_cast<AliEveTRDModule*>(*ichmb))->Paint(option);
312                 ichmb++;
313         }
314 }
315
316 //________________________________________________________
317 void    AliEveTRDLoader::SetDataType(TRDDataTypes type)
318 {
319         kLoadHits     = kFALSE;
320         kLoadDigits   = kFALSE;
321         kLoadClusters = kFALSE;
322         kLoadTracks   = kFALSE;
323         switch(type){
324         case kHits: kLoadHits = kTRUE; break;
325         case kDigits: kLoadDigits = kTRUE; break;
326         case kClusters: kLoadClusters = kTRUE; break;
327         case kTracks: kLoadTracks = kTRUE; break;
328         case kRawRoot: break;
329         case kRawData: break;
330         }
331 }
332
333 //________________________________________________________
334 void AliEveTRDLoader::Unload()
335 {
336         List_i ichmb = fChildren.begin();
337         while(ichmb != fChildren.end()){
338                 (dynamic_cast<AliEveTRDModule*>(*ichmb))->Reset();
339                 ichmb++;
340         }
341 }
342
343 ///////////////////////////////////////////////////////////
344 /////////////   AliEveTRDLoaderEditor       /////////////////////
345 ///////////////////////////////////////////////////////////
346
347 //________________________________________________________
348 AliEveTRDLoaderEditor::AliEveTRDLoaderEditor(const TGWindow* p, Int_t width, Int_t height, UInt_t options, Pixel_t back) : TGedFrame(p, width, height, options | kVerticalFrame, back)
349 {
350         MakeTitle("AliEveTRDLoader");
351
352   fFile = 0x0;
353         TGTextButton *fOpenFile = 0x0;
354         Int_t labelW = 42;
355
356         TGHorizontalFrame* f = new TGHorizontalFrame(this);
357         TGHorizontalFrame* g = new TGHorizontalFrame(f, labelW, 0, kFixedWidth);
358         TGLabel* l = new TGLabel(g, "File: ");
359         g->AddFrame(l, new TGLayoutHints(kLHintsLeft, 0,0,4,0));
360         f->AddFrame(g);
361         fFile = new TGTextEntry(f);
362         fFile->SetToolTipText("Select TRD data file or galice.root");
363         fFile->SetWidth(140);
364         fFile->Connect("DoubleClicked()", "AliEveTRDLoaderEditor", this, "FileOpen()");
365         f->AddFrame(fFile);
366
367         fOpenFile = new TGTextButton(f, "Browse");
368         f->AddFrame(fOpenFile);
369         fOpenFile->Connect("Clicked()", "AliEveTRDLoaderEditor", this, "FileOpen()");
370         AddFrame(f);
371
372
373   fEvent = new TEveGValuator(this, "AliEveEventManager:", 110, 0);
374   fEvent->SetShowSlider(kFALSE);
375   fEvent->SetLabelWidth(labelW);
376   fEvent->SetNELength(6);
377   fEvent->Build();
378   fEvent->SetLimits(-1, 1000);
379   fEvent->SetToolTip("Set event number to be monitored");
380         fEvent->Connect("ValueSet(Double_t)",
381                   "AliEveTRDLoaderEditor", this, "SetEvent(Double_t)");
382         AddFrame(fEvent);
383
384
385         // "Chamber(s) selector" group frame
386         TGGroupFrame *fGroupFrame1974 = new TGGroupFrame(this,"Chamber(s) selector");
387         TGVerticalFrame *fVerticalFrame1974 = new TGVerticalFrame(fGroupFrame1974, 150, 50,kVerticalFrame);
388
389         fSMNumber = new TEveGValuator(fVerticalFrame1974, "SM:", 0, 0);
390   fSMNumber->SetShowSlider(kFALSE);
391   fSMNumber->SetLabelWidth(labelW);
392   fSMNumber->SetNELength(6);
393   fSMNumber->Build();
394   fSMNumber->SetLimits(-1, 17);
395   fSMNumber->SetToolTip("Supermodule id [-1 for all]");
396         fVerticalFrame1974->AddFrame(fSMNumber, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterX | kLHintsExpandY,2,2,2,2));
397
398         fStackNumber = new TEveGValuator(fVerticalFrame1974, "Stack:", 0, 0);
399   fStackNumber->SetShowSlider(kFALSE);
400   fStackNumber->SetLabelWidth(labelW);
401   fStackNumber->SetNELength(6);
402   fStackNumber->Build();
403   fStackNumber->SetLimits(-1, 4);
404   fStackNumber->SetToolTip("Stack id [-1 for all in this SM]");
405         fVerticalFrame1974->AddFrame(fStackNumber, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterX | kLHintsExpandY,2,2,2,2));
406
407         fPlaneNumber = new TEveGValuator(fVerticalFrame1974, "Plane:", 0, 0);
408   fPlaneNumber->SetShowSlider(kFALSE);
409   fPlaneNumber->SetLabelWidth(labelW);
410   fPlaneNumber->SetNELength(6);
411   fPlaneNumber->Build();
412   fPlaneNumber->SetLimits(-1, 5);
413   fPlaneNumber->SetToolTip("Plane id [-1 for all in this stack]");
414
415         fVerticalFrame1974->AddFrame(fPlaneNumber, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterX | kLHintsExpandY,2,2,2,2));
416
417         fGroupFrame1974->AddFrame(fVerticalFrame1974, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandY | kLHintsCenterX,2,2,2,2));
418
419         TGTextButton *fTextButton2037 = new TGTextButton(fGroupFrame1974,"Select");
420         fTextButton2037->SetTextJustify(36);
421         fGroupFrame1974->AddFrame(fTextButton2037, new TGLayoutHints(kLHintsExpandY | kLHintsCenterX,2,2,2,2));
422   fTextButton2037->SetToolTipText("Apply selection", 400);
423         fTextButton2037->Connect("Clicked()",
424                                         "AliEveTRDLoaderEditor", this, "AddChambers()");
425
426         fGroupFrame1974->SetLayoutManager(new TGHorizontalLayout(fGroupFrame1974));
427         AddFrame(fGroupFrame1974, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterY | kLHintsExpandX,2,2,2,2));
428
429
430         TGTextButton *fTextButton2004 = new TGTextButton(this,"Load");
431         fTextButton2004->SetTextJustify(36);
432         fTextButton2004->Resize(164,22);
433         AddFrame(fTextButton2004, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterY | kLHintsExpandX,2,2,2,2));
434         fTextButton2004->SetToolTipText("Load data according to selection", 400);
435         fTextButton2004->Connect("Clicked()", "AliEveTRDLoaderEditor", this, "Load()");
436 }
437
438 //________________________________________________________
439 AliEveTRDLoaderEditor::~AliEveTRDLoaderEditor()
440 {}
441
442 //_________________________________________________________
443 void AliEveTRDLoaderEditor::SetModel(TObject* obj)
444 {
445
446         fM = dynamic_cast<AliEveTRDLoader*>(obj);
447
448         fFile->SetText(gSystem->BaseName(fM->fFilename.Data()));
449
450         Bool_t kFile = kTRUE;
451         if(fM->fFilename.CompareTo("") == 0) kFile = kFALSE;
452
453         fEvent->SetEnabled(kFile);
454         fEvent->GetEntry()->SetIntNumber(fM->fEvent);
455
456         fSMNumber->SetEnabled(kFile);
457         fSMNumber->GetEntry()->SetIntNumber(fM->fSM);
458
459
460         fStackNumber->SetEnabled(kFile);
461         fStackNumber->GetEntry()->SetIntNumber(fM->fStack);
462
463
464         fPlaneNumber->SetEnabled(kFile);
465         fPlaneNumber->GetEntry()->SetIntNumber(fM->fLy);
466 }
467
468 //________________________________________________________
469 void AliEveTRDLoaderEditor::AddChambers()
470 {
471         fM->fSM    = (int)fSMNumber->GetEntry()->GetNumber();
472         fM->fStack = (int)fStackNumber->GetEntry()->GetNumber();
473         fM->fLy    = (int)fPlaneNumber->GetEntry()->GetNumber();
474         fM->AddChambers(fM->fSM, fM->fStack, fM->fLy);
475 }
476
477 //________________________________________________________
478 void AliEveTRDLoaderEditor::FileOpen()
479 {
480   TGFileInfo fi;
481   fi.fIniDir    = StrDup(gSystem->DirName (fM->fFilename.Data()));
482   fi.fFilename  = StrDup(gSystem->BaseName(fM->fFilename.Data()));
483 //  fi.fFileTypes = tpcfiletypes;
484
485   new TGFileDialog(fClient->GetRoot(), gEve->GetMainWindow(), kFDOpen, &fi);
486   if (!fi.fFilename) return;
487
488   fFile->SetToolTipText(gSystem->DirName (fi.fFilename));
489   fFile->SetText       (gSystem->BaseName(fi.fFilename));
490
491         fM->Open(gSystem->BaseName(fi.fFilename), gSystem->DirName (fi.fFilename));
492
493         this->SetModel(fM);
494 }
495
496 void AliEveTRDLoaderEditor::Load()
497 {
498         fM->GoToEvent(fM->fEvent);
499 }