// -*- mode: c++ -*- 
#ifndef BRAT_BrTofPidModule
#define BRAT_BrTofPidModule
// $Id: BrTofPidModule.h,v 1.18 2002/06/13 20:00:12 videbaek Exp $
// $Author: videbaek $
// $Date: 2002/06/13 20:00:12 $
//
// Tof reconstruction Module
//
///////////////////////////////////////////////////////////////////////
//                                                                   //
//    BrTofPidModule                                                 //
//                                                                   //
//    BRAHMS Tof analysis class                                      //
//                                                                   //
//    Evaluate PID, mass2 from TOF Rdo and matching track info       //
//                                                                   //
//    Author  : D. Ouerdane                                          //
//    Created : April 2001                                           //
//    Version : 1.0                                                  //
//                                                                   //
///////////////////////////////////////////////////////////////////////

#ifndef ROOT_TH1
#include "TH1.h"
#endif
#ifndef ROOT_TH2
#include "TH2.h"
#endif
#ifndef ROOT_TProfile
#include "TProfile.h"
#endif
#ifndef ROOT_TNtuple
#include "TNtuple.h"
#endif
#ifndef ROOT_TString
#include "TString.h"
#endif
#ifndef BRAT_BrModule
#include "BrModule.h"
#endif
#ifndef BRAT_BrEventNode
#include <BrEventNode.h>
#endif
#ifndef BRAT_BrDetectorParamsTof
#include "BrDetectorParamsTof.h"
#endif
#ifndef BRAT_BrDetectorVolume
#include "BrDetectorVolume.h"
#endif

class BrTofPidModule : public BrModule 
{

private:

  Double_t fMaxDY;         // diff between hit pos Y and track proj Y
  Double_t fTrkCut[3];     // cuts in X, Y, Z around primary vertex
  Double_t fTrkOffset[3];  // track offsets to primary vtx

  Double_t fPScale;    // scale factor for momentum
  Double_t fDist;      // nominal distance for fixed TOF

  Bool_t   fNtuple;  // switch on/off ntuple
  Bool_t   fInMrs;
  Bool_t   fInFfs;
  Bool_t   fInBfs;
  Bool_t   fFsLined;
  Bool_t   fUseBbVertex;
  Bool_t   fUseTd1Time;
  Bool_t   fUseTMrsTime;
  TString  fMatchName;

  BrDetectorParamsTof* fTofPar;
  // temp!
  BrDetectorVolume**   fPanelVol;

  Float_t fPCut[6];

  // histograms
  TNtuple*  fPidTree;       // correlation between pid variables

  TH2F*     fRoughTvVtx;    // tof vs vertex
  TH2F*     fRoughPtY;      // Pt vs Y
  TH2F*     fRoughTvDE;     // tof vs dE
  TH2F*     fRoughPid;      // momentum vs tof for all tracks 
  TH2F*     fRoughPvBeta;   // beta vs momentum
  TH2F*     fRoughDEvP;     // dE vs momentum
  TH2F*     fRoughYvX;      // track vertex Y vs track vertex X for all
  TH2F*     fRoughYvZ;      // track vertex Y vs track vertex Z - BB Z0 (for MRS)
  TH1F*     fRoughM2[6];    // mass squared with 6 momentum cuts
  TH2F*     fRoughM2vP;     // mass squared vs  momentum
  TH1F*     fRoughBeta;     // beta distribution
  TH1F*     fRoughTof;      // time of flight corrected for vertex
  TH2F*     fRoughDTrkTof;  // difference Track - Hit in X,Y plane of tof
  TH1F*     fRoughLen;      // track length (cm)
  TH1F*     fRoughMom;      // track momentum (GeV/c)
  TH1F*     fRoughEta;      // track pseudo-rapidity eta 
  TH2F*     fRoughDy;       // difference in Y yTrack - yHit vs slat
  TH1F*     fRoughPartStat; // rough particle stat
  TH1F*     fRoughAccVtx;   // primary vertices  

  TH2F*     fCleanTvVtx;    // tof vs vertex
  TH2F*     fCleanPtY;      // Pt vs Y
  TH2F*     fCleanTvDE;     // tof vs dE
  TH2F*     fCleanPid;      // same but with cuts on vertices
  TH2F*     fCleanPvBeta;   // beta vs momentum
  TH2F*     fCleanDEvP;     // dE vs momentum
  TH2F*     fCleanYvX;      // track vertex Y vs track vertex X for all
  TH2F*     fCleanYvZ;      // track vertex Y vs track vertex Z - BB Z0 (for MRS)
  TH1F*     fCleanM2[6];    // mass squared with 6 momentum cuts
  TH2F*     fCleanM2vP;     // mass squared vs  momentum
  TH1F*     fCleanBeta;     // beta distribution
  TH1F*     fCleanTof;      // time of flight corrected for vertex
  TH2F*     fCleanDTrkTof;  // difference Track - Hit in X,Y plane of tof
  TH1F*     fCleanLen;      // track length (cm)
  TH1F*     fCleanMom;      // track momentum (GeV/c)
  TH1F*     fCleanEta;      // track pseudo-rapidity eta 
  TH2F*     fCleanDy;       // difference in Y yTrack - yHit vs slat
  TH1F*     fCleanPartStat; // clean particle stat
  TH1F*     fCleanAccVtx;   // primary vertices 

