Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:22

0001 // -*- C++ -*-
0002 //
0003 // Package:     Fireworks/Eve
0004 // Class  :     EveService
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Matevz Tadel
0010 //         Created:  Fri Jun 25 18:57:39 CEST 2010
0011 //
0012 
0013 // system include files
0014 #include <iostream>
0015 
0016 // user include files
0017 #include "Fireworks/Eve/interface/EveService.h"
0018 
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0021 
0022 #include "TROOT.h"
0023 #include "TSystem.h"
0024 #include "TColor.h"
0025 #include "TStyle.h"
0026 #include "TEnv.h"
0027 
0028 // To extract coil current from ConditionsDB
0029 #include "FWCore/Framework/interface/EventSetup.h"
0030 #include "FWCore/Framework/interface/ESHandle.h"
0031 #include "CondFormats/RunInfo/interface/RunInfo.h"
0032 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
0033 
0034 // To extract coil current from ConditionsInEdm
0035 #include <FWCore/Framework/interface/Run.h>
0036 #include <DataFormats/Common/interface/Handle.h>
0037 #include "DataFormats/Common/interface/ConditionsInEdm.h"
0038 
0039 #include "TROOT.h"
0040 #include "TSystem.h"
0041 #include "TRint.h"
0042 #include "TEveManager.h"
0043 #include "TEveEventManager.h"
0044 #include "TEveTrackPropagator.h"
0045 
0046 // GUI widgets
0047 #include "TEveBrowser.h"
0048 #include "TGFrame.h"
0049 #include "TGButton.h"
0050 #include "TGLabel.h"
0051 
0052 namespace {
0053   class CmsEveMagField : public TEveMagField {
0054   private:
0055     Float_t fField;
0056     Float_t fFieldMag;
0057 
0058   public:
0059     CmsEveMagField() : TEveMagField(), fField(-3.8), fFieldMag(3.8) {}
0060     ~CmsEveMagField() override {}
0061 
0062     // set current
0063     void SetFieldByCurrent(Float_t avg_current) {
0064       fField = -3.8 * avg_current / 18160.0;
0065       fFieldMag = TMath::Abs(fField);
0066     }
0067 
0068     // get field values
0069     Float_t GetMaxFieldMag() const override { return fFieldMag; }
0070 
0071     TEveVector GetField(Float_t x, Float_t y, Float_t z) const override {
0072       static const Float_t barrelFac = 1.2 / 3.8;
0073       static const Float_t endcapFac = 2.0 / 3.8;
0074 
0075       const Float_t R = sqrt(x * x + y * y);
0076       const Float_t absZ = TMath::Abs(z);
0077 
0078       //barrel
0079       if (absZ < 724.0f) {
0080         //inside solenoid
0081         if (R < 300.0f)
0082           return TEveVector(0, 0, fField);
0083 
0084         // outside solinoid
0085         if ((R > 461.0f && R < 490.5f) || (R > 534.5f && R < 597.5f) || (R > 637.0f && R < 700.0f)) {
0086           return TEveVector(0, 0, -fField * barrelFac);
0087         }
0088       } else {
0089         if ((absZ > 724.0f && absZ < 786.0f) || (absZ > 850.0f && absZ < 910.0f) || (absZ > 975.0f && absZ < 1003.0f)) {
0090           const Float_t fac = (z >= 0 ? fField : -fField) * endcapFac / R;
0091           return TEveVector(x * fac, y * fac, 0);
0092         }
0093       }
0094       return TEveVector(0, 0, 0);
0095     }
0096   };
0097 }  // namespace
0098 
0099 //
0100 // constants, enums and typedefs
0101 //
0102 
0103 //
0104 // static data member definitions
0105 //
0106 
0107 //==============================================================================
0108 // constructors and destructor
0109 //==============================================================================
0110 
0111 EveService::EveService(const edm::ParameterSet&, edm::ActivityRegistry& ar)
0112     : m_EveManager(nullptr),
0113       m_Rint(nullptr),
0114       m_MagField(nullptr),
0115       m_AllowStep(true),
0116       m_ShowEvent(true),
0117       m_ContinueButton(nullptr),
0118       m_StepButton(nullptr),
0119       m_StepLabel(nullptr) {
0120   printf("EveService::EveService CTOR\n");
0121 
0122   std::cout << " gApplication " << gApplication << std::endl;
0123   std::cout << " is batch " << gROOT->IsBatch() << std::endl;
0124   std::cout << " display " << gSystem->Getenv("DISPLAY") << std::endl;
0125 
0126   const char* dummyArgvArray[] = {"cmsRun"};
0127   char** dummyArgv = const_cast<char**>(dummyArgvArray);
0128   int dummyArgc = 1;
0129 
0130   m_Rint = new TRint("App", &dummyArgc, dummyArgv);
0131   assert(TApplication::GetApplications()->GetSize());
0132 
0133   gROOT->SetBatch(kFALSE);
0134   std::cout << "calling NeedGraphicsLibs()" << std::endl;
0135   TApplication::NeedGraphicsLibs();
0136 
0137   m_EveManager = TEveManager::Create();
0138 
0139   m_EveManager->AddEvent(new TEveEventManager("Event", "Event Data"));
0140 
0141   m_MagField = new CmsEveMagField();
0142 
0143   createEventNavigationGUI();
0144 
0145   // ----------------------------------------------------------------
0146 
0147   ar.watchPostBeginJob(this, &EveService::postBeginJob);
0148   ar.watchPostEndJob(this, &EveService::postEndJob);
0149 
0150   ar.watchPostGlobalBeginRun(this, &EveService::postGlobalBeginRun);
0151 
0152   ar.watchPostEvent(this, &EveService::postEvent);
0153 }
0154 
0155 EveService::~EveService() {
0156   printf("EveService::~EveService DTOR\n");
0157 
0158   delete m_MagField;
0159 }
0160 
0161 //==============================================================================
0162 // Service watchers
0163 //==============================================================================
0164 
0165 void EveService::postBeginJob() {
0166   printf("EveService::postBeginJob\n");
0167 
0168   // Show the GUI ...
0169   gSystem->ProcessEvents();
0170 }
0171 
0172 void EveService::postEndJob() {
0173   printf("EveService::postEndJob\n");
0174 
0175   TEveManager::Terminate();
0176 }
0177 
0178 //------------------------------------------------------------------------------
0179 
0180 void EveService::postGlobalBeginRun(edm::GlobalContext const&) {
0181   float current = 18160.0f;
0182   /*
0183    try 
0184    {
0185       edm::Handle<edm::ConditionsInRunBlock> runCond;
0186       bool res = iRun.getByLabel("conditionsInEdm", runCond);
0187       if (res && runCond.isValid())
0188       {
0189          printf("Got current from conds in edm %f\n", runCond->BAvgCurrent);
0190          current = runCond->BAvgCurrent;
0191       }
0192       else
0193       {
0194          printf("Could not extract run-conditions get-result=%d, is-valid=%d\n", res, runCond.isValid());
0195 
0196          edm::ESHandle<RunInfo> sum;
0197          iSetup.get<RunInfoRcd>().get(sum);
0198 
0199          current = sum->m_avg_current;
0200          printf("Got current from RunInfoRcd %f\n", sum->m_avg_current);
0201       }
0202    }
0203    catch (...) 
0204    {
0205    }
0206    */
0207   printf("RunInfo not available \n");
0208   static_cast<CmsEveMagField*>(m_MagField)->SetFieldByCurrent(current);
0209 }
0210 
0211 //------------------------------------------------------------------------------
0212 
0213 void EveService::postEvent(edm::StreamContext const&) {
0214   printf("EveService::postProcessEvent: Starting GUI loop.\n");
0215 
0216   m_StepButton->SetEnabled(kFALSE);
0217   m_ContinueButton->SetEnabled(kFALSE);
0218   m_StepLabel->SetText("");
0219 
0220   if (m_ShowEvent) {
0221     gEve->Redraw3D();
0222     m_Rint->Run(kTRUE);
0223   }
0224   m_ShowEvent = true;
0225   m_AllowStep = true;
0226 
0227   gEve->GetCurrentEvent()->DestroyElements();
0228 }
0229 
0230 //------------------------------------------------------------------------------
0231 
0232 void EveService::display(const std::string& info) {
0233   // Display whatever was registered so far, wait until user presses
0234   // the "Step" button.
0235 
0236   if (m_AllowStep) {
0237     m_ContinueButton->SetEnabled(kTRUE);
0238     m_StepButton->SetEnabled(kTRUE);
0239     m_StepLabel->SetText(info.c_str());
0240     gEve->Redraw3D();
0241     m_Rint->Run(kTRUE);
0242   }
0243 }
0244 
0245 //==============================================================================
0246 // Getters for cleints
0247 //==============================================================================
0248 
0249 TEveManager* EveService::getManager() {
0250   gEve = m_EveManager;
0251   return m_EveManager;
0252 }
0253 
0254 TEveMagField* EveService::getMagField() { return m_MagField; }
0255 void EveService::setupFieldForPropagator(TEveTrackPropagator* prop) { prop->SetMagFieldObj(m_MagField, kFALSE); }
0256 
0257 //==============================================================================
0258 // Redirectors to gEve
0259 //==============================================================================
0260 
0261 void EveService::AddElement(TEveElement* el) { m_EveManager->AddElement(el); }
0262 
0263 void EveService::AddGlobalElement(TEveElement* el) { m_EveManager->AddGlobalElement(el); }
0264 
0265 //==============================================================================
0266 // GUI Builders and callback slots
0267 //==============================================================================
0268 
0269 namespace {
0270   TGTextButton* MkTxtButton(
0271       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) {
0272     // Create a standard button.
0273     // If width is not zero, the fixed-width flag is set.
0274 
0275     TGTextButton* b = new TGTextButton(p, txt);
0276     if (width > 0) {
0277       b->SetWidth(width);
0278       b->ChangeOptions(b->GetOptions() | kFixedWidth);
0279     }
0280     p->AddFrame(b, new TGLayoutHints(kLHintsNormal, lo, ro, to, bo));
0281     return b;
0282   }
0283 }  // namespace
0284 
0285 void EveService::createEventNavigationGUI() {
0286   const TString cls("EveService");
0287 
0288   TEveBrowser* browser = gEve->GetBrowser();
0289   browser->StartEmbedding(TRootBrowser::kBottom);
0290 
0291   TGMainFrame* mf = new TGMainFrame(gClient->GetRoot(), 400, 100, kVerticalFrame);
0292 
0293   TGHorizontalFrame* f = new TGHorizontalFrame(mf);
0294   mf->AddFrame(f, new TGLayoutHints(kLHintsExpandX, 0, 0, 2, 2));
0295 
0296   MkTxtButton(f, "Exit", 100, 2, 2)->Connect("Clicked()", cls, this, "slotExit()");
0297 
0298   MkTxtButton(f, "Next Event", 100, 2, 2)->Connect("Clicked()", cls, this, "slotNextEvent()");
0299 
0300   m_ContinueButton = MkTxtButton(f, "Continue", 100, 2, 2);
0301   m_ContinueButton->Connect("Clicked()", cls, this, "slotContinue()");
0302 
0303   m_StepButton = MkTxtButton(f, "Step", 100, 2, 2);
0304   m_StepButton->Connect("Clicked()", cls, this, "slotStep()");
0305 
0306   m_StepLabel = new TGLabel(mf, "");
0307   m_StepLabel->SetTextJustify(kTextTop | kTextLeft);
0308   mf->AddFrame(m_StepLabel, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 2, 2, 2, 2));
0309 
0310   mf->SetCleanup(kDeepCleanup);
0311   mf->Layout();
0312   mf->MapSubwindows();
0313   mf->MapWindow();
0314 
0315   browser->StopEmbedding("EventCtrl");
0316 }
0317 
0318 void EveService::slotExit() {
0319   gSystem->ExitLoop();
0320   printf("EveService exiting on user request.\n");
0321 
0322   // Throwing exception here is bad because:
0323   //   a) it does not work when in a "debug step";
0324   //   b) does not restore terminal state.
0325   // So we do exit instead for now.
0326   // throw cms::Exception("UserTerminationRequest");
0327 
0328   gSystem->Exit(0);
0329 }
0330 
0331 void EveService::slotNextEvent() {
0332   gSystem->ExitLoop();
0333   m_ShowEvent = false;
0334   m_AllowStep = false;
0335 }
0336 
0337 void EveService::slotContinue() {
0338   gSystem->ExitLoop();
0339   m_AllowStep = false;
0340 }
0341 
0342 void EveService::slotStep() { gSystem->ExitLoop(); }