CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Fireworks/Core/src/FWInvMassDialog.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWInvMassDialog
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  Matevz Tadel
00010 //         Created:  Mon Nov 22 11:05:57 CET 2010
00011 // $Id: FWInvMassDialog.cc,v 1.3 2010/12/06 16:06:44 matevz Exp $
00012 //
00013 
00014 // system include files
00015 
00016 // user include files
00017 #include "Fireworks/Core/interface/FWInvMassDialog.h"
00018 #include "Fireworks/Core/interface/FWSelectionManager.h"
00019 #include "Fireworks/Core/interface/FWEventItem.h"
00020 
00021 #include "DataFormats/Candidate/interface/Candidate.h"
00022 #include "DataFormats/TrackReco/interface/TrackBase.h"
00023 
00024 #include "TClass.h"
00025 #include "TMath.h"
00026 
00027 #include "TGTextView.h"
00028 #include "TGButton.h"
00029 
00030 //
00031 // constants, enums and typedefs
00032 //
00033 
00034 //
00035 // static data member definitions
00036 //
00037 
00038 //
00039 // constructors and destructor
00040 //
00041 
00042 FWInvMassDialog::FWInvMassDialog(FWSelectionManager* sm) :
00043    TGMainFrame(gClient->GetRoot(), 470, 240),
00044    m_selectionMgr(sm),
00045    m_text(0),
00046    m_button(0)
00047 {
00048    SetWindowName("Invariant Mass Dialog");
00049    SetCleanup(kDeepCleanup);
00050 
00051    m_text = new TGTextView(this);
00052    AddFrame(m_text, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 1, 1, 1, 1));
00053 
00054    m_button = new TGTextButton(this, "Calculate");
00055    AddFrame(m_button, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 1, 1, 1, 1));
00056 
00057    m_button->Connect("Clicked()","FWInvMassDialog", this, "Calculate()");
00058 
00059    Layout();
00060    MapSubwindows();
00061 }
00062 
00063 // FWInvMassDialog::FWInvMassDialog(const FWInvMassDialog& rhs)
00064 // {
00065 //    // do actual copying here;
00066 // }
00067 
00068 FWInvMassDialog::~FWInvMassDialog()
00069 {
00070 }
00071 
00072 void FWInvMassDialog::CloseWindow()
00073 {
00074    UnmapWindow();
00075 }
00076 
00077 //
00078 // assignment operators
00079 //
00080 // const FWInvMassDialog& FWInvMassDialog::operator=(const FWInvMassDialog& rhs)
00081 // {
00082 //   //An exception safe implementation is
00083 //   FWInvMassDialog temp(rhs);
00084 //   swap(rhs);
00085 //
00086 //   return *this;
00087 // }
00088 
00089 //
00090 // member functions
00091 //
00092 
00093 void FWInvMassDialog::beginUpdate()
00094 {
00095    m_text->Clear();
00096    m_firstLine = true;
00097 }
00098 
00099 void FWInvMassDialog::addLine(const TString& line)
00100 {
00101    TGText *txt = m_text->GetText();
00102 
00103    if (m_firstLine)
00104    {
00105       txt->InsText(TGLongPosition(0, 0), line);
00106       m_firstLine = false;
00107    }
00108    else
00109    {
00110       txt->InsText(TGLongPosition(0, txt->RowCount()), line);
00111    }
00112 }
00113 
00114 void FWInvMassDialog::endUpdate()
00115 {
00116    m_text->Update();
00117 }
00118 
00119 void FWInvMassDialog::Calculate()
00120 {
00121    const std::set<FWModelId>& sted = m_selectionMgr->selected();
00122 
00123    beginUpdate();
00124 
00125    addLine(TString::Format(" %d items in selection", (int) sted.size()));
00126    addLine("");
00127    addLine("--------------------------------------------------+--------------");
00128    addLine("       px          py          pz          pT     | Collection");
00129    addLine("--------------------------------------------------+--------------");
00130 
00131    TClass *rc_class  = TClass::GetClass(typeid(reco::Candidate));
00132    TClass *rtb_class = TClass::GetClass(typeid(reco::TrackBase));
00133 
00134    math::XYZVector sum;
00135    double          sum_len = 0;
00136    double          sum_len_xy = 0;
00137 
00138    for (std::set<FWModelId>::const_iterator i = sted.begin(); i != sted.end(); ++i)
00139    {
00140       TString line;
00141 
00142       TClass *model_class = const_cast<TClass*>(i->item()->modelType());
00143       void   *model_data  = const_cast<void*>  (i->item()->modelData(i->index()));
00144 
00145       math::XYZVector v;
00146       bool            ok_p = false;
00147 
00148       reco::Candidate *rc = reinterpret_cast<reco::Candidate*>
00149          (model_class->DynamicCast(rc_class, model_data));
00150 
00151       if (rc != 0)
00152       {
00153          ok_p = true;
00154          v.SetXYZ(rc->px(), rc->py(), rc->pz());
00155       }
00156       else
00157       {
00158          reco::TrackBase *rtb = reinterpret_cast<reco::TrackBase*>
00159             (model_class->DynamicCast(rtb_class, model_data));
00160 
00161          if (rtb != 0)
00162          {
00163             ok_p = true;
00164             v.SetXYZ(rtb->px(), rtb->py(), rtb->pz());
00165          }
00166       }
00167 
00168       if (ok_p)
00169       {
00170          sum        += v;
00171          sum_len    += TMath::Sqrt(v.mag2());
00172          sum_len_xy += TMath::Sqrt(v.perp2());
00173 
00174          line = TString::Format("  %+10.3f  %+10.3f  %+10.3f  %10.3f", v.x(), v.y(), v.z(), TMath::Sqrt(v.perp2()));
00175 
00176       }
00177       else
00178       {
00179          line = TString::Format("  -------- not a Candidate or TrackBase --------");
00180       }
00181       line += TString::Format("  | %s[%d]", i->item()->name().c_str(), i->index());
00182 
00183       addLine(line);
00184    }
00185 
00186    addLine("--------------------------------------------------+--------------");
00187    addLine(TString::Format("  %+10.3f  %+10.3f  %+10.3f  %10.3f  | Sum", sum.x(), sum.y(), sum.z(), TMath::Sqrt(sum.perp2())));
00188    addLine("");
00189    addLine(TString::Format("m  = %10.3f", TMath::Sqrt(TMath::Max(0.0, sum_len    * sum_len    - sum.mag2()))));
00190    addLine(TString::Format("mT = %10.3f", TMath::Sqrt(TMath::Max(0.0, sum_len_xy * sum_len_xy - sum.perp2()))));
00191 
00192    endUpdate();
00193 }
00194 
00195 //
00196 // const member functions
00197 //
00198 
00199 //
00200 // static member functions
00201 //