CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FWFFLooper.cc
Go to the documentation of this file.
1 #include <functional>
2 #include <iostream>
3 
22 
25 
40 
41 #include "TROOT.h"
42 #include "TSystem.h"
43 #include "TRint.h"
44 #include "TGLWidget.h"
45 
46 #include "TEveManager.h"
47 #include "TEveEventManager.h"
48 #include "TEveTrackPropagator.h"
49 #include "TEveBrowser.h"
50 #include "TGeoManager.h"
51 
52 namespace edm {
53  class StreamContext;
54  class ModuleCallingContext;
55 } // namespace edm
56 
57 namespace {
58  class CmsEveMagField : public TEveMagField {
59  private:
60  Float_t fField;
61  Float_t fFieldMag;
62 
63  public:
64  CmsEveMagField() : TEveMagField(), fField(-3.8), fFieldMag(3.8) {}
65  ~CmsEveMagField() override {}
66 
67  // set current
68  void SetFieldByCurrent(Float_t avg_current) {
69  fField = -3.8 * avg_current / 18160.0;
70  fFieldMag = TMath::Abs(fField);
71  }
72 
73  // get field values
74  Float_t GetMaxFieldMag() const override { return fFieldMag; }
75 
76  TEveVector GetField(Float_t x, Float_t y, Float_t z) const override {
77  static const Float_t barrelFac = 1.2 / 3.8;
78  static const Float_t endcapFac = 2.0 / 3.8;
79 
80  const Float_t R = sqrt(x * x + y * y);
81  const Float_t absZ = TMath::Abs(z);
82 
83  //barrel
84  if (absZ < 724.0f) {
85  //inside solenoid
86  if (R < 300.0f)
87  return TEveVector(0, 0, fField);
88 
89  // outside solinoid
90  if ((R > 461.0f && R < 490.5f) || (R > 534.5f && R < 597.5f) || (R > 637.0f && R < 700.0f)) {
91  return TEveVector(0, 0, -fField * barrelFac);
92  }
93  } else {
94  if ((absZ > 724.0f && absZ < 786.0f) || (absZ > 850.0f && absZ < 910.0f) || (absZ > 975.0f && absZ < 1003.0f)) {
95  const Float_t fac = (z >= 0 ? fField : -fField) * endcapFac / R;
96  return TEveVector(x * fac, y * fac, 0);
97  }
98  }
99  return TEveVector(0, 0, 0);
100  }
101  };
102 } // namespace
103 
104 //
105 // constants, enums and typedefs
106 //
107 
108 //
109 // static data member definitions
110 //
111 
112 //==============================================================================
113 // constructors and destructor
114 //==============================================================================
115 
117  : CmsShowMainBase(),
118  m_navigator(new FWFFNavigator(*this)),
119  m_metadataManager(new FWFFMetadataManager()),
120  m_context(new fireworks::Context(
121  changeManager(), selectionManager(), eiManager(), colorManager(), m_metadataManager.get())),
122  m_Rint(m_appHelper->app()),
123  m_AllowStep(true),
124  m_ShowEvent(true),
125  m_firstTime(true),
126  m_pathsGUI(nullptr),
127  m_geomWatcher(this, &FWFFLooper::remakeGeometry),
128  m_recoGeomToken(esConsumes()),
129  m_runInfoToken(esConsumes()),
130  m_displayGeomToken(esConsumes()) {
131  setup(m_navigator.get(), m_context.get(), m_metadataManager.get());
132 
133  eiManager()->setContext(m_context.get());
134 
135  // By default, we look up geometry and configuration in the workarea, then
136  // in the release area then in the local directory. It is also possible to
137  // override those locations by using the displayConfigurationFilename and
138  // geometryFilename in the parameterset.
139  const char* releaseBase = std::getenv("CMSSW_RELEASE_BASE");
140  const char* workarea = std::getenv("CMSSW_BASE");
141  std::string displayConfigRelFilename = "/src/Fireworks/FWInterface/macros/ffw.fwc";
142  std::string geometryRelFilename = "/src/Fireworks/FWInterface/data/cmsGeom10.root";
143 
144  std::string displayConfigFilename = "ffw.fwc";
146 
147  if (releaseBase && access((releaseBase + displayConfigFilename).c_str(), R_OK) == 0)
148  displayConfigFilename = releaseBase + displayConfigRelFilename;
149  if (workarea && access((workarea + displayConfigRelFilename).c_str(), R_OK) == 0)
150  displayConfigFilename = workarea + displayConfigRelFilename;
151 
152  if (releaseBase && access((releaseBase + geometryRelFilename).c_str(), R_OK) == 0)
153  geometryFilename = releaseBase + geometryRelFilename;
154  if (workarea && access((workarea + geometryRelFilename).c_str(), R_OK) == 0)
155  geometryFilename = workarea + geometryRelFilename;
156 
157  displayConfigFilename = ps.getUntrackedParameter<std::string>("displayConfigFilename", displayConfigFilename);
158  geometryFilename = ps.getUntrackedParameter<std::string>("geometryFilename", geometryFilename);
159  if (!geometryFilename.empty()) {
161  }
162  setGeometryFilename(geometryFilename);
163  setConfigFilename(displayConfigFilename);
164 
165  if (!geometryFilename.empty()) {
167  }
168 
169  m_MagField = new CmsEveMagField();
170 }
171 
174  f = std::bind(&CmsShowMainBase::loadGeometry, this);
175  startupTasks()->addTask(f);
176 }
177 
179  m_pathsGUI = new FWPathsPopup(this, guiManager());
180 
184 }
185 
187 
188 //==============================================================================
189 // Service watchers
190 //==============================================================================
191 
192 void FWFFLooper::startingNewLoop(unsigned int count) {
193  // Initialise on first loop.
194  if (count == 0) {
196  m_pathsGUI->setup(info);
197 
198  // We need to enter the GUI loop in order to
199  // have all the callbacks executed. The last callback will
200  // be responsible for returning the control to CMSSW.
201  assert(m_Rint);
203  f = std::bind(&TApplication::Terminate, m_Rint, 0);
204  startupTasks()->addTask(f);
205  // FIXME: do we really need to delay tasks like this?
207  m_Rint->Run(kTRUE);
208  // Show the GUI ...
209  gSystem->ProcessEvents();
210  }
211 }
212 
214  printf("FWFFLooper::postEndJob\n");
215  TEveManager::Terminate();
216 }
217 
219  if (loop() && isPlaying())
220  return;
221 
224 
225  if (m_isFirstEvent)
227 
228  if (m_isLastEvent) {
229  guiManager()->disableNext();
230  // force enable play events action in --port mode
231  if (!guiManager()->playEventsAction()->isEnabled())
233  }
234 }
235 
242  bool reachedEnd = (forward() && m_isLastEvent) || (!forward() && m_isFirstEvent);
243 
244  if (!reachedEnd || loop()) {
245  // Will exit the loop here!
246  m_autoReload = true;
247  forward() ? m_navigator->nextEvent() : m_navigator->previousEvent();
248  } else {
249  m_autoReload = false;
253  }
254 }
255 
258  m_autoReload = false;
262  checkPosition();
263 }
264 
265 //------------------------------------------------------------------------------
266 
267 void FWFFLooper::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
268  // Check DisplayGeomRecord changes.
269  try {
270  m_geomWatcher.check(iSetup);
271  } catch (...) {
272  }
273 
274  // If the geometry was not picked up from a file, we try to get it from the
275  // EventSetup!
276  // FIXME: we need to check we execute only once because the view managers
277  // depend on geometry and they cannot be initialised more than once.
278  // This should actually be cleaned up so that the various view manager
279  // don't care about geometry.
280  // FIXME: we should actually be able to update the geometry when requested.
281  // this is not possible at the moment.
282  if (m_firstTime == true) {
283  if (m_context->getGeom() == nullptr) {
284  try {
285  guiManager()->updateStatus("Loading geometry...");
286  getGeom().initMap(iSetup.getData(m_recoGeomToken).idToName);
287  m_context->setGeom(&(getGeom()));
288  } catch (const cms::Exception& exception) {
289  setGeometryFilename("cmsGeom10.root");
291  }
292  }
293 
296  setupActions();
297 
298  guiManager()->showEventFilterGUI_.connect(std::bind(&FWFFLooper::showPathsGUI, this, std::placeholders::_1));
299  guiManager()->setFilterButtonText("Show paths / CMSSW configuration editor");
301 
302  m_firstTime = false;
303  m_autoReload = false;
304  }
305 
306  float current = 18160.0f;
307  try {
309  // bool res = iRun.getByType(runCond);
310  bool res = iRun.getByLabel("conditionsInEdm", runCond);
311  if (res && runCond.isValid()) {
312  printf("Got current from conds in edm %f\n", runCond->BAvgCurrent);
313  current = runCond->BAvgCurrent;
314  } else {
315  printf("Could not extract run-conditions get-result=%d, is-valid=%d\n", res, runCond.isValid());
316 
317  auto rec = iSetup.find(edm::eventsetup::EventSetupRecordKey::makeKey<RunInfoRcd>());
318  if (rec) {
319  RunInfo const& sum = iSetup.getData(m_runInfoToken);
320 
321  current = sum.m_avg_current;
322  printf("Got current from RunInfoRcd %f\n", sum.m_avg_current);
323  }
324  }
325  } catch (...) {
326  fwLog(fwlog::kInfo) << "ConditionsInRunBlock not available\n";
327  }
328  static_cast<CmsEveMagField*>(m_MagField)->SetFieldByCurrent(current);
329  context()->getField()->setFFFieldMag(m_MagField->GetMaxFieldMag());
330 }
331 
332 //------------------------------------------------------------------------------
334  const edm::EventSetup& es,
335  edm::ProcessingController& controller) {
336  // Check DisplayGeomRecord changes.
337  try {
338  m_geomWatcher.check(es);
339  } catch (...) {
340  }
341 
342  m_pathsGUI->postEvent(event);
343 
346  // If the next event id is valid, set the transition so
347  // that we go to it go to to it.
348  if (m_nextEventId != edm::EventID()) {
351  return kContinue;
352  }
353  // We handle "last event" by going to the first event and then moving to the
354  // previous event.
355  if (m_navigator->currentTransition() == FWFFNavigator::kLastEvent) {
356  m_navigator->resetTransition();
357  controller.setTransitionToPreviousEvent();
358  return kContinue;
359  }
360 
361  m_pathsGUI->hasChanges() = false;
362  m_metadataManager->update(new FWFFMetadataUpdateRequest(event));
363  m_navigator->setCurrentEvent(&event);
364  if (m_autoReload == true)
366 
367  checkPosition();
368  draw();
369 
370  m_Rint->Run(kTRUE);
371  // If the GUI changed the PSet, save the current event to reload
372  // it on next iteration.
373  if (m_pathsGUI->hasChanges()) {
375  return kStop;
376  } else if (m_navigator->currentTransition() == FWFFNavigator::kFirstEvent) {
377  m_nextEventId = m_navigator->getFirstEventID();
378  return kStop;
379  } else if (m_navigator->currentTransition() == FWFFNavigator::kLastEvent) {
380  m_nextEventId = m_navigator->getFirstEventID();
381  return kStop;
382  } else if (m_navigator->currentTransition() == FWFFNavigator::kNextEvent)
383  controller.setTransitionToNextEvent();
384  else if (m_navigator->currentTransition() == FWFFNavigator::kPreviousEvent)
385  controller.setTransitionToPreviousEvent();
386  return kContinue;
387 }
388 
389 //------------------------------------------------------------------------------
391  // Display whatever was registered so far, wait until user presses
392  // the "Step" button.
393 
394  if (m_AllowStep) {
395  gEve->Redraw3D();
396  m_Rint->Run(kTRUE);
397  }
398 }
399 
400 //==============================================================================
401 // Getters for cleints
402 //==============================================================================
403 
404 TEveMagField* FWFFLooper::getMagField() { return m_MagField; }
405 
406 void FWFFLooper::setupFieldForPropagator(TEveTrackPropagator* prop) { prop->SetMagFieldObj(m_MagField, kFALSE); }
407 
409  gSystem->ExitLoop();
410 
411  // Throwing exception here is bad because:
412  // a) it does not work when in a "debug step";
413  // b) does not restore terminal state.
414  // So we do exit instead for now.
415  // throw cms::Exception("UserTerminationRequest");
416  gSystem->Exit(0);
417 }
418 
424  // Looks like the module changer is availble only here.
425  for (ModuleChanges::iterator i = m_scheduledChanges.begin(), e = m_scheduledChanges.end(); i != e; ++i) {
426  try {
427  moduleChanger()->changeModule(i->first, i->second);
428  } catch (cms::Exception const& e) {
429  fwLog(fwlog::kError) << "FWFFLooper::endOfLoop caught exception.\n";
430  std::cerr << e.what() << std::endl;
431  }
432  }
433  m_scheduledChanges.clear();
434  return kContinue;
435 }
436 
437 void FWFFLooper::showPathsGUI(const TGWindow*) {
438  if (!m_pathsGUI)
439  return;
440  if (m_pathsGUI->IsMapped()) {
441  guiManager()->setFilterButtonText("Show paths / CMSSW configuration editor");
442  m_pathsGUI->UnmapWindow();
443  } else {
444  guiManager()->setFilterButtonText("Hide paths / CMSSW configuration editor");
445  m_pathsGUI->MapWindow();
446  }
447 }
448 
449 void FWFFLooper::requestChanges(const std::string& moduleLabel, const edm::ParameterSet& ps) {
450  m_scheduledChanges[moduleLabel] = ps;
451 }
452 
453 //______________________________________________________________________________
454 
456  fwLog(fwlog::kInfo) << "FWFFLooper set TGeo geometry from DisplayGeomRecord.\n";
457 
458  TEveGeoManagerHolder _tgeo(const_cast<TGeoManager*>(&dgRec.get(m_displayGeomToken)));
460 }
edm::ESWatcher< DisplayGeomRecord > m_geomWatcher
Definition: FWFFLooper.h:115
TEveMagField * getMagField()
Definition: FWFFLooper.cc:404
std::optional< eventsetup::EventSetupRecordGeneric > find(const eventsetup::EventSetupRecordKey &iKey) const
Definition: EventSetup.h:178
edm::EDLooperBase::Status endOfLoop(const edm::EventSetup &, unsigned int) override
Definition: FWFFLooper.cc:423
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:283
T getUntrackedParameter(std::string const &, T const &) const
void preModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
sigc::signal< void > filterButtonClicked_
Definition: FWGUIManager.h:203
static const TGPicture * info(bool iBackgroundIsBlack)
bool m_isLastEvent
Definition: FWFFLooper.h:113
void setupFieldForPropagator(TEveTrackPropagator *prop)
Definition: FWFFLooper.cc:406
const edm::ESGetToken< RunInfo, RunInfoRcd > m_runInfoToken
Definition: FWFFLooper.h:117
FWGUIManager * guiManager()
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
void initMap(const FWRecoGeom::InfoMap &map)
Definition: FWGeometry.cc:203
void watchPreModuleEvent(PreModuleEvent::slot_type const &iSlot)
void startDoingTasks() override
void enable()
Definition: CSGAction.cc:269
FWEventItemsManager * eiManager()
ModuleChanges m_scheduledChanges
Definition: FWFFLooper.h:109
std::function< void()> TaskFunctor
void autoLoadNewEvent() override
Definition: FWFFLooper.cc:240
void startingNewLoop(unsigned int) override
Definition: FWFFLooper.cc:192
FWPathsPopup * m_pathsGUI
Definition: FWFFLooper.h:106
void watchPostModuleEvent(PostModuleEvent::slot_type const &iSlot)
TEveMagField * m_MagField
Definition: FWFFLooper.h:101
void attachTo(edm::ActivityRegistry &) override
Override this method if you need to monitor the state of the processing.
Definition: FWFFLooper.cc:178
void postEndJob()
Definition: FWFFLooper.cc:213
void setup(FWNavigatorBase *navigator, fireworks::Context *context, FWJobMetadataManager *metadataManager)
void remakeGeometry(const DisplayGeomRecord &dgRec)
Definition: FWFFLooper.cc:455
void updateStatus(const char *status)
void setContext(fireworks::Context *)
FWFFLooper(const edm::ParameterSet &)
Definition: FWFFLooper.cc:116
assert(be >=bs)
void loadDefaultGeometryFile(void)
Definition: FWFFLooper.cc:172
void enableActions(bool enable=true)
void quit() override
Definition: FWFFLooper.cc:408
void requestChanges(const std::string &, const edm::ParameterSet &)
Definition: FWFFLooper.cc:449
bool getData(T &iHolder) const
Definition: EventSetup.h:128
edm::EDLooperBase::Status duringLoop(const edm::Event &, const edm::EventSetup &, edm::ProcessingController &) override
Definition: FWFFLooper.cc:333
void display(const std::string &info="")
Definition: FWFFLooper.cc:390
void setGeometryFilename(const std::string &filename)
void setTransitionToEvent(edm::EventID const &iID)
void setFFFieldMag(float)
Definition: FWMagField.cc:190
virtual void stopPlaying()
ProductT const & get(ESGetToken< ProductT, DepRecordT > const &iToken) const
FWMagField * getField() const
Definition: Context.h:66
bool loop() const
CmsShowTaskExecutor * startupTasks()
bool m_autoReload
Definition: FWFFLooper.h:111
ForwardState forwardState() const
Returns the present state of processing.
void beginRun(const edm::Run &, const edm::EventSetup &) override
Called after all event modules have processed the begin of a Run.
Definition: FWFFLooper.cc:267
T sqrt(T t)
Definition: SSEVec.h:19
std::unique_ptr< FWFFMetadataManager > m_metadataManager
Definition: FWFFLooper.h:95
sigc::signal< void, const TGWindow * > showEventFilterGUI_
Definition: FWGUIManager.h:204
printf("params %d %f %f %f\n", minT, eps, errmax, chi2max)
T Abs(T a)
Definition: MathUtil.h:49
bool & hasChanges()
Definition: FWPathsPopup.h:35
char const * what() const noexceptoverride
Definition: Exception.cc:103
fireworks::Context * context()
TRint * m_Rint
Definition: FWFFLooper.h:99
bool m_firstTime
Definition: FWFFLooper.h:105
CmsShowMainFrame * getMainFrame() const
Definition: FWGUIManager.h:197
void disablePrevious()
bool changeModule(const std::string &iLabel, const ParameterSet &iPSet)
bool isValid() const
Definition: HandleBase.h:70
ScheduleInfo const * scheduleInfo() const
This returns a non-zero value after the constructor has been called.
bool m_isFirstEvent
Definition: FWFFLooper.h:112
void checkPosition() override
Definition: FWFFLooper.cc:218
std::unique_ptr< FWFFNavigator > m_navigator
Definition: FWFFLooper.h:94
const edm::ESGetToken< TGeoManager, DisplayGeomRecord > m_displayGeomToken
Definition: FWFFLooper.h:118
#define fwLog(_level_)
Definition: fwLog.h:45
~FWFFLooper() override
Definition: FWFFLooper.cc:186
bool m_AllowStep
Definition: FWFFLooper.h:103
void setFilterButtonText(const char *txt)
void postModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &)
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
CSGContinuousAction * playEventsAction()
float m_avg_current
Definition: RunInfo.h:28
bool isPlaying() const
FWGeometry & getGeom()
void stopPlaying() override
Definition: FWFFLooper.cc:256
void postEvent(edm::Event const &event)
void showEventFilterGUI()
void addTask(const TaskFunctor &iTask)
std::unique_ptr< fireworks::Context > m_context
Definition: FWFFLooper.h:96
static void setGeoManagerRuntime(TGeoManager *)
void showPathsGUI(const TGWindow *p)
Definition: FWFFLooper.cc:437
ModuleChanger * moduleChanger()
This only returns a non-zero value during the call to endOfLoop.
bool forward() const
#define get
const std::string & geometryFilename(void)
void enableComplexNavigation(bool enable=true)
void setup(const edm::ScheduleInfo *info)
edm::EventID m_nextEventId
Definition: FWFFLooper.h:110
void disableNext()
const edm::ESGetToken< FWRecoGeometry, FWRecoGeometryRecord > m_recoGeomToken
Definition: FWFFLooper.h:116
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
void setConfigFilename(const std::string &f)
ReverseState reverseState() const
Definition: Run.h:45