CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Fireworks/FWInterface/src/FWFFService.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include "Fireworks/FWInterface/interface/FWFFService.h"
00003 #include "Fireworks/FWInterface/src/FWFFNavigator.h"
00004 #include "Fireworks/FWInterface/src/FWFFMetadataManager.h"
00005 #include "Fireworks/FWInterface/src/FWFFMetadataUpdateRequest.h"
00006 #include "Fireworks/Core/interface/FWViewManagerManager.h"
00007 #include "Fireworks/Core/interface/Context.h"
00008 #include "Fireworks/Core/interface/FWEventItemsManager.h"
00009 #include "Fireworks/Core/src/CmsShowTaskExecutor.h"
00010 #include "Fireworks/Core/interface/CmsShowMainFrame.h"
00011 #include "Fireworks/Core/interface/FWGUIManager.h"
00012 #include "Fireworks/Core/interface/CSGContinuousAction.h"
00013 #include "Fireworks/Core/interface/FWRecoGeom.h"
00014 #include "Fireworks/Geometry/interface/FWRecoGeometry.h"
00015 #include "Fireworks/Geometry/interface/FWRecoGeometryRecord.h"
00016 
00017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00018 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00019 
00020 #include "FWCore/Framework/interface/EventSetup.h"
00021 #include "FWCore/Framework/interface/ESHandle.h"
00022 #include "FWCore/Framework/interface/ESTransientHandle.h"
00023 #include "CondFormats/RunInfo/interface/RunInfo.h"
00024 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
00025 #include "FWCore/Framework/interface/Run.h"
00026 #include "DataFormats/Common/interface/Handle.h"
00027 #include "DataFormats/Common/interface/ConditionsInEdm.h"
00028 
00029 #include "TROOT.h"
00030 #include "TSystem.h"
00031 #include "TRint.h"
00032 #include "TEveManager.h"
00033 #include "TEveEventManager.h"
00034 #include "TEveTrackPropagator.h"
00035 #include "TGLWidget.h"
00036 
00037 #include "TEveBrowser.h"
00038 
00039 namespace
00040 {
00041    class CmsEveMagField : public TEveMagField
00042    {
00043    private:
00044       Float_t fField;
00045       Float_t fFieldMag;
00046 
00047    public:
00048 
00049       CmsEveMagField() : TEveMagField(), fField(-3.8), fFieldMag(3.8) {}
00050       virtual ~CmsEveMagField() {}
00051 
00052       // set current
00053       void SetFieldByCurrent(Float_t avg_current)
00054       {
00055          fField    = -3.8 * avg_current / 18160.0;
00056          fFieldMag = TMath::Abs(fField);
00057       }
00058 
00059       // get field values
00060       virtual Float_t GetMaxFieldMag() const
00061       {
00062          return fFieldMag;
00063       }
00064 
00065       virtual TEveVector GetField(Float_t x, Float_t y, Float_t z) const
00066       {
00067          static const Float_t barrelFac = 1.2 / 3.8;
00068          static const Float_t endcapFac = 2.0 / 3.8;
00069 
00070          const Float_t R    = sqrt(x*x+y*y);
00071          const Float_t absZ = TMath::Abs(z);
00072 
00073          //barrel
00074          if (absZ < 724.0f)
00075          {
00076             //inside solenoid
00077             if (R < 300.0f) return TEveVector(0, 0, fField);
00078 
00079             // outside solinoid
00080             if ((R > 461.0f && R < 490.5f) ||
00081                 (R > 534.5f && R < 597.5f) ||
00082                 (R > 637.0f && R < 700.0f))
00083             {
00084                return TEveVector(0, 0, -fField*barrelFac);
00085             }
00086          } else {
00087             if ((absZ > 724.0f && absZ < 786.0f) ||
00088                 (absZ > 850.0f && absZ < 910.0f) ||
00089                 (absZ > 975.0f && absZ < 1003.0f))
00090             {
00091                const Float_t fac = (z >= 0 ? fField : -fField) * endcapFac / R;
00092                return TEveVector(x*fac, y*fac, 0);
00093             }
00094          }
00095          return TEveVector(0, 0, 0);
00096       }
00097    };
00098 }
00099 
00100 //
00101 // constants, enums and typedefs
00102 //
00103 
00104 //
00105 // static data member definitions
00106 //
00107 
00108 //==============================================================================
00109 // constructors and destructor
00110 //==============================================================================
00111 
00112 FWFFService::FWFFService(edm::ParameterSet const&ps, edm::ActivityRegistry& ar) 
00113    : CmsShowMainBase(),
00114      m_navigator(new FWFFNavigator(*this)), 
00115      m_metadataManager(new FWFFMetadataManager()),
00116      m_context(new fireworks::Context(changeManager(),
00117                                       selectionManager(),
00118                                       eiManager(),
00119                                       colorManager(),
00120                                       m_metadataManager.get())),
00121      m_appHelper(ps, ar),
00122      m_Rint(m_appHelper.app()),
00123      m_AllowStep(true),
00124      m_ShowEvent(true),
00125      m_firstTime(true)
00126 {
00127    printf("FWFFService::FWFFService CTOR\n");
00128 
00129    setup(m_navigator.get(), m_context.get(), m_metadataManager.get());
00130 
00131    eiManager()->setContext(m_context.get());
00132 
00133    // By default, we look up geometry and configuration in the workarea, then
00134    // in the release area then in the local directory.  It is also possible to
00135    // override those locations by using the displayConfigurationFilename and
00136    // geometryFilename in the parameterset.
00137    const char *releaseBase = getenv("CMSSW_RELEASE_BASE");
00138    const char *workarea = getenv("CMSSW_BASE");
00139    std::string displayConfigRelFilename = "/src/Fireworks/FWInterface/macros/ffw.fwc";
00140    std::string geometryRelFilename = "/src/Fireworks/FWInterface/data/cmsGeom10.root";
00141 
00142    std::string displayConfigFilename = "ffw.fwc";
00143    std::string geometryFilename;
00144 
00145    if (releaseBase && access((releaseBase + displayConfigFilename).c_str(), R_OK) == 0)
00146       displayConfigFilename = releaseBase + displayConfigRelFilename; 
00147    if (workarea && access((workarea + displayConfigRelFilename).c_str(), R_OK) == 0)
00148       displayConfigFilename = workarea + displayConfigRelFilename;   
00149 
00150    if (releaseBase && access((releaseBase + geometryRelFilename).c_str(), R_OK) == 0)
00151       geometryFilename = releaseBase + geometryRelFilename;
00152    if (workarea && access((workarea + geometryRelFilename).c_str(), R_OK) == 0)
00153       geometryFilename = workarea + geometryRelFilename;
00154 
00155    displayConfigFilename = ps.getUntrackedParameter<std::string>("diplayConfigFilename", displayConfigFilename);
00156    geometryFilename = ps.getUntrackedParameter<std::string>("geometryFilename", geometryFilename);
00157 
00158    setGeometryFilename(geometryFilename);
00159    setConfigFilename(displayConfigFilename);
00160 
00161    CmsShowTaskExecutor::TaskFunctor f;
00162 
00163    if (!geometryFilename.empty())
00164    {
00165       f=boost::bind(&CmsShowMainBase::loadGeometry,this);
00166       startupTasks()->addTask(f);
00167    }
00168 
00169    m_MagField = new CmsEveMagField();
00170 
00171    // ----------------------------------------------------------------
00172 
00173    ar.watchPostBeginJob(this, &FWFFService::postBeginJob);
00174    ar.watchPostEndJob(this, &FWFFService::postEndJob);
00175 
00176    ar.watchPostBeginRun(this, &FWFFService::postBeginRun);
00177 
00178    ar.watchPostProcessEvent(this, &FWFFService::postProcessEvent);
00179 }
00180 
00181 FWFFService::~FWFFService()
00182 {
00183    printf("FWFFService::~FWFFService DTOR\n");
00184 
00185    delete m_MagField;
00186 }
00187 
00188 
00189 //==============================================================================
00190 // Service watchers
00191 //==============================================================================
00192 
00193 void
00194 FWFFService::postBeginJob()
00195 {
00196    printf("FWFFService::postBeginJob\n");
00197    // We need to enter the GUI loop in order to 
00198    // have all the callbacks executed. The last callback will
00199    // be responsible for returning the control to CMSSW. 
00200    assert(m_Rint);
00201    CmsShowTaskExecutor::TaskFunctor f;
00202    f=boost::bind(&TApplication::Terminate, m_Rint, 0);
00203    startupTasks()->addTask(f);
00204    // FIXME: do we really need to delay tasks like this?
00205    startupTasks()->startDoingTasks(); 
00206    m_Rint->Run(kTRUE);
00207    // Show the GUI ...
00208    gSystem->ProcessEvents();
00209 }
00210 
00211 void 
00212 FWFFService::postEndJob()
00213 {
00214    printf("FWFFService::postEndJob\n");
00215 
00216    TEveManager::Terminate();
00217 }
00218 
00219 void
00220 FWFFService::checkPosition()
00221 {
00222    if (loop() && isPlaying())
00223       return;
00224   
00225    guiManager()->getMainFrame()->enableNavigatorControls();
00226 
00227    if (m_navigator->isFirstEvent())
00228       guiManager()->disablePrevious();
00229 
00230    if (m_navigator->isLastEvent())
00231    {
00232       guiManager()->disableNext();
00233       // force enable play events action in --port mode
00234       if (!guiManager()->playEventsAction()->isEnabled())
00235          guiManager()->playEventsAction()->enable();
00236    }
00237 }
00238 //------------------------------------------------------------------------------
00239 
00240 void
00241 FWFFService::postBeginRun(const edm::Run& iRun, const edm::EventSetup& iSetup)
00242 {
00243    // If the geometry was not picked up from a file, we try to get it from the
00244    // EventSetup!
00245    // FIXME: we need to check we execute only once because the view managers
00246    //        depend on geometry and they cannot be initialised more than once.
00247    //        This should actually be cleaned up so that the various view manager
00248    //        don't care about geometry.
00249    // FIXME: we should actually be able to update the geometry when requested.
00250    //        this is not possible at the moment.
00251    if (m_firstTime == true)
00252    {
00253       if (m_context->getGeom() == 0)
00254       {
00255          guiManager()->updateStatus("Loading geometry...");
00256          edm::ESTransientHandle<FWRecoGeometry> geoh;
00257          iSetup.get<FWRecoGeometryRecord>().get(geoh);
00258          getGeom().initMap(geoh.product()->idToName);
00259          m_context->setGeom(&(getGeom()));
00260       }
00261 
00262       setupViewManagers();
00263       setupConfiguration();
00264       setupActions();
00265       m_firstTime = false;
00266    }
00267 
00268    float current = 18160.0f;
00269 
00270    edm::Handle<edm::ConditionsInRunBlock> runCond;
00271    bool res = iRun.getByType(runCond);
00272    //bool res = run.getByLabel("conditionsInEdm", runCond, "", "");
00273    if (res && runCond.isValid())
00274    {
00275       printf("Got current from conds in edm %f\n", runCond->BAvgCurrent);
00276       current = runCond->BAvgCurrent;
00277    }
00278    else
00279    {
00280       printf("Could not extract run-conditions get-result=%d, is-valid=%d\n", res, runCond.isValid());
00281 
00282       edm::ESHandle<RunInfo> sum;
00283       iSetup.get<RunInfoRcd>().get(sum);
00284 
00285       current = sum->m_avg_current;
00286       printf("Got current from RunInfoRcd %f\n", sum->m_avg_current);
00287    }
00288 
00289    static_cast<CmsEveMagField*>(m_MagField)->SetFieldByCurrent(current);
00290 }
00291 
00292 //------------------------------------------------------------------------------
00293 void 
00294 FWFFService::postProcessEvent(const edm::Event &event, const edm::EventSetup&es)
00295 {
00296    printf("FWFFService::postProcessEvent: Starting GUI loop.\n");
00297 
00298    m_metadataManager->update(new FWFFMetadataUpdateRequest(event));
00299    m_navigator->setCurrentEvent(&event);
00300    checkPosition();
00301    draw();
00302    m_Rint->Run(kTRUE);
00303 }
00304 
00305 //------------------------------------------------------------------------------
00306 
00307 void 
00308 FWFFService::display(const std::string& info)
00309 {
00310    // Display whatever was registered so far, wait until user presses
00311    // the "Step" button.
00312 
00313    if (m_AllowStep)
00314    {
00315       gEve->Redraw3D();
00316       m_Rint->Run(kTRUE);
00317    }
00318 }
00319 
00320 //==============================================================================
00321 // Getters for cleints
00322 //==============================================================================
00323 
00324 TEveMagField* 
00325 FWFFService::getMagField()
00326 {
00327    return m_MagField;
00328 }
00329 
00330 void 
00331 FWFFService::setupFieldForPropagator(TEveTrackPropagator* prop)
00332 {
00333    prop->SetMagFieldObj(m_MagField, kFALSE);
00334 }
00335 
00336 void 
00337 FWFFService::quit()
00338 {
00339    gSystem->ExitLoop();
00340    printf("FWFFService exiting on user request.\n");
00341 
00342    // Throwing exception here is bad because:
00343    //   a) it does not work when in a "debug step";
00344    //   b) does not restore terminal state.
00345    // So we do exit instead for now.
00346    // throw cms::Exception("UserTerminationRequest");
00347    gSystem->Exit(0);
00348 }