CMS 3D CMS Logo

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