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

SetKNNModule.cxx

Go to the documentation of this file.
00001 // $Id: SetKNNModule.cxx,v 1.4 2009/06/24 21:13:55 pawloski Exp $
00002 
00003 // C/C++
00004 #include <cassert>
00005 #include <iomanip>
00006 
00007 // ROOT
00008 #include "TClonesArray.h"
00009 
00010 // MINOS
00011 #include "CandNtupleSR/NtpSREvent.h"
00012 #include "CandNtupleSR/NtpSRTrack.h"
00013 #include "JobControl/JobCModuleRegistry.h"
00014 #include "JobControl/JobCommand.h"
00015 #include "MessageService/MsgService.h"
00016 #include "MinosObjectMap/MomNavigator.h"
00017 #include "StandardNtuple/NtpStRecord.h"
00018 
00019 // Local
00020 #include "PhysicsNtuple/Default.h"
00021 #include "PhysicsNtuple/Factory.h"
00022 #include "PhysicsNtuple/Store/FillBasic.h"
00023 #include "PhysicsNtuple/Store/Interface.h"
00024 
00025 // Local
00026 #include "SetKNNModule.h"
00027 
00028 CVSID("$Id: SetKNNModule.cxx,v 1.4 2009/06/24 21:13:55 pawloski Exp $");
00029 
00030 JOBMODULE(SetKNNModule, "SetKNNModule", "SetKNNModule");
00031 
00032 //---------------------------------------------------------------------------------------------
00033 StorekNNData::StorekNNData()
00034 {
00035 }
00036 
00037 //---------------------------------------------------------------------------------------------
00038 StorekNNData::~StorekNNData()
00039 {
00040 }
00041 
00042 //---------------------------------------------------------------------------------------------
00043 bool StorekNNData::Add(int i, const std::string &key, const float data)
00044 {
00045    char temp[3];
00046    sprintf(temp, "%02d", i);
00047    const std::string full_key = fPrefix + "_" + key + "_" + string(temp);
00048    
00049 //   if(true) cout<<"Storing: "<<full_key<<"  "<<data<<endl;
00050 
00051    return fData.insert(map<string, float>::value_type(full_key, data)).second;
00052 }
00053 
00054 //---------------------------------------------------------------------------------------------
00055 bool StorekNNData::SetValidity(VldContext &vld)
00056 {
00057    fValidity = vld;
00058    return true;
00059 }
00060 
00061 //---------------------------------------------------------------------------------------------
00062 const VldContext& StorekNNData::GetValidity()
00063 { 
00064    return fValidity;
00065 } 
00066 
00067 //---------------------------------------------------------------------------------------------
00068 bool StorekNNData::SetPrefix(std::string in)
00069 {
00070    fPrefix = in;
00071    return true;
00072 } 
00073 
00074 //---------------------------------------------------------------------------------------------
00075 bool StorekNNData::Get(int i, const std::string &key, float &data)
00076 {
00077    char temp[3];
00078    sprintf(temp, "%02d", i);
00079    const std::string full_key = fPrefix + "_" + key + "_" + string(temp);
00080 
00081    map<string, float>::const_iterator fit = fData.find(full_key);
00082    if(fit != fData.end())
00083    {
00084       data = fit -> second;
00085       return true;
00086    }
00087 
00088    return false;
00089 }
00090 
00091 //---------------------------------------------------------------------------------------------
00092 bool StorekNNData::Clear()
00093 {
00094 //   cout<<"flushing the data!"<<endl;
00095    fData.clear();
00096    return true;
00097 }  
00098 
00099 //---------------------------------------------------------------------------------------------
00100 SetKNNModule::SetKNNModule()
00101    :fInterface(new Anp::Interface()),
00102     fNPass(0),
00103     fNFail(0),
00104     fPrintEvent(false),
00105     fPrintTrack(false),
00106     fStrip(false),
00107     fIsMRCC(false),
00108     fConfig(false)
00109 {
00110 
00111 //   fPath = "junk/afs/fnal.gov/files/home/room1/rustem/data/muon-knn/";
00112    fPath = "/minos/scratch/nue/Releases/Griffin/Data/kNN_cedar/";
00113    fFastMode = false;
00114 
00115    Anp::Factory<StorekNNData>::
00116       Instance().Hold("kNNData", Anp::Handle<StorekNNData>(new StorekNNData));
00117 }
00118 
00119 //---------------------------------------------------------------------------------------------
00120 SetKNNModule::~SetKNNModule() 
00121 {
00122    //
00123    // Delete interface object
00124    //
00125    delete fInterface;
00126    fInterface = 0;
00127 
00128    //
00129    // Destroy StorekNNData if it exists
00130    //
00131    Anp::Factory<StorekNNData>::Instance().Remove("kNNData");
00132 
00133    MSG("SetKNNModule", Msg::kDebug) 
00134       << endl
00135       << "**************************************************" << std::endl
00136       << "    SetKNNModule" << std::endl
00137       << "      Number of passed records " << fNPass << std::endl
00138       << "      Number of failed records " << fNFail << std::endl
00139       << "**************************************************" << std::endl;
00140 }
00141 
00142 //---------------------------------------------------------------------------------------------
00143 JobCResult SetKNNModule::Reco(MomNavigator *mom)
00144 {  
00145   bool foundST=false;
00146   std::vector<TObject*> records = mom->GetFragmentList("NtpStRecord");
00147 
00148   if(fFastMode){
00149     std::vector<TObject*> nrRecord =  mom->GetFragmentList("NueRecord");
00150     if(nrRecord.size() == 0) return  JobCResult::kFailed;
00151   }
00152 
00153   bool pass = true;
00154 
00155   static bool first = true;
00156 
00157   Anp::Handle<StorekNNData> data = Anp::Factory<StorekNNData>::Instance().Get("kNNData");
00158 
00159   if(!data.valid())
00160   {
00161       cerr << " SetKNNModule::Analyze - Handle<StorekNNData> is invalid" << endl;
00162       return JobCResult::kFailed;
00163   }
00164   data->Clear();
00165 
00166   for(unsigned int i=0;i<records.size() && pass; i++)
00167   {
00168       NtpStRecord *str=dynamic_cast<NtpStRecord *>(records[i]);
00169       if(str){  foundST=true;  }
00170       else continue;
00171    
00172       if(first){
00173           Registry reg(false);
00174           string pub = getenv("SRT_PUBLIC_CONTEXT");
00175           string config = pub + "/PhysicsNtuple/Config/Config2008Real.txt";
00176           reg.Set("InterfaceConfigPath", config.c_str());
00177  
00178           string file = fPath;
00179 
00180 
00181           int release = str->GetRelease();
00182           bool diakon00 = ( ReleaseType::IsDaikon(release) && 0==ReleaseType::GetMCSubVersion(release) );
00183 
00184           if(str->GetHeader().GetVldContext().GetDetector() == Detector::kNear)
00185           {
00186            if(diakon00)
00187            {
00188              file += "knn.physics.near.daikon_00.cedar_phy.L010z185i.root";
00189            }
00190            else
00191            {
00192              file += "knn.physics.near.daikon_04.cedar_phy_bhcurv.L010z185i.root";
00193            }
00194           }
00195 
00196 
00197           if(str->GetHeader().GetVldContext().GetDetector() == Detector::kFar)
00198           {
00199            if(diakon00)
00200            {
00201              file += "knn.physics.far.daikon_00.cedar_phy.L010z185i.root";
00202            }
00203            else
00204            {
00205              file += "knn.physics.far.daikon_04.cedar_phy_bhcurv.L010z185i.root";
00206            }
00207           }
00208  
00209             reg.Set("FillkNNFilePath", file.c_str());
00210 
00211           fInterface->Config(reg);
00212           first = false;
00213        }    
00214 
00215 
00216       if(records.size() > 1 && !fIsMRCC){
00217          fIsMRCC = true;
00218          MSG("SetKNNModule", Msg::kInfo)<<"Multiple NtpStRecords Detected - Shifting to MRCC Mode"<<endl;
00219       }
00220 
00221       pass = pass && AnalyzeRecord(str);
00222   }
00223 
00224   if(!pass) return JobCResult::kFailed;
00225   return JobCResult::kAOK;
00226 }
00227    
00228 bool SetKNNModule::AnalyzeRecord(NtpStRecord* ntprec)
00229 {
00230    Anp::Handle<StorekNNData> data = Anp::Factory<StorekNNData>::Instance().Get("kNNData");
00231    if(!data.valid())
00232    {
00233       cerr << " SetKNNModule::Analyze - Handle<StorekNNData> is invalid" << endl;
00234       return false;
00235    }
00236 
00237    std::string name = ntprec->GetName();
00238    data->SetPrefix("SNTP");
00239    if(fIsMRCC && name == "Primary") data->SetPrefix("OldSNTP");
00240 
00241    VldContext vld = ntprec->GetHeader().GetVldContext();
00242    data->SetValidity(vld);
00243 
00244    // Fill interface with data for a new snarl
00245    if(fInterface -> FillSnarl(ntprec))
00246    {
00247       ++fNPass;
00248    }
00249    else
00250    {
00251       ++fNFail;
00252       return false; 
00253    } 
00254 
00255    TClonesArray *event_array = ntprec -> evt;
00256    TClonesArray *track_array = ntprec -> trk;
00257    if(!event_array || !track_array)
00258    {           
00259       MSG("SetKNNModule", Msg::kWarning) << "Invalid TClonesArray object(s)" << endl;
00260       return false;
00261    }
00262 
00263    // Iterate over all events and access analysis variables
00264    for(int ievent = 0; ievent < event_array -> GetEntries(); ++ievent)
00265    {
00266       NtpSREvent *event = dynamic_cast<NtpSREvent *>(event_array -> At(ievent));
00267       if(!event)
00268       {
00269          MSG("SetKNNModule", Msg::kError) << "NtpSREvent dynamic_cast failed" << endl;
00270          continue;
00271       }
00272 
00273       SetKNNModule::Analyze(ievent, event, fPrintEvent);
00274       assert(event -> index == ievent && "mismatched index");
00275    }
00276 
00277    return true;
00278 }
00279 
00280 //---------------------------------------------------------------------------------------------
00281 void SetKNNModule::Config(const Registry& reg)
00282 {
00283    MSG("SetKNNModule", Msg::kVerbose) << "SetKNNModule::Config()..." << std::endl;
00284 
00285    //
00286    // Store Registry copy
00287    //
00288    fConfig.UnLockValues();
00289    fConfig.UnLockKeys();
00290    fConfig.Merge(reg);
00291    fConfig.LockKeys();
00292    fConfig.LockValues();
00293 
00294    const char* tmps;
00295    if(reg.Get("kNNPath", tmps)) fPath = tmps;
00296    
00297    int itmp;
00298    if(reg.Get("ForceMRCC", itmp)){
00299      if(itmp == 1){
00300         fIsMRCC = true;
00301          MSG("SetKNNModule", Msg::kInfo)<<"Forced to Shift to MRCC Mode"<<endl;
00302      }
00303    }
00304 
00305    if(reg.Get("RunFast", itmp)){
00306      if(itmp == 1){
00307         fFastMode = true;
00308          MSG("SetKNNModule", Msg::kInfo)<<"Shifting to Fast Mode"<<endl;
00309      }
00310    }
00311 
00312 
00313    Anp::Read(reg, "SetKNNModulePrintEvent", fPrintEvent);
00314    Anp::Read(reg, "SetKNNModulePrintTrack", fPrintTrack);
00315    Anp::Read(reg, "SetKNNModuleStrip", fStrip);
00316 }
00317 
00318 //---------------------------------------------------------------------------------------------
00319 void SetKNNModule::BeginJob()
00320 {
00321    MSG("SetKNNModule", Msg::kVerbose) << "SetKNNModule::BeginJob()..." << std::endl;
00322 
00323    //
00324    // Configure interface object at the beginning of analysis job
00325    //
00326 //   fInterface -> Config(fConfig);
00327 }
00328 
00329 //------------------------------------------------------------------------------------------
00330 void SetKNNModule::Analyze(int i, TObject *object, const bool print)
00331 {
00332    //
00333    // Extract variables from PhysicsNtuple interface
00334    //
00335    const float numubar = fInterface -> GetVar("numubar", object);
00336    const float rel_ang = fInterface -> GetVar("rel_ang", object);
00337    const float knn_pid = fInterface -> GetVar("knn_pid", object);
00338    const float knn_01  = fInterface -> GetVar("knn_01",  object);
00339    const float knn_10  = fInterface -> GetVar("knn_10",  object);
00340    const float knn_20  = fInterface -> GetVar("knn_20",  object);
00341    const float knn_40  = fInterface -> GetVar("knn_40",  object);
00342 
00343    if(print)
00344    {
00345       cout << " For event: "<<i
00346            << "   anti neutrino selection = " << numubar << endl
00347            << "   relative angle = " << rel_ang << endl
00348            << "   knn muon pid = " << knn_pid << endl
00349            << "   number of scintillator planes = " << knn_01 << endl
00350            << "   mean track signal = " << knn_10 << endl
00351            << "   low mean signal over high mean signal = " << knn_20 << endl
00352            << "   track mean signal over track window mean signal = " << knn_40 << endl;
00353    }
00354    
00355    Anp::Handle<StorekNNData> data = Anp::Factory<StorekNNData>::Instance().Get("kNNData");
00356 
00357    if(!data.valid())
00358    {
00359       cerr << " SetKNNModule::Analyze - Handle<StorekNNData> is invalid" << endl;
00360       return;
00361    }
00362 
00363    data -> Add(i, "numubar", numubar);
00364    data -> Add(i, "rel_ang", rel_ang);
00365    data -> Add(i, "knn_pid", knn_pid);
00366    data -> Add(i, "knn_01", knn_01);
00367    data -> Add(i, "knn_10", knn_10);
00368    data -> Add(i, "knn_20", knn_20);
00369    data -> Add(i, "knn_40", knn_40);
00370 }

Generated on Mon Feb 15 11:07:35 2010 for loon by  doxygen 1.3.9.1