#ifndef ALIITSUAUX
#define ALIITSUAUX
#include <TObject.h>
#include <TMath.h>
#define _ITSU_TUNING_MODE_
class AliITSUGeomTGeo;
class AliITSsegmentation;
using namespace TMath;
namespace AliITSUAux {
template<typename F>
void BringTo02Pi(F &phi);
Bool_t OKforPhiMin(double phiMin,double phi);
Bool_t OKforPhiMax(double phiMax,double phi);
Double_t MeanPhiSmall(double phi0, double phi1);
Double_t DeltaPhiSmall(double phi0, double phi1);
UInt_t PackCluster(Int_t lr, Int_t clID);
Int_t UnpackCluster(UInt_t p, Int_t &lr);
Int_t UnpackLayer(UInt_t p);
Int_t UnpackCluster(UInt_t p);
Bool_t IsCluster(UInt_t p);
Int_t NumberOfBitsSet(UInt_t x);
void PrintBits(ULong64_t patt, Int_t maxBits);
const Double_t kNominalBz = 5.01;
const Double_t kPionMass = 1.3957e-01;
const UInt_t kLrBitLow = 28;
const UInt_t kLrMask = 0xf0000000;
const UInt_t kClMask = 0x0fffffff;
const UInt_t kMaxLayers = 15;
const UInt_t kMaxLrMask = 0x7fff;
}
template<typename F>
inline void AliITSUAux::BringTo02Pi(F &phi) {
if (phi<0) phi+=TwoPi(); else if (phi>TwoPi()) phi-=TwoPi();
}
inline Bool_t AliITSUAux::OKforPhiMin(double phiMin,double phi) {
double dphi = phi-phiMin;
return ((dphi>0 && dphi<Pi()) || dphi<-Pi()) ? kTRUE:kFALSE;
}
inline Bool_t AliITSUAux::OKforPhiMax(double phiMax,double phi) {
double dphi = phi-phiMax;
return ((dphi<0 && dphi>-Pi()) || dphi>Pi()) ? kTRUE:kFALSE;
}
inline UInt_t AliITSUAux::PackCluster(Int_t lr, Int_t clID) {
UInt_t p = (clID<0 ? 0 : clID+1) + (lr<<=kLrBitLow);
return p;
}
inline Int_t AliITSUAux::UnpackCluster(UInt_t p, Int_t &lr) {
lr = (p&kLrMask)>>kLrBitLow;
p &= kClMask;
return int(p)-1;
}
inline Int_t AliITSUAux::UnpackLayer(UInt_t p) {
return (p&kLrMask)>>kLrBitLow;
}
inline Int_t AliITSUAux::UnpackCluster(UInt_t p) {
return int(p&kClMask)-1;
}
inline Bool_t AliITSUAux::IsCluster(UInt_t p) {
return (p&kClMask);
}
inline Int_t AliITSUAux::NumberOfBitsSet(UInt_t x) {
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
return (((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
inline Double_t AliITSUAux::MeanPhiSmall(double phi0, double phi1) {
double phi;
if (!OKforPhiMin(phi0,phi1)) {phi=phi0; phi0=phi1; phi1=phi;}
if (phi0>phi1) phi = (phi1 - (TwoPi()-phi0))/2;
else phi = (phi0+phi1)/2;
BringTo02Pi(phi);
return phi;
}
inline Double_t AliITSUAux::DeltaPhiSmall(double phi0, double phi1) {
double del;
if (!OKforPhiMin(phi0,phi1)) {del=phi0; phi0=phi1; phi1=del;}
del = phi1 - phi0;
if (del<0) del += TwoPi();
return del;
}
#endif