void gaussFit(pair<int, double> bump, vector<pair<int, double> > shower, TH1D* reso, TH2D* resoPos) { TF2* g2d = new TF2("g2D", Gaus2D, -5, 5, -5, 5, 6); g2d->SetParameters(IDtoX(bump.first), IDtoY(bump.first), 1., 1., 0., (bump.second)); TH2D* histo = new TH2D("histo", "ShowerFit", 11, -5, 5, 11, -5, 5); vector<pair<int, double> >::iterator b; for (b=shower.begin(); b!=shower.end(); b++) { histo->Fill(IDtoX(b->first), IDtoY(b->first), b->second); } // g2d->FixParameter(4, 0.); histo->Fit(g2d, "Q"); //g2d->Draw("cont1 same"); reso->Fill(g2d->GetParameter(5)-gammaEnergy); resoPos->Fill(g2d->GetParameter(0), g2d->GetParameter(1)); }
//============================================================================= // GetStartPosition // is_vertex must be "true" when the vert is muon vertex // is_vertex must be "false" when the vert is for reference //============================================================================= bool HTBlob::GetStartPosition( Minerva::IDBlob *idBlob, Gaudi::XYZPoint vert, bool is_vertex ) const { debug() << " HTBlob::GetStartPosition " << endmsg; TH2D *hU = new TH2D ( "hU", "hU", 480,4510,9990,127,-1075,1075); TH2D *hV = new TH2D ( "hV", "hV", 480,4510,9990,127,-1075,1075); if ( is_vertex ){ hU->Fill( vert.z(), m_mathTool->calcUfromXY(vert.x(), vert.y()), 20 ); hV->Fill( vert.z(), m_mathTool->calcVfromXY(vert.x(), vert.y()), 20); } SmartRefVector<Minerva::IDCluster> idClusters = idBlob->clusters(); SmartRefVector<Minerva::IDCluster>::iterator itClus = idClusters.begin(); Gaudi::XYZPoint pos; double Dx = 0.0; double Dz = 0.0; double distance = 0.0; double vt_x = -9999; double vt_u; double vt_v; double vt_z = -9999; double vtX = -9999; double vtY = -9999; double vtZ = -9999; double dis_min_x = 10000; double xu[2] = {0.0}; double xv[2] = {0.0}; double zu[2] = {0.0}; double zv[2] = {0.0}; // to NC Pi0 events, there is no muon vertex int countu = 0; int countv = 0; // to NC Pi0 events, there is no muon vertex for ( ; itClus != idClusters.end(); itClus++ ){ Dx = (*itClus)->position() - vert.x(); Dz = (*itClus)->z() - vert.z();// to avoid 0 if( (*itClus)->view()== Minerva::IDCluster::X ) { distance = sqrt( pow(Dx,2) + pow(Dz,2) ); if (distance <= dis_min_x ) { dis_min_x = distance; vt_x = (*itClus)->position(); vt_z = (*itClus)->z(); } } if( (*itClus)->view()== Minerva::IDCluster::U ){ debug() << " StartPoint U view, pe " << (*itClus)->pe() << "; z = " << (*itClus)->z() << "; coord " << (*itClus)->position() << endmsg; Dx = (*itClus)->position() - m_mathTool->calcUfromXY(vert.x(),vert.y()); distance = sqrt( pow(Dx,2) + pow(Dz,2) ); if ( is_vertex ) { hU->Fill( (*itClus)->z()-12,(*itClus)->tpos1(), (*itClus)->pe()/distance ); hU->Fill( (*itClus)->z()+12,(*itClus)->tpos2(), (*itClus)->pe()/distance ); } hU->Fill( (*itClus)->z(),(*itClus)->position(), (*itClus)->pe()/distance ); if ( countu < 2 ){ zu[countu] = (*itClus)->z(); xu[countu] = (*itClus)->position(); countu++; } } if( (*itClus)->view()== Minerva::IDCluster::V ){ debug() << " StartPoint V view, pe " << (*itClus)->pe() << "; z = " << (*itClus)->z() << "; coord " << (*itClus)->position() << endmsg; Dx = (*itClus)->position() - m_mathTool->calcVfromXY(vert.x(),vert.y()); distance = sqrt( pow(Dx,2) + pow(Dz,2) ); if ( is_vertex ){ hV->Fill( (*itClus)->z()-12,(*itClus)->tpos1(), (*itClus)->pe()/distance ); hV->Fill( (*itClus)->z()+12,(*itClus)->tpos2(), (*itClus)->pe()/distance ); } hV->Fill( (*itClus)->z(),(*itClus)->position(), (*itClus)->pe()/distance ); if ( countv < 2 ){ zv[countu] = (*itClus)->z(); xv[countu] = (*itClus)->position(); countv++; } } } TF1 *fU, *fV; double slopeu = -9999; double slopev = -9999; double bu = -9999; double bv = -9999; bool goodFit_U = false; bool goodFit_V = false; if ( hU->GetEntries() > 3 ){ hU->Fit("pol1","Q0"); fU = hU->GetFunction("pol1"); bu = fU->GetParameter(0); slopeu = fU->GetParameter(1); goodFit_U = true; delete fU; } else if ( hU->GetEntries() == 2 ){ // to deal with 2 clusters on NCPi0 if ( zu[0] > zu[1] ){ slopeu = (xu[0] - xu[1]) / (zu[0] - zu[1]); bu = xu[1] - zu[1]*slopeu; goodFit_U = true; } else if (zu[0] < zu[1] ) { slopeu = (xu[1] - xu[0]) / (zu[1] - zu[0]); bu = xu[0] - zu[0]*slopeu; goodFit_U = true; } } if ( hV->GetEntries() > 3 ){ hV->Fit("pol1","Q0"); fV = hV->GetFunction("pol1"); bv = fV->GetParameter(0); slopev = fV->GetParameter(1); goodFit_V = true; delete fV; } else if ( hV->GetEntries() == 2 ){ // to deal with 2 clusters on NCPi0 if ( zv[0] > zv[1] ){ slopev = (xv[0] - xv[1]) / (zv[0] - zv[1]); bv = xv[1] - zv[1]*slopeu; goodFit_V = true; } else if (zu[1] < zu[0] ) { /* Trung: why zu instead of zv? */ slopev = (xv[1] - xv[0]) / (zv[1] - zv[0]); bv = xv[0] - zv[0]*slopev; goodFit_V = true; } } vtX = vt_x; vtZ = vt_z; debug() << " Startpoint, slope u " << slopeu << " slope v" << slopev << endmsg; if ( goodFit_U && goodFit_V ){ //3D blobs vt_u = slopeu*vt_z + bu; vt_v = slopev*vt_z + bv; vtY = m_mathTool->calcYfromUV(vt_u,vt_v); } else if ( goodFit_U ) { //2D blobs vt_u = slopeu*vt_z + bu; vtY = (vt_x*.5 - vt_u)*2/sqrt(3); //calcYfromXU? } else if ( goodFit_V ) { //2D blobs vt_v = slopev*vt_z + bv; vtY = (vt_v - vt_x*.5)*2/sqrt(3); //calcYfromXV? } pos.SetX(vtX); pos.SetY(vtY); pos.SetZ(vtZ); idBlob->setStartPoint(pos); debug() << " Setting StarPoint " << pos << " Blob" << idBlob << endmsg; delete hU; delete hV; if (goodFit_U || goodFit_V) return true; return false; }
void clusterGaussReso() { TFile* file = new TFile("complete.root"); TTree* tree = (TTree *)file->Get("Signal"); int nEvents = tree->GetEntries(); double addresses[121] = {}; for (int k=0; k<121; k++){ std::stringstream ss2; ss2 << k; string str = "Crystal_"+ss2.str(); const char* charstr = str.c_str(); tree->SetBranchAddress(charstr, &addresses[k]); } TH1D* energyResoG = new TH1D("energyResoG", "Gauss_Energy_Resolution", 100, -10, 10); TH2D* posResoG = new TH2D("posResoG", "Gauss_Pos_Resolution", 11, -5, 5, 11, -5, 5); double energyThresHi = 50.; //set energy threshold to start looking for bumps double energyThresLo = .5; //energy lower bound //Going through each scan of the calorimeter for (int k=0; k<nEvents; k++) { tree->GetEntry(k); pair<int, double> bump(0, 0.); vector<pair<int, double> > geant; vector<pair<int, double> > hitMap; for(int i=0; i<121; i++) { pair<int, double> hit(i, addresses[i]); geant.push_back(hit); if (addresses[i] > energyThresHi) { hitMap.push_back(hit);} if (hit.second>bump.second) { bump = hit;} } gaussFit(bump, geant, energyResoG, posResoG); } //end of event TCanvas* canvas = new TCanvas("canvas", "canvas", 700,700); canvas->Divide(2, 1); TF1* g1d = new TF1("g1d", "gaus", -10, 10); TF2* g2d2 = new TF2("g2d2", Gaus2D, -10, 10, -10, 10, 6); g2d2->SetParameters(0., 0., 1., 1., 0., 100000); g2d2->FixParameter(4, 0.); energyResoG->Fit(g1d); canvas->cd(1); energyResoG->Draw(); posResoG->Fit(g2d2); canvas->cd(2); posResoG->Draw("BOX"); }
void Eloss() { TString dir = getenv("VMCWORKDIR"); TString fFileNamebase; std::ifstream* ElossData; fFileNamebase = dir+"/macro/Simulation/data/109Pd_Eloss.txt"; ElossData = new std::ifstream(fFileNamebase.Data()); if(ElossData->fail()){ std::cout<<cRED<<" = No file found! Please, check the path. Current :"<<fFileNamebase.Data()<<cNORMAL<<std::endl; } TCanvas *c1 = new TCanvas(); c1->Draw(); c1->Divide(1,2); TH2D *Bragg = new TH2D("Bragg", "Bragg", 1000, 0,500, 1000, 0, 1); TH2D *Interpolate = new TH2D("Interpolate","Interpolate", 1000, 0,200000, 100, 0, 1); Double_t Eloss_elec, Eloss_nuc,SRIM_energy; Double_t x = 0; //microns Double_t x1 = 0; Double_t x0 = 0; Double_t Z = 46; Double_t A = 109; Double_t step = 1; //microns Double_t energy = 190000; //keV Double_t e1, e2, e3, dE1, dE2, dE3; Double_t param0, param1, param2,param3,param4,param5,param6; std::vector<std::tuple<Double_t,Double_t,Double_t>> SRIM_table; std::cout<<cGREEN<<"Extracting data. Please wait...."<<cNORMAL<<std::endl; while (!ElossData->eof()){ //Fill vector with datafile *ElossData>>SRIM_energy>>Eloss_elec>>Eloss_nuc; //keV >> keV/micron >> keV/micron SRIM_table.push_back(std::make_tuple(SRIM_energy, Eloss_elec, Eloss_nuc)); Interpolate->Fill(SRIM_energy, Eloss_elec); }//EndWhile /*for (Int_t n = 0; n < SRIM_table.size(); n++){ std::cout <<' '<< std::get<0> (SRIM_table.at(n))<<' '<<std::get<1> (SRIM_table.at(n))<<' '<<std::get<2> (SRIM_table.at(n))<<std::endl; }//EndFor */ /*Int_t counter = SRIM_table.size()-3; Double_t nuc_Loss, elec_Loss, total_Loss; e1 = std::get<0> (SRIM_table.at(counter)); e2 = std::get<0> (SRIM_table.at(counter-1)); e3 = std::get<0> (SRIM_table.at(counter-2)); dE1 = std::get<1> (SRIM_table.at(counter)); dE2 = std::get<1> (SRIM_table.at(counter-1)); dE3 = std::get<1> (SRIM_table.at(counter-2)); Interpolate->Fill(e1, dE1); Interpolate->Fill(e2, dE2); Interpolate->Fill(e3, dE3); Interpolate->Fit("enFit"); param0 = enFit->GetParameter(0); param1 = enFit->GetParameter(1); while (energy>20){ if(energy>std::get<0> (SRIM_table.at(counter))){ elec_Loss = param0*energy+param1; x+=step; //nuc_Loss = (std::get<2> (SRIM_table.at(counter+1)))*step; //elec_Loss = (std::get<1> (SRIM_table.at(counter+1)))*step; total_Loss = elec_Loss; //+nuc_Loss; energy -= total_Loss; Bragg->Fill(x/1000, total_Loss*10); std::cout<<energy<<" "<<total_Loss<<" "<<x<<std::endl; } else{ counter-=1; Interpolate->Reset(); e1 = std::get<0> (SRIM_table.at(counter)); e2 = std::get<0> (SRIM_table.at(counter-1)); e3 = std::get<0> (SRIM_table.at(counter-2)); dE1 = std::get<1> (SRIM_table.at(counter)); dE2 = std::get<1> (SRIM_table.at(counter-1)); dE3 = std::get<1> (SRIM_table.at(counter-2)); Interpolate->Fill(e1, dE1); Interpolate->Fill(e2, dE2); Interpolate->Fill(e3, dE3); Interpolate->Fit("enFit"); param0 = enFit->GetParameter(0); param1 = enFit->GetParameter(1); } }//EndWhile */ TF1 *enFit = new TF1("enFit", "[0]+[1]*x+[2]*pow(x,2)+[3]*pow(x,3)+[4]*pow(x,4)+[5]*pow(x,5)+[6]*pow(x,6)", 0 , 200000); enFit->SetParameters(1,1,1,1,1,1); Interpolate->Fit("enFit"); param0 = enFit->GetParameter(0); param1 = enFit->GetParameter(1); param2 = enFit->GetParameter(2); param3 = enFit->GetParameter(3); param4 = enFit->GetParameter(4); param5 = enFit->GetParameter(5); param6 = enFit->GetParameter(6); Double_t inter,loss; std::cout<<"Calculating..................."<<std::endl; while(energy>0){ x+=step; inter = param0+ param1*energy+param2*pow(energy,2)+param3*pow(energy,3)+param4*pow(energy,4)+param5*pow(energy,5)+param6*pow(energy,6); loss = inter*step; energy -= loss; Bragg->Fill(x/1000, loss); //mm, keV } std::cout<<cRED<<"Range: "<<x/1000<<" mm"<<cNORMAL<<std::endl; TF1 *brFit = new TF1("brFit", "[0]+[1]*x+[2]*pow(x,2)+[3]*pow(x,3)+[4]*pow(x,4)+[5]*pow(x,5)+[6]*pow(x,6)", 0 , 500); enFit->SetParameters(1,1,1,1,1,1); Bragg->Fit("brFit"); c1->cd(1); Bragg->Draw(); brFit->Draw("Same"); /*c1->cd(2); Interpolate->Draw(); enFit->Draw("same"); */ } //EndMacro