CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/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.14.1 2012/12/03 06:26:26 amraktad 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 #include "DataFormats/Math/interface/deltaR.h"
00024 
00025 #include "TClass.h"
00026 #include "TMath.h"
00027 
00028 #include "TGTextView.h"
00029 #include "TGButton.h"
00030 
00031 //
00032 // constants, enums and typedefs
00033 //
00034 
00035 //
00036 // static data member definitions
00037 //
00038 
00039 //
00040 // constructors and destructor
00041 //
00042 
00043 FWInvMassDialog::FWInvMassDialog(FWSelectionManager* sm) :
00044    TGMainFrame(gClient->GetRoot(), 470, 240),
00045    m_selectionMgr(sm),
00046    m_text(0),
00047    m_button(0)
00048 {
00049    SetWindowName("Invariant Mass Dialog");
00050    SetCleanup(kDeepCleanup);
00051 
00052    m_text = new TGTextView(this);
00053    AddFrame(m_text, new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY, 1, 1, 1, 1));
00054 
00055    m_button = new TGTextButton(this, "Calculate");
00056    AddFrame(m_button, new TGLayoutHints(kLHintsNormal | kLHintsExpandX, 1, 1, 1, 1));
00057 
00058    m_button->Connect("Clicked()","FWInvMassDialog", this, "Calculate()");
00059 
00060    Layout();
00061    MapSubwindows();
00062 }
00063 
00064 // FWInvMassDialog::FWInvMassDialog(const FWInvMassDialog& rhs)
00065 // {
00066 //    // do actual copying here;
00067 // }
00068 
00069 FWInvMassDialog::~FWInvMassDialog()
00070 {
00071 }
00072 
00073 void FWInvMassDialog::CloseWindow()
00074 {
00075    UnmapWindow();
00076 }
00077 
00078 //
00079 // assignment operators
00080 //
00081 // const FWInvMassDialog& FWInvMassDialog::operator=(const FWInvMassDialog& rhs)
00082 // {
00083 //   //An exception safe implementation is
00084 //   FWInvMassDialog temp(rhs);
00085 //   swap(rhs);
00086 //
00087 //   return *this;
00088 // }
00089 
00090 //
00091 // member functions
00092 //
00093 
00094 void FWInvMassDialog::beginUpdate()
00095 {
00096    m_text->Clear();
00097    m_firstLine = true;
00098 }
00099 
00100 void FWInvMassDialog::addLine(const TString& line)
00101 {
00102    TGText *txt = m_text->GetText();
00103 
00104    if (m_firstLine)
00105    {
00106       txt->InsText(TGLongPosition(0, 0), line);
00107       m_firstLine = false;
00108    }
00109    else
00110    {
00111       txt->InsText(TGLongPosition(0, txt->RowCount()), line);
00112    }
00113 }
00114 
00115 void FWInvMassDialog::endUpdate()
00116 {
00117    m_text->Update();
00118 }
00119 
00120 void FWInvMassDialog::Calculate()
00121 {
00122    const std::set<FWModelId>& sted = m_selectionMgr->selected();
00123 
00124    beginUpdate();
00125 
00126    addLine(TString::Format(" %d items in selection", (int) sted.size()));
00127    addLine("");
00128    addLine("--------------------------------------------------+--------------");
00129    addLine("       px          py          pz          pT     | Collection");
00130    addLine("--------------------------------------------------+--------------");
00131 
00132    TClass *rc_class  = TClass::GetClass(typeid(reco::Candidate));
00133    TClass *rtb_class = TClass::GetClass(typeid(reco::TrackBase));
00134 
00135    math::XYZVector sum;
00136    double          sum_len = 0;
00137    double          sum_len_xy = 0;
00138 
00139    math::XYZVector first, second; int n = 0;
00140 
00141    for (std::set<FWModelId>::const_iterator i = sted.begin(); i != sted.end(); ++i, ++n)
00142    {
00143       TString line;
00144 
00145       TClass *model_class = const_cast<TClass*>(i->item()->modelType());
00146       void   *model_data  = const_cast<void*>  (i->item()->modelData(i->index()));
00147 
00148       math::XYZVector v;
00149       bool            ok_p = false;
00150 
00151       reco::Candidate *rc = reinterpret_cast<reco::Candidate*>
00152          (model_class->DynamicCast(rc_class, model_data));
00153 
00154       if (rc != 0)
00155       {
00156          ok_p = true;
00157          v.SetXYZ(rc->px(), rc->py(), rc->pz());
00158       }
00159       else
00160       {
00161          reco::TrackBase *rtb = reinterpret_cast<reco::TrackBase*>
00162             (model_class->DynamicCast(rtb_class, model_data));
00163 
00164          if (rtb != 0)
00165          {
00166             ok_p = true;
00167             v.SetXYZ(rtb->px(), rtb->py(), rtb->pz());
00168          }
00169       }
00170 
00171       if (ok_p)
00172       {
00173          sum        += v;
00174          sum_len    += TMath::Sqrt(v.mag2());
00175          sum_len_xy += TMath::Sqrt(v.perp2());
00176 
00177          line = TString::Format("  %+10.3f  %+10.3f  %+10.3f  %10.3f", v.x(), v.y(), v.z(), TMath::Sqrt(v.perp2()));
00178 
00179       }
00180       else
00181       {
00182          line = TString::Format("  -------- not a Candidate or TrackBase --------");
00183       }
00184       line += TString::Format("  | %s[%d]", i->item()->name().c_str(), i->index());
00185 
00186       addLine(line);
00187 
00188       if (n == 0) first = v; else if (n == 1) second = v;
00189    }
00190 
00191    addLine("--------------------------------------------------+--------------");
00192    addLine(TString::Format("  %+10.3f  %+10.3f  %+10.3f  %10.3f  | Sum", sum.x(), sum.y(), sum.z(), TMath::Sqrt(sum.perp2())));
00193    addLine("");
00194    addLine(TString::Format("m  = %10.3f", TMath::Sqrt(TMath::Max(0.0, sum_len    * sum_len    - sum.mag2()))));
00195    addLine(TString::Format("mT = %10.3f", TMath::Sqrt(TMath::Max(0.0, sum_len_xy * sum_len_xy - sum.perp2()))));
00196    addLine(TString::Format("HT = %10.3f", sum_len_xy));
00197  
00198    if (n == 2) {
00199       addLine(TString::Format("deltaPhi  = %+6.4f", deltaPhi(first.Phi(), second.Phi())));
00200       addLine(TString::Format("deltaEta  = %+6.4f", first.Eta()- second.Eta()));
00201       addLine(TString::Format("deltaR    = % 6.4f", deltaR(first.Eta(), first.Phi(), second.Eta(), second.Phi())));
00202    }
00203 
00204    endUpdate();
00205 }
00206 
00207 //
00208 // const member functions
00209 //
00210 
00211 //
00212 // static member functions
00213 //