Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MoqQuantities.cxx

Go to the documentation of this file.
00001 #ifndef moqquantities_cxx
00002 #define moqquantities_cxx
00003 #include <iostream>
00004 #include <cmath>
00005 #include "TClonesArray.h"
00006 #include "TMath.h"
00007 #include "TFile.h"
00008 #include "TH1.h"
00009 #include "TH2.h"
00010 #include "Swimmer/SwimSwimmer.h"
00011 #include "Swimmer/SwimParticle.h"
00012 #include "Swimmer/SwimZCondition.h"
00013 #include "Conventions/Munits.h"
00014 #include "OfflineMonitor/MoqQuantities.h"
00015 
00016 //********************************************************
00017 
00018 MoqQuantities::MoqQuantities(TChain *chainSR,TChain *chainMC,
00019                              TChain *chainTH,TChain *chainEM)
00020 {
00021 
00022   if(!chainSR) {
00023     record = 0;
00024     emrecord = 0;
00025     mcrecord = 0;
00026     threcord = 0;
00027     Clear();
00028     whichSource = -1;
00029     isMC = true;
00030     isTH = true;
00031     isEM = true;
00032     Nentries = -1;
00033     return;
00034   }
00035 
00036 
00037   
00038   InitChain(chainSR,chainMC,chainTH,chainEM);
00039   whichSource = 0;
00040 
00041 }
00042 
00043 //********************************************************
00044 
00045 MoqQuantities::MoqQuantities(JobC *j,string path,int entries)
00046 {
00047   record = 0;
00048   emrecord = 0;
00049   mcrecord = 0;
00050   threcord = 0;
00051   Clear();
00052   isMC = true;
00053   isTH = true;
00054   isEM = true;
00055   Nentries = entries;
00056   whichSource = 1;
00057   jcPath = path;
00058   fJC = j;
00059   fChain = NULL;
00060 
00061 
00062 }
00063 
00064 //********************************************************
00065 
00066 MoqQuantities::~MoqQuantities()
00067 {
00068 
00069 }
00070 
00071 //********************************************************
00072 
00073 Bool_t MoqQuantities::IsTrack(){
00074   if(eventSummary->ntrack>0) return true;
00075   return false;
00076 }
00077 
00078 //********************************************************
00079 
00080 Bool_t MoqQuantities::IsSingleTrack(){
00081   if(eventSummary->ntrack==1) return true;
00082   return false;
00083 }
00084 
00085 //********************************************************
00086 
00087 Bool_t MoqQuantities::PassTrackCuts(Int_t itrk){
00088   if(eventSummary->nevent!=1) return false; //is an event
00089   if(!IsTrack()) return false; //is a track
00090   LoadTrack(itrk); //load track
00091   if(ntpTrack->fit.pass==0) return false; //track fit passes
00092   if(ntpTrack->momentum.qp!=ntpTrack->momentum.qp) return false; //no nans
00093   if(fabs(ntpTrack->momentum.qp)<0.0005) return false; //no moms>2000
00094   return true;
00095 }
00096 
00097 //********************************************************
00098 
00099 Bool_t MoqQuantities::PassCuts(Int_t itrk){
00100   //basic fit track cuts:
00101   if(!PassTrackCuts(itrk)) return false;
00102   //check for contained vtx:
00103   //  if(ntpTrack->fidvtx.dr<0.5||ntpTrack->fidvtx.dz<0.5) return false;
00104   return true;
00105 }
00106 
00107 
00108 //********************************************************
00109 
00110 Bool_t MoqQuantities::IsFidAll(Int_t itrk){
00111   if(!LoadTrack(itrk)) return false;
00112   if(ntpTrack->fidall.dr<0.5||ntpTrack->fidall.dz<0.5) return false;
00113   return true;
00114 }
00115 
00116 
00117 //********************************************************
00118 
00119 Int_t MoqQuantities::INu(Int_t itr){
00120   if(!LoadTruth(itr)) return 0;
00121   return ntpTruth->inu;
00122 }
00123 
00124 //********************************************************
00125 
00126 Int_t MoqQuantities::IAction(Int_t itr){
00127   if(!LoadTruth(itr)) return 0;
00128   return ntpTruth->iaction;
00129 }
00130 
00131 //********************************************************
00132 
00133 Int_t MoqQuantities::IResonance(Int_t itr){
00134   if(!LoadTruth(itr)) return 0;
00135   return ntpTruth->iresonance;
00136 }
00137 
00138 
00139 
00140 
00141 //********************************************************
00142 
00143 Float_t MoqQuantities::Y(Int_t itr){
00144   if(!LoadTruth(itr)) return 0;
00145   return ntpTruth->y;
00146 }
00147 
00148 //********************************************************
00149 
00150 Float_t MoqQuantities::W2(Int_t itr){
00151   if(!LoadTruth(itr)) return 0;
00152   return ntpTruth->w2;
00153 }
00154 
00155 //********************************************************
00156 
00157 Float_t MoqQuantities::TrueNuEnergy(Int_t itr){
00158   if(!LoadTruth(itr)) return 0.;
00159   return ntpTruth->p4neu[3];
00160 }
00161 
00162 //********************************************************
00163 
00164 Float_t *MoqQuantities::TrueVtx(Int_t itr){
00165   Float_t *vtx = new Float_t[3];  
00166   vtx[0] = 0.; 
00167   vtx[1] = 0.; 
00168   vtx[2] = 0.;
00169   if(!LoadTruth(itr)) return vtx;
00170   vtx[0] = ntpTruth->vtxx;
00171   vtx[1] = ntpTruth->vtxy;  
00172   vtx[2] = ntpTruth->vtxz;
00173   return vtx;
00174 }
00175 
00176 //********************************************************
00177 
00178 Float_t MoqQuantities::TrueMuEnergy(Int_t itr){
00179   if(!LoadTruth(itr)) return 0;
00180   if(ntpTruth->p4mu1[3]!=0) return fabs(ntpTruth->p4mu1[3]);
00181   return 0;
00182 }
00183 
00184 //********************************************************
00185 
00186 Float_t MoqQuantities::TrueMuQP(Int_t itr){
00187   if(!LoadTruth(itr)) return 0;
00188   if(ntpTruth->p4mu1[3]!=0) {
00189     Float_t p = 1./sqrt(ntpTruth->p4mu1[3]*ntpTruth->p4mu1[3]-0.10555*0.10555);
00190     Float_t q = ntpTruth->p4mu1[3]/fabs(ntpTruth->p4mu1[3]);
00191     return q*p;
00192   }
00193   return 0;
00194 }
00195 
00196 //********************************************************
00197 
00198 Float_t MoqQuantities::TrueShwEnergy(Int_t itr){
00199   if(!LoadTruth(itr)) return 0.;
00200   return ntpTruth->p4shw[3];
00201 }
00202 
00203 //********************************************************
00204 
00205 Float_t MoqQuantities::GetTrueShwSC(Int_t itr){
00206 
00207   if(!LoadTruth(itr)) return 0.;
00208   
00209   Float_t Summed_Shw_SC = 0;
00210   
00211   return Summed_Shw_SC;
00212 }
00213 
00214 //********************************************************
00215 
00216 Double_t MoqQuantities::TrueVxB(Int_t itr){
00217   
00218   if(!LoadTruth(itr)) return -1; //make sure there is an mc event
00219   if(ntpTruth->p4mu1[3]==0) return -1; //make sure there is a muon
00220   
00221   float truvtxu = ntpTruth->vtxx/sqrt(2.) + ntpTruth->vtxy/sqrt(2.);
00222   float truvtxv = - ntpTruth->vtxx/sqrt(2.) + ntpTruth->vtxy/sqrt(2.);
00223   float truvtxz = ntpTruth->vtxz;
00224   TVector3 trupos(truvtxu/100.,truvtxv/100.,truvtxz/100.);
00225   
00226   TVector3 trumom(ntpTruth->p4mu1[0]/sqrt(2.)+ntpTruth->p4mu1[1]/sqrt(2.),
00227                   -ntpTruth->p4mu1[0]/sqrt(2.)+ntpTruth->p4mu1[1]/sqrt(2.),
00228                   ntpTruth->p4mu1[2]);
00229 
00230   //assume that reco z is the same as true z for now:  
00231   return SwimToGetVxB(trupos,trumom,
00232                       int(ntpTruth->p4mu1[3]/fabs(ntpTruth->p4mu1[3])),
00233                       double(ntpTrack->end.z));
00234 }
00235 
00236 
00237 //********************************************************
00238 
00239 Float_t MoqQuantities::RecoMuEnergy(Int_t opt,Int_t itrk){
00240 
00241   if(LoadTrack(itrk)){
00242     if(opt==0){  //return the most appropriate measure of momentum
00243       if(ntpTrack->fidall.dr>0.5&&ntpTrack->fidall.dz>0.5) {
00244         return sqrt(ntpTrack->momentum.range*ntpTrack->momentum.range
00245                     + 0.10555*0.10555);
00246       }
00247       else {
00248         return sqrt(1./(ntpTrack->momentum.qp*ntpTrack->momentum.qp)
00249                     + 0.10555*0.10555);
00250       }
00251     }
00252     else if(opt==1) //return curvature measurement
00253       return sqrt(1./(ntpTrack->momentum.qp*ntpTrack->momentum.qp)
00254                   + 0.10555*0.10555);
00255     else if(opt==2) //return range measurement
00256       return sqrt(ntpTrack->momentum.range*ntpTrack->momentum.range
00257                   + 0.10555*0.10555);
00258     else return 0;
00259   }
00260   return 0.;
00261 }
00262 
00263 //********************************************************
00264 
00265 Float_t MoqQuantities::RecoMuQP(Int_t itrk){
00266   if(LoadTrack(itrk)) return ntpTrack->momentum.qp;
00267   return 0.;
00268 }
00269 
00270 //********************************************************
00271 
00272 Float_t MoqQuantities::GetNonTrkSC(Int_t itrk,Int_t npln){
00273   
00274   Float_t Summed_Stp_SC = 0;
00275   Float_t Summed_Trk_SC = 0;
00276   
00277   bool useAll = false;
00278   if(npln<=0) useAll=true;  //Most basic thing in the world ever: 
00279                             //if it's not in the track it's in the shower
00280   
00281   TClonesArray& stripArray = *(record->stp);
00282   if(LoadTrack(itrk)) {
00283     Int_t numtrkstp = ntpTrack->nstrip;
00284     for(Int_t i=0;i<numtrkstp;i++){
00285       Int_t index = ntpTrack->stp[i];
00286       ntpStrip = dynamic_cast<NtpSRStrip *>(stripArray[index]);
00287       if((ntpStrip->plane <= ntpTrack->vtx.plane + npln
00288           && ntpStrip->plane >= ntpTrack->vtx.plane - npln)||useAll){
00289         Summed_Trk_SC += ntpStrip->ph0.sigcor + ntpStrip->ph1.sigcor;
00290       }
00291     }
00292   }
00293   
00294   for(Int_t i=0;i<int(eventSummary->nstrip);i++) {
00295     if((ntpStrip->plane <= ntpTrack->vtx.plane + npln
00296         && ntpStrip->plane >= ntpTrack->vtx.plane - npln)||useAll){
00297       Summed_Stp_SC += ntpStrip->ph0.sigcor + ntpStrip->ph1.sigcor;
00298     }
00299   }
00300   return Summed_Stp_SC - Summed_Trk_SC;
00301 }
00302 
00303 //********************************************************
00304 
00305 Float_t MoqQuantities::GetShwSC(Int_t itrk){
00306 
00307   //Try something a bit smarter:
00308   //Look for a reconstructed shower near to the track vtx and sum the SigCors
00309   if(!LoadTrack(itrk)) return 0.;
00310   
00311   Float_t Summed_Shw_SC = 0;
00312   Int_t bestshower = -1;
00313   Float_t bestdistance = 1000.;
00314   if(eventSummary->nshower!=0){
00315     for(Int_t i=0;i<eventSummary->nshower;i++){
00316       LoadShower(i);
00317       Float_t distance = sqrt((ntpTrack->vtx.x - ntpShower->vtx.x)
00318                               *(ntpTrack->vtx.x - ntpShower->vtx.x)
00319                               +(ntpTrack->vtx.y - ntpShower->vtx.y)
00320                               *(ntpTrack->vtx.y - ntpShower->vtx.y)
00321                               +(ntpTrack->vtx.z - ntpShower->vtx.z)
00322                               *(ntpTrack->vtx.z - ntpShower->vtx.z));
00323       
00324       if(distance<0.5){
00325         if(bestshower==-1) {
00326           bestshower = i;
00327           bestdistance = distance;
00328         }
00329         else if(distance<bestdistance) bestshower = i;
00330       }
00331     }
00332     
00333     TClonesArray& stripArray = *(record->stp);
00334     if(bestshower!=-1){
00335       LoadShower(bestshower);
00336       Int_t *shwstrips = ntpShower->stp;
00337       for(Int_t j=0;j<ntpShower->nstrip;j++){
00338         Int_t index = shwstrips[j];
00339         ntpStrip = dynamic_cast<NtpSRStrip *>(stripArray[index]);
00340         Summed_Shw_SC += ntpStrip->ph0.sigcor + ntpStrip->ph1.sigcor;
00341       }
00342       return Summed_Shw_SC;
00343     }
00344     return 0;
00345   }
00346   else return 0; //no reconstructed shower
00347 
00348 }
00349 
00350 //********************************************************
00351 
00352 Float_t MoqQuantities::RecoShwEnergy(Int_t opt){
00353   
00354   Float_t theGeV = 0;
00355   //  if(opt==0) theGeV = GetShwSC(0)/10000.0; //MDC
00356   //else theGeV = GetNonTrkSC()/10000.0;
00357 
00358    // Return Jim's calculation
00359   if(LoadShower(opt)) theGeV=ntpShower->ph.gev;
00360   else theGeV=0.;
00361 
00362   return theGeV;
00363   
00364 }
00365 
00366 //********************************************************
00367 
00368 Int_t MoqQuantities::GetChargeSign(Int_t itrk){  
00369   if(RecoMuQP(itrk)>0) return 1;
00370   return -1;
00371 }
00372 
00373 //********************************************************
00374 
00375 Float_t MoqQuantities::RecoQENuEnergy(Int_t itrk){
00376   
00377   if(!LoadTrack(itrk)) return 0.;
00378   Float_t nucleonMass = 0.93956563; //mass of neutron by default
00379   if(GetChargeSign(itrk)==1) nucleonMass = 0.93827231; //proton mass for nubar
00380   Float_t muonEnergy = RecoMuEnergy(0,itrk);
00381   Float_t muonMass = 0.10555;
00382   Float_t muonMomentum = sqrt(muonEnergy*muonEnergy - muonMass*muonMass);
00383   Float_t bl_z = TMath::Cos(TMath::Pi()*3./180.); //3degree beam
00384   Float_t bl_y = sqrt(1. - bl_z*bl_z);
00385   Float_t costhbl = ntpTrack->vtx.dcosz*bl_z + ntpTrack->vtx.dcosy*bl_y;
00386   
00387   return (nucleonMass*muonEnergy - muonMass*muonMass/2.)
00388     /(nucleonMass - muonEnergy + muonMomentum*costhbl);  
00389 
00390 }
00391 
00392 //********************************************************
00393 
00394 Double_t MoqQuantities::VxB(Int_t itrk){
00395   
00396   double mom = RecoMuQP(itrk); //this loads ntpTrack itrk
00397   if(fabs(mom)<0.0001) return -1;  //check for no track, q/p = 0
00398   mom = 1./fabs(mom);
00399   TVector3 position(ntpTrack->vtx.u,
00400                     ntpTrack->vtx.v,
00401                     ntpTrack->vtx.z);
00402   TVector3 momentum(mom*ntpTrack->vtx.dcosu,
00403                     mom*ntpTrack->vtx.dcosv,
00404                     mom*ntpTrack->vtx.dcosz);
00405   
00406   if(mom>0.05) return SwimToGetVxB(position,momentum,
00407                                    GetChargeSign(itrk),
00408                                    double(ntpTrack->end.z));
00409   return -1;
00410 
00411 }
00412 
00413 //*******************************************************
00414 
00415 Double_t MoqQuantities::SwimToGetVxB(TVector3 pos,TVector3 mom,
00416                                     Int_t chg,Double_t z){
00417   
00418   SwimSwimmer ss(ntpHeader->GetVldContext());
00419   double muMass = 0.105658357*Munits::GeV;
00420   SwimParticle swimMu(pos,mom,muMass,chg);
00421   SwimZCondition zc(z);
00422   Bool_t done = ss.SwimForward(swimMu,zc);
00423   if(done) return swimMu.GetVxB();
00424 
00425   return swimMu.GetVxB(); //might not want to do this later
00426 
00427 }
00428 
00429 //*******************************************************
00430 void MoqQuantities::MakeHistoFile(std::string tag){
00431 
00432   std::string savename =  tag + ".ed.root";
00433   TFile save(savename.c_str(),"RECREATE"); 
00434   save.cd();
00435   
00436     //#declare lots of histos:
00437   TH1F *TrueEnergy = new TH1F("TrueEnergy","True Neutrino Energy",200,0,50);
00438 
00439   TH1F *RecoEnergy = new TH1F("RecoEnergy","Reco Neutrino Energy",200,0,50);
00440 
00441   TH1F *TrueMuEn = new TH1F("TrueMuEn","True Muon Energy",200,0,50);
00442 
00443   TH1F *RecoMuEn = new TH1F("RecoMuEn","Reco Muon Energy",200,0,50);
00444   
00445   TH1F *TrueShwEn = new TH1F("TrueShwEn","True Shower Energy",200,0,50);
00446 
00447   TH1F *RecoShwEn = new TH1F("RecoShwEn","Reco Shower Energy",200,0,50);
00448   
00449   TH1F *TrueMuQP = new TH1F("TrueMuQP","True Muon q/p",300,-3,3);
00450 
00451   TH1F *RecoMuQP = new TH1F("RecoMuQP","Reconstructed Muon q/p",300,-3,3);
00452   
00453   TH2F *TrueVsReco = new TH2F("TrueVsReco",
00454                               "Reco Vs True Neutrino Energy",
00455                               200,0,50,200,0,50);
00456 
00457   TH2F *TrueVsRecoMu = new TH2F("TrueVsRecoMu",
00458                                 "Reco Vs True Muon Energy",200,0,50,200,0,50);
00459 
00460   TH2F *TrueVsRecoShw = new TH2F("TrueVsRecoShw",
00461                                  "Reco Vs True Shower Energy",
00462                                  200,0,50,200,0,50);
00463   
00464   TH2F *TrueRecoDiff 
00465     = new TH2F("TrueRecoDiff",
00466                "(True - Reco)/True Neutrino Energy vs True Neutrino Energy",
00467                200,0,50,1000,-9,1);
00468 
00469   //  TH2F *TrueRecoDiffMuCurv 
00470   //  = new TH2F("TrueRecoDiffMuCurv",
00471   //       "(True - Reco_{Curv})/True Muon Energy vs True Muon Energy",
00472   //       200,0,50,1000,-9,1);
00473   
00474 
00475 
00476   TH2F *TrueRecoDiffShw 
00477     = new TH2F("TrueRecoDiffShw",
00478                "(True - Reco)/True Shower Energy vs True Shower Energy",
00479                200,0,50,1000,-9,1);
00480   
00481 
00482   //  TH2F *TrueCurvMu 
00483   //= new TH2F("TrueCurvMu",
00484   //       "Reco Muon Energy from Curvature vs True Muon Energy",
00485   //       200,0,50,200,0,50); 
00486 
00487 
00488   for(int i=0;i<Nentries;i++){
00489     
00490     if(i%1000==0) std::cout << float(i)*100./float(Nentries) 
00491                             << "% done" << std::endl;
00492 
00493     if(!GetEntry(i)) continue;
00494     
00495   
00496     
00497       if(this->PassCuts(0)){
00498           //neutrino
00499         TrueEnergy->Fill(this->TrueNuEnergy());
00500         RecoEnergy->Fill(this->RecoMuEnergy(1,0) + this->RecoShwEnergy());
00501         TrueVsReco->Fill(this->TrueNuEnergy(),this->RecoMuEnergy(1,0) 
00502                          + this->RecoShwEnergy());
00503         TrueRecoDiff->Fill(this->TrueNuEnergy(),
00504                            (this->TrueNuEnergy()-(this->RecoMuEnergy(1,0)
00505                                                   +this->RecoShwEnergy()))
00506                            /this->TrueNuEnergy());
00507         
00508 
00509         //shower          
00510         TrueShwEn->Fill(this->TrueShwEnergy());
00511         RecoShwEn->Fill(this->RecoShwEnergy());
00512         TrueVsRecoShw->Fill(this->TrueShwEnergy(),this->RecoShwEnergy());
00513         TrueRecoDiffShw->Fill(this->TrueShwEnergy(),
00514                               (this->TrueShwEnergy()
00515                                -this->RecoShwEnergy())
00516                               /this->TrueShwEnergy());
00517 
00518 
00519         //muon
00520         TrueMuEn->Fill(this->TrueMuEnergy());
00521         RecoMuEn->Fill(this->RecoMuEnergy(0,0));
00522         TrueVsRecoMu->Fill(this->TrueMuEnergy(),this->RecoMuEnergy(1,0));
00523         
00524         //curvature
00525         //TrueRecoDiffMuCurv->Fill(this->TrueMuEnergy(),
00526         //                       (this->TrueMuEnergy()
00527         //                -this->RecoMuEnergy(1,0))
00528         //               /this->TrueMuEnergy());
00529         //TrueCurvMu->Fill(this->TrueMuEnergy(),this->RecoMuEnergy(1,0));
00530         
00531         TrueMuQP->Fill(this->TrueMuQP());
00532         RecoMuQP->Fill(this->RecoMuQP(0));
00533         
00534       }
00535 
00536 
00537   }
00538 
00539  //#close file
00540   save.Write();
00541   save.Close();
00542 }
00543     
00544 
00545 
00546 #endif // #ifdef moqquantities_cxx

Generated on Mon Nov 23 05:27:25 2009 for loon by  doxygen 1.3.9.1