00001
00006
00007
00008 #include "GaudiKernel/Algorithm.h"
00009 #include "GaudiKernel/MsgStream.h"
00010 #include "GaudiKernel/AlgFactory.h"
00011 #include "GaudiKernel/SmartDataPtr.h"
00012 #include "GaudiKernel/IDataProviderSvc.h"
00013
00014 #include "CalibData/Nas/CalibLATAlignment.h"
00015
00016
00017 #include "Event/TopLevel/EventModel.h"
00018 #include "Event/TopLevel/Event.h"
00019
00020 #include "astro/GPS.h"
00021 #include "ntupleWriterSvc/INTupleWriterSvc.h"
00022
00023 #include "CalibData/Nas/CalibLATAlignment.h"
00024 #include "CalibSvc/ICalibPathSvc.h"
00025
00026
00027
00028 #include <cassert>
00029 #include <map>
00030
00031
00032 class FT1worker;
00033
00034 namespace {
00035 unsigned int nbOfEvtsInFile(100000);
00036 std::string treename("MeritTuple");
00037
00038 astro::GPS* gps(0);
00039 #include "Item.h"
00040 }
00041
00042
00046 class FT1Alg : public Algorithm {
00047
00048 public:
00049 FT1Alg(const std::string& name, ISvcLocator* pSvcLocator);
00050
00051 StatusCode initialize();
00052 StatusCode execute();
00053 StatusCode finalize();
00054 private:
00056 FT1worker * m_worker;
00057
00058 int m_count;
00059 IDataProviderSvc* m_pEventSvc;
00060
00061 IDataProviderSvc* m_pCalibDataSvc;
00062 ICalibPathSvc* m_pCalibPathSvc;
00063
00064 BooleanProperty m_aberrate;
00065 StringProperty m_flavor;
00066
00067 std::string m_path;
00068
00069
00070 };
00071
00072
00073
00074 static const AlgFactory<FT1Alg> Factory;
00075 const IAlgFactory& FT1AlgFactory = Factory;
00076
00077
00078
00079 class FT1worker{
00080 public:
00081 FT1worker();
00082
00083 void evaluate();
00084
00085
00086 TypedItem<unsigned int, 'i'> EvtRun;
00087 TypedItem<unsigned long long , 'l'> EvtEventId64;
00088
00089
00090 Item EvtLiveTime;
00091 Item EvtEnergyCorr;
00092 Item EvtElapsedTime;
00093 Item VtxXDir, VtxYDir, VtxZDir;
00094 Item VtxX0, VtxY0, VtxZ0;
00095 Item TkrNumTracks;
00096 Item Tkr1XDir, Tkr1YDir, Tkr1ZDir;
00097 Item Tkr1X0, Tkr1Y0, Tkr1Z0;
00098 Item Tkr1FirstLayer;
00099 Item CTBBestEnergy;
00100 Item CTBBestXDir;
00101 Item CTBBestYDir;
00102 Item CTBBestZDir;
00103 Item CTBBestEnergyProb;
00104 Item CTBBestEnergyRatio;
00105 Item CTBCORE;
00106 Item CTBClassLevel;
00107
00108
00109
00110 unsigned int m_ft1eventid;
00111 float m_ft1energy;
00112 float m_ft1theta,m_ft1phi,m_ft1ra,m_ft1dec,m_ft1l,m_ft1b;
00113 float m_ft1zen,m_ft1azim;
00114 double m_ft1livetime;
00115 float m_ft1convpointx,m_ft1convpointy,m_ft1convpointz,m_ft1convlayer;
00116 float m_ft1eventclass;
00117 };
00118
00119
00120 FT1Alg::FT1Alg(const std::string& name, ISvcLocator* pSvcLocator)
00121 : Algorithm(name, pSvcLocator)
00122 , m_count(0)
00123 {
00124 declareProperty("TreeName", treename="MeritTuple");
00125 declareProperty("NbOfEvtsInFile", nbOfEvtsInFile=100000);
00126 declareProperty("CorrectForAberration", m_aberrate=false);
00127 declareProperty("AlignmentFlavor" , m_flavor="");
00128 }
00129
00130 StatusCode FT1Alg::initialize()
00131 {
00132 StatusCode sc = StatusCode::SUCCESS;
00133
00134 MsgStream log(msgSvc(), name());
00135
00136
00137 setProperties();
00138
00139 IDataProviderSvc* eventsvc = 0;
00140 sc = serviceLocator()->service( "EventDataSvc", eventsvc, true );
00141 if(sc.isFailure()){
00142 log << MSG::ERROR << "Could not find EventDataSvc" << std::endl;
00143 return sc;
00144 }
00145 m_pEventSvc = eventsvc;
00146
00147
00148
00149 if( (sc = service("RootTupleSvc", rootTupleSvc, true) ). isFailure() ) {
00150 log << MSG::ERROR << " failed to get the RootTupleSvc" << endreq;
00151 return sc;
00152 }
00153
00154 m_worker = new FT1worker();
00155
00156
00157 gps = astro::GPS::instance();
00158
00159
00160 if( m_aberrate.value() ){
00161 gps->enableAberration(true);
00162 log << MSG::INFO << "Correction for aberration is enabled" << endreq;
00163 }
00164
00165 if( !m_flavor.value().empty() ) {
00166 if( (sc=service("CalibDataSvc", m_pCalibDataSvc, true)).isFailure()) {
00167 log << MSG::ERROR << " failed to get the CalibDataSvc" << endreq;
00168 return sc;
00169 }
00170 if( (sc=service("CalibDataSvc", m_pCalibPathSvc, true)).isFailure()){
00171 log << MSG::ERROR << " failed to get the CalibDataSvc (or maybe CalibPathSvc)" << endreq;
00172 return sc;
00173 }
00174
00175 m_path = m_pCalibPathSvc->getCalibPath(ICalibPathSvc::Calib_NAS_LATAlignment, m_flavor);
00176 log << MSG::INFO << "Using alignment flavor "<< m_flavor.value() << endreq;
00177
00178 }
00179 return sc;
00180 }
00181
00182
00183 StatusCode FT1Alg::execute()
00184 {
00185 StatusCode sc = StatusCode::SUCCESS;
00186
00187 MsgStream log(msgSvc(), name());
00188 m_count++;
00189
00190 if(! m_flavor.value().empty() ){
00191
00192
00193
00194 static unsigned serial = 0;
00195
00196 SmartDataPtr<CalibData::CalibLATAlignment> alignCalib(m_pCalibDataSvc, m_path);
00197 if ( !alignCalib ) {
00198 log << MSG::ERROR << "Failed access to CalibLATAlignment via smart ptr with path"
00199 << m_path << endreq;
00200 return StatusCode::FAILURE;
00201 }
00202 unsigned newSerial = alignCalib->getSerNo();
00203
00204 if( serial != newSerial) {
00205 serial = newSerial;
00206
00207 static double arcsec2deg(M_PI/648000);
00208 const CalibData::ALIGN_ROT* r = alignCalib->getR();
00209
00210
00211
00212 double x((*r)[0]), y((*r)[1]), z((*r)[2]);
00213 gps->setAlignmentRotation(
00214 CLHEP::HepRotationX(x * arcsec2deg)
00215 *CLHEP::HepRotationY(y* arcsec2deg)
00216 *CLHEP::HepRotationZ(z* arcsec2deg)
00217 );
00218 log << MSG::INFO << "setting alignment parameters: ("
00219 << x<<", "<< y<< "," << z << ") arcsec" << endreq;
00220 }
00221 }
00222
00223
00224 SmartDataPtr<Event::EventHeader> header(m_pEventSvc, EventModel::EventHeader);
00225
00226
00227 double etime(header->time());
00228 gps->time(etime);
00229
00230 m_worker->evaluate();
00231 return sc;
00232 }
00233
00234 StatusCode FT1Alg::finalize()
00235 {
00236 return StatusCode::SUCCESS;
00237 }
00238
00239
00240
00241
00242 FT1worker::FT1worker()
00243
00244 : EvtRun("EvtRun")
00245 , EvtEventId64("EvtEventId64")
00246 , EvtElapsedTime("EvtElapsedTime")
00247 , EvtLiveTime("EvtLiveTime")
00248 , EvtEnergyCorr("EvtEnergyCorr")
00249 , TkrNumTracks("TkrNumTracks")
00250 , VtxXDir("VtxXDir")
00251 , VtxYDir("VtxYDir")
00252 , VtxZDir("VtxZDir")
00253 , VtxX0("VtxX0")
00254 , VtxY0("VtxY0")
00255 , VtxZ0("VtxZ0")
00256 , Tkr1XDir("Tkr1XDir")
00257 , Tkr1YDir("Tkr1YDir")
00258 , Tkr1ZDir("Tkr1ZDir")
00259 , Tkr1X0("Tkr1X0")
00260 , Tkr1Y0("Tkr1Y0")
00261 , Tkr1Z0("Tkr1Z0")
00262 , Tkr1FirstLayer("Tkr1FirstLayer")
00263 , CTBBestEnergy("CTBBestEnergy")
00264 , CTBBestXDir("CTBBestXDir")
00265 , CTBBestYDir("CTBBestYDir")
00266 , CTBBestZDir("CTBBestZDir")
00267 , CTBBestEnergyProb("CTBBestEnergyProb")
00268 , CTBBestEnergyRatio("CTBBestEnergyRatio")
00269 , CTBCORE("CTBCORE")
00270 , CTBClassLevel("CTBClassLevel")
00271
00272 {
00273
00274
00275 addItem( "FT1EventId", m_ft1eventid);
00276 addItem( "FT1Energy", m_ft1energy);
00277 addItem( "FT1Theta", m_ft1theta);
00278 addItem( "FT1Phi", m_ft1phi);
00279 addItem( "FT1Ra", m_ft1ra);
00280 addItem( "FT1Dec", m_ft1dec);
00281 addItem( "FT1L", m_ft1l);
00282 addItem( "FT1B", m_ft1b);
00283 addItem( "FT1ZenithTheta", m_ft1zen);
00284 addItem( "FT1EarthAzimuth", m_ft1azim);
00285 addItem( "FT1ConvPointX", m_ft1convpointx);
00286 addItem( "FT1ConvPointY", m_ft1convpointy);
00287 addItem( "FT1ConvPointZ", m_ft1convpointz);
00288 addItem( "FT1ConvLayer", m_ft1convlayer);
00289 addItem( "FT1Livetime", m_ft1livetime);
00290 addItem( "FT1EventClass", m_ft1eventclass);
00291 }
00292
00331 #if 0
00332 void FT1worker::evaluate(const Event::Exposure* exp)
00333 #else
00334 void FT1worker::evaluate()
00335 #endif
00336 {
00337 using CLHEP::Hep3Vector;
00338 using astro::SkyDir;
00339
00340
00341 m_ft1eventid = nbOfEvtsInFile *EvtRun.value() + EvtEventId64.value();
00342
00343
00344 m_ft1energy = CTBBestEnergy;
00345 if( m_ft1energy==0) m_ft1energy = EvtEnergyCorr;
00346
00347 m_ft1theta = 666; m_ft1phi = 666; m_ft1ra = 666;
00348 m_ft1dec = 666; m_ft1zen = 666; m_ft1azim = 666;
00349 m_ft1l = 666; m_ft1b = 666;
00350 m_ft1convpointx = 999; m_ft1convpointy = 999; m_ft1convpointz = 999;
00351 m_ft1convlayer = -1;
00352 m_ft1livetime = -1;
00353 m_ft1livetime = EvtLiveTime;
00354
00355 if( TkrNumTracks==0) return;
00356 m_ft1convlayer = Tkr1FirstLayer;
00357
00358 Hep3Vector glastDir;
00359 if( CTBBestZDir!=0){
00360 glastDir= Hep3Vector(CTBBestXDir, CTBBestYDir, CTBBestZDir);
00361 }else{
00362 glastDir= Hep3Vector(Tkr1XDir, Tkr1YDir, Tkr1ZDir);
00363 }
00364
00365
00366
00367 m_ft1theta = (-glastDir).theta()*180/M_PI;
00368 double phi_deg = (-glastDir).phi();
00369 if( phi_deg<0 ) phi_deg += 2*M_PI;
00370 m_ft1phi = phi_deg*180/M_PI;
00371
00372
00373
00374
00375
00376
00377
00378 SkyDir sdir( gps->toSky(glastDir) );
00379 m_ft1ra = sdir.ra();
00380 m_ft1dec = sdir.dec();
00381 m_ft1l = sdir.l();
00382 m_ft1b = sdir.b();
00383
00384
00385
00386 SkyDir zenith(gps->zenithDir());
00387 double zenith_theta = sdir.difference(zenith);
00388 if( fabs(zenith_theta)<1e-8) zenith_theta=0;
00389
00390
00391 Hep3Vector north_pole(0,0,1);
00392 Hep3Vector east_dir( north_pole.cross(zenith()).unit() );
00393 Hep3Vector north_dir( zenith().cross(east_dir));
00394
00395 double earth_azimuth=atan2( sdir().dot(east_dir), sdir().dot(north_dir) );
00396 if( earth_azimuth <0) earth_azimuth += 2*M_PI;
00397 if( fabs(earth_azimuth)<1e-8) earth_azimuth=0;
00398
00399 m_ft1zen = zenith_theta*180/M_PI;;
00400 m_ft1azim = earth_azimuth*180/M_PI;
00401
00402
00403
00404 m_ft1eventclass = 0;
00405
00406 if ( (CTBBestEnergy<=10) || (CTBBestEnergyRatio>=5) ) {
00407 m_ft1eventclass = 0;
00408 } else if ( (CTBClassLevel>2) && (CTBCORE>0.1) && (CTBBestEnergyProb>0.1) ) {
00409 m_ft1eventclass = 3;
00410 } else if ( (CTBClassLevel>1) && (CTBCORE>0.1) && (CTBBestEnergyProb>0.1) ) {
00411 m_ft1eventclass = 2;
00412 } else if ( (CTBClassLevel>0) && (CTBCORE>0.) && (CTBBestEnergyProb>0.) ) {
00413 m_ft1eventclass = 1;
00414 }
00415
00416 }