00001 #include "DataUtil/infid.h"
00002
00003 #if !defined(__CINT__) || defined(__MAKECINT__)
00004 #include <iostream>
00005 #include <cassert>
00006
00007 #include "RVersion.h"
00008 #include "TInterpreter.h"
00009 #include "TMath.h"
00010 #include "TROOT.h"
00011 #include "TSystem.h"
00012 #include "TString.h"
00013 #endif
00014
00015 namespace FidVol {
00016
00017
00018
00019 static std::string gName = "Default";
00020
00021
00022 static bool gNearFollowBeam = true;
00023 static double gNearR = 1.0;
00024
00025
00026 static double gNearZData[2] = { 1.01080, 4.99059 };
00027 static double gNearZMC[2] = { 1.01080, 4.99059 };
00028
00029
00030 static double gBeamAngleRad = 3.34321 * TMath::DegToRad();
00031 static double gNearDyDz = TMath::Tan(-gBeamAngleRad);
00032 static double gNearX0Beam = 1.4828;
00033 static double gNearY0Beam = 0.2384;
00034
00035
00036 static double gNearX0Z = 1.4885;
00037 static double gNearY0Z = 0.1397;
00038
00039
00040 static bool gFarOctagon = false;
00041 static bool gFarCoilCut = true;
00042 static double gFarRinner = 0.5;
00043 static double gFarRouter = TMath::Sqrt(14.0);
00044
00045
00046 static double gFarZData[4] = { 0.49080, 14.29300, 16.27110, 27.98270};
00047 static double gFarZMC[4] = { 0.47692, 14.27860, 16.26470, 27.97240};
00048
00049
00050
00051
00052
00053
00054 static double gEvtVtxZOffset=0.0;
00055 static double gTrkVtxZOffset=0.0392;
00056 static double gShwVtxZOffset=0.0;
00057
00058
00059
00060 const double r_sqrt2 = 7.07106781186547462e-01;
00061
00062
00063 bool infid_near_z(SimFlag::SimFlag_t simflg, double z);
00064 bool infid_far_z(SimFlag::SimFlag_t simflg, double z);
00065 bool infid_near_circle_z(double x, double y);
00066 bool infid_near_circle_beam(double x, double y, double z);
00067 bool infid_far_coil(double x, double y);
00068 bool infid_far_circle(double x, double y);
00069 bool infid_far_octagon(double x, double y);
00070
00071 bool load_setter_script(std::string name);
00072 bool know_setter(std::string funcname);
00073
00074 bool legal_indx(int indx, int size) { return (indx>=0 && indx<size); }
00075
00076 }
00077
00079
00080
00081
00082
00083
00084 bool infid(Detector::Detector_t det, SimFlag::SimFlag_t simflg,
00085 double x, double y, double z)
00086 {
00087 if ( Detector::kNear == det ) {
00088
00089 if ( ! FidVol::infid_near_z(simflg,z) ) return false;
00090
00091 if ( FidVol::gNearFollowBeam ) return FidVol::infid_near_circle_beam(x,y,z);
00092 else return FidVol::infid_near_circle_z(x,y);
00093 }
00094 else if ( Detector::kFar == det ) {
00095
00096 if ( ! FidVol::infid_far_z(simflg,z) ) return false;
00097
00098 if ( FidVol::gFarCoilCut && ! FidVol::infid_far_coil(x,y) ) return false;
00099
00100 if ( FidVol::gFarOctagon ) return FidVol::infid_far_octagon(x,y);
00101 else return FidVol::infid_far_circle(x,y);
00102 }
00103
00104 return true;
00105 }
00106
00107
00108
00109 void print_infid()
00110 {
00111 std::cout << "Current infid settings: \"" << FidVol::gName
00112 << "\"" << std::endl;
00113 std::cout << "Near:";
00114 std::cout << " cylinder radius " << FidVol::gNearR;
00115 if ( FidVol::gNearFollowBeam ) {
00116 std::cout << ", x0=" << FidVol::gNearX0Beam
00117 << ", y0=" << FidVol::gNearY0Beam
00118 << ", follow beam" << std::endl;
00119 std::cout << " dy/dz " << FidVol::gNearDyDz
00120 << " (angle " << FidVol::gBeamAngleRad << " radians)"
00121 << std::endl;
00122 } else {
00123 std::cout << ", x0=" << FidVol::gNearX0Z
00124 << ", y0=" << FidVol::gNearY0Z
00125 << ", along z axis" << std::endl;
00126 }
00127 std::cout << " z limits data: "
00128 << Form("%8.5f",FidVol::gNearZData[0]) << " "
00129 << Form("%8.5f",FidVol::gNearZData[1]) << std::endl
00130 << " MC: "
00131 << Form("%8.5f",FidVol::gNearZMC[0]) << " "
00132 << Form("%8.5f",FidVol::gNearZMC[1]) << std::endl;
00133
00134 std::cout << "Far:";
00135 std::cout << " " << (FidVol::gFarOctagon?"octagon inscribed":"circle")
00136 << " radius " << FidVol::gFarRouter << ", ";
00137 if ( FidVol::gFarCoilCut )
00138 std::cout << "coil cut " << FidVol::gFarRinner;
00139 else
00140 std::cout << "no coil cut";
00141 std::cout << std::endl;
00142 std::cout << " z limits data: "
00143 << Form("%8.5f",FidVol::gFarZData[0]) << " "
00144 << Form("%8.5f",FidVol::gFarZData[1]) << " "
00145 << Form("%8.5f",FidVol::gFarZData[2]) << " "
00146 << Form("%8.5f",FidVol::gFarZData[3]) << std::endl
00147 << " MC: "
00148 << Form("%8.5f",FidVol::gFarZMC[0]) << " "
00149 << Form("%8.5f",FidVol::gFarZMC[1]) << " "
00150 << Form("%8.5f",FidVol::gFarZMC[2]) << " "
00151 << Form("%8.5f",FidVol::gFarZMC[3]) << std::endl;
00152
00153 std::cout << "Evt/Trk/Shw vertex offsets:"
00154 << " " << Form("%8.5f",FidVol::gEvtVtxZOffset)
00155 << "/" << Form("%8.5f",FidVol::gTrkVtxZOffset)
00156 << "/" << Form("%8.5f",FidVol::gShwVtxZOffset)
00157 << std::endl;
00158
00159 }
00160
00161 bool FidVol::load_setter_script(std::string script)
00162 {
00163 const char* paths =
00164 ".:~:$SRT_PRIVATE_CONTEXT/DataUtil:$SRT_PUBLIC_CONTEXT/DataUtil";
00165
00166 if ( script.find(".C") == std::string::npos ) script += ".C";
00167 const char* filename = gSystem->Which(paths,script.c_str());
00168 if ( filename ) {
00169
00170 int errcode = gROOT->LoadMacro(filename);
00171
00172 delete [] filename;
00173 if ( errcode == 0 ) {
00174
00175 return true;
00176 } else {
00177 std::cout << "failed loading " << script
00178 << ", status = " << filename << std::endl;
00179 }
00180 }
00181 return false;
00182 }
00183
00184 bool FidVol::know_setter(std::string funcname)
00185 {
00186
00187 TInterpreter* interp = gROOT->GetInterpreter();
00188 void* func = interp->GetInterfaceMethod(0,funcname.c_str(),"");
00189 return ( (func) ? true : false );
00190 }
00191
00192 void choose_infid_set(std::string setname, bool doassert)
00193 {
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 static bool loadGeneric = FidVol::load_setter_script("infid_sets.C");
00210 if ( ! loadGeneric ) {
00211 std::cout << "failed to find \"infid_sets.C\"" << std::endl;
00212 }
00213
00214 const char* patterns[] = { "infid_set_%s", "%s" };
00215 int npatterns = sizeof(patterns)/sizeof(const char*);
00216 bool done = false;
00217
00218 for (int i = 0; i < npatterns; ++i) {
00219 std::string funcname = Form(patterns[i],setname.c_str());
00220 if ( ! FidVol::know_setter(funcname) ) {
00221
00222 FidVol::load_setter_script(funcname);
00223 }
00224 if ( FidVol::know_setter(funcname) ) {
00225 funcname += "();";
00226 int psuccess = 0;
00227 gROOT->ProcessLine(funcname.c_str(),&psuccess);
00228 if ( psuccess == 0 ) {
00229 std::cout << "Successfully ran function \"" << funcname
00230 << "\" for set \"" << setname << "\"" << std::endl;
00231 done = true;
00232 break;
00233 } else {
00234 std::cout << "Failed running macro \"" << funcname << "\" for set \""
00235 << setname << "\"" << std::endl;
00236 if (doassert) assert(0);
00237 }
00238 } else {
00239 std::cout << "Found no setter function " << funcname
00240 << "() for \"" << setname << "\"" << std::endl;
00241 }
00242 }
00243 if ( ! done ) {
00244 if ( doassert ) assert(0);
00245 else {
00246 std::cout << "choose_infid_set(\"" << setname << "\") FAILED"
00247 << ", but user chose not to assert()" << std::endl;
00248 }
00249 }
00250 print_infid();
00251 }
00252
00253
00254
00255
00256 bool FidVol::infid_near_z(SimFlag::SimFlag_t simflg, double z)
00257 {
00258 double* zcuts = gNearZData;
00259 if ( SimFlag::kMC == simflg ) zcuts = gNearZMC;
00260 if ( z < zcuts[0] ) return false;
00261 if ( z > zcuts[1] ) return false;
00262 return true;
00263 }
00264
00265 bool FidVol::infid_far_z(SimFlag::SimFlag_t simflg, double z)
00266 {
00267 double* zcuts = gFarZData;
00268 if ( SimFlag::kMC == simflg ) zcuts = gFarZMC;
00269 if ( z < zcuts[0] ) return false;
00270 if ( z > zcuts[3] ) return false;
00271 if ( z > zcuts[1] && z < zcuts[2] ) return false;
00272 return true;
00273 }
00274
00275 double FidVol::infid_near_radius(double x, double y, double z)
00276 {
00277 double xx = x, yy = y;
00278 if ( gNearFollowBeam ) {
00279 xx = xx - gNearX0Beam;
00280 yy = yy - gNearY0Beam - z*gNearDyDz;
00281 } else {
00282 xx = xx - gNearX0Z;
00283 yy = yy - gNearY0Z;
00284 }
00285 double r2 = xx*xx + yy*yy;
00286 return TMath::Sqrt(r2);
00287 }
00288
00289 bool FidVol::infid_near_circle_z(double x, double y)
00290 {
00291 double xx = x - gNearX0Z;
00292 double yy = y - gNearY0Z;
00293 double r2 = xx*xx + yy*yy;
00294 if ( r2 > gNearR*gNearR ) return false;
00295 return true;
00296 }
00297
00298 bool FidVol::infid_near_circle_beam(double x, double y, double z)
00299 {
00300 double xx = x - gNearX0Beam;
00301 double yy = y - gNearY0Beam - z*gNearDyDz;
00302 double r2 = xx*xx + yy*yy;
00303 if ( r2 > gNearR*gNearR ) return false;
00304 return true;
00305 }
00306
00307 bool FidVol::infid_far_coil(double x, double y)
00308 {
00309 double r2 = x*x + y*y;
00310 if ( r2 < gFarRinner*gFarRinner ) return false;
00311 return true;
00312 }
00313
00314 bool FidVol::infid_far_circle(double x, double y)
00315 {
00316 double r2 = x*x + y*y;
00317 if ( r2 > gFarRouter*gFarRouter ) return false;
00318 return true;
00319 }
00320
00321 bool FidVol::infid_far_octagon(double x, double y)
00322 {
00323 if ( TMath::Abs(x) > gFarRouter ) return false;
00324 if ( TMath::Abs(y) > gFarRouter ) return false;
00325
00326 double u = ( x + y ) * r_sqrt2;
00327 double v = ( -x + y ) * r_sqrt2;
00328
00329 if ( TMath::Abs(u) > gFarRouter ) return false;
00330 if ( TMath::Abs(v) > gFarRouter ) return false;
00331
00332 return true;
00333 }
00334
00336
00337 std::string FidVol::getName() { return gName; }
00338 void FidVol::setName(std::string name) { gName = name; }
00339
00340 bool FidVol::getNearFollowBeam() { return gNearFollowBeam; }
00341 void FidVol::setNearFollowBeam(bool follow) { gNearFollowBeam = follow; }
00342 double FidVol::getNearR() { return gNearR; }
00343 void FidVol::setNearR(double r) { gNearR = r; }
00344 double FidVol::getNearZData(int indx)
00345 { if (!legal_indx(indx,2)) assert(0); return gNearZData[indx]; }
00346 void FidVol::setNearZData(int indx, double z)
00347 { if (!legal_indx(indx,2)) assert(0); gNearZData[indx] = z; }
00348 double FidVol::getNearZMC(int indx)
00349 { if (!legal_indx(indx,2)) assert(0); return gNearZMC[indx]; }
00350 void FidVol::setNearZMC(int indx, double z)
00351 { if (!legal_indx(indx,2)) assert(0); gNearZMC[indx] = z; }
00352 double FidVol::getBeamAngleRad() { return gBeamAngleRad; }
00353 void FidVol::setBeamAngleRad(double angle)
00354 { gBeamAngleRad = angle; gNearDyDz = TMath::Tan(-angle); }
00355 double FidVol::getNearDyDz() { return gNearDyDz; }
00356 void FidVol::setNearDyDz(double dydz)
00357 { gNearDyDz = dydz; gBeamAngleRad = -TMath::ATan(dydz); }
00358 double FidVol::getNearX0Beam() { return gNearX0Beam; }
00359 void FidVol::setNearX0Beam(double x0) { gNearX0Beam = x0; }
00360 double FidVol::getNearY0Beam() { return gNearY0Beam; }
00361 void FidVol::setNearY0Beam(double y0) { gNearY0Beam = y0; }
00362 double FidVol::getNearX0Z() { return gNearX0Z; }
00363 void FidVol::setNearX0Z(double x0) { gNearX0Z = x0; }
00364 double FidVol::getNearY0Z() { return gNearY0Z; }
00365 void FidVol::setNearY0Z(double y0) { gNearY0Z = y0; }
00366
00367 bool FidVol::getFarOctagon() { return gFarOctagon; }
00368 void FidVol::setFarOctagon(bool octagon) { gFarOctagon = octagon; }
00369 bool FidVol::getFarCoilCut() { return gFarCoilCut; }
00370 void FidVol::setFarCoilCut(bool coilcut) { gFarCoilCut = coilcut; }
00371 double FidVol::getFarRinner() { return gFarRinner; }
00372 void FidVol::setFarRinner(double r) { gFarRinner = r; }
00373 double FidVol::getFarRouter() { return gFarRouter; }
00374 void FidVol::setFarRouter(double r) { gFarRouter = r; }
00375 double FidVol::getFarZData(int indx)
00376 { if (!legal_indx(indx,4)) assert(0); return gFarZData[indx]; }
00377 void FidVol::setFarZData(int indx, double z)
00378 { if (!legal_indx(indx,4)) assert(0); gFarZData[indx] = z; }
00379 double FidVol::getFarZMC(int indx)
00380 { if (!legal_indx(indx,4)) assert(0); return gFarZMC[indx]; }
00381 void FidVol::setFarZMC(int indx, double z)
00382 { if (!legal_indx(indx,4)) assert(0); gFarZMC[indx] = z; }
00383
00384
00385 double FidVol::getEvtVtxZOffset() { return gEvtVtxZOffset; }
00386 void FidVol::setEvtVtxZOffset(double zoff) { gEvtVtxZOffset = zoff; }
00387 double FidVol::getTrkVtxZOffset() { return gTrkVtxZOffset; }
00388 void FidVol::setTrkVtxZOffset(double zoff) { gTrkVtxZOffset = zoff; }
00389 double FidVol::getShwVtxZOffset() { return gShwVtxZOffset; }
00390 void FidVol::setShwVtxZOffset(double zoff) { gShwVtxZOffset = zoff; }