  Int_t     fAccEvt;        // events with identified particles
  Int_t     fSeqNo;         // sequence number
  Float_t   fNoEvent;       // number of events analyzed per sequence
  Float_t   fNoCleanPart;   // number of particles within cuts
  Float_t   fNoRoughPart;   // number of particles within or without cuts
  
  Float_t   fT0offset;      // offset (for different timings e.g. pp, ZDC)
  
  void      CheckFsAngles();
  Bool_t    IsWithinCuts(Float_t, Float_t, Int_t, Int_t);
  Int_t     CheckTrigger(BrEventNode*);
  
public:
  //  constructors and destructors
  //
  BrTofPidModule();
  BrTofPidModule(const Char_t *name, const Char_t *title);
  virtual ~BrTofPidModule();

  //
  //  Methods
  //

  void SetDefaultParameters();

  virtual void Init();
  virtual void Begin();
  virtual void DefineHistograms();
  virtual void Event(BrEventNode*, BrEventNode*);
  virtual void End();
  virtual void Finish();

  void SetMaxDY (Double_t d = 1)        { fMaxDY  = d; }
  void SetNtuple(Bool_t n = kFALSE)     { fNtuple  = n; }

  void SetTrkOffsets(Double_t x = 0, Double_t y = 0, Double_t z = 0) {
    fTrkOffset[0] = x; fTrkOffset[1] = y; fTrkOffset[2] = z;
  }
  void SetTrkCuts(Double_t x = 10000, Double_t y = 10000, Double_t z = 10000) {
    fTrkCut[0] = x; fTrkCut[1] = y; fTrkCut[2] = z;
  }
  void SetUseBbVertex(Bool_t b = kTRUE) {fUseBbVertex = b;}
  void SetUseTd1Time(Bool_t b = kFALSE) {fUseTd1Time=b;}
  void SetUseTMrsTime(Bool_t b = kFALSE) {fUseTMrsTime=b;}
  void SetPScale(Double_t s = 1.) { fPScale = s; } 
  void SetT0offset(Double_t d=0.0) {fT0offset=d;}

  void Print(Option_t* option="B") const;


  ClassDef(BrTofPidModule, 0) // BRAHMS Tof hit recontruction module


};
    
#endif
    
// $Log: BrTofPidModule.h,v $
// Revision 1.18  2002/06/13 20:00:12  videbaek
// Fix a typo.
// Change fPcut to be floats so one can have p<1 cuts for Mrs.
//
// Revision 1.17  2002/06/13 18:54:31  videbaek
// Add code to deal with TMrsF t0 startcounter for pp running.
// Modify default length to 433.5 cm for MRS. Change some of summary plots
// to have differential cuts on Mass2 rather than integral.
//
// Revision 1.16  2002/04/09 02:03:57  ouerdane
// use BrFsTrack instead of BrBfsTrack for H2 PID
//
// Revision 1.15  2002/03/20 19:39:41  videbaek
// Module now work with Td1 as start counter in addition to BB.
// A seperate timing offset is needed for TD1, albeit if one creates
// new slewings for pp I suspect this will be aligned properly to the
// different path length (680 cm).
//
// Revision 1.14  2002/01/31 17:10:40  ouerdane
// added event trigger number in ntuple
//
// Revision 1.13  2001/12/13 13:38:39  ouerdane
// cleaned module a lot, separeted histograms within or outside 
// cuts, fill pid tables with every track-tof matches. 
// The cuts are only for histograms
//
// Revision 1.12  2001/11/07 10:32:35  ouerdane
// updated matching module for H2 PID
//
// Revision 1.11  2001/11/05 07:05:20  ouerdane
// changed a lot of things to deal with new track classes and BFS Pid
//
// Revision 1.10  2001/10/19 15:30:54  ouerdane
// Added member fDist to fixe a path length for tof determination in PID plots
// Fixed a couple of things in histograms and track length 
// (added or substracted a piece of length depending on the slat pos. in TOF1)
//
// Still a couple of things to be fixed but coming soon.
//
// Revision 1.9  2001/10/03 18:31:05  ouerdane
// added more histograms for mass2
//
// Revision 1.8  2001/10/03 17:29:50  ouerdane
// minor error in the track offsets setting
//
// Revision 1.7  2001/10/03 17:19:19  ouerdane
// added a scale factor for the momentum for testing
//
// Revision 1.6  2001/10/02 20:19:34  ouerdane
// remove member fNoPanels and setter method since it is now part of the
// tof detector parameter class.
//
// Revision 1.5  2001/10/02 01:55:31  ouerdane
// Updated the tof pid according to the tof track matching and 
// primary vertex (cf email)
//
// Revision 1.4  2001/08/19 15:58:27  ouerdane
// added event statistics check and vtx histogram for accepted events
//
// Revision 1.3  2001/07/24 14:41:16  ouerdane
// Removed TList private member and added histograms as private members
// (gain of time at run time)
// Added a method public SetNtuple(Bool_t) , default is false
// (an ntuple takes more and more space as the programs is running)
//
// Revision 1.2  2001/06/29 13:51:57  cholm
// Imported TOF classes from Djamel
//
// Revision 1.1  2001/06/19 12:53:10  ouerdane
// Added class BrTofPidModule
// It gets global tracks and reconstructed tof hits (BrTofRdo)
// Match them and deduce a pid (still preliminary).
//
// This class was put in brat for the transition to brat 2
// but has not been tested in its present form (although it compiles
// with brat). Don't use if you don't have a valid tof calibration
// (cf. BrTofRdoModule)
//