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

FillBasic.cxx

Go to the documentation of this file.
00001 // $Id: FillBasic.cxx,v 1.14 2009/02/28 21:46:16 gmieg Exp $
00002 
00003 // C++
00004 #include <algorithm>
00005 #include <cmath>
00006 
00007 // ROOT
00008 #include "TClonesArray.h"
00009 
00010 // MINOS
00011 #include "Conventions/PlaneView.h"
00012 #include "CandNtupleSR/NtpSREvent.h"
00013 #include "CandNtupleSR/NtpSRShower.h"
00014 #include "CandNtupleSR/NtpSRStrip.h"
00015 #include "CandNtupleSR/NtpSRTrack.h"
00016 #include "DataUtil/infid.h"
00017 #include "MessageService/MsgService.h"
00018 #include "StandardNtuple/NtpStRecord.h"
00019 
00020 // Local
00021 #include "PhysicsNtuple/Default.h"
00022 #include "PhysicsNtuple/Vertex.h"
00023 
00024 // Local
00025 #include "FillBasic.h"
00026 
00027 CVSID("$Id: FillBasic.cxx,v 1.14 2009/02/28 21:46:16 gmieg Exp $");
00028 
00029 using namespace std;
00030 
00031 //-------------------------------------------------------------------
00032 Anp::FillBasic::FillBasic()
00033    :fCheck(true),
00034     fTrackZOffset(0.0392)
00035 {    
00036 }
00037 
00038 //----------------------------------------------------------------------
00039 Anp::FillBasic::~FillBasic()
00040 {
00041 }
00042 
00043 //----------------------------------------------------------------------
00044 void Anp::FillBasic::Config(const Registry &reg)
00045 {
00046    //
00047    // Configure self
00048    //
00049    Anp::Read(reg, "FillBasicCheck", fCheck);
00050 
00051    reg.Get("TrackZOffset", fTrackZOffset);
00052 
00053    if(reg.KeyExists("PrintConfigBasic"))
00054    {
00055       cout << "FillBasic::Config" << endl
00056            << "   Check = " << fCheck << endl
00057            << "   TrackZOffset = " << fTrackZOffset << endl;
00058    }
00059 }
00060 
00061 //----------------------------------------------------------------------
00062 bool Anp::FillBasic::Fill(const NtpStRecord &ntprec, const NtpSREvent &record)
00063 {
00064    //
00065    // Fill Anp::Basic and Anp::Vertex data
00066    //
00067 
00068    fBasic.Clear();
00069    fBegVtx.Clear();
00070    fEndVtx.Clear();
00071 
00072    const bool isfid = infid(ntprec.GetHeader().GetVldContext().GetDetector(),
00073                             ntprec.GetHeader().GetVldContext().GetSimFlag(),
00074                             record.vtx.x,
00075                             record.vtx.y,
00076                             record.vtx.z);
00077 
00078    fBasic  = FillBasic::Get(ntprec, record.stp, record.nstrip);
00079    fBegVtx = FillBasic::Get(record.vtx, isfid);
00080    
00081    if(!Fill(fBasic, record.ph))
00082    {      
00083       MSG("FillAlg", Msg::kWarning) << "Fill failed for NtpSREvent \n" << record;
00084       return false;
00085    }
00086 
00087    if(fCheck && !Check(fBasic, record.plane))
00088    {
00089       MSG("FillAlg", Msg::kWarning) << "NtpSREvent plane check failed \n" << record.plane;
00090       return false;
00091    }
00092 
00093    return true;
00094 }
00095 
00096 //----------------------------------------------------------------------
00097 bool Anp::FillBasic::Fill(const NtpStRecord &ntprec, const NtpSRShower &record)
00098 {
00099    //
00100    // Fill Anp::Basic and Anp::Vertex data
00101    //
00102 
00103    fBasic.Clear();
00104    fBegVtx.Clear();
00105    fEndVtx.Clear();
00106 
00107    const bool isfid = infid(ntprec.GetHeader().GetVldContext().GetDetector(),
00108                             ntprec.GetHeader().GetVldContext().GetSimFlag(),
00109                             record.vtx.x,
00110                             record.vtx.y,
00111                             record.vtx.z);
00112    
00113    fBasic  = FillBasic::Get(ntprec, record.stp, record.nstrip);
00114    fBegVtx = FillBasic::Get(record.vtx, isfid);
00115 
00116    if(!Fill(fBasic, record.ph))
00117    {      
00118       MSG("FillAlg", Msg::kWarning) << "Fill failed for NtpSRShower \n" << record;
00119       return false;
00120    }
00121 
00122    if(fCheck && !Check(fBasic, record.plane))
00123    {
00124       MSG("FillAlg", Msg::kWarning) << "NtpSRShower plane check failed \n" << record.plane;
00125       return false;
00126    }
00127 
00128    return true;
00129 }
00130 
00131 //----------------------------------------------------------------------
00132 bool Anp::FillBasic::Fill(const NtpStRecord &ntprec, const NtpSRTrack &record)
00133 {
00134    //
00135    // Fill Anp::Basic and Anp::Vertex data
00136    //
00137 
00138    fBasic.Clear();
00139    fBegVtx.Clear();
00140    fEndVtx.Clear();
00141 
00142    const bool isfid = infid(ntprec.GetHeader().GetVldContext().GetDetector(),
00143                             ntprec.GetHeader().GetVldContext().GetSimFlag(),
00144                             record.vtx.x,
00145                             record.vtx.y,
00146                             record.vtx.z - fTrackZOffset);
00147    
00148 
00149    fBasic  = FillBasic::Get(ntprec, record.stp, record.nstrip);
00150    fBegVtx = FillBasic::Get(record.vtx, isfid);
00151    fEndVtx = FillBasic::Get(record.end, false);
00152 
00153    if(!Fill(fBasic, record.ph))
00154    {      
00155       MSG("FillAlg", Msg::kWarning) << "Fill failed for NtpSREvent \n" << record;
00156       return false;
00157    }
00158 
00159    if(fCheck && !Check(fBasic, record.plane))
00160    {
00161       MSG("FillAlg", Msg::kWarning) << "NtpSRTrack plane check failed \n" << record.plane;
00162       return false;
00163    }
00164 
00165    return true;
00166 }
00167 
00168 //----------------------------------------------------------------------
00169 const Anp::Vertex Anp::FillBasic::Get(const NtpSRVertex &vtx, const bool isfid_) const
00170 {
00171    //
00172    // This is really just a Vertex constructor, put it here to avoid NtpSRVertex dependency 
00173    //
00174 
00175    return Vertex(vtx.u,
00176                  vtx.v,
00177                  vtx.z,
00178                  vtx.dcosu,
00179                  vtx.dcosv,
00180                  vtx.dcosz,
00181                  isfid_);
00182 }
00183 
00184 //----------------------------------------------------------------------
00185 bool Anp::FillBasic::Fill(Basic &basic, const NtpSRStripPulseHeight &ph) const
00186 {
00187    //
00188    // Fill Basic with pulse height information and check consistency between standard
00189    // pulse height class NtpSRStripPulseHeight and pulse height computed by iterating over
00190    // NtpSRStrip objects that belong to reconstructed object.
00191    //
00192 
00193    basic.siglin = ph.siglin;
00194    basic.pe     = ph.pe;
00195    basic.sigmap = ph.sigmap;
00196    basic.mip    = ph.mip;
00197 
00198    if(std::fabs(basic.adcu + basic.adcv - ph.raw) > 1.001)
00199    {
00200       static unsigned int count = 0;
00201       MSG("FillAlg", Msg::kWarning) << "Error " << ++count << " in NtpSRStripPulseHeight adc "
00202                                     << basic.adcu + basic.adcv << " - " << ph.raw << " = "
00203                                     << basic.adcu + basic.adcv - ph.raw << std::endl;
00204       return false;
00205    }
00206    if(std::fabs(basic.sigcoru + basic.sigcorv - ph.sigcor) > 1.001)
00207    {
00208       static unsigned int count = 0;
00209       MSG("FillAlg", Msg::kWarning) << "Error " << ++count << " in NtpSRStripPulseHeight sigcor " 
00210                                     << basic.sigcoru + basic.sigcorv << " - " << ph.sigcor << " = "
00211                                     << basic.sigcoru + basic.sigcorv - ph.sigcor << std::endl;
00212       return false;
00213    }
00214 
00215    return true;
00216 }
00217 
00218 //----------------------------------------------------------------------
00219 bool Anp::FillBasic::Check(const Basic &basic, const NtpSRPlane &plane) const
00220 {
00221    //
00222    // Check consistency of Basic object, this is useful bug check.
00223    //
00224 
00225    bool result = true;
00226 
00227    if(plane.nu + plane.nv != plane.n)
00228    {
00229       MSG("FillAlg", Msg::kWarning) << "Number of planes does not match" << endl;
00230       result = false;
00231    }
00232    if(basic.nuplane != short(plane.nu))
00233    {
00234       MSG("FillAlg", Msg::kWarning) << "Mismatched number of U planes" << endl;
00235       result = false;
00236    }
00237    if(basic.nvplane != short(plane.nv))
00238    {
00239       MSG("FillAlg", Msg::kWarning) << "Mismatched number of V planes" << endl;
00240       result = false;
00241    }
00242    
00243    if(plane.beg < plane.end)
00244    {
00245       if(basic.beg_uplane != short(plane.begu))
00246       {
00247          MSG("FillAlg", Msg::kWarning) << "Mismatched beg U plane" << endl;
00248          result = false;
00249       }
00250       if(basic.beg_vplane != short(plane.begv))
00251       {
00252          MSG("FillAlg", Msg::kWarning) << "Mismatched beg V plane" << endl;
00253          result = false;
00254       }
00255       if(basic.end_uplane != short(plane.endu))
00256       {
00257          MSG("FillAlg", Msg::kWarning) << "Mismatched end U plane" << endl;
00258          result = false;
00259       }  
00260       if(basic.end_vplane != short(plane.endv))
00261       {
00262          MSG("FillAlg", Msg::kWarning) << "Mismatched end V plane" << endl;
00263          result = false;
00264       }
00265    }
00266    else if(plane.beg > plane.end)
00267    {    
00268       if(basic.beg_uplane != short(plane.endu))
00269       {
00270          MSG("FillAlg", Msg::kWarning) << "Mismatched beg U plane" << endl;
00271          result = false;
00272       }
00273       if(basic.beg_vplane != short(plane.endv))
00274       {
00275          MSG("FillAlg", Msg::kWarning) << "Mismatched beg V plane" << endl;
00276          result = false;
00277       }
00278       if(basic.end_uplane != short(plane.begu))
00279       {
00280          MSG("FillAlg", Msg::kWarning) << "Mismatched end U plane" << endl;
00281          result = false;
00282       }  
00283       if(basic.end_vplane != short(plane.begv))
00284       {
00285          MSG("FillAlg", Msg::kWarning) << "Mismatched end V plane" << endl;
00286          result = false;
00287       }
00288    }
00289    else
00290    {
00291       MSG("FillAlg", Msg::kWarning) << "beg and end plane are equal" << endl;
00292       result = false;
00293    }
00294 
00295    return result;
00296 }
00297 
00298 //----------------------------------------------------------------------
00299 const Anp::Basic Anp::FillBasic::Get(const NtpStRecord &record,
00300                                      const int *index_array, const int nstrip) const
00301 {  
00302    //
00303    // Iterate over strips with indexes stored in index_array and
00304    // compute pulse height and time and plane spans.
00305    //
00306 
00307    Basic data;
00308 
00309    if(nstrip < 1 || !index_array)
00310    {
00311       return data;
00312    }
00313 
00314    //
00315    // Initialize variables that use += operator
00316    //
00317    data.nustrip = 0;
00318    data.nvstrip = 0;   
00319    data.adcu = 0.0;
00320    data.adcv = 0.0;
00321    data.sigcoru = 0.0;
00322    data.sigcorv = 0.0;
00323    
00324    const TClonesArray *strip_array = record.stp;
00325    if(!strip_array)
00326    {
00327       MSG("FillAlg", Msg::kWarning) << "NtpStRecord does not have valid strip array." << endl; 
00328       return data;
00329    }
00330 
00331    vector<short> uplane_vec, vplane_vec;
00332 
00333    const int entries = strip_array -> GetEntries();
00334 
00335    const Detector::Detector_t detector = record.GetHeader().GetVldContext().GetDetector();
00336 
00337    bool init_time = false;
00338    for(int i = 0; i < nstrip; ++i)
00339    {            
00340       const short index = index_array[i];
00341       if(index < 0 || index >= entries)
00342       {
00343          MSG("FillAlg", Msg::kWarning) << "NtpSRStrip index is out of range: " << index << endl;
00344          continue; 
00345       }
00346 
00347       NtpSRStrip *ntpstp = Anp::GetSRStrip(record, index);
00348       if(!ntpstp)
00349       {
00350          continue;
00351       }
00352     
00353       if(index != ntpstp -> index)
00354       {  
00355          MSG("FillAlg", Msg::kWarning) << "Mismatched strip and TClonesArray index" << endl;
00356          continue;
00357       }
00358 
00359       const short plane = ntpstp -> plane;      
00360 
00361       if(ntpstp -> planeview == PlaneView::kU)
00362       {
00363          if(std::find(uplane_vec.begin(), uplane_vec.end(), plane) == uplane_vec.end())
00364          {
00365             uplane_vec.push_back(plane);
00366          }
00367       }
00368       else if(ntpstp -> planeview == PlaneView::kV)
00369       {
00370          if(std::find(vplane_vec.begin(), vplane_vec.end(), plane) == vplane_vec.end())
00371          {
00372             vplane_vec.push_back(plane);
00373          }
00374       }
00375       else
00376       {
00377          MSG("FillAlg", Msg::kWarning) << "Unknown planeview " << int(ntpstp -> planeview) << endl;
00378          continue;
00379       }
00380 
00381       if(detector == Detector::kNear)
00382       {
00383          if(ntpstp -> pmtindex0 > 0)
00384          {
00385             MSG("FillAlg", Msg::kWarning) << "In ND East PMT index is positive" << std::endl;
00386             continue;   
00387          }
00388 
00389          if(ntpstp -> planeview == PlaneView::kU)
00390          {
00391             ++data.nustrip;
00392             data.adcu += ntpstp -> ph1.raw; 
00393             data.sigcoru += ntpstp -> ph1.sigcor; 
00394          }
00395          else if(ntpstp -> planeview == PlaneView::kV)
00396          {
00397             ++data.nvstrip;         
00398             data.adcv += ntpstp -> ph1.raw;
00399             data.sigcorv += ntpstp -> ph1.sigcor; 
00400          }
00401 
00402          if(!init_time)
00403          {
00404             data.max_time = ntpstp -> time1;
00405             data.min_time = ntpstp -> time1;
00406             init_time = true;
00407          }
00408          else
00409          {
00410             data.max_time = std::max(ntpstp -> time1, data.max_time);
00411             data.min_time = std::min(ntpstp -> time1, data.min_time);
00412          }
00413       }
00414       else if(detector == Detector::kFar)      
00415       {
00416          if(ntpstp -> planeview == PlaneView::kU)
00417          {
00418             ++data.nustrip;
00419 
00420             if(ntpstp -> pmtindex0 > 0 ||
00421                (ntpstp -> pmtindex0 == 0 && ntpstp -> ph0.raw > 0.0))
00422             {
00423                data.adcu += ntpstp -> ph0.raw;
00424                data.sigcoru += ntpstp -> ph0.sigcor; 
00425             }
00426             
00427             if(ntpstp -> pmtindex1 > 0 ||
00428                (ntpstp -> pmtindex1 == 0 && ntpstp -> ph1.raw > 0.0))
00429             {
00430                data.adcu += ntpstp -> ph1.raw;
00431                data.sigcoru += ntpstp -> ph1.sigcor; 
00432             }
00433          }
00434          else if(ntpstp -> planeview == PlaneView::kV)
00435          {
00436             ++data.nvstrip;
00437 
00438             if(ntpstp -> pmtindex0 > 0 ||
00439                (ntpstp -> pmtindex0 == 0 && ntpstp -> ph0.raw > 0.0))
00440             {
00441                data.adcv += ntpstp -> ph0.raw;
00442                data.sigcorv += ntpstp -> ph0.sigcor; 
00443             }
00444 
00445             if(ntpstp -> pmtindex1 > 0 ||
00446                (ntpstp -> pmtindex1 == 0 && ntpstp -> ph1.raw > 0.0))
00447             {
00448                data.adcv += ntpstp -> ph1.raw;
00449                data.sigcorv += ntpstp -> ph1.sigcor; 
00450             }
00451          }
00452 
00453 
00454          if(ntpstp -> pmtindex0 > 0 ||
00455             (ntpstp -> pmtindex0 == 0 && ntpstp -> ph0.raw > 0.0))
00456          {
00457             if(!init_time)
00458             {
00459                data.max_time = ntpstp -> time0;
00460                data.min_time = ntpstp -> time0;
00461                init_time = true;
00462             }
00463             else
00464             {
00465                data.max_time = std::max(data.max_time, ntpstp -> time0);
00466                data.min_time = std::min(data.min_time, ntpstp -> time0);
00467             }
00468          }
00469          
00470          if(ntpstp -> pmtindex1 > 0 ||
00471             (ntpstp -> pmtindex1 == 0 && ntpstp -> ph1.raw > 0.0))
00472          {
00473             if(!init_time)
00474             {
00475                data.max_time = ntpstp -> time1;
00476                data.min_time = ntpstp -> time1;
00477                init_time = true;
00478             }
00479             else
00480             {
00481                data.max_time = std::max(data.max_time, ntpstp -> time1);
00482                data.min_time = std::min(data.min_time, ntpstp -> time1);
00483             }
00484          }
00485 
00486          if(!init_time)
00487          {
00488             MSG("FillAlg", Msg::kWarning) << "Failed to find initial time" << endl;
00489             continue;
00490          }
00491       }
00492       else
00493       {
00494          MSG("FillAlg", Msg::kWarning) << "Unknown detector type" << std::endl;
00495       }
00496    } 
00497 
00498    data.nuplane = uplane_vec.size();
00499    data.nvplane = vplane_vec.size();
00500 
00501    if(!uplane_vec.empty())
00502    {
00503       data.beg_uplane = *std::min_element(uplane_vec.begin(), uplane_vec.end());
00504       data.end_uplane = *std::max_element(uplane_vec.begin(), uplane_vec.end());
00505    }
00506 
00507    if(!vplane_vec.empty())
00508    {
00509       data.beg_vplane = *std::min_element(vplane_vec.begin(), vplane_vec.end());
00510       data.end_vplane = *std::max_element(vplane_vec.begin(), vplane_vec.end());
00511    }
00512 
00513    if(detector == Detector::kNear)
00514    {
00515       short min_plane = -1, max_plane = -1, count = 0;
00516 
00517       for(vector<short>::const_iterator it = uplane_vec.begin(); it != uplane_vec.end(); ++it)
00518       {
00519          const short plane = *it;
00520          if(plane > 120)
00521          {
00522             break;
00523          }
00524          
00525          ++count;
00526 
00527          if(min_plane < 0 || min_plane > plane)
00528          {
00529             min_plane = plane;
00530          }
00531          if(max_plane < 0 || max_plane < plane)
00532          {
00533             max_plane = plane;
00534          }
00535       }
00536       
00537       for(vector<short>::const_iterator it = vplane_vec.begin(); it != vplane_vec.end(); ++it)
00538       {
00539          const short plane = *it;
00540          if(plane > 120)
00541          {
00542             break;
00543          }
00544          
00545          ++count;        
00546 
00547          if(min_plane < 0 || min_plane > plane)
00548          {
00549             min_plane = plane;
00550          }
00551          if(max_plane < 0 || max_plane < plane)
00552          {
00553             max_plane = plane;
00554          }
00555       }
00556       
00557       if(max_plane - min_plane > 0)
00558       {
00559          data.active_frac = double(count)/double(1 + max_plane - min_plane);
00560       }
00561    }
00562    else
00563    {
00564       short min_plane = data.beg_uplane;
00565       short max_plane = data.end_uplane;
00566 
00567       if(min_plane > data.beg_vplane)
00568       {
00569          min_plane = data.beg_vplane;
00570       }
00571       if(max_plane < data.end_vplane)
00572       {
00573          max_plane = data.end_vplane;
00574       }
00575       
00576       const double nactive = uplane_vec.size() + vplane_vec.size();
00577       if(max_plane - min_plane > 0)
00578       {
00579          data.active_frac = nactive/double(max_plane - min_plane);
00580       }
00581    }
00582 
00583    return data;
00584 }
00585 
00586 //----------------------------------------------------------------------
00587 NtpSRStrip* Anp::GetSRStrip(const NtpStRecord &record, const int index)
00588 {
00589    //
00590    // Return pointer to NtpSRStrip with "index"
00591    //
00592    const TClonesArray *strip_array = record.stp;
00593    if(!strip_array)
00594    {
00595       MSG("FillAlg", Msg::kWarning) << "NtpStRecord does not have valid strip array." << endl; 
00596       return 0;
00597    }
00598 
00599    NtpSRStrip *ntpstp  = dynamic_cast<NtpSRStrip *>(strip_array -> At(index));
00600    if(!ntpstp)
00601    {
00602       MSG("FillAlg", Msg::kWarning) << "Could not find NtpSRStrip at " << index << endl;
00603       return 0;
00604    }      
00605    
00606    return ntpstp;
00607 }
00608 
00609 //----------------------------------------------------------------------
00610 void Anp::PrintStrips(const NtpStRecord &record, const int *index_array, int nstrip)
00611 {
00612    if(nstrip < 1 || !index_array)
00613    {
00614       return;
00615    }
00616 
00617    const TClonesArray *strip_array = record.stp;
00618    if(!strip_array)
00619    {
00620       cerr << "PrintStrips - NtpStRecord does not have valid strip array." << endl; 
00621       return;
00622    }
00623 
00624    const int entries = strip_array -> GetEntries();
00625 
00626    double raw_east = 0.0, raw_west = 0.0;
00627 
00628    cout << "Printing " << nstrip << " strips of NtpSRStrip type" << endl;
00629 
00630    for(int i = 0; i < nstrip; ++i)
00631    {            
00632       const short index = index_array[i];
00633       if(index < 0 || index >= entries)
00634       {
00635          cerr << "PrintStrips - NtpSRStrip index is out of range: " << index << endl;
00636          continue; 
00637       }
00638 
00639       NtpSRStrip *ntpstp = Anp::GetSRStrip(record, index);
00640       if(!ntpstp)
00641       {
00642          continue;
00643       }
00644     
00645       if(index != ntpstp -> index)
00646       {  
00647          cerr << "PrintStrips - mismatched strip and TClonesArray index" << std::endl;
00648          continue;
00649       }
00650       
00651       if(ntpstp -> pmtindex0 >= 0  && ntpstp -> ph0.raw > 0.0)
00652       {
00653          raw_east += ntpstp -> ph0.raw; 
00654       }
00655       if(ntpstp -> pmtindex1 >= 0 && ntpstp -> ph1.raw > 0.0)
00656       {
00657          raw_west += ntpstp -> ph1.raw; 
00658       }
00659 
00660       cout << "----------------------------------------------------------------------------" << endl;
00661       cout << "NtpSRStrip #" << i + 1 << " raw (east, west) = (" 
00662            <<  ntpstp -> ph0.raw << ", " << ntpstp -> ph1.raw << ")" << endl;
00663       ntpstp -> Print();      
00664    }
00665 
00666    cout << "total raw (east, west, total) = (" << raw_east << ", " << raw_west << ", "
00667         << raw_east + raw_west << ")" << endl;
00668 }

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