00001
00002
00003
00004
00005
00007 #include "TH1F.h"
00008 #include "TFile.h"
00009 #include "Conventions/Detector.h"
00010 #include "NueAna/CompareAll.h"
00011 #include "NueAna/NueRecord.h"
00012 #include "NueAna/EventFilter.h"
00013 #include "MessageService/MsgService.h"
00014 #include "MinosObjectMap/MomNavigator.h"
00015 #include "MessageService/MsgService.h"
00016 #include "JobControl/JobCModuleRegistry.h"
00017 #include "HistMan/HistMan.h"
00018 #include <fstream>
00019 #include "AnalysisNtuples/ANtpDefaultValue.h"
00020 #include "TClass.h"
00021 #include "TDataType.h"
00022 #include "TDataMember.h"
00023 #include "TRealData.h"
00024 #include "AnalysisNtuples/ANtpDefaultValue.h"
00025 #include <string>
00026
00027 const int CALend=110;
00028 const int SM1end=230;
00029 const int SM2end=484;
00030
00031 JOBMODULE(CompareAll, "CompareAll",
00032 "Makes hists to All variables between detectors and truth classes");
00033 CVSID("$Id:");
00034
00035
00036 CompareAll::CompareAll():
00037 counter(0),
00038 kHiPlaneTrackCut(25),
00039 kLoPlaneEventCut(-1),
00040 kHiTrackLikeCut(-1),
00041 kDPlaneCut(-1),
00042 kLoPhNStripCut(-1),
00043 kLoPhNPlaneCut(-1),
00044 kHiEnergyCut(-1),
00045 kLoEnergyCut(-1),
00046 kHiEnergyShowerCut(-1),
00047 kLoEnergyShowerCut(-1),
00048 kPhStripCut(-1),
00049 kPhPlaneCut(-1),
00050 kLoCurrentCut(0.1),
00051 kLoHorBeamWidth(0.0),
00052 kHiHorBeamWidth(2.9),
00053 kLoVertBeamWidth(0.0),
00054 kHiVertBeamWidth(2.9),
00055 kLoNuTarZ(-1),
00056 kHiNuTarZ(1000),
00057 kOscillate(0),
00058 kOutputFile("HistManOut.root")
00059 {}
00060
00061
00062
00063 CompareAll::~CompareAll()
00064 {}
00065
00066
00067 void CompareAll::BeginJob()
00068 {
00069
00070 if(counter%1000==0){
00071 cout<<"On entry "<<counter<<endl;
00072 }
00073 counter++;
00074
00075 string dumName;
00076 TString dumtype;
00077 Float_t dumstart, dumend;
00078 Int_t dumbins, nvar;
00079 ifstream ins;
00080 ins.open("AllParam.txt");
00081
00082 nvar = 0;
00083
00084 fPOT[0] = fPOT[1] = fPOT[2] = fPOT[3] = 0.0;
00085 for(int i = 0 ; i < 4; i++)
00086 fOscParams[i] = ANtpDefVal::kFloat;
00087
00088
00089 while(!ins.eof()) {
00090 ins>>dumName>>dumstart>>dumend>>dumbins>>dumtype;
00091 if(!ins.eof()){
00092 varName.push_back(dumName);
00093 beg.push_back(dumstart); end.push_back(dumend);
00094 nbins.push_back(dumbins); gtype.push_back(dumtype);
00095 nvar++;
00096 }
00097 }
00098 cout<<nvar<<" variables read in"<<endl;
00099
00100
00101
00102
00103
00104 vector<TString> names;
00105 names.push_back("_far_mc_nue");
00106 names.push_back("_far_mc_numu");
00107 names.push_back("_far_mc_bnue");
00108 names.push_back("_far_mc_nutau");
00109 names.push_back("_far_mc_nc");
00110 names.push_back("_near_mc_bnue");
00111 names.push_back("_near_mc_numu");
00112 names.push_back("_near_mc_nc");
00113 names.push_back("_far_data");
00114 names.push_back("_near_data");
00115 names.push_back("_unknown");
00116
00117
00118 for(UInt_t i = 0; i < names.size(); i++){
00119 for(UInt_t l = 0; l < varName.size(); l++){
00120 string temp = (varName[l]);
00121 string dirstring = "allcomp/" + temp.substr(0,temp.find_first_of("."));
00122 HistMan hm2(dirstring.c_str());
00123 TString param = varName[l] + names[i];
00124 hm2.Book<TH1F>(param,param,nbins[l],beg[l],end[l]);
00125 }
00126 }
00127
00128 fHistRecord = new TTree("histRecord", "Historgram Filling Information");
00129
00130 fHistRecord->Branch("POT", &fPOT, "farMCPOT/F:farDataPOT/F:nearMCPOT/F:nearDataPOT/F");
00131 fHistRecord->Branch("OscParams", &fOscParams, "Baseline/F:deltam23/F:theta23/F:Ue3/F");
00132
00133 }
00134
00135
00136 JobCResult CompareAll::Ana(const MomNavigator* mom)
00137 {
00138
00139
00140
00141 TObject *obj=0;
00142
00143 static Int_t runno = -1;
00144 static Int_t snarlno = -1;
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 TIter objiter = mom->FragmentIter();
00159 while((obj=objiter.Next())){
00160 NueRecord *nr = static_cast<NueRecord *>(obj);
00161 if(nr){
00162 MSG("CompareAll",Msg::kDebug)<<"Found a NueRecord in MOM"<<endl;
00163 }
00164 else{
00165 MSG("CompareAll",Msg::kError)<<"Didn't find a NueRecord in MOM"<<endl;
00166 continue;
00167 }
00168 MSG("CompareAll",Msg::kDebug)<<"Found a NueRecord "<<nr<<endl;
00169 SimFlag::SimFlag_t s = nr->GetHeader().GetVldContext().GetSimFlag();
00170 Detector::Detector_t d =
00171 nr->GetHeader().GetVldContext().GetDetector();
00172
00173
00174
00175 if(counter%1000==0){
00176 cout<<"On entry "<<counter<<endl;
00177 }
00178 counter++;
00179
00180 if(nr->GetHeader().GetEventNo()<0){
00181 continue;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191 if(s==SimFlag::kData){
00192 if(PassesBeamCuts(nr)){
00193 if (runno != nr->GetHeader().GetRun() ||
00194 runno == nr->GetHeader().GetRun() &&
00195 snarlno != nr->GetHeader().GetSnarl())
00196 {
00197 runno = nr->GetHeader().GetRun();
00198 snarlno = nr->GetHeader().GetSnarl();
00199
00200 if(d == Detector::kFar)
00201 fPOT[far_data] += nr->bmon.bI;
00202 if(d == Detector::kNear)
00203 fPOT[near_data] += nr->bmon.bI;
00204 }
00205 }
00206 }
00207 if(s==SimFlag::kMC){
00208 if (runno != nr->GetHeader().GetRun() )
00209 {
00210 runno = nr->GetHeader().GetRun();
00211
00212 if(d == Detector::kFar)
00213 fPOT[far_mc] += 6.5e8;
00214 if(d == Detector::kNear)
00215 fPOT[near_mc] += 550*24.;
00216 }
00217 if(fOscParams[0] < 0)
00218 {
00219 fOscParams[0] = nr->mctrue.Baseline;
00220 fOscParams[1] = nr->mctrue.DeltamSquared23;
00221 fOscParams[2] = nr->mctrue.Theta23;
00222 fOscParams[3] = nr->mctrue.Ue3Squared;
00223 }
00224
00225 }
00226
00227 if(PassesCuts(nr)){
00228 if (s==SimFlag::kData){
00229 if(PassesBeamCuts(nr)){
00230 TString id = MakeIdString(nr);
00231 FillFromList(nr,id,1.);
00232 }
00233 }else{
00234 TString id = MakeIdString(nr);
00235 Float_t weight = 1.0;
00236 if(kOscillate) weight = nr->mctrue.fOscProb*nr->mctrue.fNueWeight;
00237 FillFromList(nr,id,weight);
00238 }
00239 }
00240 }
00241
00242 return JobCResult::kPassed;
00243 }
00244
00246 void CompareAll::EndJob()
00247 {
00248
00249
00250
00251
00252
00253
00254 if(kOscillate){
00255 fPOT[far_mc] = fPOT[far_mc]/3.0;
00256 }
00257
00258 TFile* file = new TFile(kOutputFile.c_str(), "update");
00259 file->cd();
00260 fHistRecord->Fill();
00261 fHistRecord->Write();
00262
00263
00264 vector<TString> branches;
00265 for(UInt_t i = 0; i < varName.size(); i++)
00266 {
00267 string temp = (varName[i]);
00268 string dirstring = "allcomp/" + temp.substr(0,temp.find_first_of("."));
00269 bool newBranch = true;
00270
00271 for(UInt_t j = 0; j < branches.size() && newBranch; j++)
00272 {
00273 if(dirstring == branches[j])
00274 newBranch = false;
00275 }
00276 if(newBranch) branches.push_back(dirstring);
00277 }
00278
00279 for(UInt_t i = 0; i < branches.size(); i++)
00280 {
00281 HistMan *hm2 = new HistMan(branches[i]);
00282 hm2->WriteOut(*file);
00283 }
00284
00285
00286 delete file;
00287 }
00288
00289 const Registry& CompareAll::DefaultConfig() const
00290 {
00291
00292
00293
00294 MSG("CompareAll",Msg::kDebug)<<"In CompareAll::DefaultConfig"<<endl;
00295
00296 static Registry r;
00297
00298
00299 std::string name = this->GetName();
00300 name += ".config.default";
00301 r.SetName(name.c_str());
00302
00303
00304 r.UnLockValues();
00305 r.Set("HiPlaneTrackCut",25);
00306 r.Set("LoPlaneEventCut",-1);
00307 r.Set("HiTrackLikeCut",-1);
00308 r.Set("DPlaneCut",-1);
00309 r.Set("LoPhNStripCut",-1);
00310 r.Set("LoPhNPlaneCut",-1);
00311 r.Set("HiEnergyCut",-1);
00312 r.Set("LoEnergyCut",-1);
00313 r.Set("HiEnergyShowerCut",-1);
00314 r.Set("LoEnergyShowerCut",-1);
00315 r.Set("PhStripCut",-1);
00316 r.Set("PhPlaneCut",-1);
00317 r.Set("LoCurrentCut", 0.1);
00318 r.Set("LoHorBeamWidth", 0.0);
00319 r.Set("HiHorBeamWidth", 2.9);
00320 r.Set("LoVertBeamWidth", 0.0);
00321 r.Set("HiVertBeamWidth", 2.9);
00322 r.Set("LoNuTarZ", -1);
00323 r.Set("HiNuTarZ", 1000);
00324 r.Set("Oscillate", 0);
00325 r.Set("OutputFile", "HistManInfo.root");
00326
00327 r.LockValues();
00328
00329 return r;
00330 }
00331
00332 void CompareAll::Config(const Registry& r)
00333 {
00334
00335
00336
00337 MSG("CompareAll",Msg::kDebug)<<"In CompareAll::Config"<<endl;
00338
00339
00340 int imps;
00341 if(r.Get("HiPlaneTrackCut",imps)) { kHiPlaneTrackCut=imps;}
00342 if(r.Get("LoPlaneEventCut",imps)) { kLoPlaneEventCut=imps;}
00343 if(r.Get("HiTrackLikeCut",imps)) { kHiTrackLikeCut=imps;}
00344 if(r.Get("DPlaneCut",imps)) { kDPlaneCut=imps;}
00345 if(r.Get("LoPhNStripCut",imps)) { kLoPhNStripCut=imps;}
00346 if(r.Get("LoPhNPlaneCut",imps)) { kLoPhNPlaneCut=imps;}
00347
00348 if(r.Get("LoNuTarZ", imps)) {kLoNuTarZ = imps;}
00349 if(r.Get("HiNuTarZ", imps)) {kHiNuTarZ = imps;}
00350 if(r.Get("Oscillate", imps)) {kOscillate = imps;}
00351
00352 double fmps;
00353 if(r.Get("HiEnergyCut",fmps)) { kHiEnergyCut=fmps;}
00354 if(r.Get("LoEnergyCut",fmps)) { kLoEnergyCut=fmps;}
00355 if(r.Get("HiEnergyShowerCut",fmps)) { kHiEnergyShowerCut=fmps;}
00356 if(r.Get("LoEnergyShowerCut",fmps)) { kLoEnergyShowerCut=fmps;}
00357 if(r.Get("PhStripCut",fmps)) { kPhStripCut=fmps;}
00358 if(r.Get("PhPlaneCut",fmps)) { kPhPlaneCut=fmps;}
00359
00360 if(r.Get("LoCurrentCut", fmps)) {kLoCurrentCut = fmps;}
00361 if(r.Get("LoHorBeamWidth", fmps)) {kLoHorBeamWidth = fmps;}
00362 if(r.Get("HiHorBeamWidth", fmps)) {kHiHorBeamWidth = fmps;}
00363 if(r.Get("LoVertBeamWidth", fmps)) {kLoVertBeamWidth = fmps;}
00364 if(r.Get("HiVertBeamWidth", fmps)) {kHiVertBeamWidth = fmps;}
00365
00366 const char* tmps;
00367 if(r.Get("OutputFile", tmps)) {kOutputFile = tmps;}
00368
00369 }
00370
00371
00372
00373
00374 bool CompareAll::PassesBeamCuts(NueRecord* nr)
00375 {
00376
00377 if (nr->GetHeader().GetVldContext().GetSimFlag()!=SimFlag::kData) return true;
00378
00379 if(kLoCurrentCut > 0)
00380 if (nr->bmon.bI < kLoCurrentCut) return false;
00381 if(kLoHorBeamWidth > 0)
00382 if (nr->bmon.hbw*1e3 < kLoHorBeamWidth) return false;
00383 if(kHiHorBeamWidth > 0)
00384 if (nr->bmon.hbw*1e3 > kHiHorBeamWidth) return false;
00385 if(kLoVertBeamWidth > 0)
00386 if (nr->bmon.vbw*1e3 < kLoVertBeamWidth) return false;
00387 if(kHiVertBeamWidth > 0)
00388 if (nr->bmon.vbw*1e3 > kHiVertBeamWidth) return false;
00389 if(kLoNuTarZ > 0)
00390 if (nr->bmon.nuTarZ < kLoNuTarZ) return false;
00391 if(kHiNuTarZ > 0)
00392 if (nr->bmon.nuTarZ > kHiNuTarZ) return false;
00393
00394 return true;
00395 }
00396
00397
00398
00399 bool CompareAll::PassesCuts(NueRecord* nr)
00400 {
00401 bool passes = true;
00402
00403
00404 if(nr->srtrack.planes > 25) passes = false;
00405 if(nr->anainfo.inFiducialVolume != 1)
00406 passes = false;
00407
00408 if (nr->srtrack.planes > 25) passes = false;
00409 if (nr->srevent.pulseHeight<2e4) passes = false;
00410
00411 if (nr->anainfo.isFullyContained != 1 && nr->anainfo.isFullyContained!= -2)
00412 passes = false;
00413
00414 if (nr->srevent.phMeu>150) passes = false;
00415 if (nr->srevent.hotch==1) passes = false;
00416 if((TMath::Max(nr->srtrack.pulseHeight,nr->srshower.pulseHeight)<5000))
00417 passes = false;
00418
00419
00420 if(nr->GetHeader().GetTrackLength()>25)
00421 passes = false;
00422
00423 if(!EventFilter::PassesAllCuts(nr,kHiPlaneTrackCut,kHiTrackLikeCut,
00424 kLoPlaneEventCut, kHiEnergyCut,
00425 kLoEnergyCut, kHiEnergyShowerCut,
00426 kLoEnergyShowerCut))
00427 passes = false;
00428
00429 return passes;
00430 }
00431
00432
00433 TString CompareAll::MakeIdString(NueRecord *nr)
00434 {
00435 Detector::Detector_t d = nr->GetHeader().GetVldContext().GetDetector();
00436 SimFlag::SimFlag_t s = nr->GetHeader().GetVldContext().GetSimFlag();
00437
00438 TString det, dm;
00439 TString type;
00440
00441 if(d==Detector::kFar){
00442 det = "_far";
00443 }
00444 else if(d==Detector::kNear){
00445 det = "_near";
00446 }
00447 else{
00448 return "_unknown";
00449 }
00450
00451 if(s==SimFlag::kData){
00452 dm = "_data";
00453 TString id = det+dm;
00454 return id;
00455 }
00456 else
00457 if (s==SimFlag::kMC){
00458 dm = "_mc";
00459
00460 if(nr->mctrue.interactionType==1){
00461 if(abs(nr->mctrue.nuFlavor)==12 &&
00462 abs(nr->mctrue.nonOscNuFlavor)==12){
00463 type = "bnue";
00464 }
00465 else if(abs(nr->mctrue.nuFlavor)==12){
00466 type = "nue";
00467 }
00468 else if(abs(nr->mctrue.nuFlavor)==14){
00469 type = "numu";
00470 }
00471 else if(abs(nr->mctrue.nuFlavor)==16){
00472 type = "nutau";
00473 }
00474 else{
00475 return "unknown";
00476 }
00477 }
00478 else{
00479 type = "nc";
00480 }
00481
00482 TString id = det+dm+ "_" + type;
00483 return id;
00484 }
00485 else {
00486 return "unknown";
00487 }
00488
00489 }
00490
00491
00492 void CompareAll::FillFromList(NueRecord* nr, TString id, Float_t weight)
00493 {
00494 if(varName.size() == 0) return;
00495 TString hname;
00496 UInt_t count = 0;
00497
00498 TClass *cl;
00499 TRealData *rd;
00500 string vName;
00501 TDataMember *member;
00502 TDataType *membertype;
00503 Float_t value = 0.0;
00504
00505 cl=nr->IsA();
00506 TIter next(cl->GetListOfRealData());
00507 while ((rd =dynamic_cast<TRealData*>(next()))) {
00508 member = rd->GetDataMember();
00509 membertype = member->GetDataType();
00510 vName=rd->GetName();
00511
00512 Int_t offset = rd->GetThisOffset();
00513 char *pointer = (char*)nr + offset;
00514
00515 for(UInt_t i = 0; i < varName.size();i++){
00516 if(vName == varName[i]){
00517 value = -9999;
00518 if(!NeedsSpecialAttention(vName, nr, value))
00519 value=atof(membertype->AsString(pointer));
00520 MSG("CompareAll",Msg::kDebug)<<"Found variable "
00521 <<vName<<" with value "<<value;
00522 MSG("CompareAll",Msg::kDebug)<<"Storing it w/ id "<<id<<endl;
00523
00524
00525 if(!ANtpDefVal::IsDefault(value) &&
00526 !ANtpDefVal::IsDefault(static_cast<Double_t> (value)) &&
00527 !ANtpDefVal::IsDefault(static_cast<Int_t> (value))){
00528
00529 string dirstring = "allcomp/" + vName.substr(0,vName.find_first_of("."));
00530 HistMan hm2(dirstring.c_str());
00531
00532 hname = varName[i]+id;
00533 TH1F* hist = hm2.Get<TH1F>(hname);
00534 hist->Fill(value, weight);
00535 }
00536 MSG("CompareAll",Msg::kDebug)<<"Found variable "
00537 <<vName<<" with value "<<value;
00538
00539 count++;
00540 i = varName.size();
00541 }
00542 }
00543 if(count == varName.size()) break;
00544 }
00545
00546 return;
00547 }
00548
00549
00550 bool CompareAll::NeedsSpecialAttention(TString name, NueRecord *nr, Float_t &value)
00551 {
00552
00553
00554 if(name == "fHeader.fSnarl") {
00555 value = nr->GetHeader().GetSnarl();
00556 }if(name == "fHeader.fRun") {
00557 value = nr->GetHeader().GetRun();
00558 }if(name == "fHeader.fSubRun") {
00559 value = nr->GetHeader().GetSubRun();
00560 }if(name == "fHeader.fEvtNo") {
00561 value = nr->GetHeader().GetEventNo();
00562 }if(name == "fHeader.fEvents") {
00563 value = nr->GetHeader().GetEvents();
00564 }if(name == "fHeader.fTrackLength") {
00565 value = nr->GetHeader().GetTrackLength();
00566 }
00567
00568 if(name == "mstvars.eallw1") {
00569 if(nr->mstvars.enn1 > 0) value = 0.0;
00570 for(int i=0;i<nr->mstvars.enn1;i++){
00571 value += nr->mstvars.eallw1[i];
00572 }
00573 }
00574 if(name == "mstvars.oallw1") {
00575 if(nr->mstvars.onn1 > 0) value = 0.0;
00576 for(int i=0;i<nr->mstvars.onn1;i++){
00577 value += nr->mstvars.oallw1[i];
00578 }
00579 }
00580 if(name == "mstvars.eallm1") {
00581 if(nr->mstvars.enn1 > 0) value = 0.0;
00582 for(int i=0;i<nr->mstvars.enn1;i++){
00583 value += nr->mstvars.eallm1[i];
00584 }
00585 }
00586 if(name == "mstvars.oallm1") {
00587 if(nr->mstvars.onn1 > 0) value = 0.0;
00588 for(int i=0;i<nr->mstvars.onn1;i++){
00589 value += nr->mstvars.oallm1[i];
00590 }
00591 }
00592
00593 if(value > -9999) return true;
00594 return false;
00595 }