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

Truthifier.cxx

Go to the documentation of this file.
00001 #include "Truthifier.h"
00002 #include "MessageService/MsgService.h"
00003 #include "RecoBase/CandStripHandle.h"
00004 #include <TClonesArray.h>
00005 
00006 CVSID( " $Id: Truthifier.cxx,v 1.14 2006/05/19 14:55:42 rhatcher Exp $");
00007 ClassImp(Truthifier)
00008 
00009 
00010 
00026 
00027 
00028 // ----------------------------------------------------------------------- //
00029 const Truthifier& Truthifier::Instance( const MomNavigator* mom, Bool_t supressErrors )
00030 {
00035 
00036   // Deal with the case of a null mom.
00037   static Truthifier nullTruth(0);
00038   if(mom==0) return nullTruth;
00039 
00040   // Look for the simSnarl in Mom.
00041   SimSnarlRecord* simsnarl = 0;
00042   TObject* tobj;
00043   TIter    fragiter = mom->FragmentIter();
00044   while( ( tobj = fragiter.Next() ) ) {
00045     simsnarl = dynamic_cast<SimSnarlRecord*>(tobj);
00046     if(simsnarl) break;
00047   }
00048   
00049   if(simsnarl==0) return nullTruth; // No good.
00050 
00051   // look for the Truthifier in the simSnarl.
00052   const TObject* o;
00053   o = simsnarl->FindTemporary("Truthifier","Truthifier");
00054   if(o) {
00055     // Found one.
00056     Truthifier* ct = const_cast<Truthifier*>(dynamic_cast<const Truthifier*>(o));
00057     if(ct){
00058       if(ct->IsValid()) return *ct;
00059       else {
00060         ct->Reset(mom,supressErrors);
00061         return *ct;
00062         // it's not valid. Tell the record to drop it.
00063         // whooops, this is illegal.
00064         //simsnarl->RemoveTemporary(o);
00065       }
00066     }    
00067   }
00068 
00069   // Ok, now there's no truthifier.
00070   // Make one.
00071   Truthifier* t = new Truthifier(mom, supressErrors);
00072 
00073   // Save it.
00074   simsnarl->AdoptTemporary(t);
00075   
00076   // return it.
00077   return *t;
00078 }
00079 
00080 // ----------------------------------------------------------------------- //
00081 void Truthifier::Reset( const MomNavigator* mom, Bool_t supressErrors ) 
00082 {
00088 
00089   fMom = mom;
00090   fRddb = 0;
00091   fStdHep = 0;
00092   fNeutrinoList.clear();
00093   fTrackIndexToNeutrinoIndex.clear();
00094   fNeutrinoIndexToTrackIndex.clear();
00095   fTrackToHit.clear();
00096   fHitToSignal.clear();
00097   fSignalToDigit.clear();
00098   fSignals.clear(); 
00099   fValid=false;
00100 
00101   if(fMom ==0 ) return;
00102   
00103   // Find the simsnarl.
00104   SimSnarlRecord* simsnarl = 0;
00105   TObject* tobj;
00106   TIter    fragiter = mom->FragmentIter();
00107 
00108   // Get the simsnarl.
00109   while( ( tobj = fragiter.Next() ) ) {
00110     simsnarl = dynamic_cast<SimSnarlRecord*>(tobj);
00111     if(simsnarl) break;
00112   }
00113   if(simsnarl==0) { MSG("DataUtil",Msg::kWarning) << "Can't find simsnarl.\n"; return; };
00114   
00115   // Get the scint hit array.
00116   fScintHitArray = 
00117     dynamic_cast<const TObjArray*>(simsnarl->FindComponent(0,"DigiScintHits"));
00118   if(fScintHitArray==0) { 
00119 
00120     if(!supressErrors) MSG("DataUtil",Msg::kWarning) << "Can't find scint hit array.\n"; 
00121 
00122   } else {
00123   
00124     // Build the array of hits.
00125     TIter hitarrayIter(fScintHitArray);
00126     while( (tobj = hitarrayIter.Next()) ) {
00127       const DigiScintHit* scinthit = dynamic_cast<DigiScintHit*>(tobj);
00128       if(scinthit)
00129         fTrackToHit[scinthit->TrackId()].push_back(scinthit);
00130     }
00131 
00132   }
00133 
00134   // Get the signal array.
00135   const TObjArray* signalArray = 
00136     dynamic_cast<const TObjArray*>(simsnarl->FindComponent(0,"DigiSignal"));
00137   if(signalArray==0) { 
00138 
00139     if(!supressErrors) MSG("DataUtil",Msg::kWarning) << "Can't find signal array.\n";
00140   
00141   } else {
00142 
00143     // Build the array of signals.
00144     TIter signalIter(signalArray);
00145     while( (tobj = signalIter.Next()) ) {
00146       const DigiSignal* signal = dynamic_cast<DigiSignal*>(tobj);
00147       if(signal) {
00148         fSignals[signal->GetId()] = signal;
00149         for(UInt_t i=0;i<signal->GetNumberOfHits();i++) {
00150           const DigiScintHit* hit = signal->GetHit(i);
00151           if(hit) 
00152             fHitToSignal[hit].push_back(signal);
00153         }
00154       }
00155     }
00156     if(!supressErrors) MSG("DataUtil",Msg::kDebug) << "Got " << fSignals.size() << " signals." << std::endl;
00157 
00158   }
00159 
00160   // Get the StdHep list.
00161   fStdHep = dynamic_cast<const TClonesArray*>(simsnarl->FindComponent("TClonesArray","StdHep"));
00162   if(fStdHep ==0) {
00163    if(!supressErrors)  MSG("DataUtil",Msg::kWarning) << "Can't find StdHep list.\n";
00164   } else {
00165     // Look through initial pariticle list. Associate track ID with originating neutrinos
00166     Int_t siz = fStdHep->GetEntriesFast();
00167     fTrackIndexToNeutrinoIndex.resize(siz);
00168     fNeutrinoList.clear();
00169     Int_t neu = 0;
00170     for(Int_t ind=0; ind<siz; ind++) {
00171       TParticle* part = dynamic_cast<TParticle*>((*fStdHep)[ind]);
00172       
00173       // Is this a lepton?
00174       if(abs(part->GetPdgCode())>=12 &&
00175          abs(part->GetPdgCode())<=16 
00176          ) {
00177         // Is the status code "Documentation" or "null"?
00178         // This should indicate that it was an initial particle
00179         // into the event generator.
00180         Int_t status = part->GetStatusCode();
00181         if( status == 3 || status == 0 ) {
00182           // We got a neutrino event!
00183           neu=ind;
00184           fNeutrinoList.push_back(ind);
00185         }
00186       }
00187       fTrackIndexToNeutrinoIndex[ind]=neu;
00188       fNeutrinoIndexToTrackIndex[neu].push_back(ind);
00189     }    
00190   }
00191 
00192   // Get the NeuKin list.
00193   fNeuKins = dynamic_cast<const TClonesArray*>(simsnarl->FindComponent("TClonesArray","NeuKinList"));
00194   
00195   // Find the RDDB.
00196   const RawHeader* rawhead =0;
00197   fRddb =0;
00198   fragiter.Reset();
00199   while( ( tobj = fragiter.Next() ) ) {
00200     RawRecord* rawRec = dynamic_cast<RawRecord*>(tobj);
00201     if(rawRec) {
00202       rawhead = rawRec->GetRawHeader();
00203       TIter itr = rawRec->GetRawBlockIter();
00204       RawDataBlock* rawBlk;
00205       while ( ( rawBlk = dynamic_cast<RawDataBlock*>(itr.Next()) ) ) {
00206         fRddb = dynamic_cast<RawDigitDataBlock*>(rawBlk);
00207         if(fRddb) {
00208           // Got one!
00209           TIter diter = fRddb->GetDatumIter();
00210           RawDigit* rawDigit;
00211           diter.Reset();
00212           while( ( rawDigit = (RawDigit*) diter.Next() ) ) {
00213             // Relate this digit to the signal
00214             const DigiSignal* signal = GetSignal(rawDigit);
00215             if(signal) {
00216               fSignalToDigit[signal] = rawDigit;
00217             }
00218           }
00219         }
00220       }
00221     }
00222   } // Ugly ugly ugly
00223 
00224   // See if we're good enough.
00225   fValid = true;
00226 
00227 
00228   if(fRddb==0) {
00229     //if(!supressErrors) MSG("DataUtil",Msg::kError) << "Couldn't find RDDB. Truthifier is invalid. \n";
00230     fValid = false;
00231   }
00232   if(fTrackToHit.size()==0) {
00233     //if(!supressErrors) MSG("DataUtil",Msg::kError) << "No hits found. Truthifier is invalid. \n";
00234     fValid = false;
00235   }
00236   if(fHitToSignal.size()==0) {
00237     //if(!supressErrors) MSG("DataUtil",Msg::kError) << "No map hit->signal. Truthifier is invalid. \n";
00238     fValid = false;
00239   }
00240   if(fSignalToDigit.size()==0) {
00241     //if(!supressErrors) MSG("DataUtil",Msg::kError) << "No map signal->digit found. Truthifier is invalid. \n";
00242     fValid = false;
00243   }
00244   if(fSignals.size()==0) {
00245     //if(!supressErrors)  MSG("DataUtil",Msg::kError) << "No signals found. Truthifier is invalid. \n";
00246     fValid = false;
00247   }  
00248   if ((!fValid)&&(!supressErrors)) {
00249     MAXMSG("DataUtil",Msg::kWarning,5) 
00250       << "Truthifier built but is invalid. Not MC or null snarl." << endl;
00251   }
00252 }
00253 
00255 // Utility.
00256 std::vector<Int_t> Truthifier::GetListOfNeutrinoIndecies() const
00257 {
00258   return fNeutrinoList;
00259 }
00260 
00261 const TParticle*   Truthifier::GetStdHepParticle(Int_t index) const
00262 {
00263   if(fStdHep) {
00264     TParticle* part = dynamic_cast<TParticle*>((*fStdHep)[index]);
00265     return part;
00266   }
00267   return 0;
00268 }
00269 
00270 Int_t   Truthifier::GetStdHepSize() const
00271 {
00272   if(fStdHep) {
00273     return fStdHep->GetEntries();
00274   }
00275   return 0;
00276 }
00277 
00278 const REROOT_NeuKin* Truthifier::GetNeuKin(Int_t i) const
00279 {
00280   if(fNeuKins) {
00281     return dynamic_cast<const REROOT_NeuKin*>((*fNeuKins)[i]);
00282   }
00283   return 0;
00284 }
00285 
00286 const REROOT_NeuKin* Truthifier::GetNeuKinOfParticle(Int_t index) const
00287 {
00288   // Find matching neutrino
00289   for(Int_t i=fNeutrinoList.size()-1;i>=0;i--) {
00290     if(fNeutrinoList[i] <= index) {
00291       return GetNeuKin(i);
00292     }
00293   }
00294   return 0;
00295 }
00296 
00297 Int_t   Truthifier::GetScintHitListSize() const
00298 {
00299   if(fScintHitArray) {
00300     return fScintHitArray->GetEntriesFast();
00301   }
00302   return 0;
00303 }
00304 
00305 const DigiScintHit* Truthifier::GetScintHit(Int_t index) const
00306 {
00307   if(index >= GetScintHitListSize()) return 0;
00308   return dynamic_cast<const DigiScintHit*>( (*fScintHitArray)[index] );
00309 }
00310 
00311 
00312 
00313 const RawDigit*   Truthifier::GetRawDigit(const CandDigitHandle& cdh) const
00314 {
00315   int digitIndex = cdh.GetRawDigitIndex();
00316   const RawDigit* digit = 0;
00317   if(fRddb) digit = fRddb->At(digitIndex);
00318   return digit;
00319 }
00320 
00321 
00323 // FORWARD TRACKING
00324 // 
00325 // Routines to go from Particle->Digit
00326 //
00327 
00328 Int_t Truthifier::GetNeutrinoOfTrack( Int_t track ) const
00329 {
00330   return fTrackIndexToNeutrinoIndex[abs(track)];
00331 }
00332 
00333 
00334 // ----------------------------------------------------------------------- //
00335 const std::vector<const DigiSignal*>& 
00336 Truthifier::GetSignalVector( const DigiScintHit* hit ) const
00337 {
00341   HitToSignal_t::const_iterator it = fHitToSignal.find(hit);
00342   if(it!=fHitToSignal.end()) return it->second;
00343 
00344   static std::vector<const DigiSignal*> nullResult;
00345   return nullResult;
00346 }
00347 
00348 // ----------------------------------------------------------------------- //
00349 const RawDigit* 
00350 Truthifier::GetRawDigit( const DigiSignal* signal ) const
00351 {
00355   SignalToDigit_t::const_iterator it = fSignalToDigit.find(signal);
00356   if(it!=fSignalToDigit.end()) return it->second;
00357   return 0;
00358 }
00359 
00360 // ----------------------------------------------------------------------- //
00361 const std::vector<const RawDigit*>& 
00362 Truthifier::GetRawDigitVector( const DigiScintHit* hit ) const
00363 {
00371   static std::vector<const RawDigit*> output;
00372   output.clear();
00373   const std::vector<const DigiSignal*>& signalList
00374     = GetSignalVector(hit);
00375   for(UInt_t i=0;i<signalList.size();i++) {
00376     output.push_back(GetRawDigit(signalList[i]));
00377   }
00378   return output;
00379 }
00380 
00381 
00382 // ----------------------------------------------------------------------- //
00383 template<class T>
00384 std::vector<const T*> Truthifier::GetCandidatesFromParticle(int ParticleId, 
00385                                                               const CandHandle* list) const
00386 {
00391   std::vector<const T*> output;
00392 
00393   // Loop throuth the list.
00394   TIter it = list->GetDaughterIterator();
00395   TObject* obj;
00396   while( (obj=it.Next()) ) {
00397     const T* thing = dynamic_cast<const T*>(obj);
00398     if(thing) {
00399       const CandHandle* handle = dynamic_cast<const CandHandle*>(thing);
00400       if(handle)
00401         if(IsCandidateFromParticle(ParticleId,handle)) output.push_back(thing);
00402     }
00403   }; 
00404   
00405   return output;
00406 }
00407 
00408 // ----------------------------------------------------------------------- //
00409 template<class T>
00410 std::vector<const T*> Truthifier::GetCandidatesFromTrack(int TrackId, 
00411                                                          const CandHandle* list) const
00412 {
00417   std::vector<const T*> output;
00418 
00419   // Loop throuth the list.
00420   TIter it = list->GetDaughterIterator();
00421   TObject* obj;
00422   while( (obj=it.Next()) ) {
00423     const T* thing = dynamic_cast<const T*>(obj);
00424     if(thing) {
00425       const CandHandle* handle = dynamic_cast<const CandHandle*>(thing);
00426       if(handle)
00427         if(IsCandidateFromTrack(TrackId,handle)) output.push_back(thing);
00428     }
00429   }; 
00430   
00431   return output;
00432 }
00433 
00434 
00435 /*
00436   // For me.
00437 void Truthifier::Test() const 
00438 {
00439   std::vector<const CandDigitHandle*> output;
00440   
00441   if(fMom==0) return;
00442   const CandHandle* list = DataUtil::GetCandidate<CandHandle>
00443     (fMom,"CandStripSRListHandle","CandStripSRList");
00444 
00445   CandDigitsFrom(output,list);
00446   MSG("DataUtil",Msg::kInfo) << "total cand digits:" << output.size() << endl;
00447 
00448   std::vector<const CandStripHandle*> outlist;
00449   outlist = GetCandidatesFromParticle<CandStripHandle>(13,list);
00450   
00451   MSG("DataUtil",Msg::kInfo) << "total muon strips:" << outlist.size() << endl;  
00452 }
00453 */
00454 
00456 // BACKWARD TRACKING
00457 // 
00458 // Routines to go from Digit->Particle
00459 //
00460 
00461 
00462 // ----------------------------------------------------------------------- //
00463 const DigiSignal* Truthifier::GetSignal( const RawDigit* digit ) const
00464 {
00469 
00470   if(digit==0) return NULL;
00471 
00472   const RawDigiDigitMixIn* ddigit = dynamic_cast<const RawDigiDigitMixIn*>(digit);
00473   if(ddigit==0) return NULL;
00474 
00475   UInt_t id = ddigit->GetDigiSignalIndex();
00476   
00477   SignalMap_t::const_iterator it = fSignals.find(id);
00478   if(it!=fSignals.end()) return it->second;
00479 
00480   return NULL;
00481 }
00482 
00483 // ----------------------------------------------------------------------- //
00484 const DigiSignal* Truthifier::GetSignal( const CandDigitHandle& cdh ) const
00485 {
00490   int digitIndex = cdh.GetRawDigitIndex();
00491   const RawDigit* digit = 0;
00492   if(fRddb) digit = fRddb->At(digitIndex);
00493   return GetSignal(digit);
00494 }
00495 
00496 
00497 // ----------------------------------------------------------------------- //
00498 DigiSignal::DigiPmtTruth_t
00499 Truthifier::GetSignalTruthFlags( const CandDigitHandle& cdh ) const
00500 {
00504   const DigiSignal* signal = GetSignal(cdh);
00505   if(signal==0) return DigiSignal::kUnknown;
00506   return signal->GetTruth();
00507 }
00508 
00509 // ----------------------------------------------------------------------- //
00510 DigiSignal::DigiPmtTruth_t 
00511 Truthifier::GetSignalTruthFlags( const RawDigit* digit ) const
00512 {
00516   const DigiSignal* signal = GetSignal(digit);
00517   if(signal==0) return DigiSignal::kUnknown;
00518   return signal->GetTruth();
00519 }
00520 
00521 // ----------------------------------------------------------------------- //
00522 Bool_t Truthifier::DigitIsOnlyCrosstalk( const CandDigitHandle& cdh ) const
00523 {
00529   DigiSignal::DigiPmtTruth_t flags = GetSignalTruthFlags(cdh);
00530   if(  ( (flags & DigiSignal::kCrosstalk) || (flags & DigiSignal::kCrosstalkOptical) )
00531        && ( ! (flags & DigiSignal::kGenuine) ) ) {
00532     return true;
00533   }
00534   return false;
00535 }
00536  
00537 // ----------------------------------------------------------------------- //
00538 Bool_t Truthifier::DigitIsOnlyCrosstalk( const RawDigit* digit ) const
00539 {
00545   DigiSignal::DigiPmtTruth_t flags = GetSignalTruthFlags(digit);
00546   if(  ( (flags & DigiSignal::kCrosstalk) || (flags & DigiSignal::kCrosstalkOptical) )
00547        && ( ! (flags & DigiSignal::kGenuine) ) ) {
00548     return true;
00549   }
00550   return false;
00551 
00552 }
00553 
00554 
00555 // ----------------------------------------------------------------------- //
00556 Float_t 
00557 Truthifier::IsSignalFromParticle( Int_t particleID,  const DigiSignal* signal ) const
00558 {
00563   if(! signal) return 0;
00564   Float_t total = 0;
00565   Float_t totalMatch = 0;
00566   UInt_t n = signal->GetNumberOfHits();
00567   for(UInt_t i=0; i<n; i++) {
00568     const DigiScintHit* hit = signal->GetHit(i);
00569     Float_t weight = signal->GetHitWeight(i);
00570     if(hit->ParticleId() == particleID)  totalMatch += weight;
00571     total += weight;
00572   }
00573   if(total<=0) return 0;  // Guard against FPEs
00574   return totalMatch/total;
00575 }
00576 
00577 // ----------------------------------------------------------------------- //
00578 Float_t 
00579 Truthifier::IsSignalFromTrack( Int_t trackID,  const DigiSignal* signal ) const
00580 {
00585   if(! signal) return 0;
00586   Float_t total = 0;
00587   Float_t totalMatch = 0;
00588   UInt_t n = signal->GetNumberOfHits();
00589   for(UInt_t i=0; i<n; i++) {
00590     const DigiScintHit* hit = signal->GetHit(i);
00591     Float_t weight = signal->GetHitWeight(i);
00592     if(hit->TrackId() == trackID)  totalMatch += weight;
00593     total += weight;
00594   }
00595   if(total<=0) return 0;  // Guard against FPEs
00596   return totalMatch/total;
00597 }
00598 
00599 // ----------------------------------------------------------------------- //
00600 Float_t 
00601 Truthifier::IsSignalFromNeutrino( Int_t neutrinoID,  const DigiSignal* signal, Double_t max_t ) const
00602 {
00606   if(! signal) return 0;
00607   Float_t total = 0;
00608   Float_t totalMatch = 0;
00609   UInt_t n = signal->GetNumberOfHits();
00610   for(UInt_t i=0; i<n; i++) {
00611     const DigiScintHit* hit = signal->GetHit(i);
00612     Float_t weight = signal->GetHitWeight(i);
00613     total += weight;
00614     int trackId = abs(hit->TrackId());
00615 
00616     if(fTrackIndexToNeutrinoIndex[trackId]==neutrinoID) {
00617       if(hit->T1()<=max_t)
00618         totalMatch += weight;
00619     }
00620   }
00621   if(total<=0) return 0;  // Guard against FPEs
00622   return totalMatch/total;
00623 }
00624 
00625 
00626 // ----------------------------------------------------------------------- //
00627 const DigiScintHit* 
00628 Truthifier::GetBiggestHit( const CandDigitHandle& cdh ) const
00629 {
00634   const DigiSignal* signal = GetSignal(cdh);
00635   if(!signal) return 0;
00636   return signal->GetBiggestHit();
00637 }
00638 // ----------------------------------------------------------------------- //
00639 const DigiScintHit* 
00640 Truthifier::GetBiggestHit( const RawDigit* digit ) const
00641 {
00646   const DigiSignal* signal = GetSignal(digit);
00647   if(!signal) return 0;
00648   return signal->GetBiggestHit();
00649 }
00650 
00651 
00652 
00653 // ----------------------------------------------------------------------- //
00654 Float_t 
00655 Truthifier::IsDigitFromParticle( Int_t particleID,  const RawDigit* digit ) const
00656 {
00661   return IsSignalFromParticle(particleID,GetSignal(digit));
00662 }
00663 
00664 // ----------------------------------------------------------------------- //
00665 Float_t 
00666 Truthifier::IsDigitFromParticle( Int_t particleID,  const CandDigitHandle& cdh ) const
00667 {
00672   return IsSignalFromParticle(particleID,GetSignal(cdh));
00673 }
00674 
00675 
00676 // ----------------------------------------------------------------------- //
00677 Float_t 
00678 Truthifier::IsDigitFromTrack( Int_t trackID,  const RawDigit* digit ) const
00679 {
00684   return IsSignalFromTrack(trackID,GetSignal(digit));
00685 }
00686 
00687 // ----------------------------------------------------------------------- //
00688 Float_t 
00689 Truthifier::IsDigitFromNeutrino( Int_t neutrinoID,  const RawDigit* digit, Double_t max_t ) const
00690 {
00695   return IsSignalFromNeutrino(neutrinoID,GetSignal(digit),max_t);
00696 }
00697 
00698 // ----------------------------------------------------------------------- //
00699 Float_t 
00700 Truthifier::IsDigitFromTrack( Int_t trackID,  const CandDigitHandle& cdh ) const
00701 {
00706   return IsSignalFromTrack(trackID,GetSignal(cdh));
00707 }
00708 
00709 
00710 // ----------------------------------------------------------------------- //
00711 Float_t 
00712 Truthifier::IsDigitFromNeutrino( Int_t neutrinoID,  const CandDigitHandle& cdh, Double_t max_t ) const
00713 {
00718   return IsSignalFromNeutrino(neutrinoID,GetSignal(cdh),max_t);
00719 }
00720 
00721 
00722 
00723 // ----------------------------------------------------------------------- //
00724 PlexStripEndId Truthifier::BestSEIdOfDigit(const CandDigitHandle* cdh) const
00725 {
00730   // Get list of the stripends.
00731   std::map<PlexStripEndId,float> ends;
00732   std::map<PlexStripEndId,float>::iterator endit;
00733 
00734   if(!cdh) {
00735     MSG("DataUtil",Msg::kError) << "BestSEIdOfDigit was handed null CandDigitHandle*" << endl;
00736     return PlexStripEndId();
00737   }
00738 
00739   const DigiSignal* signal = GetSignal(*cdh);
00740   if(signal) {
00741     for(UInt_t i=0; i<signal->GetNumberOfHits(); i++) {
00742       PlexStripEndId seid = signal->GetHit(i)->StripEndId();
00743       float weight = signal->GetHitWeight(i);
00744       ends[seid] += weight;
00745     }
00746   } else return PlexStripEndId();
00747 
00748   // Find the best one.
00749   PlexStripEndId best;
00750   float bestpe = 0;
00751   for(endit = ends.begin(); endit!= ends.end(); endit++) {
00752     if(endit->second > bestpe) {
00753       bestpe = endit->second;
00754       best = endit->first;
00755     }
00756   }
00757   
00758   if(best.IsValid()) best.SetEnd(cdh->GetPlexSEIdAltL().GetEnd());
00759   
00760   return best;
00761 }
00762 
00763 // ----------------------------------------------------------------------- //
00764 void
00765 Truthifier::GetAllScintHits(const RawDigit* digit,       
00766                             std::set<const DigiScintHit*>& outSet) const
00767 {
00768   const DigiSignal* signal = GetSignal(digit);
00769   if(!signal) return;
00770   UInt_t n = signal->GetNumberOfHits();
00771   for(UInt_t i=0; i<n; i++) {
00772     const DigiScintHit* hit = signal->GetHit(i);
00773     outSet.insert(hit);
00774   }
00775 }
00776 
00777 // ----------------------------------------------------------------------- //
00778 void
00779 Truthifier::GetAllScintHits(const CandDigitHandle& cdh,   
00780                             std::set<const DigiScintHit*>& outSet) const
00781 {
00782   const DigiSignal* signal = GetSignal(cdh);
00783   if(!signal) return;
00784   UInt_t n = signal->GetNumberOfHits();
00785   for(UInt_t i=0; i<n; i++) {
00786     const DigiScintHit* hit = signal->GetHit(i);
00787     outSet.insert(hit);
00788   }
00789 }
00790 
00791 // ----------------------------------------------------------------------- //
00792 void
00793 Truthifier::GetAllScintHits(const CandHandle* candidate,   
00794                             std::set<const DigiScintHit*>& outSet) const
00795 {
00796   if(!candidate) return;
00797   std::vector<const CandDigitHandle*> digits;
00798   CandDigitsFrom(digits,candidate);
00799 
00800   for(UInt_t i=0;i<digits.size();i++) 
00801     GetAllScintHits(*(digits[i]),outSet);
00802 }
00803 
00804 // ----------------------------------------------------------------------- //
00805 Float_t 
00806 Truthifier::GetTotalEnergy( const RawDigit* digit, Int_t particleID ) const 
00807 {
00808   // Get the total energy of all scinthits that lent energy to this digit.
00809   // If particleID is non-zero, only add energy from that particle type
00810   // (-ve or +ve of that type)
00811  
00812   std::set<const DigiScintHit*> hits;
00813   std::set<const DigiScintHit*>::iterator it;
00814   Float_t total = 0;
00815   GetAllScintHits(digit,hits);
00816   for(it = hits.begin(); it!= hits.end(); it++) {
00817     const DigiScintHit* hit = *it;
00818     if(hit)
00819       if((particleID==0) || (abs(hit->ParticleId()) == particleID))
00820         total += hit->DE();
00821   }
00822 
00823   return total;
00824 }
00825 
00826 // ----------------------------------------------------------------------- //
00827 Float_t 
00828 Truthifier::GetTotalEnergy( const CandDigitHandle& cdh, Int_t particleID ) const 
00829 {
00830   // Get the total energy of all scinthits that lent energy to this digit.
00831   // If particleID is non-zero, only add energy from that particle type
00832   // (-ve or +ve of that type)
00833  
00834   std::set<const DigiScintHit*> hits;
00835   std::set<const DigiScintHit*>::iterator it;
00836   Float_t total = 0;
00837   GetAllScintHits(cdh,hits);
00838   for(it = hits.begin(); it!= hits.end(); it++) {
00839     const DigiScintHit* hit = *it;
00840     if(hit)
00841       if((particleID==0) || (abs(hit->ParticleId()) == particleID))
00842         total += hit->DE();
00843   }
00844 
00845   return total;
00846 }
00847 
00848 // ----------------------------------------------------------------------- //
00849 Float_t 
00850 Truthifier::GetTotalEnergy( const CandHandle* candidate, Int_t particleID ) const
00851 {
00852   // Get the total energy of all scinthits that lent energy to this digit.
00853   // If particleID is non-zero, only add energy from that particle type
00854   // (-ve or +ve of that type)
00855  
00856   std::set<const DigiScintHit*> hits;
00857   std::set<const DigiScintHit*>::iterator it;
00858   Float_t total = 0;
00859   GetAllScintHits(candidate,hits);
00860   for(it = hits.begin(); it!= hits.end(); it++) {
00861     const DigiScintHit* hit = *it;
00862     if(hit)
00863       if((particleID==0) || (abs(hit->ParticleId()) == particleID))
00864         total += hit->DE();
00865   }
00866 
00867   return total;
00868 }
00869 
00870 
00871 
00872 // ----------------------------------------------------------------------- //
00873 Float_t 
00874 Truthifier::IsCandidateFromParticle( Int_t particleID,  const CandHandle* candidate ) const
00875 {
00879   if(!candidate) return 0;
00880   std::vector<const CandDigitHandle*> digits;
00881   CandDigitsFrom(digits,candidate);
00882 
00883   // Test each digit. to see if it satisfies.
00884   Float_t total = 0;
00885   for(UInt_t i=0;i<digits.size();i++) 
00886     total += IsDigitFromParticle(particleID,*digits[i]);
00887   
00888   return total;
00889 }
00890 
00891 // ----------------------------------------------------------------------- //
00892 Float_t 
00893 Truthifier::IsCandidateFromTrack( Int_t trackID,  const CandHandle* candidate ) const
00894 {
00898   if(!candidate) return 0;
00899   std::vector<const CandDigitHandle*> digits;
00900   CandDigitsFrom(digits,candidate);
00901 
00902   // Test each digit. to see if it satisfies.
00903   Float_t total = 0;
00904   for(UInt_t i=0;i<digits.size();i++) 
00905     total += IsDigitFromTrack(trackID,*digits[i]);
00906   
00907   return total;
00908 }
00909 
00910 // ----------------------------------------------------------------------- //
00911 Float_t 
00912 Truthifier::IsCandidateGenuine( const CandHandle* candidate ) const
00913 {
00917   if(!candidate) return 0;
00918   std::vector<const CandDigitHandle*> digits;
00919   CandDigitsFrom(digits,candidate);
00920 
00921   // Test each digit. to see if it satisfies.
00922   Float_t total = 0;
00923   for(UInt_t i=0;i<digits.size();i++) {
00924     DigiSignal::DigiPmtTruth_t flags = GetSignalTruthFlags(*digits[i]);       
00925     if(flags & DigiSignal::kGenuine) {
00926       total++;
00927     }
00928   }
00929 
00930   return total/(float)(digits.size());
00931 }
00932 
00933 // ----------------------------------------------------------------------- //
00934 void Truthifier::Print(const Option_t*) const
00935 {
00941 
00942   // Go through all tracks.
00943   TrackToHit_t::const_iterator hitItr = fTrackToHit.begin();
00944   for( ; hitItr!=fTrackToHit.end(); hitItr++) {
00945     std::cout << "+--- TRACK " << hitItr->first << " ---------\n";
00946     
00947     // For each scint hit
00948     for(UInt_t ihit=0; ihit<hitItr->second.size(); ihit++) {
00949       const DigiScintHit* hit =  (hitItr->second)[ihit];
00950       std::cout << "|+-- DigiScintHit: " << hit->AsString() << std::endl;
00951       
00952       // For each signal
00953       HitToSignal_t::const_iterator htsItr = fHitToSignal.find(hit);
00954       if(htsItr != fHitToSignal.end()) {
00955         for(UInt_t isig = 0; isig<htsItr->second.size(); isig++) {
00956           
00957           const DigiSignal* signal = (htsItr->second)[isig];
00958           SignalToDigit_t::const_iterator stdItr = fSignalToDigit.find(signal);
00959           if(stdItr != fSignalToDigit.end()) {
00960             const RawDigit* digit = stdItr->second;
00961             std::cout << " |-- RawDigit: " << *digit << std::endl;
00962             std::cout << " |   DigiSignal: " 
00963                       << signal->GetCharge() / Munits::fC 
00964                << " fC" 
00965                       << "  Truth: " << AsString(signal->GetTruth()) << std::endl;      
00966           }
00967         }
00968         
00969       }
00970       
00971     }
00972   }
00973 }
00974 
00975 
00977 // Private functions.
00978 // 
00979 //
00980 
00981 // ----------------------------------------------------------------------- //
00982 void Truthifier::CandDigitsFrom(std::vector<const CandDigitHandle*>& output, 
00983                                 const CandHandle* list) const
00984 {
00985   if(!list) return;
00986  
00987   const CandDigitHandle* cdh = 
00988     dynamic_cast<const CandDigitHandle*>(list);
00989  
00990   if(cdh){
00991     output.push_back(cdh);
00992     return;
00993   }
00994 
00995   TIter it = list->GetDaughterIterator();
00996   TObject* obj;
00997   while( (obj=it.Next()) ) {
00998     const CandHandle* newHandle = dynamic_cast<const CandHandle*>(obj);
00999     if(newHandle)
01000       CandDigitsFrom(output,newHandle);
01001   }; 
01002 }

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