00001 #ifndef gnumiinterface_cxx
00002 #define gnumiinterface_cxx
00003 #include "StandardNtuple/NtpStRecord.h"
00004 #include "MCNtuple/NtpMCRecord.h"
00005 #include "MCNtuple/NtpMCTruth.h"
00006 #include "MCNtuple/NtpMCStdHep.h"
00007 #include "AnalysisNtuples/ANtpTruthInfoBeam.h"
00008 #include "MCReweight/GnumiInterface.h"
00009 #include "MCReweight/NuParent.h"
00010 #include "TClonesArray.h"
00011 #include <iostream>
00012
00013 using namespace std;
00014
00015
00016 GnumiInterface::GnumiInterface()
00017 {
00018 fok = false;
00019 char *dir = getenv("GNUMIAUX");
00020 if(dir!=NULL) fdir = dir;
00021 else fdir = ".";
00022 if(OpenFiles()) SetAddresses();
00023 }
00024
00025
00026 GnumiInterface::~GnumiInterface()
00027 {
00028 CloseFiles();
00029 }
00030
00031
00032 Bool_t GnumiInterface::SetFileDir(std::string dir)
00033 {
00034 fdir = dir;
00035 if(OpenFiles()) {
00036 SetAddresses();
00037 return true;
00038 }
00039 return false;
00040 }
00041
00042
00043 Bool_t GnumiInterface::OpenFiles()
00044 {
00045 map<Int_t,TFile*>::iterator End = fFile.end();
00046 for(int i=1;i<=20;i++){
00047 map<Int_t,TFile*>::iterator Cur = fFile.find(i);
00048 if(Cur!=End){
00049 TFile *f1 = Cur->second;
00050 f1->Close();
00051 delete f1;
00052 fFile.erase(Cur->first);
00053 fTree.erase(Cur->first);
00054 }
00055 char name[256];
00056 sprintf(name,"%s/job%i/le_%i.root",fdir.c_str(),i,i);
00057 cout << name << endl;
00058 TFile *f = new TFile(name,"READ");
00059 if(f->IsOpen() && !f->IsZombie()){
00060 fFile[i] = f;
00061 TTree *tree = (TTree*) f->Get("h10");
00062 fTree[i] = tree;
00063 }
00064 else if(i==1) return false;
00065 else break;
00066 }
00067 fok = true;
00068 return true;
00069 }
00070
00071
00072 void GnumiInterface::SetAddresses()
00073 {
00074 if(!fok) return;
00075 map<Int_t,TTree*>::iterator Beg = fTree.begin();
00076 map<Int_t,TTree*>::iterator End = fTree.end();
00077 while(Beg!=End){
00078 TTree *tree = Beg->second;
00079 tree->SetBranchAddress("run",&fRunNum);
00080 tree->SetBranchAddress("evtno",&fEvtNum);
00081 tree->SetBranchAddress("tvx",&fX);
00082 tree->SetBranchAddress("tvy",&fY);
00083 tree->SetBranchAddress("tvz",&fZ);
00084 tree->SetBranchAddress("tpx",&fPX);
00085 tree->SetBranchAddress("tpy",&fPY);
00086 tree->SetBranchAddress("tpz",&fPZ);
00087 tree->SetBranchAddress("tptype",&fPID);
00088 tree->SetBranchAddress("tgen",&fGen);
00089 Beg++;
00090 }
00091
00092 }
00093
00094
00095 void GnumiInterface::CloseFiles()
00096 {
00097 fTree.clear();
00098 map<Int_t,TFile*>::iterator Beg = fFile.begin();
00099 map<Int_t,TFile*>::iterator End = fFile.end();
00100 while(Beg!=End){
00101 TFile *f = Beg->second;
00102 f->Close();
00103 delete f;
00104 fFile.erase(Beg->first);
00105 Beg++;
00106 }
00107 fok = false;
00108 }
00109
00110
00111 void GnumiInterface::GetParent(Int_t fileNum,Int_t entryNum,
00112 Int_t runNum,Int_t evtNum,
00113 NuParent &par)
00114 {
00115 par.Zero();
00116
00117
00118
00119
00120
00121
00122 fileNum = runNum;
00123
00124
00125
00126
00127
00128 if(fileNum==99) fileNum = 9;
00129
00130
00131
00132 entryNum -= 1;
00133
00134 map<Int_t,TTree*>::iterator End = fTree.end();
00135 map<Int_t,TTree*>::iterator Cur = fTree.find(fileNum);
00136 if(Cur==End) return;
00137 Cur->second->GetEntry(entryNum);
00138 if(fRunNum==runNum && fEvtNum==evtNum) {
00139 par.SetX(fX);
00140 par.SetY(fY);
00141 par.SetZ(fZ);
00142 par.SetPx(fPX);
00143 par.SetPy(fPY);
00144 par.SetPz(fPZ);
00145 par.SetPID(fPID);
00146 par.SetGen(fGen);
00147 }
00148 return;
00149 }
00150
00151
00152 void GnumiInterface::GetParent(NtpMCRecord *rec,int mcevent,
00153 NuParent &par)
00154 {
00155
00156 Int_t filenum = -1;
00157 Int_t entrynum = -1;
00158 Int_t runnum = -1;
00159 Int_t evtnum = -1;
00160
00161 TClonesArray& heparray = *(rec->stdhep);
00162 Int_t nhep = heparray.GetEntries();
00163 for(int i=0;i<nhep;i++){
00164 NtpMCStdHep *sh = static_cast<NtpMCStdHep *>(heparray[i]);
00165 if(sh->mc==mcevent){
00166 if(sh->IstHEP==999){
00167 runnum = Int_t(sh->p4[0]);
00168 evtnum = Int_t(sh->p4[1]);
00169 filenum = Int_t(sh->p4[2]);
00170 entrynum = Int_t(sh->p4[3]);
00171 }
00172 }
00173 }
00174 this->GetParent(filenum,entrynum,runnum,evtnum,par);
00175 }
00176
00177
00178 void GnumiInterface::GetParent(NtpStRecord *rec,int mcevent,
00179 NuParent &par)
00180 {
00181
00182 Int_t filenum = -1;
00183 Int_t entrynum = -1;
00184 Int_t runnum = -1;
00185 Int_t evtnum = -1;
00186
00187 TClonesArray& heparray = *(rec->stdhep);
00188 Int_t nhep = heparray.GetEntries();
00189 for(int i=0;i<nhep;i++){
00190 NtpMCStdHep *sh = static_cast<NtpMCStdHep *>(heparray[i]);
00191 if(sh->mc==mcevent){
00192 if(sh->IstHEP==999){
00193 runnum = Int_t(sh->p4[0]);
00194 evtnum = Int_t(sh->p4[1]);
00195 filenum = Int_t(sh->p4[2]);
00196 entrynum = Int_t(sh->p4[3]);
00197 }
00198 }
00199 }
00200 this->GetParent(filenum,entrynum,runnum,evtnum,par);
00201 }
00202
00203
00204 void GnumiInterface::FillANtpTruth(Int_t fileNum,
00205 Int_t entryNum,
00206 Int_t runNum,
00207 Int_t evtNum,
00208 ANtpTruthInfoBeam &ntpBeam)
00209 {
00210 NuParent parent;
00211 this->GetParent(fileNum,entryNum,runNum,evtNum,parent);
00212 ntpBeam.parentX = parent.GetX();
00213 ntpBeam.parentY = parent.GetY();
00214 ntpBeam.parentZ = parent.GetZ();
00215 ntpBeam.parentPX = parent.GetPx();
00216 ntpBeam.parentPY = parent.GetPy();
00217 ntpBeam.parentPZ = parent.GetPz();
00218 ntpBeam.parentPID = parent.GetPID();
00219 ntpBeam.parentGen = parent.GetGen();
00220 }
00221
00222
00223 void GnumiInterface::FillANtpTruth(NtpMCRecord *rec,int mcevent,
00224 ANtpTruthInfoBeam &ntpBeam)
00225 {
00226 NuParent parent;
00227 this->GetParent(rec,mcevent,parent);
00228 ntpBeam.parentX = parent.GetX();
00229 ntpBeam.parentY = parent.GetY();
00230 ntpBeam.parentZ = parent.GetZ();
00231 ntpBeam.parentPX = parent.GetPx();
00232 ntpBeam.parentPY = parent.GetPy();
00233 ntpBeam.parentPZ = parent.GetPz();
00234 ntpBeam.parentPID = parent.GetPID();
00235 ntpBeam.parentGen = parent.GetGen();
00236 }
00237
00238
00239 void GnumiInterface::FillANtpTruth(NtpStRecord *rec,int mcevent,
00240 ANtpTruthInfoBeam &ntpBeam)
00241 {
00242 NuParent parent;
00243 this->GetParent(rec,mcevent,parent);
00244 ntpBeam.parentX = parent.GetX();
00245 ntpBeam.parentY = parent.GetY();
00246 ntpBeam.parentZ = parent.GetZ();
00247 ntpBeam.parentPX = parent.GetPx();
00248 ntpBeam.parentPY = parent.GetPy();
00249 ntpBeam.parentPZ = parent.GetPz();
00250 ntpBeam.parentPID = parent.GetPID();
00251 ntpBeam.parentGen = parent.GetGen();
00252 }
00253
00254 #endif