CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/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.7 2010/07/15 13:02:04 matevz 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    edm::Handle<edm::ConditionsInRunBlock> runCond;
00195    bool res = iRun.getByType(runCond);
00196    //bool res = run.getByLabel("conditionsInEdm", runCond, "", "");
00197    if (res && runCond.isValid())
00198    {
00199       printf("Got current from conds in edm %f\n", runCond->BAvgCurrent);
00200       current = runCond->BAvgCurrent;
00201    }
00202    else
00203    {
00204       printf("Could not extract run-conditions get-result=%d, is-valid=%d\n", res, runCond.isValid());
00205 
00206       edm::ESHandle<RunInfo> sum;
00207       iSetup.get<RunInfoRcd>().get(sum);
00208 
00209       current = sum->m_avg_current;
00210       printf("Got current from RunInfoRcd %f\n", sum->m_avg_current);
00211    }
00212 
00213    static_cast<CmsEveMagField*>(m_MagField)->SetFieldByCurrent(current);
00214 }
00215 
00216 //------------------------------------------------------------------------------
00217 
00218 void EveService::postProcessEvent(const edm::Event&, const edm::EventSetup&)
00219 {
00220    printf("EveService::postProcessEvent: Starting GUI loop.\n");
00221 
00222    m_StepButton->SetEnabled(kFALSE);
00223    m_ContinueButton->SetEnabled(kFALSE);
00224    m_StepLabel->SetText("");
00225 
00226    if (m_ShowEvent)
00227    {
00228      gEve->Redraw3D();
00229      m_Rint->Run(kTRUE);
00230    }
00231    m_ShowEvent = true;
00232    m_AllowStep = true;
00233 
00234    gEve->GetCurrentEvent()->DestroyElements();
00235 }
00236 
00237 //------------------------------------------------------------------------------
00238 
00239 void EveService::display(const std::string& info)
00240 {
00241    // Display whatever was registered so far, wait until user presses
00242    // the "Step" button.
00243 
00244    if (m_AllowStep)
00245    {
00246       m_ContinueButton->SetEnabled(kTRUE);
00247       m_StepButton->SetEnabled(kTRUE);
00248       m_StepLabel->SetText(info.c_str());
00249       gEve->Redraw3D();
00250       m_Rint->Run(kTRUE);
00251    }
00252 }
00253 
00254 //==============================================================================
00255 // Getters for cleints
00256 //==============================================================================
00257 
00258 TEveManager* EveService::getManager()
00259 {
00260    gEve = m_EveManager;
00261    return m_EveManager;
00262 }
00263 
00264 TEveMagField* EveService::getMagField()
00265 {
00266    return m_MagField;
00267 }
00268 void EveService::setupFieldForPropagator(TEveTrackPropagator* prop)
00269 {
00270    prop->SetMagFieldObj(m_MagField, kFALSE);
00271 }
00272 
00273 //==============================================================================
00274 // Redirectors to gEve
00275 //==============================================================================
00276 
00277 void EveService::AddElement(TEveElement* el)
00278 {
00279   m_EveManager->AddElement(el);
00280 }
00281 
00282 void EveService::AddGlobalElement(TEveElement* el)
00283 {
00284   m_EveManager->AddGlobalElement(el);
00285 }
00286 
00287 //==============================================================================
00288 // GUI Builders and callback slots
00289 //==============================================================================
00290 
00291 namespace
00292 {
00293    TGTextButton*
00294    MkTxtButton(TGCompositeFrame* p, const char* txt, Int_t width=0,
00295                Int_t lo=0, Int_t ro=0, Int_t to=0, Int_t bo=0)
00296    {
00297       // Create a standard button.
00298       // If width is not zero, the fixed-width flag is set.
00299 
00300       TGTextButton* b = new TGTextButton(p, txt);
00301       if (width > 0) {
00302          b->SetWidth(width);
00303          b->ChangeOptions(b->GetOptions() | kFixedWidth);
00304       }
00305       p->AddFrame(b, new TGLayoutHints(kLHintsNormal, lo,ro,to,bo));
00306       return b;
00307    }
00308 }
00309 
00310 void EveService::createEventNavigationGUI()
00311 {
00312    const TString cls("EveService");
00313 
00314    TEveBrowser *browser = gEve->GetBrowser();
00315    browser->StartEmbedding(TRootBrowser::kBottom);
00316 
00317    TGMainFrame *mf = new TGMainFrame(gClient->GetRoot(), 400, 100, kVerticalFrame);
00318 
00319    TGHorizontalFrame* f = new TGHorizontalFrame(mf);
00320    mf->AddFrame(f, new TGLayoutHints(kLHintsExpandX, 0,0,2,2));
00321 
00322    MkTxtButton(f, "Exit", 100, 2, 2)->
00323       Connect("Clicked()", cls, this, "slotExit()");
00324 
00325    MkTxtButton(f, "Next Event", 100, 2, 2)->
00326       Connect("Clicked()", cls, this, "slotNextEvent()");
00327 
00328    m_ContinueButton = MkTxtButton(f, "Continue", 100, 2, 2);
00329    m_ContinueButton->Connect("Clicked()", cls, this, "slotContinue()");
00330 
00331    m_StepButton = MkTxtButton(f, "Step", 100, 2, 2);
00332    m_StepButton->Connect("Clicked()", cls, this, "slotStep()");
00333    
00334    m_StepLabel = new TGLabel(mf, "");
00335    m_StepLabel->SetTextJustify(kTextTop | kTextLeft);
00336    mf->AddFrame(m_StepLabel, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 2, 2, 2, 2));
00337 
00338    mf->SetCleanup(kDeepCleanup);
00339    mf->Layout();
00340    mf->MapSubwindows();
00341    mf->MapWindow();
00342 
00343    browser->StopEmbedding("EventCtrl");
00344 }
00345 
00346 void EveService::slotExit()
00347 {
00348    gSystem->ExitLoop();
00349    printf("EveService exiting on user request.\n");
00350 
00351    // Throwing exception here is bad because:
00352    //   a) it does not work when in a "debug step";
00353    //   b) does not restore terminal state.
00354    // So we do exit instead for now.
00355    // throw cms::Exception("UserTerminationRequest");
00356 
00357    gSystem->Exit(0);
00358 }
00359 
00360 void EveService::slotNextEvent()
00361 {
00362    gSystem->ExitLoop();
00363    m_ShowEvent = false;
00364    m_AllowStep = false;
00365 }
00366 
00367 void EveService::slotContinue()
00368 {
00369    gSystem->ExitLoop();
00370    m_AllowStep = false;
00371 }
00372 
00373 void EveService::slotStep()
00374 {
00375    gSystem->ExitLoop();
00376 }