// $Header: CalRecon.h $

#ifndef NLD_CALRECON_H
#define NLD_CALRECON_H

#include "RecModule.h"  // includes stdio, Event.h, TObject
#include "TMath.h"
#include "TRandom.h"
#include "McPart.h"
#include "SmearVolume.h"
#include "SmearTraj.h"

// #include "CalDetect.h"
// #include "CalSmearPar.h"

//=========================================================
class CalRecon : public RecModule {
public:
  CalRecon(const TString&  name = "Small");
  ~CalRecon() {};
  // Implicit destructor
  // Implicit copy constructor
  // Implicit assignment operator
  void cleanup();
  void doit(Event* event);
  void spew(FILE* ofile)const;
  // read from/write to an open stream

  TObjArray* GetSmears(){return smears;}


private:
  Int_t       nPart;         // Total number of MC particles in event
  TObjArray*  MCparticles;   // Private pointer to event particles
  TObjArray*  smears;        // "Clusters" -- actually of class CalSmeared 
  // Ultimately should learn how to use TCloneArray for smears


  static Double_t RanExpDecay(TRandom *pRand, Double_t mean = 1.0)
    {return -TMath::Log(pRand->Rndm()) * mean;}

  void GetSmall();
  void GetLarge();

  SmearVolume*   Swim(SmearTraj* pTraj, Bool_t hadron);

  // Field is characterized by two values and two radii.  
  // For r = sqrt(x**2 + y**2), field strength is fieldInner, 0 or
  // fieldOuter for  
  //         r < inner radius of coil
  //         inner radius < r < outer radius of coil
  //         outer radius of coil < r,             resp.
  //  Double_t    fieldInner;
  //  Double_t    fieldOuter;
  //  Double_t    fieldInnerR;   // inner radius of coil
  //  Double_t    fieldOuterR;   // outer radius of coil
  // Detector info

  Int_t  Smearable(McPart*);
  Int_t  GetDetector(const TString& name);    // sets up nVolumes, pVolumes
  // void   InitSmearPar();   For now part of GetDetector
 
  TRandom     *pRand;  // Use Root for randoms, gaussian or uniform

  // Assume all relevant volumes are either barrels or endcaps, in any
  // case cylinders

  SmearVolume* pVolumes;       // Array of detector volumes
  SmearVolume* pInnerVolume;   // Innermost volume about IP
  Int_t        nVolumes;       // Number of detector volumes in array

public:
  ClassDef(CalRecon,0)    // for Cint dictionary
};

// energy smearing is done according to the formula
//   sigma = E-perfect * sqrt(a**2/E-perfect + b**2)
// Magnitude of transverse position smear is
//   sigma_x = sqrt(a**2/E-perfect + b**2)
//   phi (with axis = momentum angle) is uniformly distributed in (0, 2pi)
#endif
