00001 #include "NtpTools.h"
00002
00003 #include "BeamDataUtil/BeamMonSpill.h"
00004 #include "BeamDataUtil/BMSpillAna.h"
00005 #include "MessageService/MsgService.h"
00006 #include "Conventions/Munits.h"
00007 #include "EnergyCorrections.h"
00008 #include <set>
00009 #include <cassert>
00010
00011 #include "TruthHelperNtuple/NtpTHEvent.h"
00012 #include "MCNtuple/NtpMCStdHep.h"
00013
00014 #include "MCReweight/MCReweight.h"
00015
00016
00017 #include "MCReweight/Zfluk.h"
00018
00019
00020
00021 CVSID("$Id: NtpTools.cxx,v 1.5 2009/05/27 01:01:09 rbpatter Exp $");
00022
00023 NtpTools::NtpTools()
00024 : fCanvas(NULL),
00025 fUZAxis(NULL),
00026 fVZAxis(NULL)
00027 {
00028 MSG("NtpAna",Msg::kDebug) << " NtpTools::NtpTools() "<<endl;
00029 fCanvas = new TCanvas("canv", " NtpTools ", 800, 422);
00030 fFluxModel = new Zfluk();
00031 fFluxModel->UseParameterization("SKZP");
00032 }
00033
00034
00035 NtpTools::~NtpTools(){
00036 if(fCanvas) delete fCanvas;
00037 if(fUZAxis) delete fUZAxis;
00038 if(fVZAxis) delete fVZAxis;
00039 }
00040
00041 float NtpTools::GetMCEventWeight(const NtpStRecord* , const int ){
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 cout << "CAUTION: RETURNING 1.0 AS ZFLUK WEIGHT" << endl;
00064
00065 return 1.0;
00066
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 float NtpTools::GetTotHadPt(const NtpStRecord* record, const NtpMCTruth* truth){
00078
00079 float totpt=0;
00080 int index=truth->index;
00081
00082 vector<int> pid;
00083 vector<double> px;
00084 vector<double> py;
00085 vector<double> pz;
00086 vector<double> eng;
00087 vector<double> mass;
00088
00089 double hspx = 0;
00090 double hspy = 0;
00091 double hspz = 0;
00092 double hse = 0;
00093
00094
00095 Int_t nStdHep = record->mchdr.nstdhep;
00096 NtpMCStdHep *ntpStdHep = 0;
00097 for(int i=0; i<nStdHep; i++){
00098 ntpStdHep = dynamic_cast<NtpMCStdHep *>(record->stdhep->At(i));
00099
00100 if(ntpStdHep->mc!=index) continue;
00101
00102 if(ntpStdHep->IstHEP==3){
00103 if (TMath::Abs(ntpStdHep->p4[3])>hse){
00104 hspx = ntpStdHep->p4[0];
00105 hspy = ntpStdHep->p4[1];
00106 hspz = ntpStdHep->p4[2];
00107 hse = TMath::Abs(ntpStdHep->p4[3]);
00108 }
00109 }
00110 else {
00111
00112 bool resdecay = true;
00113 for (int ip = ntpStdHep->parent[0]; ip<=ntpStdHep->parent[1]; ip++){
00114 if (ip==-1) {
00115 resdecay = false;
00116 break;
00117 }
00118 NtpMCStdHep *parent = dynamic_cast<NtpMCStdHep *>(record->stdhep->At(ip));
00119 resdecay = resdecay && (parent->IstHEP==3);
00120 if (!resdecay) break;
00121 }
00122 if (resdecay&&ntpStdHep->IdHEP) {
00123 pid.push_back(ntpStdHep->IdHEP);
00124 px.push_back(ntpStdHep->p4[0]);
00125 py.push_back(ntpStdHep->p4[1]);
00126 pz.push_back(ntpStdHep->p4[2]);
00127 eng.push_back(ntpStdHep->p4[3]);
00128 mass.push_back(ntpStdHep->mass);
00129 }
00130 }
00131 }
00132
00133 double Phs = sqrt(pow(hspx,2)+pow(hspy,2)+pow(hspz,2));
00134 for (unsigned ipar = 0; ipar<pid.size(); ipar++){
00135 double ptot = sqrt(pow(px[ipar],2)+pow(py[ipar],2)+pow(pz[ipar],2));
00136 double Pz = (px[ipar]*hspx+py[ipar]*hspy+pz[ipar]*hspz)/Phs;
00137 double pt = pow(ptot,2)-pow(Pz,2);
00138 if (pt>0.00000001) pt = sqrt(pt);
00139 else pt = 0;
00140
00141 totpt += pt;
00142 }
00143
00144 return totpt;
00145
00146 }
00147
00148 bool NtpTools::FillRecoEInfo( const NtpStRecord* record, const int eventno, RecoE* e_reco){
00149 e_reco->Reset();
00150 const NtpSRTrack* track = GetPrimaryTrack(record, eventno);
00151 const NtpSREvent* event = GetEvent(record, eventno);
00152 Detector::Detector_t detector =record->GetHeader().GetVldContext().GetDetector();
00153 const Bool_t isdata= record->GetHeader().GetVldContext().GetSimFlag()==SimFlag::kData;
00154 const int pitt_flag = GetPittContainmentFlag(event);
00155 e_reco->EOkay = 1;
00156
00157 if(track){
00158 if(pitt_flag ==1 || pitt_flag==3){
00159 e_reco->trkEMethod = 1;
00160 }else if(pitt_flag==2 || pitt_flag==4){
00161 e_reco->trkEMethod = 2;
00162 }
00163 Float_t mr=CorrectMomentumFromRange(track->momentum.range,isdata, detector);
00164 e_reco->trkERange = sqrt(mr*mr+0.105*0.105);
00165
00166 if (track->momentum.qp!=0){
00167 Float_t p= CorrectSignedMomentumFromCurvature(1./track->momentum.qp,isdata,detector);
00168 if(p>0 && p<10000){
00169 e_reco->trkECurve = sqrt(pow(p,2)+pow(0.105,2));
00170 } else e_reco->trkECurve = 0;
00171 }
00172 else{
00173 e_reco->trkECurve = 0;
00174 e_reco->EOkay = 0;
00175 }
00176 if(e_reco->trkEMethod==1) e_reco->trkE = e_reco->trkERange;
00177 else if(e_reco->trkEMethod==2) e_reco->trkE = e_reco->trkECurve;
00178 else e_reco->EOkay = 0;
00179 }else{
00180 e_reco->trkE = 0;
00181 e_reco->trkEMethod = 0;
00182 e_reco->trkERange = 0;
00183 e_reco->trkECurve = 0;
00184 }
00185
00186
00187 e_reco->shwE = 0;
00188 for(int ishw = 0; ishw<event->nshower; ++ishw){
00189 const NtpSRShower* ishower = dynamic_cast<const NtpSRShower*>(record->shw->At(event->shw[ishw]));
00190 float showere = CorrectShowerEnergy(ishower->shwph.linCCgev,
00191 detector,
00192 CandShowerHandle::kCC,
00193 1, isdata);
00194 assert(ishower);
00195 e_reco->shwE+=showere;
00196 }
00197
00198 e_reco->totalE= (e_reco->trkE + e_reco->shwE);
00199 return true;
00200 }
00201
00202 int NtpTools::GetNEventsPerSlice(const NtpStRecord* record, const int eventno){
00203 const NtpSREvent* event = GetEvent(record, eventno);
00204 if(!event) return -1;
00205
00206 int nmatch = 0;
00207 for(int ievent = 0; ievent<record->evthdr.nevent; ++ievent){
00208 const NtpSREvent* tmpevent = dynamic_cast<const NtpSREvent*>(record->evt->At(ievent));
00209 if(tmpevent->slc == event->slc){
00210 nmatch++;
00211 }
00212 }
00213 return nmatch;
00214 }
00215
00216 Int_t NtpTools::GetPittContainmentFlag(const NtpSREvent* evt)
00217 {
00218
00219
00220 Int_t pitt_flag=0;
00221
00222
00223
00224
00225
00226
00227 Float_t x=evt->end.x;
00228 Float_t y=evt->end.y;
00229 Float_t z=evt->end.z;
00230 Float_t u=evt->end.u;
00231 Float_t v=evt->end.v;
00232 Float_t rad=TMath::Sqrt(x*x + y*y);
00233
00234 Float_t calZ=7;
00235 Float_t specZ=15.6;
00236 Float_t minR=0.8;
00237
00238 Bool_t down_stop=false;
00239 Bool_t down_exit=false;
00240 Bool_t up_stop=false;
00241 Bool_t up_exit=false;
00242
00243
00244 if (z>calZ) {
00245 if (u>-0.85 && u<2.1 &&
00246 v>-2.1 && v<0.85 &&
00247 x>-1.5 && x<2.4 &&
00248 y>-1.4 && y<1.4 &&
00249 rad>minR &&
00250 z>calZ && z<=specZ) down_stop=true;
00251 else down_exit=true;
00252 }
00253
00254 else if (z<=calZ && z>=0) {
00255 if (u>0.3 && u<1.8 &&
00256 v>-1.8 && v<-0.3 &&
00257 x<2.4 &&
00258 rad>minR &&
00259 z<calZ && z>=0) up_stop=true;
00260 else up_exit=true;
00261 }
00262 else cout<<"Ahhh, z="<<z<<endl;
00263
00264
00265 if (down_stop) pitt_flag = 3;
00266 if (up_stop ) pitt_flag = 1;
00267 if (down_exit) pitt_flag = 4;
00268 if (up_exit ) pitt_flag = 2;
00269
00270 return pitt_flag;
00271 }
00272
00273 bool NtpTools::FillMyShwInfo(const NtpStRecord* record, const int eventno, MyShwInfo* shwinfo){
00274 shwinfo->Reset();
00275 static float planePH[500];
00276 for(int i=0; i<500; ++i) planePH[i] = 0;
00277 const NtpSREvent* event = GetEvent(record, eventno);
00278 for(int istp = 0; istp<event->nstrip; ++istp){
00279 const NtpSRStrip* strip = dynamic_cast<const NtpSRStrip*>(record->stp->At(event->stp[istp]));
00280 shwinfo->totalPH+=(strip->ph0.sigcor+strip->ph1.sigcor);
00281 if(strip->plane<500){
00282 planePH[strip->plane]+=(strip->ph0.sigcor+strip->ph1.sigcor);
00283 }else{
00284 cout << "Failed to get plane number; " << strip->plane<< endl;
00285 }
00286 }
00287 shwinfo->maxPH3Plane = 0;
00288 for(int i=1; i<499; ++i){
00289 float tmp = planePH[i-1] + planePH[i+1] + planePH[i];
00290 if(tmp>shwinfo->maxPH3Plane) shwinfo->maxPH3Plane = tmp;
00291 }
00292 return true;
00293 }
00294
00295 const NtpSRTrack* NtpTools::GetPrimaryTrack(const NtpStRecord* record, const int eventno){
00296 const NtpSREvent* event = dynamic_cast<const NtpSREvent*>(record->evt->At(eventno));
00297 if(event==NULL){
00298 MSG("NtpAna",Msg::kError) << " NtpTools::GetPrimaryTrack passed null event " <<endl;
00299 }
00300 int best_track = -1;
00301 int nplanes = 0;
00302 for(int itrk=0; itrk<event->ntrack; ++itrk){
00303 const NtpSRTrack* track = dynamic_cast<const NtpSRTrack*>(record->trk->At(event->trk[itrk]));
00304
00305 int tpln = abs(track->end.plane - track->vtx.plane);
00306 if(tpln>nplanes) {
00307 nplanes = tpln;
00308 best_track = itrk;
00309 }
00310 }
00311 if(best_track==-1){
00312 return NULL;
00313 }else{
00314 return (dynamic_cast<const NtpSRTrack*>(record->trk->At(event->trk[best_track])));
00315 }
00316 }
00317
00318 bool NtpTools::PassBeamCuts(const BeamType_t beamtype, const NtpBDLiteRecord* beamntp, float& pot, bool isMC ){
00319
00320
00321
00322
00323 if(isMC){
00324 pot = 24.2;
00325 return true;
00326 }
00327
00328 static Registry* beam_config = NULL;
00329 static BeamMonSpill spill;
00330 static BMSpillAna bmsa;
00331 static BeamType_t lastbeamtype = beamtype;
00332
00333 if(beamntp==NULL) return 0;
00334
00335 if(beam_config==NULL){
00336 beam_config = new Registry();
00337 beam_config->UnLockValues();
00338
00339
00340
00341 beam_config->Set("BeamType",(int)(beamtype));
00342
00343 beam_config->Set("PosTgtXMax",-0.0000001*Munits::mm);
00344 beam_config->Set("PosTgtYMin", 0.0000001*Munits::mm);
00345
00346 beam_config->LockValues();
00347 bmsa.Config(*beam_config);
00348
00349 beam_config->PrettyPrint(cout);
00350 }
00351 if(lastbeamtype!=beamtype){
00352 beam_config->UnLockValues();
00353 beam_config->Set("BeamType",(int)(beamtype));
00354 beam_config->LockValues();
00355 bmsa.Config(*beam_config);
00356 lastbeamtype = beamtype;
00357 }
00358
00359 bmsa.SetSpill(*beamntp,spill);
00360 pot = beamntp->tortgt;
00361 if (!bmsa.SelectSpill()){
00362 MAXMSG("NtpAna",Msg::kInfo,10)
00363 <<"Rejected spill!"
00364 <<", tortgt="<<beamntp->tortgt
00365 <<", timeDiff="<<beamntp->GetHeader().GetTimeDiffStreamSpill() <<endl;
00366 return 0;
00367 }else{
00368 return true;
00369 }
00370 }
00371
00372
00373
00374 bool NtpTools::ResetCanvas(Detector::Detector_t det){
00375 if(fUZAxis) delete fUZAxis;
00376 if(fVZAxis) delete fVZAxis;
00377 fUZAxis = NULL;
00378 fVZAxis = NULL;
00379 fUZMarkers.clear();
00380 fVZMarkers.clear();
00381 fCanvas->Clear();
00382 fCanvas->Divide(1,2);
00383
00384 switch(det){
00385 case Detector::kFar:
00386 fUZAxis = new TH2F("fUZAxis", "", 100, 0, 17, 100, -4, 4.);
00387 fVZAxis = new TH2F("fVZAxis", "", 100, 0, 17, 100, -4, 4.);
00388 break;
00389 case Detector::kNear:
00390 fUZAxis = new TH2F("fUZAxis", "", 100, 0, 17, 100, -1.5, 3.);
00391 fVZAxis = new TH2F("fVZAxis", "", 100, 0, 17, 100, -3, 1.5);
00392 break;
00393 default:
00394 return false;
00395 break;
00396 };
00397
00398 fUZAxis->SetXTitle("Z/m");
00399 fUZAxis->SetYTitle("U/m");
00400 fVZAxis->SetXTitle("Z/m");
00401 fVZAxis->SetYTitle("V/m");
00402 return true;
00403 }
00404
00405 bool NtpTools::MakeEventPict(const char* title, const NtpStRecord* record, const int eventno){
00406 bool rc = false;
00407 if(ResetCanvas(GetDetector(record))==false) return false;
00408
00409 const NtpSREvent* event = GetEvent(record, eventno);
00410 const TClonesArray* strips = (record->stp);
00411 const TClonesArray* tracks = (record->trk);
00412
00413 std::set<int> trkstp_index;
00414
00415 for(int itrk=0; itrk<event->ntrack; ++itrk){
00416 const NtpSRTrack* track = dynamic_cast<const NtpSRTrack*>(tracks->At(event->trk[itrk]));
00417 if(track==NULL){
00418 MSG("NtpAna",Msg::kError) << " failed to cast track " <<endl;
00419 }else{
00420 for(int istp=0; istp<track->nstrip; istp++){
00421 trkstp_index.insert(track->stp[istp]) ;
00422 }
00423 }
00424 }
00425
00426 for(int istp =0; istp< event->nstrip ; ++istp){
00427 const int stp_index = event->stp[istp];
00428 const NtpSRStrip* strip = dynamic_cast<const NtpSRStrip*>(strips->At(stp_index));
00429 if(strip==NULL){
00430 MSG("NtpAna",Msg::kError) << " failed to cast strip " <<endl;
00431 rc = false;
00432 }else{
00433
00434 TMarker tmpmarker(strip->z, strip->tpos,
00435 20);
00436 tmpmarker.SetMarkerSize(0.5);
00437 if(trkstp_index.count(stp_index)>0) tmpmarker.SetMarkerColor(2);
00438 if(strip->planeview==2){
00439 fUZMarkers.push_back(tmpmarker);
00440 }else{
00441 fVZMarkers.push_back(tmpmarker);
00442 }
00443 }
00444
00445 }
00446
00447
00448 rc = !PrintCanvas(title);
00449
00450 return rc;
00451 }
00452
00453 bool NtpTools::PrintCanvas(const char* title){
00454 fCanvas->cd(1);
00455 fUZAxis->Draw("axis");
00456 float minz = 999;
00457 float maxz = 0;
00458
00459 for(unsigned int i=0; i<fUZMarkers.size(); i++){
00460 fUZMarkers[i].Draw();
00461 if(minz>fUZMarkers[i].GetX()) minz=fUZMarkers[i].GetX();
00462 if(maxz<fUZMarkers[i].GetX()) maxz=fUZMarkers[i].GetX();
00463 }
00464 fUZAxis->GetXaxis()->SetRangeUser(minz, maxz);
00465
00466 fCanvas->cd(2);
00467 fVZAxis->Draw("axis");
00468 minz = 999;
00469 maxz = 0;
00470
00471 for(unsigned int i=0; i<fVZMarkers.size(); i++){
00472 fVZMarkers[i].Draw();
00473 if(minz>fVZMarkers[i].GetX()) minz=fVZMarkers[i].GetX();
00474 if(maxz<fVZMarkers[i].GetX()) maxz=fVZMarkers[i].GetX();
00475 }
00476 fVZAxis->GetXaxis()->SetRangeUser(minz, maxz);
00477
00478 fCanvas->Print(title);
00479 return true;
00480 }
00481
00482 const NtpMCTruth* NtpTools::GetTruth(const NtpStRecord* record, const int eventno){
00483 if(!record || record->thevt==NULL){
00484 cout << "*** no truth helper! " <<endl;
00485 return NULL;
00486 }
00487 if(record->evt->GetEntries() != record->thevt->GetEntries()){
00488 cout << " funny matching evt - >thevt " <<endl;
00489 return NULL;
00490 }
00491 const NtpTHEvent* thevent = dynamic_cast<const NtpTHEvent*>(record->thevt->At(eventno));
00492 if(!thevent){
00493 cout << "*** unable to get thevt" <<endl;
00494 return NULL;
00495 }
00496 return (dynamic_cast<const NtpMCTruth*>(record->mc->At(thevent->neumc)));
00497
00498 }
00499
00500 bool NtpTools::GetTrueEnergy(const NtpStRecord* record, const int eventno, float& mceneu){
00501 const NtpMCTruth* mcevent = GetTruth(record, eventno);
00502
00503 if(!mcevent){
00504 cout << "*** unable to get mcevent" <<endl;
00505 return false;
00506 }
00507 mceneu = fabs(mcevent->p4neu[3]);
00508 return true;
00509 }
00510