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

CalHit.cxx

Go to the documentation of this file.
00001 
00002 #include <sstream>
00003 #include "RawData/RawDigit.h"
00004 #include "CalHit/CalHit.h"
00005 #include "JobControl/Exception.h"
00006 #include "Geometry/Geometry.h"
00007 #include "Geometry/PlaneGeo.h"
00008 #include "Geometry/CellGeo.h"
00009 
00010 using namespace calhit;
00011 
00012 MODULE_DECL(CalHit);
00013 
00014 CalHit::CalHit(const char* version) : 
00015 jobc::Module("CalHit"),
00016 fInputFolder("."),
00017 fOutputFolder("Hits"),
00018 fDoCalib(1),
00019 fWarnedPE(false),
00020 fWarnedPEAC(false),
00021 fWarnedMIP(false),
00022 fWarnedNS(false),
00023 fWarnedTT0(false),
00024 fWarnedTTrans(false)
00025 {
00026 
00027    this->SetCfgVersion(version);
00028 }
00029 
00030 CalHit::~CalHit()
00031 {
00032 
00033 }
00034 
00035 jobc::Result CalHit::Reco(edm::EventHandle& evt)
00036 {
00037 
00038   // base class Reco method.  
00039 
00040    if (jobc::kFailed==GetData(evt)) {
00041            std::cerr <<"CalHit::GetData failed.  Returning"<<std::endl;
00042            return jobc::kFailed;
00043    }
00044 
00045    if (jobc::kFailed==AddToData(evt)) {
00046            std::cerr <<"CalHit::AddToData failed.  Returning"<<std::endl;
00047            return jobc::kFailed;
00048    }
00049 
00050    if (jobc::kFailed==SaveData(evt)) {
00051            std::cerr <<"CalHit::SaveData failed.  Returning"<<std::endl;
00052            return jobc::kFailed;
00053    }
00054 
00055  
00056    return jobc::kPassed;
00057 
00058 }
00059 
00060 
00061 jobc::Result CalHit::GetData(edm::EventHandle& evt)
00062 {
00063       // clear the local copy
00064       fCellVec.clear();
00065       fCellInput.clear();
00066 
00067        // get the data from the appropriate place
00068       if (DoCalib(recobase::kRAW)) {
00069 
00070          // create cal hits, and copy over raw data
00071              if (Raw(evt)==jobc::kFailed) return jobc::kFailed;
00072 
00073       } else {
00074 
00075          // start with existing cal hits
00076              if (Read(evt)==jobc::kFailed) return jobc::kFailed;
00077 
00078       }
00079 
00080       return jobc::kPassed;
00081 }
00082 
00083 jobc::Result CalHit::Raw(edm::EventHandle& evt)
00084 {
00085     // get raw data
00086     
00087     std::vector<const rawdata::RawDigit*> diglist;
00088     try {
00089        evt.Raw().Get(fInputFolder.c_str(),diglist);
00090     } catch (edm::Exception e) {
00091        std::cerr << "Error retrieving digit list, while looking for raw "<<
00092          "digits in CalHit::Raw()"<<std::endl;
00093        return jobc::kFailed;
00094     }
00095   
00096     // loop over all raw digits, and create basic calibrated hits in local
00097     // record
00098     std::vector<const rawdata::RawDigit*>::iterator iter=diglist.begin();
00099     // get geometry N.S.
00100     short fDetGeom=1;
00101     fGeo = &geo::Geometry::Instance(evt.Header().Run(), fDetGeom);
00102     
00103     while (iter!=diglist.end())
00104     {
00105        const rawdata::RawDigit *rd=*iter;
00106        recobase::CellHit* ch=new recobase::CellHit(rd->Channel(),
00107                                                       rd->ADC(0),rd->TDC(0));
00108         ch->SetCell (rd->GetCell());
00109         ch->SetPlane(rd->GetPlane());
00110         
00111         // N.S 08/25/08
00112         double posit[3];
00113 
00114         fGeo->Plane(rd->GetPlane()).Cell(rd->GetCell()).GetCenter(posit,0.0);
00115         if(fGeo->Plane(rd->GetPlane()).View()==geo::kY) {
00116         ch->SetTCpos(posit[1]);
00117         ch->SetPLVC(2);
00118         }
00119         else{
00120         ch->SetTCpos(posit[0]);
00121         ch->SetPLVC(1);
00122         }
00123         ch->SetZCPos(posit[2]);
00124         // End N.S. 08/25/08
00125                 
00126         fCellVec.push_back(ch);
00127 
00128        // if we'll be doing an attenuation correction, collect
00129        // necessary information here
00130        if (DoCalib(recobase::kPEAC)) AddAttCorr(ch);
00131        iter++;
00132     }
00133     
00134     return jobc::kPassed;
00135 
00136 }
00137   
00138 jobc::Result CalHit::Read(edm::EventHandle& evt)
00139 {
00140     // get existing CalHit's
00141     
00142     try {
00143        evt.Reco().Get(fInputFolder.c_str(),fCellInput);
00144     } catch (edm::Exception e) {
00145        std::cerr << "Error retrieving CellHit list in "<<fInputFolder.c_str()
00146                <<std::endl;
00147        return jobc::kFailed;
00148     }
00149   
00150 
00151   // if we will need attenuation correction, accumulate necessary info
00152   // here
00153     std::vector<const recobase::CellHit*>::iterator iter=fCellInput.begin();
00154     std::cerr<<"Found "<<fCellInput.size()<<" cell hits in input"<<std::endl;
00155     while (iter!=fCellInput.end())
00156     {
00157        const recobase::CellHit* oldch=*iter;
00158        recobase::CellHit* ch=new recobase::CellHit(*oldch);
00159        fCellVec.push_back(ch);
00160        if (DoCalib(recobase::kPEAC)) 
00161        {
00162           AddAttCorr(ch);
00163        }
00164        iter++;
00165     }
00166 
00167     return jobc::kPassed;
00168 }
00169 
00170 jobc::Result CalHit::AddToData(edm::EventHandle& evt)
00171 {
00172 
00173    std::vector<recobase::CellHit*>::iterator iter=fCellVec.begin();
00174    while (iter!=fCellVec.end())
00175    {
00176        recobase::CellHit* ch=*iter;
00177 
00178        // create Photo Electron equivalent ADC
00179        if (DoCalib(recobase::kPE))
00180                 if (ADC2PEHit(ch)==jobc::kFailed) return jobc::kFailed;
00181 
00182        //... with Attenuation correction
00183        if (DoCalib(recobase::kPEAC))
00184                 if (ADC2PEACHit(ch)==jobc::kFailed) return jobc::kFailed;
00185 
00186        //... in MIP units
00187        if (DoCalib(recobase::kMIP))
00188                 if (ADC2MIPHit(ch)==jobc::kFailed) return jobc::kFailed;
00189 
00190        // tdc in ns
00191        if (DoCalib(recobase::kTNS))
00192                 if (TDC2NSHit(ch)==jobc::kFailed) return jobc::kFailed;
00193 
00194        // tdc in ns, with transit time correction
00195        if (DoCalib(recobase::kTTrans))
00196                 if (TDC2TTransHit(ch)==jobc::kFailed) return jobc::kFailed;
00197 
00198        // tdc in ns, with transit time correction, from a T0
00199        if (DoCalib(recobase::kTT0))
00200                 if (TDC2TT0Hit(ch)==jobc::kFailed) return jobc::kFailed;
00201 
00202        iter++;
00203    }
00204 
00205    return jobc::kPassed;
00206 
00207 }
00208 
00209 jobc::Result CalHit::SaveData(edm::EventHandle& evt)
00210 {
00211    if (evt.Reco().GetFolder(fOutputFolder.c_str())) {
00212        std::cerr << "Output Folder reco/"<<fOutputFolder.c_str()<<
00213          " already exists"<<std::endl;
00214        return jobc::kFailed;
00215    }
00216 
00217    TFolder* folder=evt.Reco().MakeFolder(fOutputFolder.c_str());
00218 
00219    unsigned int nsave=evt.Reco().PutVector(fCellVec,fOutputFolder.c_str());
00220    if (nsave!=fCellVec.size())
00221    {
00222        std::cerr <<"CalHit::SaveData: wrong number CellHits saved: "<<
00223           nsave<<" instead of expected "<<fCellVec.size()<<std::endl;
00224        return jobc::kFailed;
00225    }
00226    float pe,peac;
00227    fCellVec[0]->PE(pe);
00228    fCellVec[0]->PEAC(peac);
00229    std::cerr << "saved "<<nsave<<" CellHits.  first pe="<<pe<<", atcor="<<peac<<
00230          std::endl;
00231 
00232    return jobc::kPassed;
00233        
00234 }
00235 
00236 bool CalHit::DoCalib(recobase::CellHit_t level, int set)
00237 {
00238     // if set<0, then caller is asking what calibrations are done
00239     //    set=0, caller wants to turn off a calibration
00240     //    set>0, caller wants to turn on a calibration
00241 
00242      if (set < 0 ) {
00243          return (fDoCalib & (1<<level) );
00244      } else if (set == 0 ) {
00245          fDoCalib = fDoCalib&=(~(1<<level));
00246          return false;
00247      } else  {
00248          fDoCalib = fDoCalib|=(1<<level);
00249          return true;
00250      }
00251      
00252 }
00253 
00254 
00255 void CalHit::AddAttCorr(recobase::CellHit* ch)
00256 {
00257    // add this cell to attenuation correction
00258 
00259    // but a dummy for the time being...
00260 
00261 
00262 }
00263 
00264 jobc::Result CalHit::ADC2PEHit(recobase::CellHit* ch)
00265 {
00266     // Boneheaded base clase
00267     if (!fWarnedPE) {
00268        std::cerr<<"\nWarning!  Using baseclass CalHit::ADC2PEHit. "<<
00269                    " Do not use this for Physics!"<<std::endl;
00270        fWarnedPE=true; 
00271     }  
00272 
00273 
00274     unsigned short adc;
00275     if (!ch->ADC(adc)) {
00276        std::cerr<<"CalHit::ADC2PEHit(): raw adc not set in cell hit for channel "<<
00277                   ch->Channel()<<std::endl;
00278        return jobc::kFailed;
00279     }
00280     ch->SetPE(adc/4.095);// dummy value
00281 
00282     return jobc::kPassed;
00283 
00284 } // ADC2PEHit
00285 
00286 jobc::Result CalHit::ADC2PEACHit(recobase::CellHit* ch)
00287 {
00288     // Boneheaded base clase - represents attenuation-corrected PEs
00289 
00290     if (!fWarnedPEAC) {
00291        std::cerr<<"\nWarning!  Using baseclass CalHit::ADC2PEACHit. "<<
00292                    " Do not use this for Physics!"<<std::endl;
00293        fWarnedPEAC=true; 
00294     }  
00295 
00296 
00297   // require PE number before applying attenuation correction
00298     float pe;
00299     if (!ch->PE(pe)) {
00300            if (!ADC2PEHit(ch)) return jobc::kFailed;
00301            ch->PE(pe);
00302     }
00303     ch->SetPEAC(pe/0.55);
00304 
00305     return jobc::kPassed;
00306 
00307 } // ADC2PEACHit
00308 
00309 jobc::Result CalHit::ADC2MIPHit(recobase::CellHit* ch)
00310 {
00311     // Boneheaded base clase - represents MIP-corrected ph
00312 
00313     if (!fWarnedMIP) {
00314        std::cerr<<"\nWarning!  Using baseclass CalHit::ADC2MIPHit. "<<
00315                    " Do not use this for Physics!"<<std::endl;
00316        fWarnedMIP=true; 
00317     }  
00318 
00319 
00320   // require PE attcor'd number before converting to MIPs
00321     float pe;
00322     if (!ch->PEAC(pe)) {
00323            if (!ADC2PEACHit(ch)) return jobc::kFailed;
00324            ch->PEAC(pe);
00325     }
00326     ch->SetMIP(pe/30.);
00327 
00328     return jobc::kPassed;
00329 
00330 } // ADC2MIPHit
00331 
00332 jobc::Result CalHit::TDC2NSHit(recobase::CellHit* ch)
00333 {
00334     // Boneheaded base clase - represents time in ns
00335     if (!fWarnedNS) {
00336        std::cerr<<"\nWarning!  Using baseclass CalHit::TDC2NSHit. "<<
00337                    " Do not use this for Physics!"<<std::endl;
00338        fWarnedNS=true; 
00339     }  
00340 
00341 
00342     unsigned short tdc;
00343     if (!ch->TDC(tdc)) {
00344        std::cerr<<"CalHit::TDC2NSHit(): raw tdc not set in cell hit  for channel "<<
00345                   ch->Channel()<<std::endl;
00346        return jobc::kFailed;
00347     }
00348     ch->SetTNS(tdc*1.5e3); 
00349 
00350     return jobc::kPassed;
00351 
00352 } // TDC2NSHit
00353 
00354 jobc::Result CalHit::TDC2TT0Hit(recobase::CellHit* ch)
00355 {
00356     // Boneheaded base clase - represents hit times corrected for global event
00357     // t0
00358 
00359     if (!fWarnedTT0) {
00360        std::cerr<<"\nWarning!  Using baseclass CalHit::TDC2TT0Hit. "<<
00361                    " Do not use this for Physics!"<<std::endl;
00362        fWarnedTT0=true; 
00363     }  
00364 
00365 
00366   // require NS number before applying t0
00367     double ns;
00368     if (!ch->TNS(ns)) {
00369            if (!TDC2NSHit(ch)) return jobc::kFailed;
00370            ch->TNS(ns); // should be there now...
00371     }
00372     ch->SetTT0(ns+24.6);
00373 
00374     return jobc::kPassed;
00375 
00376 } // TDC2TT0Hit
00377 
00378 jobc::Result CalHit::TDC2TTransHit(recobase::CellHit* ch)
00379 {
00380     // Boneheaded base clase - represents transit-time corrected hits
00381 
00382     if (!fWarnedTTrans) {
00383        std::cerr<<"\nWarning!  Using baseclass CalHit::TDC2TTransHit. "<<
00384                    " Do not use this for Physics!"<<std::endl;
00385        fWarnedTTrans=true; 
00386     }  
00387 
00388   // require t0-corrected time before handling transit time
00389     double tt0;
00390     if (!ch->TT0(tt0)) {
00391            if (!TDC2TT0Hit(ch)) return jobc::kFailed; // try setting ns
00392            ch->TT0(tt0); // should be there now...
00393     }
00394     ch->SetTTrans(tt0-7.5*5); 
00395 
00396     return jobc::kPassed;
00397 
00398 } // TDC2TTransHit
00399 
00400 void CalHit::Update(const cfg::Config& c)
00401 {
00402   c("Input"). Get(fInputFolder);
00403   c("Output"). Get(fOutputFolder);
00404 
00405 
00406 
00407     if (fInputFolder==fOutputFolder) {
00408        std::cerr<<"CalHit::CalHit() - output folder ("<<fOutputFolder.c_str()<<
00409           ") is not allowed to equal input folder ("<<fInputFolder.c_str()
00410                  <<")"<< std::endl;
00411        assert(0);
00412     }
00413 
00414    std::cerr<<"Set Input Folder ="<<fInputFolder.c_str()<<std::endl;
00415    std::cerr<<"Set Output Folder ="<<fOutputFolder.c_str()<<std::endl;
00416 
00417 
00418   // zero out calibrations
00419   fDoCalib=0;
00420 
00421   std::string calibs;
00422   c("Calibs"). Get(calibs);
00423 
00424   // set calibrations according to the string
00425   std::istringstream icalibs(calibs);
00426   while (icalibs.good())
00427   {
00428      std::string tmp;
00429      icalibs>>tmp;
00430      if (tmp=="RAW") DoCalib(recobase::kRAW,1);
00431      else if (tmp=="PE") DoCalib(recobase::kPE,1);
00432      else if (tmp=="PEAC")  DoCalib(recobase::kPEAC,1);
00433      else if (tmp=="MIP")  DoCalib(recobase::kMIP,1);
00434      else if (tmp=="TNS")  DoCalib(recobase::kTNS,1);
00435      else if (tmp=="TT0")  DoCalib(recobase::kTT0,1);
00436      else if (tmp=="TTRANS")  DoCalib(recobase::kTTrans,1);
00437      else {
00438          std::cerr<<"Unknown calibration requested: "<<tmp.c_str()<<std::endl;
00439          abort();
00440      }
00441 
00442 
00443   } // parse calibration string
00444 
00445   PrintCalibs();
00446   
00447 
00448 }
00449 void CalHit::PrintCalibs()
00450 {
00451    std::cerr<<"Calibrations Requested:"<<std::endl;
00452 
00453    std::cerr<<" Starting from Raw Data: " ;
00454      if (DoCalib(recobase::kRAW)) std::cerr<<" Yes"<<std::endl; else
00455                          std::cerr<<" NO"<<std::endl;
00456 
00457    std::cerr<<" PE Units: ";
00458      if (DoCalib(recobase::kPE)) std::cerr<<" Yes"<<std::endl; else
00459                          std::cerr<<" NO"<<std::endl;
00460 
00461    std::cerr<<" Attenuation Correction (to PEs): ";
00462      if (DoCalib(recobase::kPEAC)) std::cerr<<" Yes"<<std::endl; else
00463                          std::cerr<<" NO"<<std::endl;
00464 
00465    std::cerr<<" MIP Units: ";
00466      if (DoCalib(recobase::kMIP)) std::cerr<<" Yes"<<std::endl; else
00467                          std::cerr<<" NO"<<std::endl;
00468 
00469    std::cerr<<" Time in ns units: " ;
00470      if (DoCalib(recobase::kTNS)) std::cerr<<" Yes"<<std::endl; else
00471                          std::cerr<<" NO"<<std::endl;
00472 
00473    std::cerr<<" T-zero corrected time in ns: " ;
00474      if (DoCalib(recobase::kTT0)) std::cerr<<" Yes"<<std::endl; else
00475                          std::cerr<<" NO"<<std::endl;
00476 
00477    std::cerr<<" Transit time corrected time in ns: " ;
00478      if (DoCalib(recobase::kTTrans)) std::cerr<<" Yes"<<std::endl; else
00479                          std::cerr<<" NO"<<std::endl;
00480 }
00481 
00482 //} // namespace calhit

Generated on Thu Sep 4 02:05:26 2008 for NOvA Offline by doxygen 1.3.5