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.6 2010/07/13 19:59:05 matevz 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 
195  bool res = iRun.getByType(runCond);
196  //bool res = run.getByLabel("conditionsInEdm", runCond, "", "");
197  if (res && runCond.isValid())
198  {
199  printf("Got current from conds in edm %f\n", runCond->BAvgCurrent);
200  current = runCond->BAvgCurrent;
201  }
202  else
203  {
204  printf("Could not extract run-conditions get-result=%d, is-valid=%d\n", res, runCond.isValid());
205 
207  iSetup.get<RunInfoRcd>().get(sum);
208 
209  current = sum->m_avg_current;
210  printf("Got current from RunInfoRcd %f\n", sum->m_avg_current);
211  }
212 
213  static_cast<CmsEveMagField*>(m_MagField)->SetFieldByCurrent(current);
214 }
215 
216 //------------------------------------------------------------------------------
217 
219 {
220  printf("EveService::postProcessEvent: Starting GUI loop.\n");
221 
222  m_StepButton->SetEnabled(kFALSE);
223  m_ContinueButton->SetEnabled(kFALSE);
224  m_StepLabel->SetText("");
225 
226  if (m_ShowEvent)
227  {
228  gEve->Redraw3D();
229  m_Rint->Run(kTRUE);
230  }
231  m_ShowEvent = true;
232  m_AllowStep = true;
233 
234  gEve->GetCurrentEvent()->DestroyElements();
235 }
236 
237 //------------------------------------------------------------------------------
238 
239 void EveService::display(const std::string& info)
240 {
241  // Display whatever was registered so far, wait until user presses
242  // the "Step" button.
243 
244  if (m_AllowStep)
245  {
246  m_ContinueButton->SetEnabled(kTRUE);
247  m_StepButton->SetEnabled(kTRUE);
248  m_StepLabel->SetText(info.c_str());
249  gEve->Redraw3D();
250  m_Rint->Run(kTRUE);
251  }
252 }
253 
254 //==============================================================================
255 // Getters for cleints
256 //==============================================================================
257 
259 {
260  gEve = m_EveManager;
261  return m_EveManager;
262 }
263 
264 TEveMagField* EveService::getMagField()
265 {
266  return m_MagField;
267 }
268 void EveService::setupFieldForPropagator(TEveTrackPropagator* prop)
269 {
270  prop->SetMagFieldObj(m_MagField, kFALSE);
271 }
272 
273 //==============================================================================
274 // Redirectors to gEve
275 //==============================================================================
276 
277 void EveService::AddElement(TEveElement* el)
278 {
279  m_EveManager->AddElement(el);
280 }
281 
282 void EveService::AddGlobalElement(TEveElement* el)
283 {
284  m_EveManager->AddGlobalElement(el);
285 }
286 
287 //==============================================================================
288 // GUI Builders and callback slots
289 //==============================================================================
290 
291 namespace
292 {
293  TGTextButton*
294  MkTxtButton(TGCompositeFrame* p, const char* txt, Int_t width=0,
295  Int_t lo=0, Int_t ro=0, Int_t to=0, Int_t bo=0)
296  {
297  // Create a standard button.
298  // If width is not zero, the fixed-width flag is set.
299 
300  TGTextButton* b = new TGTextButton(p, txt);
301  if (width > 0) {
302  b->SetWidth(width);
303  b->ChangeOptions(b->GetOptions() | kFixedWidth);
304  }
305  p->AddFrame(b, new TGLayoutHints(kLHintsNormal, lo,ro,to,bo));
306  return b;
307  }
308 }
309 
311 {
312  const TString cls("EveService");
313 
314  TEveBrowser *browser = gEve->GetBrowser();
315  browser->StartEmbedding(TRootBrowser::kBottom);
316 
317  TGMainFrame *mf = new TGMainFrame(gClient->GetRoot(), 400, 100, kVerticalFrame);
318 
319  TGHorizontalFrame* f = new TGHorizontalFrame(mf);
320  mf->AddFrame(f, new TGLayoutHints(kLHintsExpandX, 0,0,2,2));
321 
322  MkTxtButton(f, "Exit", 100, 2, 2)->
323  Connect("Clicked()", cls, this, "slotExit()");
324 
325  MkTxtButton(f, "Next Event", 100, 2, 2)->
326  Connect("Clicked()", cls, this, "slotNextEvent()");
327 
328  m_ContinueButton = MkTxtButton(f, "Continue", 100, 2, 2);
329  m_ContinueButton->Connect("Clicked()", cls, this, "slotContinue()");
330 
331  m_StepButton = MkTxtButton(f, "Step", 100, 2, 2);
332  m_StepButton->Connect("Clicked()", cls, this, "slotStep()");
333 
334  m_StepLabel = new TGLabel(mf, "");
335  m_StepLabel->SetTextJustify(kTextTop | kTextLeft);
336  mf->AddFrame(m_StepLabel, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 2, 2, 2, 2));
337 
338  mf->SetCleanup(kDeepCleanup);
339  mf->Layout();
340  mf->MapSubwindows();
341  mf->MapWindow();
342 
343  browser->StopEmbedding("EventCtrl");
344 }
345 
347 {
348  gSystem->ExitLoop();
349  printf("EveService exiting on user request.\n");
350 
351  // Throwing exception here is bad because:
352  // a) it does not work when in a "debug step";
353  // b) does not restore terminal state.
354  // So we do exit instead for now.
355  // throw cms::Exception("UserTerminationRequest");
356 
357  gSystem->Exit(0);
358 }
359 
361 {
362  gSystem->ExitLoop();
363  m_ShowEvent = false;
364  m_AllowStep = false;
365 }
366 
368 {
369  gSystem->ExitLoop();
370  m_AllowStep = false;
371 }
372 
374 {
375  gSystem->ExitLoop();
376 }
void watchPostBeginRun(PostBeginRun::slot_type const &iSlot)
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:367
EveService(const edm::ParameterSet &, edm::ActivityRegistry &)
Definition: EveService.cc:120
void postProcessEvent(const edm::Event &, const edm::EventSetup &)
Definition: EveService.cc:218
void postEndJob()
Definition: EveService.cc:181
double double double z
TGTextButton * m_ContinueButton
Definition: EveService.h:96
void slotExit()
Definition: EveService.cc:346
void watchPostProcessEvent(PostProcessEvent::slot_type const &iSlot)
T sqrt(T t)
Definition: SSEVec.h:28
TEveMagField * getMagField()
Definition: EveService.cc:264
double f[11][100]
TGTextButton * m_StepButton
Definition: EveService.h:97
bool isValid() const
Definition: HandleBase.h:76
void slotStep()
Definition: EveService.cc:373
void display(const std::string &info="")
Definition: EveService.cc:239
bool m_ShowEvent
Definition: EveService.h:94
bool getByType(Handle< PROD > &result) const
Definition: Run.h:202
TEveManager * getManager()
Definition: EveService.cc:258
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:310
void setupFieldForPropagator(TEveTrackPropagator *prop)
Definition: EveService.cc:268
bool m_AllowStep
Definition: EveService.h:93
void AddElement(TEveElement *el)
Definition: EveService.cc:277
virtual ~EveService()
Definition: EveService.cc:161
void slotNextEvent()
Definition: EveService.cc:360
void postBeginRun(const edm::Run &, const edm::EventSetup &)
Definition: EveService.cc:190
tuple cout
Definition: gather_cfg.py:41
Definition: DDAxes.h:10
TEveMagField * m_MagField
Definition: EveService.h:91
void AddGlobalElement(TEveElement *el)
Definition: EveService.cc:282
TRint * m_Rint
Definition: EveService.h:89
Definition: Run.h:32
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal