00001 #include "NueAna/ParticlePID/MiniMakerPID.h"
00002 #include "NueAna/NueStandard.h"
00003 #include "NueAna/ParticlePID/NueMiniAnaPID.h"
00004
00005 #include "DataUtil/MCInfo.h"
00006 #include "DataUtil/DataQualDB.h"
00007 #include "NueAna/AnnAna.h"
00008
00009 MiniMakerPID::MiniMakerPID(){
00010 outSet = false;
00011 fReweight = false;
00012 fBaseline = 735;
00013 fDeltaMSquare = 0.0027;
00014 fUe3Square = 0.025;
00015 fTheta23 = TMath::Pi()/4;
00016
00017 fOverWritePOT = false;
00018
00019 fIsMRCC=0;
00020 }
00021
00022 void MiniMakerPID::AddFiles(string infiles)
00023 {
00024 files.push_back(infiles);
00025 }
00026
00027 void MiniMakerPID::SetOutputFile(string file)
00028 {
00029 outSet = true;
00030 outFile = file;
00031 }
00032
00033 void MiniMakerPID::Config(Registry &r)
00034 {
00035 fCuts.Config(r);
00036 }
00037
00038 void MiniMakerPID::SetDeltaMSquare(double dm2) { fDeltaMSquare = dm2; fReweight = true;}
00039 void MiniMakerPID::SetUe3Square(double dUe32) { fUe3Square = dUe32; fReweight = true;}
00040 void MiniMakerPID::SetTheta23(double t23) { fTheta23 = t23; fReweight = true;}
00041 void MiniMakerPID::SetBaseline(double bl) { fBaseline = bl; fReweight = true;}
00042
00043 void MiniMakerPID::RunMiniMakerPID()
00044 {
00045 NueRecord *nr = new NueRecord();
00046 NuePOT *np = new NuePOT();
00047 NuePOT *total = new NuePOT();
00048
00049 TChain *chain = new TChain("ana_nue");
00050 chain->SetBranchAddress("NueRecord",&nr);
00051
00052 TChain *pchain = new TChain("pottree");
00053 pchain->SetBranchAddress("NuePOT", &np);
00054
00055 int nchain=0;
00056 int npchain=0;
00057
00058 for(unsigned int i = 0; i < files.size(); i++)
00059 {
00060 nchain += chain->Add(files[i].c_str());
00061 npchain += pchain->Add(files[i].c_str());
00062 }
00063
00064
00065 if(nchain != npchain || !nchain || !npchain)
00066 {
00067 printf("missing tree in input file.... exiting with error!\n");
00068 exit(1);
00069 }
00070
00071 if(pchain->GetEntries() > 0){
00072 pchain->GetEntry(0);
00073 total->beamtype = np->beamtype;
00074 }
00075
00076 if(!outSet){
00077 string file;
00078
00079 if(pchain->GetEntries() > 0){
00080 pchain->GetEntry(0);
00081 total->beamtype = np->beamtype;
00082 file = pchain->GetFile()->GetName();
00083 }
00084 string minifile = file.substr(file.find_last_of("/")+1, file.find_last_of(".root")-file.find_last_of("/") - 5);
00085 minifile += "-Mini.root";
00086
00087 outFile = minifile;
00088 }
00089 if(outFile == "-Mini.root"){
00090 cout<<"No input file found"<<endl;
00091 return;
00092 }
00093
00094 cout<<"Setting output to "<<outFile<<endl;
00095
00096
00097
00098 TFile *save = new TFile(outFile.c_str(),"RECREATE");
00099 save->SetCompressionLevel(9);
00100 save->cd();
00101 TTree *tree = new TTree("NueMiniPID","NueMiniPID");
00102 TTree::SetBranchStyle(1);
00103
00104 if(chain->GetEntries() > 0) chain->GetEvent(0);
00105 Detector::Detector_t det = nr->GetHeader().GetVldContext().GetDetector();
00106 ReleaseType::Release_t rel = nr->GetHeader().GetRelease();
00107 BeamType::BeamType_t beam = nr->GetHeader().GetBeamType();
00108
00109 NueMiniAnaPID nma;
00110
00111 NueMiniPID* nm = new NueMiniPID(beam, det, rel);
00112
00113 TBranch* br = tree->Branch("NueMiniPID","NueMiniPID", &nm );
00114 br->SetAutoDelete(kFALSE);
00115
00116 Int_t n = chain->GetEntries();
00117 int count = 0;
00118
00119 bool isMC = false;
00120 double TotPOT = 0.0;
00121
00122 NueStandard::SetDefaultOscParam();
00123 NueStandard::SetOscNoMatter();
00124
00125 for(int i=0;i<n;i++){
00126 if(i%10000==0) cout << 100*float(i)/float(n)
00127 << "% done" << endl;
00128 chain->GetEvent(i);
00129
00130 if(i == 0){
00131 SimFlag::SimFlag_t sim = nr->GetHeader().GetVldContext().GetSimFlag();
00132 isMC = (sim == SimFlag::kMC);
00133 }
00134
00135
00136 nm->fPOT=0;
00137
00138
00139
00140 nr->eventq.passFarDetQuality = DataUtil::IsGoodData(nr->GetHeader().GetVldContext());
00141 nr->eventq.passNearDetQuality = nr->eventq.passFarDetQuality;
00142 if( NueStandard::PassesPOTStandards(nr))
00143 {
00144
00145 if(nr->GetHeader().GetEventNo() == 0 ||
00146 nr->GetHeader().GetEvents() == 0){
00147 TotPOT += nr->bmon.bI;
00148 nm->fPOT=nr->bmon.bI;
00149 if(nr->bmon.bI < 0 && !isMC)
00150 {
00151 cout<<"Unexpected behavior"<<endl;
00152 exit(2);
00153 }
00154 }
00155 }
00156
00157
00158
00159 AnnAna z(*nr);
00160 z.Analyze();
00161
00162
00163
00164 nma.FillMini(nr, nm, fIsMRCC);
00165
00166
00167 if(EvaluateCuts(nr,nm)){
00168
00169 if(fReweight && nr->mctrue.nuFlavor > -10)
00170 {
00171
00172
00173
00174
00175 Float_t newWeight = NueStandard::GetOscWeight(nr->mctrue.nuFlavor,nr->mctrue.nonOscNuFlavor,nr->mctrue.nuEnergy);
00176
00177 nr->mctrue.Ue3Squared = fUe3Square;
00178 nr->mctrue.DeltamSquared23 = fDeltaMSquare;
00179 nr->mctrue.Theta23 = fTheta23;
00180 nr->mctrue.fOscProb = newWeight;
00181
00182
00183 nma.FillMini(nr, nm, fIsMRCC);
00184 }
00185
00186
00187 tree->Fill();
00188 count++;
00189 }else{
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 }
00200 }
00201 cout<<count<<" of "<<n<<" entries were passed"<<endl;
00202
00203 TTree *ptree = new TTree("pottree","pottree");
00204 TBranch* br2 = ptree->Branch("NuePOT","NuePOT", &total );
00205 br2->SetAutoDelete(kFALSE);
00206
00207 for(int i = 0; i < pchain->GetEntries(); i++)
00208 {
00209 pchain->GetEntry(i);
00210
00211 cout <<"bt "<<total->beamtype <<" "<<np->beamtype<<"\n";
00212 if(total->beamtype != np->beamtype)
00213 cerr<<"You are merging files of different beamtyp - BAD"<<endl;
00214
00215 total->nruns += np->nruns;
00216 total->nsnarls += np->nsnarls;
00217 total->pot += np->pot;
00218 }
00219
00220 if(fOverWritePOT && !isMC) total->pot = TotPOT;
00221
00222 ptree->Fill();
00223
00224 save->cd();
00225 tree->Write();
00226 ptree->Write();
00227
00228 cout<<"Compression level of output file "<<save->GetCompressionLevel()<<"\n";
00229
00230 cout<<"Trimming completed with "<<total->pot<<"x10^12 POT"<<endl;
00231 save->Close();
00232 }
00233
00234 void MiniMakerPID::SetCuts(string type, int level)
00235 {
00236 cutSet = type;
00237 cutLevel = level;
00238 }
00239
00240 bool MiniMakerPID::EvaluateCuts(NueRecord *nr, NueMiniPID *nm)
00241 {
00242 NueConvention::NueEnergyCorrection(nr);
00243
00244 if(cutSet == "Fiducial")
00245 return NueStandard::PassesSelection(nr, Selection::kFid);
00246
00247 if(cutSet == "Standard"){
00248 bool ret;
00249 switch(cutLevel){
00250 case 0: return NueStandard::PassesSelection(nr, Selection::kDataQual);
00251 case 1: return NueStandard::PassesSelection(nr, Selection::kFid);
00252 case 2:
00253 ret = NueStandard::PassesSelection(nr, Selection::kFid);
00254 ret = ret && NueStandard::PassesNonHEPreSelection(nr);
00255 return ret;
00256 case 3: return NueStandard::PassesSelection(nr, Selection::kPre);
00257 case 4: return nm->infid && nm->contained && ( fIsMRCC ? nm->mrcc_s : 1 );
00258 case 6: return NueStandard::PassesSelection(nr, Selection::kDataQual) && ((nm->infid && nm->contained ) || NueStandard::PassesSelection(nr, Selection::kFid) ) || nm->fPOT;
00259
00260
00261 }
00262 }
00263 if(cutSet == "MRE" || cutSet == "MRCC"){
00264 bool dq = NueStandard::PassesSelection(nr, Selection::kDataQual);
00265 bool mrefid = NueStandard::PassesMREFiducial(nr);
00266 bool mrepre = NueStandard::PassesMREPreSelection(nr) && mrefid && dq;
00267 bool ret;
00268 switch(cutLevel){
00269 case 0: return mrefid && dq;
00270 case 1: return mrepre;
00271 case 2:
00272 return NueStandard::PassesSelection(nr, Selection::kFid) && mrepre;
00273 case 3:
00274 ret = NueStandard::PassesSelection(nr, Selection::kFid);
00275 ret = ret && NueStandard::PassesNonHEPreSelection(nr);
00276 return ret && mrepre;
00277 case 4:
00278 return NueStandard::PassesSelection(nr, Selection::kPre) && mrepre;
00279 }
00280 }
00281 if(cutSet == "Presel")
00282 return NueStandard::PassesSelection(nr, Selection::kPre);
00283
00284 if(cutSet == "PreselNoHE")
00285 return NueStandard::PassesNonHEPreSelection(nr);
00286
00287 if(cutSet == "Systematic")
00288 return NueStandard::PassesSysPreSelection(nr);
00289
00290 if(cutSet == "CC")
00291 return NueStandard::PassesSelection(nr, Selection::kCC);
00292
00293
00294 cout<<"Invalid Cut Level for "<<cutSet<<": "<<cutLevel<<endl;
00295 return false;
00296 }