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