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