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

CondensedNtpModuleNC.cxx

Go to the documentation of this file.
00001 
00002 //$Id: CondensedNtpModuleNC.cxx,v 1.29 2009/11/24 16:50:39 rodriges Exp $
00003 //
00004 //CondensedNtpModuleNC.cxx
00005 //
00006 //Module for making analysis trees from SR ntuples
00007 //
00008 //B. Rebel 10/2004
00010 
00011 #include "AnalysisNtuples/Module/CondensedNtpModuleNC.h"
00012 
00013 #include "AnalysisNtuples/ANtpNueInfo.h"
00014 #include "AnalysisNtuples/ANtpEventInfoNC.h"
00015 #include "AnalysisNtuples/ANtpShowerInfoNC.h"
00016 #include "AnalysisNtuples/ANtpTrackInfoNC.h"
00017 #include "AnalysisNtuples/ANtpTruthInfoBeam.h"
00018 #include "AnalysisNtuples/ANtpBeamInfo.h"
00019 #include "AnalysisNtuples/ANtpHeaderInfo.h"
00020 #include "AnalysisNtuples/ANtpDefaultValue.h"
00021 #include "AnalysisNtuples/ANtpEventInfoMRCC.h"
00022 
00023 #include "AnalysisNtuples/Module/ANtpInfoObjectFillerNC.h"
00024 #include "AnalysisNtuples/Module/ANtpInfoObjectFillerMRCC.h"
00025 #include "AnalysisNtuples/Module/ANtpInfoObjectFillerNue.h"
00026 #include "AnalysisNtuples/Module/ANtpRecoNtpManipulator.h"
00027 
00028 #include "Conventions/Detector.h"
00029 #include "Conventions/SimFlag.h"
00030 #include "MessageService/MsgService.h"
00031 #include "MinosObjectMap/MomNavigator.h"
00032 #include "JobControl/JobCModuleRegistry.h" // For JOBMODULE macro
00033 
00034 #include "TFile.h"
00035 #include "TTree.h"
00036 
00037 #include <cassert>
00038 
00039 class NtpSRRecord;
00040 class NtpMCRecord;
00041 class NtpMRRecord;
00042 class NtpStRecord;
00043 class NtpTHRecord;
00044 
00045 
00046 CVSID("$Id: CondensedNtpModuleNC.cxx,v 1.29 2009/11/24 16:50:39 rodriges Exp $");
00047 
00048 // Declare this module to JobControl. Arguments are:
00049 //  (1) The class name
00050 //  (2) The human-readable name
00051 //  (3) A short, human-readable description of what the module does
00052 JOBMODULE(CondensedNtpModuleNC,
00053           "CondensedNtpModuleNC",
00054           "A module used for making analysis tress from SR ntuples");
00055 
00056 //......................................................................
00057 CondensedNtpModuleNC::CondensedNtpModuleNC() :
00058   fFileName("analysisNtuple.root"),
00059   fTreeName("analysisNtuple"),
00060   fNtpFile(0),
00061   fNtuple(0),
00062   fVHSPlanes(20),
00063   fVHSStrips(20)
00064 {
00065   MSG("JobC", Msg::kDebug) << "CondensedNtpModuleNC::Constructor" << endl;
00066 
00067   fHeaderInfo = new ANtpHeaderInfo;
00068   fEventInfo = new ANtpEventInfoNC;
00069   fNueInfo = new ANtpNueInfo;
00070   fTrackInfo = new ANtpTrackInfoNC;
00071   fShowerInfo = new ANtpShowerInfoNC;
00072   fTruthInfo = new ANtpTruthInfoBeam;
00073   fBeamInfo = new ANtpBeamInfo;
00074   fEventInfoMRCC = new ANtpEventInfoMRCC;
00075 
00076   fInfoFiller = new ANtpInfoObjectFillerNC;
00077   fInfoFillerNue = new ANtpInfoObjectFillerNue;
00078   fInfoFillerMRCC = new ANtpInfoObjectFillerMRCC;
00079 
00080   fFailedDeMux = 0;
00081 }
00082 
00083 //----------------------------------------------------------------------
00084 CondensedNtpModuleNC::~CondensedNtpModuleNC()
00085 {
00086 
00087   MSG("JobC", Msg::kDebug) << "CondensedNtpModuleNC::Destructor" << endl;
00088 
00089   if(fHeaderInfo) delete fHeaderInfo;
00090   if(fEventInfo) delete fEventInfo;
00091   if(fEventInfoMRCC) delete fEventInfoMRCC;
00092   if(fShowerInfo) delete fShowerInfo;
00093   if(fTrackInfo) delete fTrackInfo;
00094   if(fTruthInfo) delete fTruthInfo;
00095   if(fBeamInfo) delete fBeamInfo;
00096   if(fNueInfo) delete fNueInfo;
00097 }
00098 
00099 //----------------------------------------------------------------------
00100 void CondensedNtpModuleNC::BeginJob()
00101 {
00102   MSG("CondensedNtpModuleNC", Msg::kDebug)
00103     << "in BeginJob" << endl;
00104 
00105   // save the current working directory
00106   //TDirectory* savedir  = gDirectory;
00107 
00108   //create the new TFile for holding the electronics tree
00109   // this changes the value of gDirectory
00110   fNtpFile = new TFile(fFileName,"RECREATE");
00111 //   if(!fNtpFile->IsOpen() ) MSG("CondensedNtpModuleNC", Msg::kFatal) << "file not open "
00112 //                                                                  << fFileName << endl;
00113 
00114   // create TTree, these will attach themselves to the current
00115   //working directory
00116   fNtuple = new TTree(fTreeName, "Analysis Tree");
00117 
00118   //-----------------------------------------------------------------------
00119   //here is where you define your tree
00120   fNtuple->Branch("header.", "ANtpHeaderInfo", &fHeaderInfo, 64000, 2);
00121   fNtuple->Branch("event.", "ANtpEventInfoNC", &fEventInfo, 64000, 2);
00122   fNtuple->Branch("shower.", "ANtpShowerInfoNC", &fShowerInfo, 64000, 2);
00123   fNtuple->Branch("track.", "ANtpTrackInfoNC", &fTrackInfo, 64000, 2);
00124   fNtuple->Branch("beam.", "ANtpBeamInfo", &fBeamInfo, 64000, 2);
00125   if(fMakeNueBranch)fNtuple->Branch("nue.", "ANtpNueInfo", &fNueInfo, 64000, 2);
00126   if(fDataType==1)
00127     fNtuple->Branch("truth.", "ANtpTruthInfoBeam", &fTruthInfo, 64000, 2);
00128 
00129   MSG("CondensedNtpModuleNC", Msg::kDebug) << "got branches" << endl;
00130 
00131   //-----------------------------------------------------------------------
00132 
00133   // Setup Kregg's image recognition filling
00134   //fInfoFiller->InitializeVHSTraining(fVHSPlanes, fVHSStrips);
00135 
00136   // change back to current working directory before leaving constructor
00137   //savedir->cd();
00138 
00139   return;
00140 }
00141 
00142 //......................................................................
00143 //here is where you would loop over the events in the snarl to fill the
00144 //analysis tree
00145 JobCResult CondensedNtpModuleNC::Ana(const MomNavigator *mom)
00146 {
00147   JobCResult result(JobCResult::kPassed);
00148 
00149   //get the records from MOM
00150   assert(mom);
00151 
00152   NtpSRRecord* record     = (NtpSRRecord*)(mom->GetFragment("NtpSRRecord"));
00153   NtpMRRecord* mrRecord   = (NtpMRRecord*)(mom->GetFragment("NtpMRRecord"));
00154 
00155   NtpStRecord* stRecord=0;
00156   NtpStRecord* stRecordOrig=0;
00157   // Only want to create the MRCC output branch if the input file had MRCC in
00158   // This information isn't available to us in BeginJob, I can't think of a
00159   // better way than this.
00160   if(mrRecord){
00161 
00162     // This is a little fiddly: for MRCC processing, we need two
00163     // NtpStRecords; the one that got produced when we processed with
00164     // MRCC, and the one for the same snarl as processed without
00165     // MRCC. The former has name "MuonRemoved", while the latter has
00166     // name "Primary"
00167     stRecordOrig = (NtpStRecord*)(mom->GetFragment("NtpStRecord",
00168                                                    "Primary"));
00169     stRecord     = (NtpStRecord*)(mom->GetFragment("NtpStRecord",
00170                                                    "MuonRemoved"));
00171     
00172     if(!stRecordOrig){
00173       // Would really like to exit if this happens, to avoid the case
00174       // where only one file is given (although this check is done in
00175       // the macro). However, concatenation is not our friend, and
00176       // sometimes the concatenated sntp file gets chopped before the
00177       // mrnt file, so it doesn't have all the same subruns in it. In
00178       // this case, just ignore this snarl.
00179       MAXMSG("CondensedNtpModuleNC", Msg::kWarning, 10)
00180         << "No NtpStRecord found. Probably the sntp file is shorter than the mrnt."
00181         << "Ignoring this snarl" << endl;
00182       return JobCResult::kFailed;
00183     }
00184     static bool once = true;
00185     if(once){
00186       fNtuple->Branch("mrcc.", "ANtpEventInfoMRCC", &fEventInfoMRCC, 64000, 2);
00187       once = false;
00188     }
00189   }
00190   else{
00191     // If we're not running MRCC, the Primary NtpStRecord is the one to use
00192     stRecord = (NtpStRecord*)(mom->GetFragment("NtpStRecord",
00193                                                "Primary"));
00194   }
00195 
00196   if(!record && !stRecord){
00197     MSG("CondensedNtpModuleNC", Msg::kWarning) << "Could not get NtpSR or NtpSt "
00198                                                 << "Record from MOM" << endl;
00199     result.SetWarning().SetFailed();
00200     return result;
00201   }//end if no NtpSRRecord
00202 
00203   NtpMCRecord* mcRecord = (NtpMCRecord*)(mom->GetFragment("NtpMCRecord"));
00204   NtpTHRecord* thRecord = (NtpTHRecord*)(mom->GetFragment("NtpTHRecord"));
00205 
00206   //instantiate a NtpHelper object to help you get the info you want
00207   ANtpRecoNtpManipulator *ntpManipulator = 0;
00208   if(record) ntpManipulator = new ANtpRecoNtpManipulator(record, mcRecord, thRecord);
00209   else if(stRecord) ntpManipulator = new ANtpRecoNtpManipulator(stRecord);
00210 
00211   const VldContext *vldc = stRecord ? stRecord->GetVldContext() : record->GetVldContext();
00212   VldTimeStamp timeStamp = vldc->GetTimeStamp();
00213 
00214   //fill the header information
00215   Detector::Detector_t det = vldc->GetDetector();
00216 
00217   //get a sim flag
00218   SimFlag::SimFlag_t dataType = vldc->GetSimFlag();
00219 
00220   fInfoFiller->SetDetector(det);
00221   fInfoFiller->SetStripArray(ntpManipulator->GetStripArray());
00222   fInfoFiller->SetClusterArray(ntpManipulator->GetClusterArray());
00223   fInfoFiller->FillHeaderInformation(ntpManipulator, det, dataType, fHeaderInfo);
00224 
00225   if(fHeaderInfo->snarl%500==0)
00226     MSG("CondensedNtpModuleNC", Msg::kInfo) << "snarl " << fHeaderInfo->snarl << endl;
00227 
00228 //   MAXMSG("CondensedNtpModuleNC",Msg::kDebug,20)
00229 //     << "detector = " << fHeaderInfo->detector << " " << det << endl;
00230 
00231   if(fHeaderInfo->year > 2004){
00232     fInfoFiller->FillBeamInformation(timeStamp, det, dataType, fBeamInfo);
00233     MAXMSG("CondensedNtpModuleNC",Msg::kDebug,1)
00234       << "Calling db method to fill beam info." << endl;
00235   }
00236 
00237   if(fHeaderInfo->passedDeMux == 0)
00238     ++fFailedDeMux;
00239 
00240   //set up which flags you want to use to determine the primary shower or track
00241   //a value of 0 for a flag means it will not be used
00242   ntpManipulator->SetPrimaryTrackCriteria(0,1,0); // nplanes, length, total pulse height
00243 
00244   //for now set the primary shower criteria both to 0, that will pick out the first
00245   //shower in the reco array, which has been determined to be the one to use.  some
00246   //other cuts still apply, but can be put in at the analysis level, not the ntuple
00247   //making level.  this was settled at the reco meeting on 2/9/06 - bjr
00248   ntpManipulator->SetPrimaryShowerCriteria(0,0); // nplanes, total pulse height
00249 
00250   //--------------------------------------------------------------------
00251   //here is where you start looping over the events in the ntuple to fill
00252   //your analysis tree
00253 
00254   //handle the special case where you have a spill but no events
00255   if(fHeaderInfo->events < 1){
00256     //reset all the event, track and shower info objects to their default values
00257     //the beam and header values remain
00258     fEventInfo->Reset();
00259     fTrackInfo->Reset();
00260     fShowerInfo->Reset();
00261     fEventInfoMRCC->Reset();
00262     fNtuple->Fill();
00263     return result;
00264   }
00265 
00266   //initialize the kNN stuff as it should only be done once per snarl
00267   fInfoFiller->InitializekNN(ntpManipulator);
00268 
00269   //loop over the events in the snarl
00270   for(Int_t i = 0; i < fHeaderInfo->events; ++i){
00271 
00272     MSG("CondensedNtpModuleNC", Msg::kDebug) << "on event " << i << endl;
00273 
00274     //reset the variables for the tree
00275     ResetTreeVariables();
00276 
00277     //if this isnt the first event in the snarl, then set the fHeaderInfo->newSnarl
00278     //to 0
00279     if(i > 0) fHeaderInfo->newSnarl = 0;
00280 
00281     if(fMakeNueBranch)fInfoFillerNue->Analyze(i, ntpManipulator->GetNtpStRecord(), fNueInfo);
00282 
00283     // HACK HACK HACK: 
00284     //
00285     // With MRCC files, this line will fill fTruthInfo
00286     // with the truth found from the mrnt file. Sadly, due to the
00287     // processing snafu documented in docdb 6599, the truth
00288     // information in mrnts isn't filled, so we'll later on fill the
00289     // truth from fInfoFillerMRCC::FillEventInformation
00290     if( fInfoFiller->FillInformation(i, ntpManipulator, fEventInfo, fTrackInfo,
00291                                      fShowerInfo, fTruthInfo) ){
00292       // The index field should actually be the index in this snarl. If this
00293       // isn't true then I don't know how the indexing works.
00294       assert(fEventInfo->index == i);
00295       // Fill the MRCC branch if this appears to be an MRCC file. Will
00296       // also fill the truth information, because of the problem
00297       // explained a couple of lines above
00298       if(mrRecord){
00299         fInfoFillerMRCC->FillEventInformation(i, mrRecord, stRecordOrig, 
00300                                               fEventInfoMRCC, 
00301                                               fDataType==1 ? fTruthInfo : 0);
00302         // Make sure it did the right thing
00303         assert(fEventInfoMRCC->bestEvent == fEventInfo->index || 
00304                fEventInfoMRCC->bestEvent == ANtpDefaultValue::kInt);
00305       } // end if mrRecord
00306 
00307       fNtuple->Fill();
00308 
00309     }//end if filling succeeded
00310   }//end loop over events for this snarl
00311   //-------------------------------------------------------------------
00312 
00313   return result;
00314 }
00315 
00316 //......................................................................
00317 void CondensedNtpModuleNC::Help()
00318 {
00319   MSG("JobC", Msg::kDebug)
00320     << "CondensedNtpModuleNC::Help\n"
00321     <<"CondensedNtpModuleNC is a module which demultiplexes events "
00322     <<"in the far detector."
00323     << endl;
00324 }
00325 
00326 //----------------------------------------------------------------------
00327 void CondensedNtpModuleNC::EndJob()
00328 {
00329 
00330   MSG("CondensedNtpModuleNC", Msg::kInfo) << "start end job method " << fFailedDeMux
00331                                           << " failed demuxing" << endl;
00332 
00333   //cd to the "directory" of the file
00334   fNtpFile->cd();
00335 
00336   // only write file  -- this is enough and prevents duplication
00337   fNtpFile->Write();
00338   fNtpFile->Close();
00339 
00340   return;
00341 }
00342 
00343 //......................................................................
00344 const Registry& CondensedNtpModuleNC::DefaultConfig() const
00345 {
00346 
00347   int itrue = 1;  // work around for lack of bool in registry
00348   int ifalse = 0; // work around for lack of bool in registry
00349 
00350   static Registry r;
00351 
00352   r.UnLockValues();
00353 
00354   r.Set("FileName",               "analysisTree.root");
00355   r.Set("TreeName",               "analysisTree");
00356   r.Set("DataType",               1);
00357 
00358   r.Set("VHSPlanes",20);
00359   r.Set("VHSStrips",20);
00360 
00361   r.Set("MakeNueBranch",          false);
00362 
00363   r.LockValues();
00364 
00365   //quiet the compiler
00366   itrue = ifalse;
00367 
00368   return r;
00369 }
00370 
00371 //......................................................................
00372 void CondensedNtpModuleNC::Config(const Registry& r)
00373 {
00374   int         tmpb;  // a temp bool. See comment under DefaultConfig...
00375   int         tmpi;  // a temp int.
00376   const char* tmps;  // a temp string
00377 
00378   if (r.Get("FileName",               tmps)) fFileName             =  tmps;
00379   if (r.Get("TreeName",               tmps)) fTreeName             =  tmps;
00380   if (r.Get("DataType",               tmpi)) fDataType             =  tmpi;
00381 
00382   if (r.Get("VHSPlanes",              tmpi)) fVHSPlanes            =  tmpi;
00383   if (r.Get("VHSStrips",              tmpi)) fVHSStrips            =  tmpi;
00384 
00385   if (r.Get("MakeNueBranch",          tmpb)) fMakeNueBranch        =  tmpb;
00386 
00387 }
00388 
00389 //......................................................................
00390 void CondensedNtpModuleNC::ResetTreeVariables()
00391 {
00392 
00393   fEventInfo->Reset();
00394   fTrackInfo->Reset();
00395   fShowerInfo->Reset();
00396   fTruthInfo->Reset();
00397   if(fMakeNueBranch)fNueInfo->Reset();
00398   fEventInfoMRCC->Reset();
00399 
00400   return;
00401 }

Generated on Mon Feb 15 11:06:32 2010 for loon by  doxygen 1.3.9.1