CMS 3D CMS Logo

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