CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/Fireworks/Eve/src/EveService.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Fireworks/Eve
00004 // Class  :     EveService
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  Matevz Tadel
00010 //         Created:  Fri Jun 25 18:57:39 CEST 2010
00011 // $Id: EveService.cc,v 1.8 2012/04/21 01:04:37 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <iostream>
00016 
00017 // user include files
00018 #include "Fireworks/Eve/interface/EveService.h"
00019 
00020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00021 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00022 
00023 // To extract coil current from ConditionsDB
00024 #include "FWCore/Framework/interface/EventSetup.h"
00025 #include "FWCore/Framework/interface/ESHandle.h"
00026 #include "CondFormats/RunInfo/interface/RunInfo.h"
00027 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
00028 
00029 // To extract coil current from ConditionsInEdm
00030 #include <FWCore/Framework/interface/Run.h>
00031 #include <DataFormats/Common/interface/Handle.h>
00032 #include "DataFormats/Common/interface/ConditionsInEdm.h"
00033 
00034 #include "TROOT.h"
00035 #include "TSystem.h"
00036 #include "TRint.h"
00037 #include "TEveManager.h"
00038 #include "TEveEventManager.h"
00039 #include "TEveTrackPropagator.h"
00040 
00041 // GUI widgets
00042 #include "TEveBrowser.h"
00043 #include "TGFrame.h"
00044 #include "TGButton.h"
00045 #include "TGLabel.h"
00046 
00047 namespace
00048 {
00049    class CmsEveMagField : public TEveMagField
00050    {
00051    private:
00052       Float_t fField;
00053       Float_t fFieldMag;
00054 
00055    public:
00056 
00057       CmsEveMagField() : TEveMagField(), fField(-3.8), fFieldMag(3.8) {}
00058       virtual ~CmsEveMagField() {}
00059 
00060       // set current
00061       void SetFieldByCurrent(Float_t avg_current)
00062       {
00063          fField    = -3.8 * avg_current / 18160.0;
00064          fFieldMag = TMath::Abs(fField);
00065       }
00066 
00067       // get field values
00068       virtual Float_t GetMaxFieldMag() const
00069       {
00070          return fFieldMag;
00071       }
00072 
00073       virtual TEveVector GetField(Float_t x, Float_t y, Float_t z) const
00074       {
00075          static const Float_t barrelFac = 1.2 / 3.8;
00076          static const Float_t endcapFac = 2.0 / 3.8;
00077 
00078          const Float_t R    = sqrt(x*x+y*y);
00079          const Float_t absZ = TMath::Abs(z);
00080 
00081          //barrel
00082          if (absZ < 724.0f)
00083          {
00084             //inside solenoid
00085             if (R < 300.0f) return TEveVector(0, 0, fField);
00086 
00087             // outside solinoid
00088             if ((R > 461.0f && R < 490.5f) ||
00089                 (R > 534.5f && R < 597.5f) ||
00090                 (R > 637.0f && R < 700.0f))
00091             {
00092                return TEveVector(0, 0, -fField*barrelFac);
00093             }
00094          } else {
00095             if ((absZ > 724.0f && absZ < 786.0f) ||
00096                 (absZ > 850.0f && absZ < 910.0f) ||
00097                 (absZ > 975.0f && absZ < 1003.0f))
00098             {
00099                const Float_t fac = (z >= 0 ? fField : -fField) * endcapFac / R;
00100                return TEveVector(x*fac, y*fac, 0);
00101             }
00102          }
00103          return TEveVector(0, 0, 0);
00104       }
00105    };
00106 }
00107 
00108 //
00109 // constants, enums and typedefs
00110 //
00111 
00112 //
00113 // static data member definitions
00114 //
00115 
00116 //==============================================================================
00117 // constructors and destructor
00118 //==============================================================================
00119 
00120 EveService::EveService(const edm::ParameterSet&, edm::ActivityRegistry& ar) :
00121    m_EveManager(0), m_Rint(0),
00122    m_MagField(0),
00123    m_AllowStep(true), m_ShowEvent(true),
00124    m_ContinueButton(0), m_StepButton(0), m_StepLabel(0)
00125 {
00126    printf("EveService::EveService CTOR\n");
00127 
00128    std::cout <<" gApplication "<< gApplication <<std::endl;
00129    std::cout <<" is batch "    << gROOT->IsBatch() <<std::endl;
00130    std::cout <<" display "     << gSystem->Getenv("DISPLAY") <<std::endl;
00131 
00132    const char* dummyArgvArray[] = {"cmsRun"};
00133    char**      dummyArgv = const_cast<char**>(dummyArgvArray);
00134    int         dummyArgc = 1;
00135 
00136    m_Rint = new TRint("App", &dummyArgc, dummyArgv);
00137    assert(TApplication::GetApplications()->GetSize());
00138   
00139    gROOT->SetBatch(kFALSE);
00140    std::cout<<"calling NeedGraphicsLibs()"<<std::endl;
00141    TApplication::NeedGraphicsLibs();
00142 
00143    m_EveManager = TEveManager::Create();
00144 
00145    m_EveManager->AddEvent(new TEveEventManager("Event", "Event Data"));
00146 
00147    m_MagField = new CmsEveMagField();
00148 
00149    createEventNavigationGUI();
00150 
00151    // ----------------------------------------------------------------
00152 
00153    ar.watchPostBeginJob(this, &EveService::postBeginJob);
00154    ar.watchPostEndJob  (this, &EveService::postEndJob);
00155 
00156    ar.watchPostBeginRun(this, &EveService::postBeginRun);
00157 
00158    ar.watchPostProcessEvent(this, &EveService::postProcessEvent);
00159 }
00160 
00161 EveService::~EveService()
00162 {
00163    printf("EveService::~EveService DTOR\n");
00164 
00165    delete m_MagField;
00166 }
00167 
00168 
00169 //==============================================================================
00170 // Service watchers
00171 //==============================================================================
00172 
00173 void EveService::postBeginJob()
00174 {
00175    printf("EveService::postBeginJob\n");
00176 
00177    // Show the GUI ...
00178    gSystem->ProcessEvents();
00179 }
00180 
00181 void EveService::postEndJob()
00182 {
00183    printf("EveService::postEndJob\n");
00184 
00185    TEveManager::Terminate();
00186 }
00187 
00188 //------------------------------------------------------------------------------
00189 
00190 void EveService::postBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup)
00191 {
00192    float current = 18160.0f;
00193 
00194    try 
00195    {
00196       edm::Handle<edm::ConditionsInRunBlock> runCond;
00197       bool res = iRun.getByType(runCond);
00198   
00199       //bool res = run.getByLabel("conditionsInEdm", runCond, "", "");
00200       if (res && runCond.isValid())
00201       {
00202          printf("Got current from conds in edm %f\n", runCond->BAvgCurrent);
00203          current = runCond->BAvgCurrent;
00204       }
00205       else
00206       {
00207          printf("Could not extract run-conditions get-result=%d, is-valid=%d\n", res, runCond.isValid());
00208 
00209          edm::ESHandle<RunInfo> sum;
00210          iSetup.get<RunInfoRcd>().get(sum);
00211 
00212          current = sum->m_avg_current;
00213          printf("Got current from RunInfoRcd %f\n", sum->m_avg_current);
00214       }
00215    }
00216    catch (...) 
00217    {
00218       printf("RunInfo not available \n");
00219    }
00220    static_cast<CmsEveMagField*>(m_MagField)->SetFieldByCurrent(current);
00221 }
00222 
00223 //------------------------------------------------------------------------------
00224 
00225 void EveService::postProcessEvent(const edm::Event&, const edm::EventSetup&)
00226 {
00227    printf("EveService::postProcessEvent: Starting GUI loop.\n");
00228 
00229    m_StepButton->SetEnabled(kFALSE);
00230    m_ContinueButton->SetEnabled(kFALSE);
00231    m_StepLabel->SetText("");
00232 
00233    if (m_ShowEvent)
00234    {
00235      gEve->Redraw3D();
00236      m_Rint->Run(kTRUE);
00237    }
00238    m_ShowEvent = true;
00239    m_AllowStep = true;
00240 
00241    gEve->GetCurrentEvent()->DestroyElements();
00242 }
00243 
00244 //------------------------------------------------------------------------------
00245 
00246 void EveService::display(const std::string& info)
00247 {
00248    // Display whatever was registered so far, wait until user presses
00249    // the "Step" button.
00250 
00251    if (m_AllowStep)
00252    {
00253       m_ContinueButton->SetEnabled(kTRUE);
00254       m_StepButton->SetEnabled(kTRUE);
00255       m_StepLabel->SetText(info.c_str());
00256       gEve->Redraw3D();
00257       m_Rint->Run(kTRUE);
00258    }
00259 }
00260 
00261 //==============================================================================
00262 // Getters for cleints
00263 //==============================================================================
00264 
00265 TEveManager* EveService::getManager()
00266 {
00267    gEve = m_EveManager;
00268    return m_EveManager;
00269 }
00270 
00271 TEveMagField* EveService::getMagField()
00272 {
00273    return m_MagField;
00274 }
00275 void EveService::setupFieldForPropagator(TEveTrackPropagator* prop)
00276 {
00277    prop->SetMagFieldObj(m_MagField, kFALSE);
00278 }
00279 
00280 //==============================================================================
00281 // Redirectors to gEve
00282 //==============================================================================
00283 
00284 void EveService::AddElement(TEveElement* el)
00285 {
00286   m_EveManager->AddElement(el);
00287 }
00288 
00289 void EveService::AddGlobalElement(TEveElement* el)
00290 {
00291   m_EveManager->AddGlobalElement(el);
00292 }
00293 
00294 //==============================================================================
00295 // GUI Builders and callback slots
00296 //==============================================================================
00297 
00298 namespace
00299 {
00300    TGTextButton*
00301    MkTxtButton(TGCompositeFrame* p, const char* txt, Int_t width=0,
00302                Int_t lo=0, Int_t ro=0, Int_t to=0, Int_t bo=0)
00303    {
00304       // Create a standard button.
00305       // If width is not zero, the fixed-width flag is set.
00306 
00307       TGTextButton* b = new TGTextButton(p, txt);
00308       if (width > 0) {
00309          b->SetWidth(width);
00310          b->ChangeOptions(b->GetOptions() | kFixedWidth);
00311       }
00312       p->AddFrame(b, new TGLayoutHints(kLHintsNormal, lo,ro,to,bo));
00313       return b;
00314    }
00315 }
00316 
00317 void EveService::createEventNavigationGUI()
00318 {
00319    const TString cls("EveService");
00320 
00321    TEveBrowser *browser = gEve->GetBrowser();
00322    browser->StartEmbedding(TRootBrowser::kBottom);
00323 
00324    TGMainFrame *mf = new TGMainFrame(gClient->GetRoot(), 400, 100, kVerticalFrame);
00325 
00326    TGHorizontalFrame* f = new TGHorizontalFrame(mf);
00327    mf->AddFrame(f, new TGLayoutHints(kLHintsExpandX, 0,0,2,2));
00328 
00329    MkTxtButton(f, "Exit", 100, 2, 2)->
00330       Connect("Clicked()", cls, this, "slotExit()");
00331 
00332    MkTxtButton(f, "Next Event", 100, 2, 2)->
00333       Connect("Clicked()", cls, this, "slotNextEvent()");
00334 
00335    m_ContinueButton = MkTxtButton(f, "Continue", 100, 2, 2);
00336    m_ContinueButton->Connect("Clicked()", cls, this, "slotContinue()");
00337 
00338    m_StepButton = MkTxtButton(f, "Step", 100, 2, 2);
00339    m_StepButton->Connect("Clicked()", cls, this, "slotStep()");
00340    
00341    m_StepLabel = new TGLabel(mf, "");
00342    m_StepLabel->SetTextJustify(kTextTop | kTextLeft);
00343    mf->AddFrame(m_StepLabel, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 2, 2, 2, 2));
00344 
00345    mf->SetCleanup(kDeepCleanup);
00346    mf->Layout();
00347    mf->MapSubwindows();
00348    mf->MapWindow();
00349 
00350    browser->StopEmbedding("EventCtrl");
00351 }
00352 
00353 void EveService::slotExit()
00354 {
00355    gSystem->ExitLoop();
00356    printf("EveService exiting on user request.\n");
00357 
00358    // Throwing exception here is bad because:
00359    //   a) it does not work when in a "debug step";
00360    //   b) does not restore terminal state.
00361    // So we do exit instead for now.
00362    // throw cms::Exception("UserTerminationRequest");
00363 
00364    gSystem->Exit(0);
00365 }
00366 
00367 void EveService::slotNextEvent()
00368 {
00369    gSystem->ExitLoop();
00370    m_ShowEvent = false;
00371    m_AllowStep = false;
00372 }
00373 
00374 void EveService::slotContinue()
00375 {
00376    gSystem->ExitLoop();
00377    m_AllowStep = false;
00378 }
00379 
00380 void EveService::slotStep()
00381 {
00382    gSystem->ExitLoop();
00383 }