CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EveService.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Fireworks/Eve
4 // Class : EveService
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Matevz Tadel
10 // Created: Fri Jun 25 18:57:39 CEST 2010
11 // $Id: EveService.cc,v 1.9 2012/08/16 01:09:21 amraktad Exp $
12 //
13 
14 // system include files
15 #include <iostream>
16 
17 // user include files
19 
22 
23 // To extract coil current from ConditionsDB
28 
29 // To extract coil current from ConditionsInEdm
33 
34 #include "TROOT.h"
35 #include "TSystem.h"
36 #include "TRint.h"
37 #include "TEveManager.h"
38 #include "TEveEventManager.h"
39 #include "TEveTrackPropagator.h"
40 
41 // GUI widgets
42 #include "TEveBrowser.h"
43 #include "TGFrame.h"
44 #include "TGButton.h"
45 #include "TGLabel.h"
46 
47 namespace
48 {
49  class CmsEveMagField : public TEveMagField
50  {
51  private:
52  Float_t fField;
53  Float_t fFieldMag;
54 
55  public:
56 
57  CmsEveMagField() : TEveMagField(), fField(-3.8), fFieldMag(3.8) {}
58  virtual ~CmsEveMagField() {}
59 
60  // set current
61  void SetFieldByCurrent(Float_t avg_current)
62  {
63  fField = -3.8 * avg_current / 18160.0;
64  fFieldMag = TMath::Abs(fField);
65  }
66 
67  // get field values
68  virtual Float_t GetMaxFieldMag() const
69  {
70  return fFieldMag;
71  }
72 
73  virtual TEveVector GetField(Float_t x, Float_t y, Float_t z) const
74  {
75  static const Float_t barrelFac = 1.2 / 3.8;
76  static const Float_t endcapFac = 2.0 / 3.8;
77 
78  const Float_t R = sqrt(x*x+y*y);
79  const Float_t absZ = TMath::Abs(z);
80 
81  //barrel
82  if (absZ < 724.0f)
83  {
84  //inside solenoid
85  if (R < 300.0f) return TEveVector(0, 0, fField);
86 
87  // outside solinoid
88  if ((R > 461.0f && R < 490.5f) ||
89  (R > 534.5f && R < 597.5f) ||
90  (R > 637.0f && R < 700.0f))
91  {
92  return TEveVector(0, 0, -fField*barrelFac);
93  }
94  } else {
95  if ((absZ > 724.0f && absZ < 786.0f) ||
96  (absZ > 850.0f && absZ < 910.0f) ||
97  (absZ > 975.0f && absZ < 1003.0f))
98  {
99  const Float_t fac = (z >= 0 ? fField : -fField) * endcapFac / R;
100  return TEveVector(x*fac, y*fac, 0);
101  }
102  }
103  return TEveVector(0, 0, 0);
104  }
105  };
106 }
107 
108 //
109 // constants, enums and typedefs
110 //
111 
112 //
113 // static data member definitions
114 //
115 
116 //==============================================================================
117 // constructors and destructor
118 //==============================================================================
119 
121  m_EveManager(0), m_Rint(0),
122  m_MagField(0),
123  m_AllowStep(true), m_ShowEvent(true),
124  m_ContinueButton(0), m_StepButton(0), m_StepLabel(0)
125 {
126  printf("EveService::EveService CTOR\n");
127 
128  std::cout <<" gApplication "<< gApplication <<std::endl;
129  std::cout <<" is batch " << gROOT->IsBatch() <<std::endl;
130  std::cout <<" display " << gSystem->Getenv("DISPLAY") <<std::endl;
131 
132  const char* dummyArgvArray[] = {"cmsRun"};
133  char** dummyArgv = const_cast<char**>(dummyArgvArray);
134  int dummyArgc = 1;
135 
136  m_Rint = new TRint("App", &dummyArgc, dummyArgv);
137  assert(TApplication::GetApplications()->GetSize());
138 
139  gROOT->SetBatch(kFALSE);
140  std::cout<<"calling NeedGraphicsLibs()"<<std::endl;
141  TApplication::NeedGraphicsLibs();
142 
143  m_EveManager = TEveManager::Create();
144 
145  m_EveManager->AddEvent(new TEveEventManager("Event", "Event Data"));
146 
147  m_MagField = new CmsEveMagField();
148 
150 
151  // ----------------------------------------------------------------
152 
155 
157 
159 }
160 
162 {
163  printf("EveService::~EveService DTOR\n");
164 
165  delete m_MagField;
166 }
167 
168 
169 //==============================================================================
170 // Service watchers
171 //==============================================================================
172 
174 {
175  printf("EveService::postBeginJob\n");
176 
177  // Show the GUI ...
178  gSystem->ProcessEvents();
179 }
180 
182 {
183  printf("EveService::postEndJob\n");
184 
185  TEveManager::Terminate();
186 }
187 
188 //------------------------------------------------------------------------------
189 
190 void EveService::postBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup)
191 {
192  float current = 18160.0f;
193 
194  try
195  {
197  bool res = iRun.getByLabel("conditionsInEdm", runCond);
198  if (res && runCond.isValid())
199  {
200  printf("Got current from conds in edm %f\n", runCond->BAvgCurrent);
201  current = runCond->BAvgCurrent;
202  }
203  else
204  {
205  printf("Could not extract run-conditions get-result=%d, is-valid=%d\n", res, runCond.isValid());
206 
208  iSetup.get<RunInfoRcd>().get(sum);
209 
210  current = sum->m_avg_current;
211  printf("Got current from RunInfoRcd %f\n", sum->m_avg_current);
212  }
213  }
214  catch (...)
215  {
216  printf("RunInfo not available \n");
217  }
218  static_cast<CmsEveMagField*>(m_MagField)->SetFieldByCurrent(current);
219 }
220 
221 //------------------------------------------------------------------------------
222 
224 {
225  printf("EveService::postProcessEvent: Starting GUI loop.\n");
226 
227  m_StepButton->SetEnabled(kFALSE);
228  m_ContinueButton->SetEnabled(kFALSE);
229  m_StepLabel->SetText("");
230 
231  if (m_ShowEvent)
232  {
233  gEve->Redraw3D();
234  m_Rint->Run(kTRUE);
235  }
236  m_ShowEvent = true;
237  m_AllowStep = true;
238 
239  gEve->GetCurrentEvent()->DestroyElements();
240 }
241 
242 //------------------------------------------------------------------------------
243 
244 void EveService::display(const std::string& info)
245 {
246  // Display whatever was registered so far, wait until user presses
247  // the "Step" button.
248 
249  if (m_AllowStep)
250  {
251  m_ContinueButton->SetEnabled(kTRUE);
252  m_StepButton->SetEnabled(kTRUE);
253  m_StepLabel->SetText(info.c_str());
254  gEve->Redraw3D();
255  m_Rint->Run(kTRUE);
256  }
257 }
258 
259 //==============================================================================
260 // Getters for cleints
261 //==============================================================================
262 
264 {
265  gEve = m_EveManager;
266  return m_EveManager;
267 }
268 
269 TEveMagField* EveService::getMagField()
270 {
271  return m_MagField;
272 }
273 void EveService::setupFieldForPropagator(TEveTrackPropagator* prop)
274 {
275  prop->SetMagFieldObj(m_MagField, kFALSE);
276 }
277 
278 //==============================================================================
279 // Redirectors to gEve
280 //==============================================================================
281 
282 void EveService::AddElement(TEveElement* el)
283 {
284  m_EveManager->AddElement(el);
285 }
286 
287 void EveService::AddGlobalElement(TEveElement* el)
288 {
289  m_EveManager->AddGlobalElement(el);
290 }
291 
292 //==============================================================================
293 // GUI Builders and callback slots
294 //==============================================================================
295 
296 namespace
297 {
298  TGTextButton*
299  MkTxtButton(TGCompositeFrame* p, const char* txt, Int_t width=0,
300  Int_t lo=0, Int_t ro=0, Int_t to=0, Int_t bo=0)
301  {
302  // Create a standard button.
303  // If width is not zero, the fixed-width flag is set.
304 
305  TGTextButton* b = new TGTextButton(p, txt);
306  if (width > 0) {
307  b->SetWidth(width);
308  b->ChangeOptions(b->GetOptions() | kFixedWidth);
309  }
310  p->AddFrame(b, new TGLayoutHints(kLHintsNormal, lo,ro,to,bo));
311  return b;
312  }
313 }
314 
316 {
317  const TString cls("EveService");
318 
319  TEveBrowser *browser = gEve->GetBrowser();
320  browser->StartEmbedding(TRootBrowser::kBottom);
321 
322  TGMainFrame *mf = new TGMainFrame(gClient->GetRoot(), 400, 100, kVerticalFrame);
323 
324  TGHorizontalFrame* f = new TGHorizontalFrame(mf);
325  mf->AddFrame(f, new TGLayoutHints(kLHintsExpandX, 0,0,2,2));
326 
327  MkTxtButton(f, "Exit", 100, 2, 2)->
328  Connect("Clicked()", cls, this, "slotExit()");
329 
330  MkTxtButton(f, "Next Event", 100, 2, 2)->
331  Connect("Clicked()", cls, this, "slotNextEvent()");
332 
333  m_ContinueButton = MkTxtButton(f, "Continue", 100, 2, 2);
334  m_ContinueButton->Connect("Clicked()", cls, this, "slotContinue()");
335 
336  m_StepButton = MkTxtButton(f, "Step", 100, 2, 2);
337  m_StepButton->Connect("Clicked()", cls, this, "slotStep()");
338 
339  m_StepLabel = new TGLabel(mf, "");
340  m_StepLabel->SetTextJustify(kTextTop | kTextLeft);
341  mf->AddFrame(m_StepLabel, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 2, 2, 2, 2));
342 
343  mf->SetCleanup(kDeepCleanup);
344  mf->Layout();
345  mf->MapSubwindows();
346  mf->MapWindow();
347 
348  browser->StopEmbedding("EventCtrl");
349 }
350 
352 {
353  gSystem->ExitLoop();
354  printf("EveService exiting on user request.\n");
355 
356  // Throwing exception here is bad because:
357  // a) it does not work when in a "debug step";
358  // b) does not restore terminal state.
359  // So we do exit instead for now.
360  // throw cms::Exception("UserTerminationRequest");
361 
362  gSystem->Exit(0);
363 }
364 
366 {
367  gSystem->ExitLoop();
368  m_ShowEvent = false;
369  m_AllowStep = false;
370 }
371 
373 {
374  gSystem->ExitLoop();
375  m_AllowStep = false;
376 }
377 
379 {
380  gSystem->ExitLoop();
381 }
void watchPostBeginRun(PostBeginRun::slot_type const &iSlot)
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:177
void postBeginJob()
Definition: EveService.cc:173
TGLabel * m_StepLabel
Definition: EveService.h:98
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
void slotContinue()
Definition: EveService.cc:372
EveService(const edm::ParameterSet &, edm::ActivityRegistry &)
Definition: EveService.cc:120
void postProcessEvent(const edm::Event &, const edm::EventSetup &)
Definition: EveService.cc:223
void postEndJob()
Definition: EveService.cc:181
double double double z
TGTextButton * m_ContinueButton
Definition: EveService.h:96
void slotExit()
Definition: EveService.cc:351
void watchPostProcessEvent(PostProcessEvent::slot_type const &iSlot)
T sqrt(T t)
Definition: SSEVec.h:46
TEveMagField * getMagField()
Definition: EveService.cc:269
double f[11][100]
TGTextButton * m_StepButton
Definition: EveService.h:97
bool isValid() const
Definition: HandleBase.h:76
void slotStep()
Definition: EveService.cc:378
void display(const std::string &info="")
Definition: EveService.cc:244
bool m_ShowEvent
Definition: EveService.h:94
TEveManager * getManager()
Definition: EveService.cc:263
TEveManager * m_EveManager
Definition: EveService.h:88
const T & get() const
Definition: EventSetup.h:55
double b
Definition: hdecay.h:120
void createEventNavigationGUI()
Definition: EveService.cc:315
void setupFieldForPropagator(TEveTrackPropagator *prop)
Definition: EveService.cc:273
bool m_AllowStep
Definition: EveService.h:93
void AddElement(TEveElement *el)
Definition: EveService.cc:282
virtual ~EveService()
Definition: EveService.cc:161
void slotNextEvent()
Definition: EveService.cc:365
void postBeginRun(const edm::Run &, const edm::EventSetup &)
Definition: EveService.cc:190
tuple cout
Definition: gather_cfg.py:121
Definition: DDAxes.h:10
TEveMagField * m_MagField
Definition: EveService.h:91
void AddGlobalElement(TEveElement *el)
Definition: EveService.cc:287
TRint * m_Rint
Definition: EveService.h:89
Definition: Run.h:33
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal