CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoTracker/DeDx/plugins/DeDxDiscriminatorLearnerFromCalibTree.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    DeDxDiscriminatorLearnerFromCalibTree
00004 // Class:      DeDxDiscriminatorLearnerFromCalibTree
00005 // 
00013 //
00014 // Original Author:  Loic Quertenmont(querten)
00015 //         Created:  Mon October 20 10:09:02 CEST 2008
00016 //
00017 
00018 
00019 // system include files
00020 #include <memory>
00021 
00022 #include "RecoTracker/DeDx/plugins/DeDxDiscriminatorLearnerFromCalibTree.h"
00023 
00024 //#include "DataFormats/TrackReco/interface/DeDxData.h"
00025 #include "DataFormats/TrackReco/interface/Track.h"
00026 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00027 
00028 #include "FWCore/Framework/interface/ESHandle.h"
00029 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00030 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00031 
00032 using namespace reco;
00033 using namespace std;
00034 using namespace edm;
00035 
00036 DeDxDiscriminatorLearnerFromCalibTree::DeDxDiscriminatorLearnerFromCalibTree(const edm::ParameterSet& iConfig) : ConditionDBWriter<PhysicsTools::Calibration::HistogramD3D>(iConfig)
00037 {
00038 std::cout << "TEST 0 " << endl;
00039 
00040    P_Min               = iConfig.getParameter<double>  ("P_Min"  );
00041    P_Max               = iConfig.getParameter<double>  ("P_Max"  );
00042    P_NBins             = iConfig.getParameter<int>     ("P_NBins");
00043    Path_Min            = iConfig.getParameter<double>  ("Path_Min"  );
00044    Path_Max            = iConfig.getParameter<double>  ("Path_Max"  );
00045    Path_NBins          = iConfig.getParameter<int>     ("Path_NBins");
00046    Charge_Min          = iConfig.getParameter<double>  ("Charge_Min"  );
00047    Charge_Max          = iConfig.getParameter<double>  ("Charge_Max"  );
00048    Charge_NBins        = iConfig.getParameter<int>     ("Charge_NBins");
00049 
00050    MinTrackTMomentum   = iConfig.getUntrackedParameter<double>  ("minTrackMomentum"   ,  5.0);
00051    MaxTrackTMomentum   = iConfig.getUntrackedParameter<double>  ("maxTrackMomentum"   ,  99999.0); 
00052    MinTrackEta         = iConfig.getUntrackedParameter<double>  ("minTrackEta"        , -5.0);
00053    MaxTrackEta         = iConfig.getUntrackedParameter<double>  ("maxTrackEta"        ,  5.0);
00054    MaxNrStrips         = iConfig.getUntrackedParameter<unsigned>("maxNrStrips"        ,  255);
00055    MinTrackHits        = iConfig.getUntrackedParameter<unsigned>("MinTrackHits"       ,  4);
00056 
00057    HistoFile           = iConfig.getUntrackedParameter<string>  ("HistoFile"        ,  "out.root");
00058 
00059    VInputFiles         = iConfig.getParameter<vector<string> >  ("InputFiles");
00060 
00061 std::cout << "TEST 1 " << endl;
00062 
00063 
00064    useCalibration      = iConfig.getUntrackedParameter<bool>("UseCalibration", false);
00065    m_calibrationPath   = iConfig.getUntrackedParameter<string>("calibrationPath");
00066 
00067 std::cout << "TEST 2 " << endl;
00068 
00069 }
00070 
00071 
00072 DeDxDiscriminatorLearnerFromCalibTree::~DeDxDiscriminatorLearnerFromCalibTree(){
00073 
00074 std::cout << "TEST Z " << endl;
00075 }
00076 
00077 // ------------ method called once each job just before starting event loop  ------------
00078 
00079 void  DeDxDiscriminatorLearnerFromCalibTree::algoBeginJob(const edm::EventSetup& iSetup)
00080 {
00081 std::cout << "TEST 3 " << endl;
00082 
00083 //   Charge_Vs_Path = new TH2F ("Charge_Vs_Path"     , "Charge_Vs_Path" , 24, 0.2, 1.4, 250, 0, 5000);
00084    Charge_Vs_Path = new TH3F ("Charge_Vs_Path"     , "Charge_Vs_Path" , P_NBins, P_Min, P_Max, Path_NBins, Path_Min, Path_Max, Charge_NBins, Charge_Min, Charge_Max);
00085 
00086 std::cout << "TEST A " << endl;
00087 
00088    edm::ESHandle<TrackerGeometry> tkGeom;
00089    iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
00090    m_tracker = tkGeom.product();
00091 
00092    vector<GeomDet*> Det = tkGeom->dets();
00093    for(unsigned int i=0;i<Det.size();i++){
00094       DetId  Detid  = Det[i]->geographicalId();
00095       int    SubDet = Detid.subdetId();
00096 
00097       if( SubDet == StripSubdetector::TIB ||  SubDet == StripSubdetector::TID ||
00098           SubDet == StripSubdetector::TOB ||  SubDet == StripSubdetector::TEC  ){
00099 
00100           StripGeomDetUnit* DetUnit     = dynamic_cast<StripGeomDetUnit*> (Det[i]);
00101           if(!DetUnit)continue;
00102 
00103           const StripTopology& Topo     = DetUnit->specificTopology();
00104           unsigned int         NAPV     = Topo.nstrips()/128;
00105 
00106           double Eta           = DetUnit->position().basicVector().eta();
00107           double R             = DetUnit->position().basicVector().transverse();
00108           double Thick         = DetUnit->surface().bounds().thickness();
00109           for(unsigned int j=0;j<NAPV;j++){
00110 
00111              stAPVInfo* APV       = new stAPVInfo;
00112              APV->DetId           = Detid.rawId();
00113              APV->SubDet          = SubDet;
00114              APV->Eta             = Eta;
00115              APV->R               = R;
00116              APV->Thickness       = Thick;
00117              APV->APVId           = j;
00118              APVsColl[APV->DetId<<3 | APV->APVId] = APV;
00119          }
00120       }
00121    }
00122 
00123 std::cout << "TEST B " << endl;
00124 
00125 
00126    MakeCalibrationMap();
00127 std::cout << "TEST C " << endl;
00128 
00129 
00130    algoAnalyzeTheTree();
00131 std::cout << "TEST D " << endl;
00132 
00133 }
00134 
00135 // ------------ method called once each job just after ending the event loop  ------------
00136 
00137 
00138 void DeDxDiscriminatorLearnerFromCalibTree::algoEndJob()
00139 {
00140         TFile* Output = new TFile(HistoFile.c_str(), "RECREATE");
00141         Charge_Vs_Path->Write();
00142         Output->Write();
00143         Output->Close();
00144         TFile* Input = new TFile(HistoFile.c_str() );
00145         Charge_Vs_Path = (TH3F*)(Input->FindObjectAny("Charge_Vs_Path"))->Clone();  
00146         Input->Close();
00147 }
00148 
00149 void DeDxDiscriminatorLearnerFromCalibTree::algoAnalyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00150 {
00151 }
00152 
00153 
00154 void DeDxDiscriminatorLearnerFromCalibTree::algoAnalyzeTheTree()
00155 {
00156    unsigned int NEvent = 0;
00157    for(unsigned int i=0;i<VInputFiles.size();i++){
00158       printf("Openning file %3i/%3i --> %s\n",i+1, (int)VInputFiles.size(), (char*)(VInputFiles[i].c_str())); fflush(stdout);
00159       TChain* tree = new TChain("gainCalibrationTree/tree");
00160       tree->Add(VInputFiles[i].c_str());
00161 
00162       TString EventPrefix("");
00163       TString EventSuffix("");
00164 
00165       TString TrackPrefix("track");
00166       TString TrackSuffix("");
00167 
00168       TString CalibPrefix("GainCalibration");
00169       TString CalibSuffix("");
00170 
00171       unsigned int                 eventnumber    = 0;    tree->SetBranchAddress(EventPrefix + "event"          + EventSuffix, &eventnumber   , NULL);
00172       unsigned int                 runnumber      = 0;    tree->SetBranchAddress(EventPrefix + "run"            + EventSuffix, &runnumber     , NULL);
00173       std::vector<bool>*           TrigTech       = 0;    tree->SetBranchAddress(EventPrefix + "TrigTech"       + EventSuffix, &TrigTech   , NULL);
00174 
00175       std::vector<double>*         trackchi2ndof  = 0;    tree->SetBranchAddress(TrackPrefix + "chi2ndof"       + TrackSuffix, &trackchi2ndof , NULL);
00176       std::vector<float>*          trackp         = 0;    tree->SetBranchAddress(TrackPrefix + "momentum"       + TrackSuffix, &trackp        , NULL);
00177       std::vector<float>*          trackpt        = 0;    tree->SetBranchAddress(TrackPrefix + "pt"             + TrackSuffix, &trackpt       , NULL);
00178       std::vector<double>*         tracketa       = 0;    tree->SetBranchAddress(TrackPrefix + "eta"            + TrackSuffix, &tracketa      , NULL);
00179       std::vector<double>*         trackphi       = 0;    tree->SetBranchAddress(TrackPrefix + "phi"            + TrackSuffix, &trackphi      , NULL);
00180       std::vector<unsigned int>*   trackhitsvalid = 0;    tree->SetBranchAddress(TrackPrefix + "hitsvalid"      + TrackSuffix, &trackhitsvalid, NULL);
00181 
00182       std::vector<int>*            trackindex     = 0;    tree->SetBranchAddress(CalibPrefix + "trackindex"     + CalibSuffix, &trackindex    , NULL);
00183       std::vector<unsigned int>*   rawid          = 0;    tree->SetBranchAddress(CalibPrefix + "rawid"          + CalibSuffix, &rawid         , NULL);
00184       std::vector<unsigned short>* firststrip     = 0;    tree->SetBranchAddress(CalibPrefix + "firststrip"     + CalibSuffix, &firststrip    , NULL);
00185       std::vector<unsigned short>* nstrips        = 0;    tree->SetBranchAddress(CalibPrefix + "nstrips"        + CalibSuffix, &nstrips       , NULL);
00186       std::vector<unsigned int>*   charge         = 0;    tree->SetBranchAddress(CalibPrefix + "charge"         + CalibSuffix, &charge        , NULL);
00187       std::vector<float>*          path           = 0;    tree->SetBranchAddress(CalibPrefix + "path"           + CalibSuffix, &path          , NULL);
00188       std::vector<unsigned char>*  amplitude      = 0;    tree->SetBranchAddress(CalibPrefix + "amplitude"      + CalibSuffix, &amplitude     , NULL);
00189       std::vector<double>*         gainused       = 0;    tree->SetBranchAddress(CalibPrefix + "gainused"       + CalibSuffix, &gainused      , NULL);
00190 
00191       printf("Number of Events = %i + %i = %i\n",NEvent,(unsigned int)tree->GetEntries(),(unsigned int)(NEvent+tree->GetEntries()));NEvent+=tree->GetEntries();
00192       printf("Progressing Bar              :0%%       20%%       40%%       60%%       80%%       100%%\n");
00193       printf("Looping on the Tree          :");
00194       int TreeStep = tree->GetEntries()/50;if(TreeStep<=1)TreeStep=1;
00195       for (unsigned int ientry = 0; ientry < tree->GetEntries(); ientry++) {
00196       if(ientry%TreeStep==0){printf(".");fflush(stdout);}
00197          tree->GetEntry(ientry);
00198 
00199          int FirstAmplitude = 0;
00200          for(unsigned int c=0;c<(*path).size();c++){
00201             FirstAmplitude+=(*nstrips)[c];
00202             int t = (*trackindex)[c];
00203             if((*trackpt)[t]<5)continue;
00204             if((*trackhitsvalid)[t]<5)continue;
00205 
00206             int Charge = 0; 
00207             if(useCalibration){
00208                stAPVInfo* APV = APVsColl[((*rawid)[c]<<3) | (unsigned int)((*firststrip)[c]/128)];
00209                for(unsigned int s=0;s<(*nstrips)[c];s++){
00210                  int StripCharge =  (*amplitude)[FirstAmplitude-(*nstrips)[c]+s];
00211                  if(StripCharge<254){
00212                     StripCharge=(int)(StripCharge/APV->CalibGain);
00213                     if(StripCharge>=1024){
00214                        StripCharge = 255;
00215                     }else if(StripCharge>=254){
00216                        StripCharge = 254;
00217                     }
00218                  }
00219                  Charge += StripCharge;
00220                }
00221             }else{
00222                Charge = (*charge)[c];
00223             }
00224 
00225 //          printf("ChargeDifference = %i Vs %i with Gain = %f\n",(*charge)[c],Charge,Gains[(*rawid)[c]]);
00226             double ClusterChargeOverPath   =  ( (double) Charge )/(*path)[c] ;       
00227             Charge_Vs_Path->Fill((*trackp)[t],(*path)[c],ClusterChargeOverPath);
00228          }
00229       }printf("\n");
00230   }
00231 }
00232 
00233 
00234 PhysicsTools::Calibration::HistogramD3D* DeDxDiscriminatorLearnerFromCalibTree::getNewObject()
00235 {
00236 std::cout << "TEST X " << endl;
00237 
00238 
00239 //   if( strcmp(algoMode.c_str(),"MultiJob")==0)return NULL;
00240 
00241    PhysicsTools::Calibration::HistogramD3D* obj;
00242    obj = new PhysicsTools::Calibration::HistogramD3D(
00243                 Charge_Vs_Path->GetNbinsX(), Charge_Vs_Path->GetXaxis()->GetXmin(),  Charge_Vs_Path->GetXaxis()->GetXmax(),
00244                 Charge_Vs_Path->GetNbinsY(), Charge_Vs_Path->GetYaxis()->GetXmin(),  Charge_Vs_Path->GetYaxis()->GetXmax(),
00245                 Charge_Vs_Path->GetNbinsZ(), Charge_Vs_Path->GetZaxis()->GetXmin(),  Charge_Vs_Path->GetZaxis()->GetXmax());
00246 
00247 std::cout << "TEST Y " << endl;
00248 
00249 
00250    for(int ix=0; ix<=Charge_Vs_Path->GetNbinsX()+1; ix++){
00251       for(int iy=0; iy<=Charge_Vs_Path->GetNbinsY()+1; iy++){
00252          for(int iz=0; iz<=Charge_Vs_Path->GetNbinsZ()+1; iz++){
00253             obj->setBinContent(ix, iy, iz, Charge_Vs_Path->GetBinContent(ix,iy, iz) );       
00254 //          if(Charge_Vs_Path->GetBinContent(ix,iy)!=0)printf("%i %i %i --> %f\n",ix,iy, iz, Charge_Vs_Path->GetBinContent(ix,iy,iz)); 
00255          }
00256       }
00257    }
00258 
00259 std::cout << "TEST W " << endl;
00260 
00261    return obj;
00262 }
00263 
00264 
00265 
00266 void DeDxDiscriminatorLearnerFromCalibTree::MakeCalibrationMap(){
00267    if(!useCalibration)return;
00268 
00269   
00270    TChain* t1 = new TChain("SiStripCalib/APVGain");
00271    t1->Add(m_calibrationPath.c_str());
00272 
00273    unsigned int  tree_DetId;
00274    unsigned char tree_APVId;
00275    double        tree_Gain;
00276 
00277    t1->SetBranchAddress("DetId"             ,&tree_DetId      );
00278    t1->SetBranchAddress("APVId"             ,&tree_APVId      );
00279    t1->SetBranchAddress("Gain"              ,&tree_Gain       );
00280 
00281    for (unsigned int ientry = 0; ientry < t1->GetEntries(); ientry++) {
00282       t1->GetEntry(ientry);
00283        stAPVInfo* APV = APVsColl[(tree_DetId<<3) | (unsigned int)tree_APVId];
00284        APV->CalibGain = tree_Gain;
00285    }
00286 
00287 }
00288 
00289 
00290 //define this as a plug-in
00291 DEFINE_FWK_MODULE(DeDxDiscriminatorLearnerFromCalibTree);