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

CondensedNtpModule.cxx

Go to the documentation of this file.
00001 
00002 //$Id: CondensedNtpModule.cxx,v 1.35 2007/02/12 21:25:37 asousa Exp $
00003 //
00004 //CondensedNtpModule.cxx
00005 //
00006 //Module for making analysis trees from SR ntuples
00007 //
00008 //B. Rebel 10/2004
00010 
00011 #include "AnalysisNtuples/Module/CondensedNtpModule.h"
00012 #include "AnalysisNtuples/Module/ANtpRecoNtpManipulator.h"
00013 #include "MessageService/MsgService.h"
00014 #include "MinosObjectMap/MomNavigator.h"
00015 #include "JobControl/JobCModuleRegistry.h" // For JOBMODULE macro
00016 #include "JobControl/JobCommand.h"
00017 #include "Validity/VldTimeStamp.h" 
00018 #include "CandNtupleSR/NtpSRRecord.h"
00019 #include "CandNtupleSR/NtpSREvent.h"
00020 #include "CandNtupleSR/NtpSREventSummary.h"
00021 #include "CandNtupleSR/NtpSRSlice.h"
00022 #include "CandNtupleSR/NtpSRTrack.h"
00023 #include "CandNtupleSR/NtpSRShower.h"
00024 #include "CandNtupleSR/NtpSRShieldStrip.h"
00025 #include "CandNtupleSR/NtpSRShieldSummary.h"
00026 #include "CandNtupleSR/NtpSRCosmicRay.h"
00027 #include "CandNtupleSR/NtpSRDmxStatus.h"
00028 #include "MCNtuple/NtpMCRecord.h"
00029 #include "MCNtuple/NtpMCTruth.h"
00030 #include "MCNtuple/NtpMCStdHep.h"
00031 #include "TruthHelperNtuple/NtpTHRecord.h"
00032 #include "TruthHelperNtuple/NtpTHEvent.h"
00033 #include "TruthHelperNtuple/NtpTHTrack.h"
00034 #include "TruthHelperNtuple/NtpTHShower.h"
00035 #include "TruthHelperNtuple/NtpTHStrip.h"
00036 #include "Record/RecCandHeader.h"
00037 #include "Conventions/Detector.h"
00038 #include "Conventions/SimFlag.h"
00039 
00040 #include "TFolder.h"
00041 #include "TDirectory.h"
00042 #include "TObjArray.h"
00043 
00044 #include <cassert>
00045 #include <algorithm>
00046 
00047 ClassImp(CondensedNtpModule)
00048 
00049 CVSID("$Id: CondensedNtpModule.cxx,v 1.35 2007/02/12 21:25:37 asousa Exp $");
00050 
00051 // Declare this module to JobControll. Arguments are:
00052 //  (1) The class name 
00053 //  (2) The human-readable name 
00054 //  (3) A short, human-readable description of what the module does
00055 JOBMODULE(CondensedNtpModule, 
00056           "CondensedNtpModule",
00057           "A module used for making analysis tress from SR ntuples");
00058 
00059 //......................................................................
00060 CondensedNtpModule::CondensedNtpModule() :
00061   fFileName("analysisNtuple.root"),
00062   fTreeName("analysisNtuple"),
00063   fNtpFile(0),
00064   fNtuple(0),
00065   fDetector(1),
00066   fEventInfo(0),
00067   fHeaderInfo(0),
00068   fShowerInfo(0),
00069   fTrackInfo(0),
00070   fTruthInfo(0),
00071   fStripInfoPresent(1),
00072   fDataType(1),
00073   fReconstructed(1)
00074 {
00075 
00076   MSG("JobC", Msg::kDebug) << "CondensedNtpModule::Constructor" << endl;
00077   
00078   fHeaderInfo = new ANtpHeaderInfo();
00079   fEventInfo = new ANtpEventInfo();
00080   fTrackInfo = new ANtpTrackInfo();
00081   fShowerInfo = new ANtpShowerInfo();
00082   fTruthInfo = new ANtpTruthInfo();
00083   
00084   //instantiate the ANtpInfoObjectFiller object
00085   fInfoFiller = new ANtpInfoObjectFiller();
00086         
00087 }
00088 
00089 //----------------------------------------------------------------------
00090 CondensedNtpModule::~CondensedNtpModule()
00091 {
00092   
00093   MSG("JobC", Msg::kDebug) << "CondensedNtpModule::Destructor" << endl;
00094   
00095   if(fHeaderInfo) delete fHeaderInfo;     
00096   if(fEventInfo) delete fEventInfo;       
00097   if(fShowerInfo) delete fShowerInfo;     
00098   if(fTrackInfo) delete fTrackInfo;       
00099   if(fTruthInfo) delete fTruthInfo;
00100     
00101 }
00102 
00103 //---------------------------------------------------------------------- 
00104 void CondensedNtpModule::BeginJob()
00105 {
00106   MSG("CondensedNtpModule", Msg::kDebug) 
00107     << "In BeginJob\n" << fFileName.c_str() 
00108     << " " << fTreeName.c_str() << endl;
00109 
00110   // Tell the InfoFillerObject which detector it is working on.
00111   // Module uses an int:     0-->NearDet & 1-->FarDet 
00112   // (Detector_t uses kNear = 1, kFar = 2) 
00113     switch (fDetector)
00114     {
00115     case 0:
00116       fInfoFiller->SetDetector(Detector::kNear);
00117       break;
00118     case 1:
00119       fInfoFiller->SetDetector(Detector::kFar);
00120       break;
00121     default:
00122       fInfoFiller->SetDetector(Detector::kUnknown); //Nothing is certain
00123       break;
00124     }
00125  
00126   // save the current working directory
00127   TDirectory* savedir  = gDirectory;  
00128   
00129   // create the new TFile for holding the electronics tree
00130   // this changes the value of gDirectoy
00131   fNtpFile = new TFile(fFileName.c_str(),"RECREATE");  
00132   
00133   // create TTree, these will attach themselves to the current 
00134   // working directory   
00135   fNtuple = new TTree(fTreeName.c_str(), "Analysis Tree");
00136   
00137   // the branches are all objects of type ANtp*Info*
00138   fNtuple->Branch("header.", "ANtpHeaderInfo", &fHeaderInfo, 64000, 2);
00139   fNtuple->Branch("event.", "ANtpEventInfo", &fEventInfo, 64000, 2);
00140   fNtuple->Branch("shower.", "ANtpShowerInfo", &fShowerInfo, 64000, 2);
00141   fNtuple->Branch("track.", "ANtpTrackInfo", &fTrackInfo, 64000, 2);
00142   if(fDataType>0){
00143     fNtuple->Branch("truth.", "ANtpTruthInfo", &fTruthInfo, 64000, 2);  
00144   } 
00145   
00146   // change back to current working directory before leaving constructor
00147   savedir->cd();   
00148   
00149   return;
00150 }
00151 
00152 //----------------------------------------------------------------------
00153 //here is where you would loop over the events in the snarl to fill the 
00154 //analysis tree
00155 JobCResult CondensedNtpModule::Ana(const MomNavigator *mom)
00156 {
00157   MSG("CondensedNtpModule", Msg::kDebug) << "start ana" << endl;
00158   
00159   JobCResult result(JobCResult::kPassed);
00160   
00161   //get the records from MOM
00162   assert(mom);
00163   
00164   NtpSRRecord *record = dynamic_cast<NtpSRRecord *>(mom->GetFragment("NtpSRRecord"));
00165   NtpStRecord *stRecord = dynamic_cast<NtpStRecord *>(mom->GetFragment("NtpStRecord"));
00166   
00167   if(!record && !stRecord){
00168     MSG("CondensedNtpModuleAtm", Msg::kWarning) << "Could not get NtpSR or NtpSt Record from MOM" << endl;
00169     result.SetWarning().SetFailed();
00170     return result;
00171   }//end if no NtpSRRecord
00172   
00173   //if there is no NtpMCRecord, then mom returns a null pointer
00174   NtpMCRecord *mcRecord = 0;
00175   mcRecord = dynamic_cast<NtpMCRecord *>(mom->GetFragment("NtpMCRecord"));
00176   if(!mcRecord&&fDataType&&!stRecord){
00177     MSG("CondensedNtpModule", Msg::kWarning) << "Could not get NtpMCRecord from MOM " 
00178                                              << " - hopefully you werent expecting one " << endl;
00179     result.SetWarning().SetFailed();
00180     return result;
00181   }
00182   
00183   NtpTHRecord *thRecord = 0;
00184   thRecord = dynamic_cast<NtpTHRecord *>(mom->GetFragment("NtpTHRecord"));
00185   
00186   //fill the header information
00187   Detector::Detector_t det = Detector::kFar;
00188   if(fDetector<1) det = Detector::kNear;
00189   //get a sim flag
00190   SimFlag::SimFlag_t dataType = SimFlag::kData;
00191   if(fDataType) dataType = SimFlag::kMC;
00192   
00193   //instantiate a ANtpRecoNtpManipulator object to help you get the info you want
00194   ANtpRecoNtpManipulator *ntpManipulator = 0;
00195   if(record) ntpManipulator = new ANtpRecoNtpManipulator(record, mcRecord, thRecord);
00196   else if(stRecord) ntpManipulator = new ANtpRecoNtpManipulator(stRecord);
00197 
00198   fInfoFiller->SetStripArray(ntpManipulator->GetStripArray());
00199   fInfoFiller->FillHeaderInformation(ntpManipulator, det, dataType, fHeaderInfo);  
00200 
00201   //set up which flags you want to use to determine the primary shower or track
00202   //a value of 0 for a flag means it will not be used
00203   ntpManipulator->SetPrimaryTrackCriteria(0,1,0); // nplanes, length, total pulse height
00204   ntpManipulator->SetPrimaryShowerCriteria(0,1); // nplanes, total pulse height
00205   
00206   //mc data because fDataType = 1, but no reconstructed event because 
00207   //record doesnt exist.  still want to have the mc info in the tree though 
00208   //to understand events that werent reconstructed
00209   if((mcRecord&&!record&&fDataType) 
00210      || (stRecord&&fDataType&&ntpManipulator->GetSnarlEventSummary().nevent<1)){
00211     
00212     MSG("CondensedNtpModule", Msg::kDebug) << "why am i here?" << endl;
00213     
00214     fReconstructed = 0;
00215     
00216     //flag the run, subRun values to indicated that 
00217     //this snarl was not reconstructed.  keep track of 
00218     //how many times that happens
00219     FillUnRecoedMCInformation(ntpManipulator->GetMCArray(), ntpManipulator->GetStdHepArray());
00220     fNtuple->Fill();
00221     ResetTreeVariables();
00222     
00223     return result;              
00224   }
00225   
00226   fReconstructed = 1;
00227   
00228   //-----------------------------------------------------------------------------------
00229   //here is where you start looping over the events in the ntuple to fill your analysis tree
00230   
00231   //set up some pointers to typical NtpSR objects
00232   NtpSRTrack *ntpTrack = 0;
00233   NtpSRStrip *ntpStrip = 0;
00234   NtpSREvent *ntpEvent = 0;
00235   NtpSRShower *ntpShower = 0;
00236   NtpMCTruth *ntpMCTruth = 0;
00237   NtpMCStdHep *ntpMCStdHep = 0;
00238   NtpTHEvent *ntpTHEvent = 0;
00239   //NtpTHStrip *ntpTHStrip = 0;
00240   //NtpTHTrack *ntpTHTrack = 0;
00241   //NtpTHShower *ntpTHShower = 0;
00242   
00243   //loop over the events in the snarl - it is also possible to just loop over 
00244   //showers, tracks or slices in the record using a loop like
00245   //for(Int_t i = 0; i < record->evthdr.ntrack; ++i){
00246   //  ntpTrack = ntpManipulator->GetSnarlTrack(i);
00247   //}
00248   //
00249   //you can get the primary track/shower from the snarl using 
00250   //ntpManipulator->GetSnarlPrimaryTrack()/GetSnarlPrimaryShower()
00251   
00252   for(Int_t i = 0; i < ntpManipulator->GetSnarlEventSummary().nevent; ++i){
00253     
00254     MSG("CondensedNtpModule", Msg::kDebug) << "on event " << i << endl;
00255     
00256     fEventInfo->index = i;
00257     
00258     //get event i.  the call to NtpHelper::GetEventInSnarl sets the 
00259     //NtpSREvent data member in that object to the current event so that
00260     //you can later call for the primary track, shower, etc.
00261     ntpManipulator->GetEventManipulator()->SetEventInSnarl(i);
00262     ntpEvent = ntpManipulator->GetEventManipulator()->GetEvent();               
00263     if(ntpEvent) fInfoFiller->FillEventInformation(ntpManipulator, 
00264                                                    ntpEvent, 
00265                                                    fEventInfo);
00266     
00267     //get the primary track for the event - if no track is present it 
00268     //returns 0
00269     ntpTrack = ntpManipulator->GetEventManipulator()->GetPrimaryTrack();                
00270     if(ntpTrack) fInfoFiller->FillTrackInformation(ntpTrack, fTrackInfo);
00271     
00272     //get the primary shower for the event - if no shower is present it 
00273     //returns 0
00274     ntpShower = ntpManipulator->GetEventManipulator()->GetPrimaryShower();              
00275     if(ntpShower) fInfoFiller->FillShowerInformation(ntpShower, fShowerInfo);
00276     
00277     //why not get a strip as well - if there is a strip in the event, the 
00278     //following line will get it
00279     if(fStripInfoPresent) ntpStrip = ntpManipulator->GetEventManipulator()->GetStrip(0);
00280     
00281     MSG("CondensedNtpModule", Msg::kDebug) << "CondensedNtpModule::Ana mcRecord="<<mcRecord << endl;
00282     MSG("CondensedNtpModule", Msg::kDebug) << "CondensedNtpModule::Ana thRecord="<<thRecord << endl;
00283     //check to see if there is MC information around and get the NtpMCTruth and
00284     //NtpTHEvent objects
00285 
00286     //the GetNtpTHEvent method takes as its argument the same index as the 
00287     //GetEventInSnarl method, ie the index of the current event.  you can 
00288     //then use the NtpTHEvent object returned to figure out which index to 
00289     //give the GetNtpMCTruth and GetNtpMCStdHep methods to return the appropriate 
00290     //truth information corresponding to this event.  what is going on behind the 
00291     //scenes is that there are arrays of NtpMCTruth and NtpMCStdHep objects in 
00292     //the NtpMCRecord but to figure out which entry is the appropriate one you need 
00293     //to have the NtpTHRecord array of thevt which is filled in the same order as 
00294     //the evt array in the NtpSRRecord.
00295     ntpTHEvent = ntpManipulator->GetMCManipulator()->GetNtpTHEvent(i);
00296     if(ntpTHEvent){
00297       ntpMCTruth = ntpManipulator->GetMCManipulator()->GetNtpMCTruth(ntpTHEvent->neumc);        
00298       ntpMCStdHep = ntpManipulator->GetMCManipulator()->GetNtpMCStdHep(ntpTHEvent->neustdhep);  
00299     }
00300     
00301     //can also do the above using NtpTHStrips, NtpTHShowers or NtpTHTracks.  get the index 
00302     //of the strip, track or shower in the NtpSRRecord stp, trk or shw arrays first, then 
00303     //get the NtpTHTrack/Shower object using the NtpHelper.  have to uncomment the variable 
00304     //declarations in lines 223-225 to use this
00305     //uncomment out the disired lines below
00306     /*
00307     
00308       if(ntpStrip) ntpTHStrip = ntpManipulator->GetMCManipulator()->GetNtpTHStrip(ntpStrip->index);
00309       if(ntpTHStrip){
00310       ntpMCTruth = ntpManipulator->GetMCManipulator()->GetNtpMCTruth(ntpTHStrip->neumc);        
00311       ntpMCStdHep = ntpManipulator->GetMCManipulator()->GetNtpMCStdHep(ntpTHStrip->neustdhep);  
00312       }
00313     
00314       if(ntpTrack) ntpTHTrack = ntpManipulator->GetMCManipulator()->GetNtpTHTrack((Int_t)ntpTrack->index);
00315       if(ntpTHTrack){
00316       ntpMCTruth = ntpManipulator->GetMCManipulator()->GetNtpMCTruth(ntpTHTrack->neumc);        
00317       ntpMCStdHep = ntpManipulator->GetMCManipulator()->GetNtpMCStdHep(ntpTHTrack->neustdhep);  
00318       }
00319       if(ntpShower) ntpTHShower = ntpManipulator->GetMCManipulator()->GetNtpTHShower((Int_t)ntpShower->index);   
00320       if(ntpTHShower){
00321       ntpMCTruth = ntpManipulator->GetMCManipulator()->GetNtpMCTruth(ntpTHShower->neumc);       
00322       ntpMCStdHep = ntpManipulator->GetMCManipulator()->GetNtpMCStdHep(ntpTHShower->neustdhep); 
00323       }
00324     */
00325       
00326     //got the NtpMCTruth object now, so fill the desired info
00327     MSG("CondensedNtpModule", Msg::kDebug) << "CondensedNtpModule::Ana ntpMCTruth="<<ntpMCTruth<< endl;
00328     if(ntpMCTruth){
00329       fInfoFiller->FillMCTruthInformation(ntpMCTruth, fTruthInfo);
00330     }                           
00331   
00332     
00333     fNtuple->Fill();
00334     ResetTreeVariables();
00335   }//end loop over events for this snarl
00336   //-----------------------------------------------------------------------------------
00337 
00338   delete ntpManipulator;
00339 
00340   return result;
00341 }
00342 
00343 //......................................................................
00344 void CondensedNtpModule::Help() 
00345 {
00346   MSG("JobC", Msg::kInfo) 
00347     << "NearElectronicsCheck::Help\n"
00348     <<"CondensedNtpModule is a module which demultiplexes events "
00349     <<"in the far detector."
00350     << endl;
00351 }
00352 
00353 //----------------------------------------------------------------------
00354 void CondensedNtpModule::EndJob() 
00355 {
00356   
00357   MSG("CondensedNtpModule", Msg::kInfo) << "start end job method" << endl;      
00358   
00359   //save current working directory
00360   TDirectory *savedir = gDirectory;
00361   
00362   //cd to the "directory" of the file
00363   fNtpFile->cd();
00364 
00365   fNtuple->Write();
00366   
00367   //go back to original working directory
00368   savedir->cd();  
00369   
00370   fNtpFile->Write();
00371   fNtpFile->Close();
00372   
00373   return;
00374 }
00375 
00376 //......................................................................
00377 const Registry& CondensedNtpModule::DefaultConfig() const
00378 {
00379   
00380   int itrue = 1;  // work around for lack of bool in registry
00381   int ifalse = 0; // work around for lack of bool in registry
00382   
00383   static Registry r;
00384   
00385   r.UnLockValues();
00386   
00387   r.Set("FileName",           "analysisTree.root");
00388   r.Set("TreeName",           "analysisTree");
00389   r.Set("EndPlane",           485);
00390   r.Set("Detector",           1);
00391   r.Set("StripInfoPresent",   1);
00392   r.Set("DataType",           1);
00393   
00394   r.LockValues();
00395   
00396   //quiet compiler warnings
00397   itrue = ifalse;
00398   
00399   return r;
00400 }
00401 
00402 //......................................................................
00403 void CondensedNtpModule::Config(const Registry& r)
00404 {
00405   int         tmpb;  // a temp bool. See comment under DefaultConfig...
00406   int         tmpi;  // a temp int.
00407   double      tmpd;  // a temp double.
00408   const char* tmps;  // a temp string
00409   
00410   if (r.Get("FileName",           tmps)) fFileName              =  tmps;
00411   if (r.Get("TreeName",           tmps)) fTreeName              =  tmps;
00412   if (r.Get("EndPlane",           tmpi)) fEndPlane              =  tmpi;
00413   if (r.Get("Detector",           tmpi)) fDetector              =  tmpi;
00414   if (r.Get("StripInfoPresent",   tmpi)) fStripInfoPresent      =  tmpi;
00415   if (r.Get("DataType",           tmpi)) fDataType              =  tmpi;
00416   
00417   //quiet compiler warnings
00418   tmpb = 0;
00419   tmpd = 0.;
00420   
00421   return;
00422 }
00423 
00424 //----------------------------------------------------------------------
00425 void CondensedNtpModule::FillUnRecoedMCInformation(TClonesArray *mcArray, TClonesArray *stdArray)
00426 {
00427   NtpMCTruth *ntpMCTruth = 0;
00428   NtpMCStdHep *ntpMCStdHep = 0;
00429   
00430   //get the number of entries in the arrays
00431   Int_t numMCEntries = mcArray->GetLast()+1;
00432   Int_t stdHepLow = 0;
00433   Int_t stdHepHigh = 0;
00434   
00435   //loop over the individual events - only one in the far detector, 
00436   //many in the near
00437   for(Int_t i = 0; i < numMCEntries; ++i){
00438     
00439     fEventInfo->index = i;
00440     
00441     ntpMCTruth = dynamic_cast<NtpMCTruth *>(mcArray->At(i));
00442     if(ntpMCTruth) fInfoFiller->FillMCTruthInformation(ntpMCTruth, fTruthInfo);
00443     
00444     stdHepLow = ntpMCTruth->stdhep[0];
00445     stdHepHigh = ntpMCTruth->stdhep[1];
00446     
00447     //loop over the stdhep entries for the current mc event
00448     for(Int_t j = stdHepLow; j < stdHepHigh+1; ++j){
00449       ntpMCStdHep = dynamic_cast<NtpMCStdHep *>(stdArray->At(j));
00450       
00451       //add the code for what you want to fill here 
00452     }                   
00453     
00454   }//end loop over mc events
00455   
00456   
00457   return;
00458 }
00459 
00460 //----------------------------------------------------------------------
00461 void CondensedNtpModule::ResetTreeVariables()
00462 {
00463     
00464   fEventInfo->Reset();
00465   fShowerInfo->Reset();
00466   fTrackInfo->Reset();
00467   fTruthInfo->Reset();
00468   
00469   return;
00470 }

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