#include "THaSpectrometer.h"
#include "THaTrack.h"
#include "THaBeam.h"
#include "VarDef.h"
#include <iostream>
#include "TClass.h"
#include "TMath.h"
#include "TList.h"
#include "TROOT.h"
#include "THaString.h"
#include <map>
#include <cstdio>
#include <cstdlib>
using namespace std;
#include "ha_compiledata.h" //include for analyzer version check
#include "THaOpticsHRS.h"
THaOpticsHRS::THaOpticsHRS(const char* name, const char* desc, THaSpectrometer *pspec, TString strBeamDetectorName)
:THaOptics(name,desc,pspec,strBeamDetectorName)
{
DEBUG_LEVEL_RELATED_PERFORMACE_CHECKER;
DEBUG_HALL_A_ANALYZER_DEBUGER_INIT;
}
THaOpticsHRS::~THaOpticsHRS() {}
Int_t THaOpticsHRS::ReadDatabase(const TDatime& date)
{
THaOptics::ReadDatabase(date);
static const char* const here = "ReadDatabase";
const int LEN = 200;
char buff[LEN];
FILE* file = OpenFile( date );
if( !file ) return kFileError;
DEBUG_INFO(Here(here),"\tDB File open OK" );
TString tag("[matrix]");
TString line, tag2(tag);
tag.ToLower();
bool found = false;
while (!found && fgets (buff, LEN, file) != NULL) {
char* buf = ::Compress(buff);
line = buf;
delete [] buf;
if( line.EndsWith("\n") ) line.Chop();
line.ToLower();
if ( tag == line )
found = true;
}
if( !found ) {
Error(Here(here), "Database entry %s not found!", tag2.Data() );
fclose(file);
return kInitError;
}
fTMatrixElems.clear();
fDMatrixElems.clear();
fPMatrixElems.clear();
fPTAMatrixElems.clear();
fYMatrixElems.clear();
fYTAMatrixElems.clear();
fLMatrixElems.clear();
fFPMatrixElems.clear();
fFPMatrixElems.resize(3);
#if ANALYZER_VERSION_CODE<=ANALYZER_VERSION(1,5,0)
typedef vector<THaString>::size_type vsiz_t;
#else
typedef vector<string>::size_type vsiz_t;
#endif
map<string,vsiz_t> power;
power["t"] = 3;
power["y"] = 3;
power["p"] = 3;
power["D"] = 3;
power["T"] = 3;
power["Y"] = 3;
power["YTA"] = 4;
power["P"] = 3;
power["PTA"] = 4;
power["L"] = 4;
power["XF"] = 5;
power["TF"] = 5;
power["PF"] = 5;
power["YF"] = 5;
map<string,vector<THaMatrixElement>*> matrix_map;
matrix_map["t"] = &fFPMatrixElems;
matrix_map["y"] = &fFPMatrixElems;
matrix_map["p"] = &fFPMatrixElems;
matrix_map["D"] = &fDMatrixElems;
matrix_map["T"] = &fTMatrixElems;
matrix_map["Y"] = &fYMatrixElems;
matrix_map["YTA"] = &fYTAMatrixElems;
matrix_map["P"] = &fPMatrixElems;
matrix_map["PTA"] = &fPTAMatrixElems;
matrix_map["L"] = &fLMatrixElems;
map <string,int> fp_map;
fp_map["t"] = 0;
fp_map["y"] = 1;
fp_map["p"] = 2;
while( fgets(buff, LEN, file) ) {
#if ANALYZER_VERSION_CODE<=ANALYZER_VERSION(1,5,0)
THaString line(buff);
#else
string line(buff);
#endif
if( line.size() > 0 && line[line.size()-1] == '\n' ) {
buff[line.size()-1] = 0;
line.erase(line.size()-1,1);
}
#if ANALYZER_VERSION_CODE<=ANALYZER_VERSION(1,5,0)
vector<THaString> line_spl = line.Split();
#else
vector<string> line_spl = THaString::Split(line);
#endif
if(line_spl.empty())
continue;
const char* w = line_spl[0].c_str();
vsiz_t npow = power[w];
if( npow == 0 )
break;
THaMatrixElement ME;
ME.pw.resize(npow);
ME.iszero = true; ME.order = 0;
vsiz_t pos;
for (pos=1; pos<=npow && pos<line_spl.size(); pos++) {
ME.pw[pos-1] = atoi(line_spl[pos].c_str());
}
vsiz_t p_cnt;
for ( p_cnt=0; pos<line_spl.size() && p_cnt<kPORDER && pos<=npow+kPORDER;
pos++,p_cnt++ ) {
ME.poly[p_cnt] = atof(line_spl[pos].c_str());
if (ME.poly[p_cnt] != 0.0) {
ME.iszero = false;
ME.order = p_cnt+1;
}
}
if (p_cnt < 1) {
Error(Here(here), "Could not read in Matrix Element %s%d%d%d!",
w, ME.pw[0], ME.pw[1], ME.pw[2]);
Error(Here(here), "Line looks like: %s",line.c_str());
fclose(file);
return kInitError;
}
if( ME.iszero )
continue;
vector<THaMatrixElement> *mat = matrix_map[w];
if (mat) {
if( mat == &fFPMatrixElems ) {
if( ME.pw[0] == 0 && ME.pw[1] == 0 && ME.pw[2] == 0 ) {
THaMatrixElement& m = (*mat)[fp_map[w]];
if( m.order > 0 ) {
Warning(Here(here), "Duplicate definition of focal plane "
"matrix element: %s. Using first definition.", buff);
} else
m = ME;
} else
Warning(Here(here), "Bad coefficients of focal plane matrix "
"element %s", buff);
}
else {
bool match = false;
for( vector<THaMatrixElement>::iterator it = mat->begin();
it != mat->end() && !(match = it->match(ME)); it++ );
if( match ) {
Warning(Here(here), "Duplicate definition of "
"matrix element: %s. Using first definition.", buff);
} else
mat->push_back(ME);
}
}
else if ( fDebug > 0 )
Warning(Here(here), "Not storing matrix for: %s !", w);
}
CalcMatrix(1.,fLMatrixElems);
fclose(file);
return kOK;
}
Int_t THaOpticsHRS::ApplyOptics
(
Double_t trackX,
Double_t trackY,
Double_t trackTheta,
Double_t trackPhi,
TVector3 beamPos,
TVector3 beamDir,
TVector3 &P,
TVector3 &Vertex,
Double_t &TargetX,
Double_t &TargetY,
Double_t &TargetTheta,
Double_t &TargetPhi,
Double_t &PathLen
)
{
const Int_t kNUM_PRECOMP_POW = 10;
Double_t x_fp, y_fp, th_fp, ph_fp;
Double_t powers[kNUM_PRECOMP_POW][5];
Double_t x, y, theta, phi, dp, p, pathl;
x_fp = trackX;
y_fp = trackY;
th_fp =trackTheta;
ph_fp =trackPhi;
for(int i=0; i<kNUM_PRECOMP_POW; i++) {
powers[i][0] = pow(x_fp, i);
powers[i][1] = pow(th_fp, i);
powers[i][2] = pow(y_fp, i);
powers[i][3] = pow(ph_fp, i);
powers[i][4] = pow(TMath::Abs(th_fp),i);
}
CalcMatrix(x_fp, fDMatrixElems);
CalcMatrix(x_fp, fTMatrixElems);
CalcMatrix(x_fp, fYMatrixElems);
CalcMatrix(x_fp, fYTAMatrixElems);
CalcMatrix(x_fp, fPMatrixElems);
CalcMatrix(x_fp, fPTAMatrixElems);
theta = CalcTargetVar(fTMatrixElems, powers);
phi = CalcTargetVar(fPMatrixElems, powers)+CalcTargetVar(fPTAMatrixElems,powers);
y = CalcTargetVar(fYMatrixElems, powers)+CalcTargetVar(fYTAMatrixElems,powers);
dp = CalcTargetVar(fDMatrixElems, powers);
p = GetPcentral() * (1.0+dp);
pathl = CalcTarget2FPLen(fLMatrixElems, powers);
x = 0.0;
TargetX=x;
TargetY=y;
TargetTheta=theta;
TargetPhi=phi;
TOpticsDir vp(TOpticsDir::kTCS,this,theta,phi,1);
vp.Unit();
vp=vp*p;
vp.TransCoordSys(TOpticsDir::kHCS);
P=vp.GetVector();
PathLen=pathl;
return kOK;
};
void THaOpticsHRS::CalcMatrix( const Double_t x, vector<THaMatrixElement>& matrix )
{
for( vector<THaMatrixElement>::iterator it=matrix.begin();
it!=matrix.end(); it++ ) {
it->v = 0.0;
if(it->order > 0) {
for(int i=it->order-1; i>=1; i--)
it->v = x * (it->v + it->poly[i]);
it->v += it->poly[0];
}
}
}
Double_t THaOpticsHRS::CalcTargetVar(const vector<THaMatrixElement>& matrix,
const Double_t powers[][5])
{
Double_t retval=0.0;
Double_t v=0;
for( vector<THaMatrixElement>::const_iterator it=matrix.begin();
it!=matrix.end(); it++ )
if(it->v != 0.0) {
v = it->v;
unsigned int np = it->pw.size();
for (unsigned int i=0; i<np; i++)
v *= powers[it->pw[i]][i+1];
retval += v;
}
return retval;
}
Double_t THaOpticsHRS::CalcTarget2FPLen(const vector<THaMatrixElement>& matrix,
const Double_t powers[][5])
{
Double_t retval=0.0;
for( vector<THaMatrixElement>::const_iterator it=matrix.begin();
it!=matrix.end(); it++ )
if(it->v != 0.0)
retval += it->v * powers[it->pw[0]][0]
* powers[it->pw[1]][1]
* powers[it->pw[2]][2]
* powers[it->pw[3]][3];
return retval;
}
ClassImp(THaOpticsHRS);
Last update: Tue Jul 7 19:26:17 2009
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.