00001 #include "TClonesArray.h"
00002 #include "MCNtuple/NtpMCRecord.h"
00003 #include "MCNtuple/NtpMCTruth.h"
00004 #include "MCNtuple/NtpMCStdHep.h"
00005 #include "TruthHelperNtuple/NtpTHRecord.h"
00006 #include "TruthHelperNtuple/NtpTHEvent.h"
00007 #include "CandNtupleSR/NtpSRRecord.h"
00008 #include "CandNtupleSR/NtpSRStrip.h"
00009 #include "CandNtupleSR/NtpSREvent.h"
00010 #include "CandNtupleSR/NtpSRTrack.h"
00011 #include "CandNtupleSR/NtpSRShower.h"
00012 #include "CandNtupleSR/NtpSRCluster.h"
00013 #include "CandNtupleSR/NtpSRSlice.h"
00014 #include "CandNtupleSR/NtpSRShieldStrip.h"
00015 #include "CandNtupleSR/NtpSRShieldSummary.h"
00016 #include "StandardNtuple/NtpStRecord.h"
00017 #include "Record/RecRecordImp.h"
00018 #include "NueAna/NueAnaTools/SntpHelpers.h"
00019 #include "MuonRemoval/NtpMRRecord.h"
00020 #include "MuonRemoval/NtpMREvent.h"
00021 #include "MuonRemoval/NtpMRTruth.h"
00022 #include "MessageService/MsgService.h"
00023
00024 #include <iostream>
00025
00026 CVSID("$Id: SntpHelpers.cxx,v 1.9 2008/09/07 18:21:49 boehm Exp $");
00027
00028 using namespace std;
00029
00030 NtpSREvent *SntpHelpers::GetEvent(int evnt, RecRecordImp<RecCandHeader> *rri)
00031 {
00032 NtpStRecord *st=dynamic_cast<NtpStRecord *>(rri);
00033
00034 if(st!=0){
00035 return GetEvent(evnt,st);
00036 }
00037
00038 NtpSRRecord *sr=dynamic_cast<NtpSRRecord *>(rri);
00039 if(sr!=0){
00040 return GetEvent(evnt,sr);
00041 }
00042
00043 return 0;
00044 }
00045
00046 NtpSRTrack *SntpHelpers::GetTrack(int trkn, RecRecordImp<RecCandHeader> *rri)
00047 {
00048 NtpStRecord *st=dynamic_cast<NtpStRecord *>(rri);
00049 if(st!=0){
00050 return GetTrack(trkn,st);
00051 }
00052
00053 NtpSRRecord *sr=dynamic_cast<NtpSRRecord *>(rri);
00054 if(sr!=0){
00055 return GetTrack(trkn,sr);
00056 }
00057 return 0;
00058 }
00059
00060 NtpSRShower *SntpHelpers::GetShower(int shwn, RecRecordImp<RecCandHeader> *rri)
00061 {
00062 NtpStRecord *st=dynamic_cast<NtpStRecord *>(rri);
00063 if(st!=0){
00064 return GetShower(shwn,st);
00065 }
00066
00067 NtpSRRecord *sr=dynamic_cast<NtpSRRecord *>(rri);
00068 if(sr!=0){
00069 return GetShower(shwn,sr);
00070 }
00071 return 0;
00072 }
00073
00074 NtpSRShower *SntpHelpers::GetPrimaryShower(int evnt, RecRecordImp<RecCandHeader> *rri)
00075 {
00076 NtpSRShower *shower=0;
00077 if(rri==0){
00078 return shower;
00079 }
00080
00081 NtpSREvent *event = SntpHelpers::GetEvent(evnt,rri);
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 if (event==0){
00095 return shower;
00096 }
00097
00098 Int_t nshws = event->nshower;
00099 if (nshws){
00100 if(event->primshw < 0){
00101
00102 return shower;
00103 }
00104 int index = event->shw[event->primshw];
00105
00106 if(index < 0) return shower;
00107 shower = SntpHelpers::GetShower(index,rri);
00108 }
00109 return shower;
00110 }
00111
00112 NtpSRCluster *SntpHelpers::GetCluster(int clun, RecRecordImp<RecCandHeader> *rri)
00113 {
00114 NtpStRecord *st=dynamic_cast<NtpStRecord *>(rri);
00115 if(st!=0){
00116 return GetCluster(clun,st);
00117 }
00118
00119 NtpSRRecord *sr=dynamic_cast<NtpSRRecord *>(rri);
00120 if(sr!=0){
00121 return GetCluster(clun,sr);
00122 }
00123 return 0;
00124 }
00125
00126 NtpSRStrip *SntpHelpers::GetStrip(int stpn, RecRecordImp<RecCandHeader> *rri)
00127 {
00128 NtpStRecord *st=dynamic_cast<NtpStRecord *>(rri);
00129 if(st!=0){
00130 return GetStrip(stpn,st);
00131 }
00132
00133 NtpSRRecord *sr=dynamic_cast<NtpSRRecord *>(rri);
00134 if(sr!=0){
00135 return GetStrip(stpn,sr);
00136 }
00137 return 0;
00138 }
00139
00140 NtpSRShieldSummary *SntpHelpers::shieldSummary(RecRecordImp<RecCandHeader> *rri)
00141 {
00142 NtpStRecord *st=dynamic_cast<NtpStRecord *>(rri);
00143 if(st!=0){
00144 return shieldSummary(st);
00145 }
00146
00147 NtpSRRecord *sr=dynamic_cast<NtpSRRecord *>(rri);
00148 if(sr!=0){
00149 return shieldSummary(sr);
00150 }
00151 return 0;
00152 }
00153
00154 NtpSRShieldStrip *SntpHelpers::GetShieldStrip(int stpn, RecRecordImp<RecCandHeader> *rri)
00155 {
00156 NtpStRecord *st=dynamic_cast<NtpStRecord *>(rri);
00157 if(st!=0){
00158 return GetShieldStrip(stpn,st);
00159 }
00160
00161 NtpSRRecord *sr=dynamic_cast<NtpSRRecord *>(rri);
00162 if(sr!=0){
00163 return GetShieldStrip(stpn,sr);
00164 }
00165 return 0;
00166 }
00167
00168 NtpSREvent *SntpHelpers::GetEvent(int evnt,NtpSRRecord *sr)
00169 {
00170 NtpSREvent *event=0;
00171 if(sr==0){
00172 return event;
00173 }
00174 if(evnt>=sr->evthdr.nevent){
00175 return event;
00176 }
00177
00178 event = dynamic_cast<NtpSREvent *>((*sr->evt)[evnt]);
00179
00180 return event;
00181 }
00182
00183 NtpSRTrack *SntpHelpers::GetTrack(int trkn,NtpSRRecord *sr)
00184 {
00185 NtpSRTrack *track=0;
00186 if(sr==0){
00187 return track;
00188 }
00189 if(trkn>=sr->evthdr.ntrack){
00190 return track;
00191 }
00192
00193 track = dynamic_cast<NtpSRTrack *>((*sr->trk)[trkn]);
00194
00195 return track;
00196 }
00197
00198 NtpSRShower *SntpHelpers::GetShower(int shwn,NtpSRRecord *sr)
00199 {
00200 NtpSRShower *shower=0;
00201 if(sr==0){
00202 return shower;
00203 }
00204 if(shwn>=sr->evthdr.nshower){
00205 return shower;
00206 }
00207
00208 shower = dynamic_cast<NtpSRShower *>((*sr->shw)[shwn]);
00209
00210 return shower;
00211 }
00212
00213 NtpSRCluster *SntpHelpers::GetCluster(int clun,NtpSRRecord *sr)
00214 {
00215 NtpSRCluster *cluster=0;
00216 if(sr==0){
00217 return cluster;
00218 }
00219 if(clun>=sr->evthdr.ncluster){
00220 return cluster;
00221 }
00222
00223 cluster = dynamic_cast<NtpSRCluster *>((*sr->clu)[clun]);
00224
00225 return cluster;
00226 }
00227
00228 NtpSRSlice *SntpHelpers::GetSlice(int slcn,NtpSRRecord *sr)
00229 {
00230 NtpSRSlice *slice=0;
00231 if(sr==0){
00232 return slice;
00233 }
00234 if(slcn>=sr->evthdr.nslice){
00235 return slice;
00236 }
00237
00238 slice = dynamic_cast<NtpSRSlice *>((*sr->slc)[slcn]);
00239
00240 return slice;
00241 }
00242
00243 NtpSRStrip *SntpHelpers::GetStrip(int stpn,NtpSRRecord *sr)
00244 {
00245 NtpSRStrip *strip=0;
00246 if(sr==0){
00247 return strip;
00248 }
00249 if(stpn>=(int)(sr->evthdr.nstrip)){
00250 return strip;
00251 }
00252
00253 strip = dynamic_cast<NtpSRStrip *>((*sr->stp)[stpn]);
00254
00255 return strip;
00256 }
00257
00258 NtpSRShieldSummary *SntpHelpers::shieldSummary(NtpSRRecord *sr)
00259 {
00260 NtpSRShieldSummary *shsm=0;
00261 if(sr==0){
00262 return shsm;
00263 }
00264
00265 shsm = dynamic_cast<NtpSRShieldSummary *>(&sr->vetohdr);
00266
00267 return shsm;
00268 }
00269
00270 NtpSRShieldStrip *SntpHelpers::GetShieldStrip(int stpn,NtpSRRecord *sr)
00271 {
00272 NtpSRShieldStrip *strip=0;
00273 if(sr==0){
00274 return strip;
00275 }
00276 int nshdigt = sr->vetohdr.ndigit[0]+sr->vetohdr.ndigit[1]+sr->vetohdr.ndigit[2];
00277 if(sr->vetohdr.ishit==0||stpn>=nshdigt){
00278 return strip;
00279 }
00280 strip = dynamic_cast<NtpSRShieldStrip *>((*sr->vetostp)[stpn]);
00281
00282 return strip;
00283 }
00284
00285 NtpSREvent *SntpHelpers::GetEvent(int evnt,NtpStRecord *st)
00286 {
00287 NtpSREvent *event=0;
00288 if(st==0){
00289 return event;
00290 }
00291 if(evnt>=st->evthdr.nevent){
00292 return event;
00293 }
00294
00295 event = dynamic_cast<NtpSREvent *>((*st->evt)[evnt]);
00296
00297 return event;
00298 }
00299
00300 NtpSRTrack *SntpHelpers::GetTrack(int trkn,NtpStRecord *st)
00301 {
00302 NtpSRTrack *track=0;
00303 if(st==0){
00304 return track;
00305 }
00306 if(trkn>=st->evthdr.ntrack){
00307 return track;
00308 }
00309
00310
00311 track = dynamic_cast<NtpSRTrack *>((*st->trk)[trkn]);
00312
00313 return track;
00314 }
00315
00316 NtpSRTrack *SntpHelpers::GetPrimaryTrack(int evtn,NtpStRecord *st)
00317 {
00318 NtpSRTrack *ntpTrack=0;
00319 NtpSRTrack *primaryTrack = 0;
00320 if(st==0){ return ntpTrack; }
00321 NtpSREvent* event = SntpHelpers::GetEvent(evtn,st);
00322
00323 int length = 0;
00324 int tmpLength = 0;
00325
00326 for(int i = 0; i < event->ntrack; i++){
00327 int index = event->trk[i];
00328 ntpTrack = SntpHelpers::GetTrack(index, st);
00329 if(!primaryTrack) primaryTrack = ntpTrack;
00330
00331 tmpLength = TMath::Abs(ntpTrack->plane.end - ntpTrack->plane.beg);
00332
00333 if(tmpLength > length){
00334 length = tmpLength;
00335 primaryTrack = ntpTrack;
00336 }
00337 }
00338
00339 return primaryTrack;
00340 }
00341
00342
00343
00344 NtpSRShower *SntpHelpers::GetShower(int shwn,NtpStRecord *st)
00345 {
00346 NtpSRShower *shower=0;
00347 if(st==0){
00348 return shower;
00349 }
00350 if(shwn>=st->evthdr.nshower){
00351 return shower;
00352 }
00353
00354
00355 shower = dynamic_cast<NtpSRShower *>((*st->shw)[shwn]);
00356
00357 return shower;
00358 }
00359
00360 NtpSRCluster *SntpHelpers::GetCluster(int clun,NtpStRecord *st)
00361 {
00362 NtpSRCluster *cluster=0;
00363 if(st==0){
00364 return cluster;
00365 }
00366 if(clun>=st->evthdr.ncluster){
00367 return cluster;
00368 }
00369
00370 cluster = dynamic_cast<NtpSRCluster *>((*st->clu)[clun]);
00371
00372 return cluster;
00373 }
00374
00375 NtpSRSlice *SntpHelpers::GetSlice(int slcn,NtpStRecord *st)
00376 {
00377 NtpSRSlice *slice=0;
00378 if(st==0){
00379 return slice;
00380 }
00381 if(slcn>=st->evthdr.nslice){
00382 return slice;
00383 }
00384
00385 slice = dynamic_cast<NtpSRSlice *>((*st->slc)[slcn]);
00386
00387 return slice;
00388 }
00389
00390 NtpSRStrip *SntpHelpers::GetStrip(int stpn,NtpStRecord *st)
00391 {
00392 NtpSRStrip *strip=0;
00393 if(st==0){
00394 return strip;
00395 }
00396 if(stpn>=(int)(st->evthdr.nstrip) || stpn < 0){
00397 return strip;
00398 }
00399
00400 strip = dynamic_cast<NtpSRStrip *>((*st->stp)[stpn]);
00401
00402 return strip;
00403 }
00404
00405 NtpSRShieldSummary *SntpHelpers::shieldSummary(NtpStRecord *st)
00406 {
00407 NtpSRShieldSummary *shsm=0;
00408 if(st==0){
00409 return shsm;
00410 }
00411
00412 shsm = dynamic_cast<NtpSRShieldSummary *>(&st->vetohdr);
00413
00414 return shsm;
00415 }
00416
00417 NtpSRShieldStrip *SntpHelpers::GetShieldStrip(int stpn,NtpStRecord *st)
00418 {
00419 NtpSRShieldStrip *strip=0;
00420 if(st==0){
00421 return strip;
00422 }
00423 int nshdigt = st->vetohdr.ndigit[0]+st->vetohdr.ndigit[1]+st->vetohdr.ndigit[2];
00424 if(st->vetohdr.ishit==0||stpn>=nshdigt){
00425 return strip;
00426 }
00427
00428 strip = dynamic_cast<NtpSRShieldStrip *>((*st->vetostp)[stpn]);
00429
00430 return strip;
00431 }
00432
00433 int SntpHelpers::GetStripIndex(int stpn, TObject *obj)
00434 {
00435
00436
00437 if(obj==0){
00438 return -1;
00439 }
00440
00441 const char* cn = obj->ClassName();
00442
00443 if(strcmp(cn,"NtpSREvent")==0){
00444 NtpSREvent *event = dynamic_cast<NtpSREvent *>(obj);
00445 if(stpn>=event->nstrip){
00446 return -1;
00447 }
00448 else{
00449 return event->stp[stpn];
00450 }
00451 }
00452 else if(strcmp(cn,"NtpSRTrack")==0){
00453 NtpSRTrack *track = dynamic_cast<NtpSRTrack *>(obj);
00454 if(stpn>=track->nstrip){
00455 return -1;
00456 }
00457 else{
00458 return track->stp[stpn];
00459 }
00460 }
00461 else if(strcmp(cn,"NtpSRShower")==0){
00462 NtpSRShower *shower = dynamic_cast<NtpSRShower *>(obj);
00463 if(stpn>=shower->nstrip){
00464 return -1;
00465 }
00466 else{
00467 return shower->stp[stpn];
00468 }
00469
00470 }
00471 else if(strcmp(cn,"NtpSRCluster")==0){
00472 NtpSRCluster *cluster = dynamic_cast<NtpSRCluster *>(obj);
00473 if(stpn>=cluster->nstrip){
00474 return -1;
00475 }
00476 else{
00477 return cluster->stp[stpn];
00478 }
00479
00480 }else if(strcmp(cn,"NtpSRSlice")==0){
00481 NtpSRSlice *slice = dynamic_cast<NtpSRSlice *>(obj);
00482 if(stpn>=slice->nstrip){
00483 return -1;
00484 }
00485 else{
00486 return slice->stp[stpn];
00487 }
00488 }
00489 else{
00490 return -1;
00491 }
00492
00493 return -1;
00494 }
00495
00496 int SntpHelpers::GetTrackIndex(int trkn, NtpSREvent *event)
00497 {
00498 if(trkn>=event->ntrack){
00499 return -1;
00500 }
00501 else{
00502 return event->trk[trkn];
00503 }
00504 return -1;
00505 }
00506
00507 int SntpHelpers::GetShowerIndex(int shwn, NtpSREvent *event)
00508 {
00509 if(shwn>=event->nshower){
00510 return -1;
00511 }
00512 else{
00513 return event->shw[shwn];
00514 }
00515 return -1;
00516 }
00517
00518 int SntpHelpers::GetClusterIndex(int clun, NtpSRShower *shower)
00519 {
00520 if(clun>=shower->ncluster){
00521 return -1;
00522 }
00523 else{
00524 return shower->clu[clun];
00525 }
00526 return -1;
00527 }
00528
00529 int SntpHelpers::GetEvent2MCIndex(int evnt, NtpTHRecord *th)
00530 {
00531 int thsize=th->thevt->GetEntries();
00532 if(evnt>=thsize){
00533 return -1;
00534 }
00535 NtpTHEvent *the = dynamic_cast<NtpTHEvent *>((*th->thevt)[evnt]);
00536 return the->neumc;
00537
00538 }
00539
00540 int SntpHelpers::GetEvent2MCIndex(int evnt, NtpStRecord *st)
00541 {
00542 int thsize=st->thevt->GetEntries();
00543 if(evnt>=thsize){
00544 return -1;
00545 }
00546 NtpTHEvent *the = dynamic_cast<NtpTHEvent *>((*st->thevt)[evnt]);
00547 return the->neumc;
00548
00549 }
00550
00551 NtpMCTruth *SntpHelpers::GetMCTruth(int index, NtpMCRecord *mc)
00552 {
00553 NtpMCTruth *mct=0;
00554
00555 if(index>=mc->mc->GetEntries()){
00556 return mct;
00557 }
00558 if(index<0){
00559 return mct;
00560 }
00561
00562 mct=dynamic_cast<NtpMCTruth *>((*mc->mc)[index]);
00563 return mct;
00564
00565 }
00566
00567 NtpMCTruth *SntpHelpers::GetMCTruth(int index, NtpStRecord *st)
00568 {
00569 NtpMCTruth *mct=0;
00570
00571 if(index>=st->mc->GetEntries()){
00572 return mct;
00573 }
00574 if(index<0){
00575 return mct;
00576 }
00577
00578 mct=dynamic_cast<NtpMCTruth *>((*st->mc)[index]);
00579 return mct;
00580
00581 }
00582
00583 std::vector<NtpMCStdHep *> SntpHelpers::GetStdHepArray(int index, NtpMCRecord *mc)
00584 {
00585 std::vector<NtpMCStdHep *> v;
00586 for(int i=0;i<mc->stdhep->GetEntries();i++){
00587 NtpMCStdHep *stdhep = dynamic_cast<NtpMCStdHep *>((*mc->stdhep)[i]);
00588 if(stdhep->mc==index){
00589 v.push_back(stdhep);
00590 }
00591 }
00592 return v;
00593 }
00594
00595 std::vector<NtpMCStdHep *> SntpHelpers::GetStdHepArray(int index, NtpStRecord *st)
00596 {
00597 std::vector<NtpMCStdHep *> v;
00598 for(int i=0;i<st->stdhep->GetEntries();i++){
00599 NtpMCStdHep *stdhep = dynamic_cast<NtpMCStdHep *>((*st->stdhep)[i]);
00600 if(stdhep->mc==index){
00601 v.push_back(stdhep);
00602 }
00603 }
00604 return v;
00605 }
00606
00607 Int_t SntpHelpers::InPartialRegion(UShort_t plane, UShort_t strip){
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625 static bool first=true;
00626 static UShort_t ptype[282];
00627 if(first){
00628 ptype[0]=0;
00629 for(int i=1; i<=281; i++){
00630 if(i%2==0) ptype[i]=1;
00631 else ptype[i]=2;
00632 if((i-1)%5 == 0) ptype[i]+=2;
00633 else if(i>120) ptype[i]=0;
00634 }
00635 first=false;
00636 }
00637 if(plane>281){
00638
00639 return 0;
00640 }
00641 UShort_t pt = ptype[plane];
00642
00643 Int_t result;
00644 switch(pt){
00645 case 1:
00646 case 3:
00647 if(strip<=4 || strip>=67) result=1;
00648 else result = -1;
00649 break;
00650 case 2:
00651 if(strip==0 || strip == 63) result=1;
00652 else result = -1;
00653 break;
00654 case 4:
00655 if(strip<=26 || strip>=88) result=1;
00656 else result = -1;
00657 break;
00658 case 0:
00659 default:
00660 result=0;
00661 break;
00662 }
00663 return result;
00664
00665 }
00666
00667 NtpMREvent *SntpHelpers::GetMREvent(int ind,NtpMRRecord *mr)
00668 {
00669 NtpMREvent *mrevt=0;
00670 if(mr==0){
00671 return mrevt;
00672 }
00673 if(ind>=(int)(mr->mrhdr.nmrevt)){
00674 return mrevt;
00675 }
00676 mrevt = dynamic_cast<NtpMREvent *>((*mr->mrevt)[ind]);
00677 return mrevt;
00678 }
00679
00680 NtpMRTruth *SntpHelpers::GetMRTruth(int ind,NtpMRRecord *mr)
00681 {
00682 NtpMRTruth *mrtru=0;
00683 if(mr==0) return mrtru;
00684 if(mr->GetHeader().GetVldContext().GetSimFlag()!=4) return mrtru;
00685 if(ind>=(int)(mr->mrhdr.nmrevt)) return mrtru;
00686 mrtru = dynamic_cast<NtpMRTruth *>((*mr->mrtru)[ind]);
00687 return mrtru;
00688 }
00689
00690 void SntpHelpers::FillEventEnergy(float* ph0, float* ph1, int evtn, NtpStRecord* st, const int SIZE)
00691 {
00692 NtpSREvent *event = 0;
00693 event = SntpHelpers::GetEvent(evtn,st);
00694 if(event == 0) return;
00695
00696 for(int j=0;j<event->ntrack;j++){
00697 int tindex = SntpHelpers::GetTrackIndex(j,event);
00698 NtpSRTrack *track = SntpHelpers::GetTrack(tindex,st);
00699 for(int k = 0; k < track->nstrip; k++){
00700 int index = SntpHelpers::GetStripIndex(k, track);
00701 if(index > SIZE){
00702 MSG("SntpHelper",Msg::kFatal)<<" evt mip array insufficiently large: "
00703 <<"index "<<index<<"/"<<SIZE<<" requested"<<std::endl;
00704 }
00705
00706 float mips1 = track->stpph1mip[k];
00707 float mips0 = track->stpph0mip[k];
00708
00709 if(ph0[index] < 0) ph0[index] = mips0;
00710 if(ph1[index] < 0) ph1[index] = mips1;
00711 }
00712 }
00713 for(int j=0;j<event->nshower;j++){
00714 int sindex = SntpHelpers::GetShowerIndex(j,event);
00715 NtpSRShower *shw = SntpHelpers::GetShower(sindex,st);
00716 for(int k = 0; k < shw->nstrip; k++){
00717 int index = SntpHelpers::GetStripIndex(k, shw);
00718 if(index > SIZE){
00719 MSG("SntpHelper",Msg::kFatal)<<" evt mip array insufficiently large: "
00720 <<"index "<<index<<"/"<<SIZE<<" requested"<<std::endl;
00721 }
00722
00723 float mips1 = shw->stpph1mip[k];
00724 float mips0 = shw->stpph0mip[k];
00725
00726 if(ph0[index] < 0) ph0[index] = mips0;
00727 if(ph1[index] < 0) ph1[index] = mips1;
00728 }
00729 }
00730 return;
00731 }
00732