mchview related changes.
[u/mrichter/AliRoot.git] / MUON / AliMUONPainterPlotSelector.cxx
CommitLineData
0145e89a 1/**************************************************************************
2* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3* *
4* Author: The ALICE Off-line Project. *
5* Contributors are mentioned in the code where appropriate. *
6* *
7* Permission to use, copy, modify and distribute this software and its *
8* documentation strictly for non-commercial purposes is hereby granted *
9* without fee, provided that the above copyright notice appears in all *
10* copies and that both the copyright notice and this permission notice *
11* appear in the supporting documentation. The authors make no claims *
12* about the suitability of this software for any purpose. It is *
13* provided "as is" without express or implied warranty. *
14**************************************************************************/
15
16// $Id$
17
18/// \class AliMUONPainterPlotSelector
19///
20/// Widget to select which data to plot for painters
21///
22/// \author Laurent Aphecetche
23///
24/// See AliMUONPainterInterfaceHelper for an important implementation note
25/// about our use of TGButtonGroup
26///
27
28#include "AliMUONPainterPlotSelector.h"
29
30#include "AliMUONPainterGroup.h"
31#include "AliMUONPainterInterfaceHelper.h"
32#include "AliMUONPainterMatrix.h"
33#include "AliMUONPainterRegistry.h"
34#include "AliMUONVPainter.h"
35#include "AliMUONVTrackerData.h"
36#include "AliLog.h"
37#include <Riostream.h>
38#include <TGButton.h>
39#include <TGButtonGroup.h>
40#include <TObjArray.h>
41#include <TObjString.h>
42
43///\cond CLASSIMP
44ClassImp(AliMUONPainterPlotSelector)
45///\endcond
46
47const char* AliMUONPainterPlotSelector::fgkDefaultSourceName = "none";
48
49//_____________________________________________________________________________
50AliMUONPainterPlotSelector::AliMUONPainterPlotSelector(const TGWindow* window, UInt_t w, UInt_t h)
51: TGCompositeFrame(window,w,h,kHorizontalFrame),
52fTypes(0x0),
53fDataSourceNames(0x0),
54fDataSourceDimensions(0x0),
55fDimensionButtonMap(new TMap),
56fCurrentType(""),
57fCurrentData(0x0),
58fCurrentDimension(-1)
59{
60 /// ctor
61 fTypes = new TGButtonGroup(this,"Plot");
62
63 fDataSourceNames = new TGButtonGroup(this,"Sources");
64
65 AliMUONPainterRegistry* reg = AliMUONPainterRegistry::Instance();
66
67 reg->Connect("DataSourceWasRegistered(AliMUONVTrackerData*)",
68 "AliMUONPainterPlotSelector",
69 this,
70 "DataSourceWasRegistered(AliMUONVTrackerData*)");
71
72 reg->Connect("DataSourceWasUnregistered(AliMUONVTrackerData*)",
73 "AliMUONPainterPlotSelector",
74 this,
75 "DataSourceWasUnregistered(AliMUONVTrackerData*)");
76
77 AliMUONPainterInterfaceHelper::AddRadioButton(*fDataSourceNames,
78 fgkDefaultSourceName,
79 0x0,
80 kTRUE);
81
82 CreateDimensionButtons(fgkDefaultSourceName);
83
84 fDataSourceDimensions = new TGButtonGroup(this,0,3,5,0,"Dimensions");
85
86 for ( Int_t i = 0; i < reg->NumberOfDataSources(); ++i )
87 {
88 AliMUONVTrackerData* data = reg->DataSource(i);
89 DataSourceWasRegistered(data);
90 }
91
92 fDataSourceNames->Connect("Clicked(Int_t)","AliMUONPainterPlotSelector",
93 this,
94 "SourceButtonWasClicked(Int_t)");
95
96 AddFrame(fTypes);
97 AddFrame(fDataSourceNames);
98 AddFrame(fDataSourceDimensions);
99}
100
101//_____________________________________________________________________________
102AliMUONPainterPlotSelector::~AliMUONPainterPlotSelector()
103{
104 /// dtor
105}
106
107//_____________________________________________________________________________
108void
109AliMUONPainterPlotSelector::BackupDimensionButtons()
110{
111 /// Backup the dimension button group
112
113 TString name = fDataSourceDimensions->GetTitle();
114
115 AliDebug(1,Form("name %s",name.Data()));
116
117 if ( name != fgkDefaultSourceName )
118 {
119 TGButtonGroup* group = static_cast<TGButtonGroup*>(fDimensionButtonMap->GetValue(name));
120 if (!group)
121 {
122 AliError(Form("Did not find group %s",name.Data()));
123 }
124 else
125 {
126 AliMUONPainterInterfaceHelper::Copy(*fDataSourceDimensions,*group);
127 }
128
129 }
130
131 fDataSourceDimensions->Disconnect("Clicked(Int_t)",
132 this,
133 "DimensionButtonWasClicked(Int_t)");
134}
135
136
137//_____________________________________________________________________________
138void
139AliMUONPainterPlotSelector::CreateDimensionButtons(const char* dataSourceName)
140{
141 /// Create the dimension button group for a given data source
142
143 AliDebug(1,Form("Creating dimension buttons for dataSource %s",dataSourceName));
144
49419555 145 AliMUONVTrackerData* data = AliMUONPainterRegistry::Instance()->DataSource(dataSourceName);
0145e89a 146
147 TGButtonGroup* bg = new TGButtonGroup(this,0,3,5,0,dataSourceName);
148
149 if ( data )
150 {
151 for ( Int_t i = 0; i < data->NumberOfDimensions(); ++i )
152 {
153 AliMUONPainterInterfaceHelper::AddRadioButton(*bg,
154 data->DimensionName(i),
155 reinterpret_cast<void*>(i));
156 }
157 }
158
159 fDimensionButtonMap->Add(new TObjString(dataSourceName),bg);
160
161 AliDebug(1,Form("bg is %p Count=%d",bg,bg->GetCount()));
162 StdoutToAliDebug(1,AliMUONPainterInterfaceHelper::Dump(*bg));
163
164 bg->Connect("Clicked(Int_t)","AliMUONPainterPlotSelector",this,
165 "DimensionButtonWasClicked(Int_t)");
166}
167
168//_____________________________________________________________________________
169void
170AliMUONPainterPlotSelector::CreateTypeButtons(const TObjArray& types)
171{
172 /// Create the type button group
173
174 AliMUONPainterInterfaceHelper::ClearButtons(*fTypes);
175
176 TIter nextType(&types);
177 TObjString* str;
178
179 while ( ( str = static_cast<TObjString*>(nextType()) ) )
180 {
181 AliMUONPainterInterfaceHelper::AddRadioButton(*fTypes,str->String());
182 }
183
184 fTypes->Connect("Clicked(Int_t)","AliMUONPainterPlotSelector",this,
185 "TypeButtonWasClicked(Int_t)");
186
187 fTypes->Show();
188}
189
190//_____________________________________________________________________________
191void
192AliMUONPainterPlotSelector::DataSourceWasChanged()
193{
194 /// Data source was changed
195 DataSourceWasChanged(fCurrentType.Data(),fCurrentData,fCurrentDimension);
196}
197
198//_____________________________________________________________________________
199void
200AliMUONPainterPlotSelector::DataSourceWasChanged(const char* type,
201 AliMUONVTrackerData* data,
202 Int_t dataIndex)
203{
204 /// Emit a signal to tell data source was changed
205 AliDebug(1,Form("type=%s data=%s (%s)",
206 type,
207 ( data ? data->GetName() : "" ),
208 ( ( data && dataIndex >= 0 ) ? data->DimensionName(dataIndex).Data() :
209 "")));
210
211 Long_t param[] = { (Long_t)type, (Long_t)data,
212 (Long_t)dataIndex };
213
214 Emit("DataSourceWasChanged(const char*,AliMUONVTrackerData*,Int_t)",param);
215}
216
217//_____________________________________________________________________________
218void
219AliMUONPainterPlotSelector::DataSourceWasRegistered(AliMUONVTrackerData* data)
220{
221 /// A new data source has been registered : add it to the interface
222
223 AliDebug(1,Form("Registering %s",data->GetName()));
224
225 AliMUONPainterInterfaceHelper::AddRadioButton(*fDataSourceNames,
226 data->GetName(),
227 data);
228
229 data->Connect("NumberOfEventsChanged()",
230 "AliMUONPainterPlotSelector",
231 this,
232 "NumberOfEventsChanged()");
233
234 CreateDimensionButtons(data->GetName());
235
236 fDataSourceNames->Show();
237
238 Layout();
239}
240
241//_____________________________________________________________________________
242void
243AliMUONPainterPlotSelector::NumberOfEventsChanged()
244{
245 /// Change the tool tip of the corresponding data source button
246
247 // find first the sender of the signal
248
49419555 249// AliMUONVTrackerData* data = reinterpret_cast<AliMUONVTrackerData*>(gTQSender);
250//
251// TGButton* button = AliMUONPainterInterfaceHelper::FindButtonByUserData(*fDataSourceNames,data);
252//
253// if (button)
254// {
255// button->SetToolTipText(Form("%d events",data->NumberOfEvents()),250);
256// }
0145e89a 257}
258
259//_____________________________________________________________________________
260void
261AliMUONPainterPlotSelector::DataSourceWasUnregistered(AliMUONVTrackerData* data)
262{
263 /// A data source has been unregistered : remove it from the interface
264
49419555 265 TGButton* button = AliMUONPainterInterfaceHelper::FindButtonByUserData(*fDataSourceNames,data);
0145e89a 266
49419555 267 TGButton* bd = AliMUONPainterInterfaceHelper::FindDownButton(*fDataSourceNames);
268
269 if ( bd == button )
0145e89a 270 {
49419555 271 // selected data source is the one we are removing...
272 // revert to "none" before actually removing it.
273 SourceButtonWasClicked(1);
0145e89a 274 }
275
49419555 276 AliMUONPainterInterfaceHelper::RemoveButton(*fDataSourceNames,button);
277
278 // do not forget to re-connect things
279 fDataSourceNames->Connect("Clicked(Int_t)","AliMUONPainterPlotSelector",
280 this,
281 "SourceButtonWasClicked(Int_t)");
0145e89a 282
49419555 283
0145e89a 284 TObject* o = fDimensionButtonMap->Remove(new TObjString(data->GetName()));
285
286 if (!o)
287 {
288 AliError("Remove failed. Please check");
289 }
49419555 290
291 fDataSourceNames->Show();
0145e89a 292
293 Layout();
294}
295
296//_____________________________________________________________________________
297void
298AliMUONPainterPlotSelector::DimensionButtonWasClicked(Int_t id)
299{
300 /// One dim button was clicked
301
302 AliDebug(1,Form("id=%d",id));
303
304 TGTextButton* button = (TGTextButton*)fDataSourceDimensions->GetButton(id);
305
c9756e69 306 SetCurrentDimension(reinterpret_cast<Long_t>(button->GetUserData()));
0145e89a 307
308 if ( fCurrentDimension >= 0 )
309 {
310 AliMUONPainterInterfaceHelper::SetState(*fTypes,kTRUE);
311 AliMUONPainterInterfaceHelper::Select(*fTypes,fCurrentType.Data(),kFALSE);
312 fTypes->Show();
313 }
314
c9756e69 315 AliDebug(1,Form("fCurrentDimension=%ld fCurrentData=%p fCurrentType=%s",
0145e89a 316 fCurrentDimension,fCurrentData,fCurrentType.Data()));
317
318 DataSourceWasChanged();
319}
320
321//_____________________________________________________________________________
322void
323AliMUONPainterPlotSelector::ResetDimensionButtonMap()
324{
325 /// Reset the button group map
326
327 TIter next(fDimensionButtonMap);
328 TObjString* str;
329
330 while ( ( str = static_cast<TObjString*>(next()) ) )
331 {
332 TGButtonGroup* bg = static_cast<TGButtonGroup*>(fDimensionButtonMap->GetValue(str->String()));
333 AliMUONPainterInterfaceHelper::Unselect(*bg,"*");
334 }
335}
336
337//_____________________________________________________________________________
338void
339AliMUONPainterPlotSelector::RestoreDimensionButtons(const char* dataSourceName,
340 Bool_t updateCurrentDimension)
341{
342 /// Restore (i.e. contrary of Backup) a given dimension button group
343
344 AliDebug(1,Form("name %s",dataSourceName));
345 TGButtonGroup* group = static_cast<TGButtonGroup*>(fDimensionButtonMap->GetValue(dataSourceName));
346
347 AliMUONPainterInterfaceHelper::Copy(*group,*fDataSourceDimensions);
348
349 fDataSourceDimensions->Connect("Clicked(Int_t)",
350 "AliMUONPainterPlotSelector",this,
351 "DimensionButtonWasClicked(Int_t)");
352
353 if ( updateCurrentDimension )
354 {
355 TGButton* button = AliMUONPainterInterfaceHelper::FindDownButton(*fDataSourceDimensions);
356 if ( button )
357 {
c9756e69 358 SetCurrentDimension(reinterpret_cast<Long_t>(button->GetUserData()));
0145e89a 359 }
360 else
361 {
362 SetCurrentDimension(-1);
363 }
364 }
365
366 fDataSourceDimensions->Show();
367}
368
369//_____________________________________________________________________________
370void
371AliMUONPainterPlotSelector::SetCurrentData(AliMUONVTrackerData* data)
372{
373 /// Set the current data pointer
374 AliDebug(1,Form("fCurrentData %p -> %p",fCurrentData,data));
375 fCurrentData = data;
376}
377
378//_____________________________________________________________________________
379void
c9756e69 380AliMUONPainterPlotSelector::SetCurrentDimension(Long_t i)
0145e89a 381{
382 /// Set the current dimension
c9756e69 383 AliDebug(1,Form("fCurrentDimension %ld -> %ld",fCurrentDimension,i));
0145e89a 384 fCurrentDimension = i;
385}
386
387//_____________________________________________________________________________
388void
389AliMUONPainterPlotSelector::SetCurrentType(const char* type)
390{
391 /// Set the current type
392 AliDebug(1,Form("fCurrentType %s -> %s",fCurrentType.Data(),type));
393 fCurrentType = type;
394}
395
396//_____________________________________________________________________________
397void
398AliMUONPainterPlotSelector::SourceButtonWasClicked(Int_t id)
399{
400 /// A source button was clicked
c9756e69 401 AliDebug(1,Form("BEGIN id %d fCurrentDimension=%ld fCurrentData=%p fCurrentType=%s",
0145e89a 402 id,
403 fCurrentDimension,fCurrentData,fCurrentType.Data()));
404
405 BackupDimensionButtons();
406
407 TGButton* button = fDataSourceNames->GetButton(id);
408 if ( !button )
409 {
410 AliError(Form("Could not get DataSource button id=%d",id));
411 StdoutToAliDebug(1,AliMUONPainterInterfaceHelper::Dump(*fDataSourceNames));
412 button->GetUserData(); // to trigger a crash so gdb is possible ;-)
413 }
414
415 AliMUONVTrackerData* data = reinterpret_cast<AliMUONVTrackerData*>(button->GetUserData());
416
417 TString name = data ? data->GetName() : fgkDefaultSourceName;
418
419 RestoreDimensionButtons(name,kTRUE);
420
421 if ( data != 0 &&
422 fCurrentDimension >= 0 &&
c9756e69 423 fCurrentDimension < (Long_t)(data->NumberOfDimensions()) )
0145e89a 424 {
425 AliMUONPainterInterfaceHelper::SetState(*fTypes,kTRUE);
426 }
427 else
428 {
429 AliMUONPainterInterfaceHelper::SetState(*fTypes,kFALSE);
430 }
431
432 SetCurrentData(data);
433
434 UpdateTypeButton();
435
436 fDataSourceNames->Show();
437 fDataSourceDimensions->Show();
438 fTypes->Show();
439
440 Resize();
441 Layout();
442
c9756e69 443 AliDebug(1,Form("END fCurrentDimension=%ld fCurrentData=%p fCurrentType=%s",
0145e89a 444 fCurrentDimension,fCurrentData,fCurrentType.Data()));
445
446 DataSourceWasChanged();
447}
448
449//_____________________________________________________________________________
450void
451AliMUONPainterPlotSelector::TypeButtonWasClicked(Int_t id)
452{
453 /// A type button was clicked
c9756e69 454 AliDebug(1,Form("fCurrentDimension=%ld fCurrentData=%p",
0145e89a 455 fCurrentDimension,fCurrentData));
456
457 TGTextButton* button = (TGTextButton*)fTypes->GetButton(id);
458 SetCurrentType(button->GetTitle());
459
460 AliDebug(1,Form("fCurrentType=%s",fCurrentType.Data()));
461
462 DataSourceWasChanged();
463}
464
465//_____________________________________________________________________________
466void AliMUONPainterPlotSelector::Update(const AliMUONPainterMatrix& painterMatrix)
467{
468 /// Update ourselves from a new painter matrix
469
470 AliDebug(1,"BEGIN");
471
472 SetCurrentType("");
473 SetCurrentData(0x0);
474 SetCurrentDimension(-1);
475
476 AliMUONPainterInterfaceHelper::Unselect(*fDataSourceNames,"*");
477 AliMUONPainterInterfaceHelper::Unselect(*fDataSourceDimensions,"*");
478
479 ResetDimensionButtonMap();
480
481 TObjArray types;
482 types.SetOwner(kTRUE);
483 painterMatrix.GetTypes(types);
484 CreateTypeButtons(types);
485
486 if ( painterMatrix.Size() > 0 )
487 {
488 AliMUONVPainter* painter = painterMatrix.Painter(painterMatrix.Size()-1);
489
490 AliMUONPainterGroup* plotterGroup = painter->PlotterGroup();
491
492 if ( plotterGroup )
493 {
494 // the group have some data to plot, so update our internal pointers
495 // to reflect that
496 SetCurrentData(plotterGroup->Data());
497 SetCurrentDimension(plotterGroup->DataIndex());
498 SetCurrentType(plotterGroup->Type());
499 }
500 }
501
c9756e69 502 AliDebug(1,Form("After update type=%s data=%p dim=%ld",
0145e89a 503 fCurrentType.Data(),fCurrentData,fCurrentDimension));
504
505 // the *order* of the 3 following lines is *very* important
506
507 AliDebug(1,"Will update source buttons");
508 UpdateSourceButton();
509 AliDebug(1,"Will update dimension buttons");
510 UpdateDimensionButton();
511 AliDebug(1,"Will update type buttons");
512 UpdateTypeButton();
513
514 Resize();
515 Layout();
516
c9756e69 517 AliDebug(1,Form("END fCurrentType=%s fCurrentData=%p fCurrentDimension=%ld",
0145e89a 518 fCurrentType.Data(),fCurrentData,
519 fCurrentDimension));
520}
521
522//_____________________________________________________________________________
523void
524AliMUONPainterPlotSelector::UpdateDimensionButton()
525{
526 /// Update the dim buttons
527
528 TGTextButton* button = static_cast<TGTextButton*>
529 (AliMUONPainterInterfaceHelper::FindButtonByUserData(*fDataSourceDimensions,
530 reinterpret_cast<void*>(fCurrentDimension)));
531
532 if ( button )
533 {
534 // set this button as "ON"
535 AliMUONPainterInterfaceHelper::Select(*fDataSourceDimensions,button->GetTitle());
536 }
537 else
538 {
539 AliMUONPainterInterfaceHelper::Unselect(*fDataSourceDimensions,"*");
540 }
541
542 fDataSourceDimensions->Show();
543}
544
545//_____________________________________________________________________________
546void
547AliMUONPainterPlotSelector::UpdateSourceButton()
548{
549 /// Update the source buttons
550
551 TGTextButton* button = static_cast<TGTextButton*>
552 (AliMUONPainterInterfaceHelper::FindButtonByUserData(*fDataSourceNames,
553 fCurrentData));
554
1ffbeb9d 555 if ( button )
556 {
557 AliMUONPainterInterfaceHelper::Select(*fDataSourceNames,button->GetTitle());
0145e89a 558
1ffbeb9d 559 RestoreDimensionButtons(button->GetTitle(),kFALSE);
560 }
0145e89a 561
562 fDataSourceNames->Show();
563}
564
565//_____________________________________________________________________________
566void
567AliMUONPainterPlotSelector::UpdateTypeButton()
568{
569 /// Update the type buttons
570
571 AliDebug(1,Form("fCurrentType=%s",fCurrentType.Data()));
572
573 TGTextButton* button = static_cast<TGTextButton*>
574 (AliMUONPainterInterfaceHelper::FindButtonByName(*fTypes,fCurrentType));
575
576 if ( button )
577 {
578 AliMUONPainterInterfaceHelper::Select(*fTypes,button->GetTitle());
579 }
580 else
581 {
582 AliMUONPainterInterfaceHelper::Unselect(*fTypes,"*");
583 }
584
585 if (!fCurrentData)
586 {
587 AliMUONPainterInterfaceHelper::SetState(*fTypes,kFALSE);
588 }
589
590 fTypes->Show();
591}
592