8550bce465bbf3eeccdaf19c5910555081bc801f
[u/mrichter/AliRoot.git] / EVE / EveBase / AliEveTrackCounterEditor.cxx
1 // @(#)root/eve:$Id$
2 // Author: Matevz Tadel 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
10 #include "AliEveTrackCounterEditor.h"
11 #include "AliEveTrackCounter.h"
12 #include "AliEveEventManager.h"
13
14 #include "TGedEditor.h"
15 #include "TVirtualPad.h"
16 #include "TColor.h"
17
18 // Cleanup these includes:
19 #include "TGLabel.h"
20 #include "TGNumberEntry.h"
21 #include "TGComboBox.h"
22 #include "TGMsgBox.h"
23
24 #include "TH1F.h"
25
26 #include "TCanvas.h"
27 #include "TEveManager.h"
28
29 #include "TROOT.h"
30 #include "TSystem.h" // File input/output for track-count status.
31
32 //______________________________________________________________________________
33 // GUI editor for AliEveTrackCounter.
34 //
35
36 ClassImp(AliEveTrackCounterEditor)
37
38 //______________________________________________________________________________
39 AliEveTrackCounterEditor::AliEveTrackCounterEditor(const TGWindow *p, Int_t width, Int_t height,
40                                                UInt_t options, Pixel_t back) :
41    TGedFrame(p, width, height, options | kVerticalFrame, back),
42    fM(0),
43    fClickAction(0),
44    fInfoLabelTracks   (0),
45    fInfoLabelTracklets(0),
46    fEventId(0)
47 {
48    // Constructor.
49
50    MakeTitle("AliEveTrackCounter");
51
52    Int_t labelW = 42;
53
54    { // ClickAction
55       TGHorizontalFrame* f = new TGHorizontalFrame(this);
56       TGLabel* lab = new TGLabel(f, "Click:");
57       f->AddFrame(lab, new TGLayoutHints(kLHintsLeft|kLHintsBottom, 1, 10, 1, 2));
58       fClickAction = new TGComboBox(f);
59       fClickAction->AddEntry("Print", 0);
60       fClickAction->AddEntry("Toggle", 1);
61       TGListBox* lb = fClickAction->GetListBox();
62       lb->Resize(lb->GetWidth(), 2*16);
63       fClickAction->Resize(70, 20);
64       fClickAction->Connect("Selected(Int_t)", "AliEveTrackCounterEditor", this,
65                             "DoClickAction(Int_t)");
66       f->AddFrame(fClickAction, new TGLayoutHints(kLHintsLeft, 1, 2, 1, 1));
67
68       AddFrame(f);
69    }
70
71    { // fInfoLabelTracks
72       TGHorizontalFrame* f = new TGHorizontalFrame(this);
73       TGLabel* lab = new TGLabel(f, "Tracks:");
74       f->AddFrame(lab, new TGLayoutHints(kLHintsLeft, 1, 5, 1, 2));
75
76       fInfoLabelTracks = new TGLabel(f);
77       f->AddFrame(fInfoLabelTracks, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 9, 1, 2));
78
79       AddFrame(f);
80    }
81    { // fInfoLabelTracklets
82       TGHorizontalFrame* f = new TGHorizontalFrame(this);
83       TGLabel* lab = new TGLabel(f, "Tracklets:");
84       f->AddFrame(lab, new TGLayoutHints(kLHintsLeft, 1, 5, 1, 2));
85
86       fInfoLabelTracklets = new TGLabel(f);
87       f->AddFrame(fInfoLabelTracklets, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 9, 1, 2));
88
89       AddFrame(f);
90    }
91
92    {
93       TGHorizontalFrame* f = new TGHorizontalFrame(this, 210, 20, kFixedWidth);
94
95       TGHorizontalFrame* g = new TGHorizontalFrame(f, labelW, 0, kFixedWidth);
96       TGLabel* l = new TGLabel(g, "Event:");
97       g->AddFrame(l, new TGLayoutHints(kLHintsLeft, 0,0,4,0));
98       f->AddFrame(g);
99
100       TGTextButton* b;
101
102       b = new TGTextButton(f, "Prev");
103       f->AddFrame(b, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 1, 0, 0));
104       b->Connect("Clicked()", "AliEveTrackCounterEditor", this, "DoPrev()");
105
106       fEventId = new TGNumberEntry(f, 0, 3, -1,TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative,
107                                    TGNumberFormat::kNELLimitMinMax, 0, 10000);
108       f->AddFrame(fEventId, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 1, 0, 0));
109       fEventId->Connect("ValueSet(Long_t)", "AliEveTrackCounterEditor", this, "DoSetEvent()");
110
111       b = new TGTextButton(f, "Next");
112       f->AddFrame(b, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 1, 0, 0));
113       b->Connect("Clicked()", "AliEveTrackCounterEditor", this, "DoNext()");
114
115       AddFrame(f);
116    }
117
118    {
119       TGHorizontalFrame* f = new TGHorizontalFrame(this, 210, 20, kFixedWidth);
120
121       TGHorizontalFrame* g = new TGHorizontalFrame(f, labelW, 0, kFixedWidth);
122       TGLabel* l = new TGLabel(g, "Report:");
123       g->AddFrame(l, new TGLayoutHints(kLHintsLeft, 0,0,4,0));
124       f->AddFrame(g);
125
126       TGTextButton* b;
127
128       b = new TGTextButton(f, "Print");
129       f->AddFrame(b, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 1, 0, 0));
130       b->Connect("Clicked()", "AliEveTrackCounterEditor", this, "DoPrintReport()");
131
132       b = new TGTextButton(f, "File");
133       f->AddFrame(b, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 1, 0, 0));
134       b->Connect("Clicked()", "AliEveTrackCounterEditor", this, "DoFileReport()");
135
136       AddFrame(f, new TGLayoutHints(kLHintsLeft, 0, 0, 4, 0));
137    }
138    {
139       TGHorizontalFrame* f = new TGHorizontalFrame(this, 210, 20, kFixedWidth);
140
141       TGHorizontalFrame* g = new TGHorizontalFrame(f, labelW, 0, kFixedWidth);
142       TGLabel* l = new TGLabel(g, "Histos:");
143       g->AddFrame(l, new TGLayoutHints(kLHintsLeft, 0,0,4,0));
144       f->AddFrame(g);
145
146       TGTextButton* b;
147
148       b = new TGTextButton(f, "Show");
149       f->AddFrame(b, new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 1, 1, 0, 0));
150       b->Connect("Clicked()", "AliEveTrackCounterEditor", this, "DoShowHistos()");
151
152       AddFrame(f, new TGLayoutHints(kLHintsLeft, 0, 0, 0, 0));
153    }
154
155   AliEveEventManager::GetMaster()->Connect("NewEventLoaded()",
156                         "AliEveTrackCounterEditor", this, "UpdateModel()");
157 }
158
159 AliEveTrackCounterEditor::~AliEveTrackCounterEditor()
160 {
161   // Destructor.
162
163   AliEveEventManager::GetMaster()->Disconnect("NewEventLoaded()", this);
164 }
165
166 /******************************************************************************/
167
168 void AliEveTrackCounterEditor::UpdateModel()
169 {
170   if (fGedEditor && fM && fGedEditor->GetModel() == fM->GetEditorObject())
171   {
172     SetModel(fM->GetEditorObject());
173   }
174 }
175
176 //______________________________________________________________________________
177 void AliEveTrackCounterEditor::SetModel(TObject* obj)
178 {
179    // Set model object.
180
181    fM = dynamic_cast<AliEveTrackCounter*>(obj);
182
183    fClickAction->Select(fM->fClickAction, kFALSE);
184    fInfoLabelTracks   ->SetText(Form("All: %3d; Primaries: %3d", fM->fAllTracks,    fM->fGoodTracks));
185    fInfoLabelTracklets->SetText(Form("All: %3d; Primaries: %3d", fM->fAllTracklets, fM->fGoodTracklets));
186    fEventId->SetNumber(fM->GetEventId());
187 }
188
189 /******************************************************************************/
190
191 //______________________________________________________________________________
192 void AliEveTrackCounterEditor::DoPrev()
193 {
194    // Slot for Prev.
195
196    AliEveEventManager::GetMaster()->PrevEvent();
197 }
198
199 //______________________________________________________________________________
200 void AliEveTrackCounterEditor::DoNext()
201 {
202    // Slot for Next.
203
204    AliEveEventManager::GetMaster()->NextEvent();
205 }
206
207 //______________________________________________________________________________
208 void AliEveTrackCounterEditor::DoSetEvent()
209 {
210    // Slot for SetEvent.
211    AliEveEventManager::GetMaster()->GotoEvent((Int_t) fEventId->GetNumber());
212 }
213
214 /******************************************************************************/
215
216 //______________________________________________________________________________
217 void AliEveTrackCounterEditor::DoPrintReport()
218 {
219    // Slot for PrintReport.
220
221    fM->OutputEventTracks();
222 }
223
224 //______________________________________________________________________________
225 void AliEveTrackCounterEditor::DoFileReport()
226 {
227    // Slot for FileReport.
228
229    TString file(Form("ev-report-%03d.txt", fM->GetEventId()));
230    if (gSystem->AccessPathName(file) == kFALSE)
231    {
232       Int_t ret;
233       new TGMsgBox(fClient->GetRoot(), GetMainFrame(),
234                    "File Exist",
235                    Form("Event record for event %d already exist.\n Replace?", fM->GetEventId()),
236                    kMBIconQuestion, kMBYes | kMBNo, &ret);
237       if (ret == kMBNo)
238          return;
239    }
240    FILE* out = fopen(file, "w");
241    if (out) {
242       fM->OutputEventTracks(out);
243       fclose(out);
244    } else {
245       Error("AliEveTrackCounterEditor::DoFileReport",
246             "Can not open file '%s' for writing.", file.Data());
247    }
248 }
249
250 //______________________________________________________________________________
251 void AliEveTrackCounterEditor::DoShowHistos()
252 {
253   // Slot for ShowHistos.
254
255   TH1F* hcnt = new TH1F("cnt", "Primeries per event", 41, -0.5, 40.5);
256   TH1F* hchg = new TH1F("chg", "Primary charge",       3, -1.5,  1.5);
257   TH1F* hpt  = new TH1F("pt",  "pT distribution",     40,  0.0,  8.0);
258   TH1F* heta = new TH1F("eta", "eta distribution",    40, -1.0,  1.0);
259   TH1F* hphi = new TH1F("phi", "phi distribution",    40, -3.1416, 3.1416);
260
261   Int_t nn; // fscanf return value
262
263   for (Int_t i=0; i<1000; ++i)
264   {
265     TString file(Form("ev-report-%03d.txt", i));
266     if (gSystem->AccessPathName(file) == kFALSE)
267     {
268       Int_t   ev, ngoodtr, nalltr;
269       FILE* f = fopen(file, "read");
270
271       nn = fscanf(f, "Event=%d\n", &ev);
272       if (nn != 1) { printf("SAFR0 %d\n", nn); fclose(f); return;  }
273
274       nn = fscanf(f, "GoodTracks=%d  AllTracks=%d\n", &ngoodtr, &nalltr);
275       if (nn != 2) { printf("SAFR1 %d\n", nn); fclose(f); return;  }
276
277       hcnt->Fill(ngoodtr);
278       for (Int_t t = 0; t < ngoodtr; ++t)
279       {
280         Int_t   id, chg;
281         Float_t pt, eta, phi;
282         nn = fscanf(f, "%d: chg=%d pt=%f eta=%f phi=%f\n", &id, &chg, &pt, &eta, &phi);
283         if (nn != 5) { printf("SAFR2 %d\n", nn); fclose(f); return;  }
284         hchg->Fill(chg);
285         hpt ->Fill(pt);
286         heta->Fill(eta);
287         hphi->Fill(phi);
288       }
289       fclose(f);
290     }
291   }
292
293   TCanvas* c;
294   if (gPad == 0 || gPad->GetCanvas()->IsEditable() == kFALSE) {
295     c = new TCanvas("Scanwas", "Scanning Results", 800, 600);
296   } else {
297     c = gPad->GetCanvas();
298     c->Clear();
299   }
300   c->Divide(2, 3);
301
302   c->cd(1); hcnt->Draw();
303   c->cd(2); hchg->Draw();
304   c->cd(3); hpt ->Draw();
305   c->cd(4); heta->Draw();
306   c->cd(5); hphi->Draw();
307
308   c->Modified();
309   c->Update();
310 }
311
312 /******************************************************************************/
313
314 //______________________________________________________________________________
315 void AliEveTrackCounterEditor::DoClickAction(Int_t mode)
316 {
317    // Slot for ClickAction.
318
319    fM->SetClickAction(mode);
320 }