get_dca.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include <Rtypes.h>
  2. #include <TChain.h>
  3. #include <TClonesArray.h>
  4. #include <TStopwatch.h>
  5. #include <TROOT.h>
  6. #include <TSystem.h>
  7. #include <TMath.h>
  8. // #include <TTree.h>
  9. #include <TFile.h>
  10. #include <TH1.h>
  11. #include <TF1.h>
  12. #include <FairMCEventHeader.h>
  13. #include <FairMCTrack.h>
  14. #include <MpdEvent.h>
  15. #include <MpdKalmanTrack.h>
  16. #include <MpdVertex.h>
  17. #include <MpdTrack.h>
  18. #include <iostream>
  19. #include "get_fit.c"
  20. #include "MakeFitDCA.c"
  21. using namespace std;
  22. int GetPtBin(Float_t pt);
  23. int GetEtaBin(Float_t eta);
  24. void get_fit(TString inFileName, TString outFileName);
  25. // const float ptBins[] = {0.,0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.5, 3.};
  26. // const int NptBins = 12;
  27. // const float etaBins[] = {-1.5,-1.2,-1.,-0.8,-0.6,-0.4,-0.2,0.,0.2,0.4,0.6,0.8,1.,1.2,1.5};
  28. // const int NetaBins = 14;
  29. // const int Ndim = 3;
  30. const Double_t F_CUR0 = 0.3 * 0.01 * 5 / 10; // 5kG
  31. MpdHelix MakeHelix(const MpdKalmanTrack *tr)
  32. {
  33. Double_t r = tr->GetPosNew();
  34. Double_t phi = tr->GetParam(0) / r;
  35. Double_t x = r * TMath::Cos(phi);
  36. Double_t y = r * TMath::Sin(phi);
  37. Double_t dip = tr->GetParam(3);
  38. Double_t cur = F_CUR0 * TMath::Abs (tr->GetParam(4));
  39. TVector3 o(x, y, tr->GetParam(1));
  40. Int_t h = (Int_t) TMath::Sign(1.1,tr->GetParam(4));
  41. MpdHelix helix(cur, dip, tr->GetParam(2)-TMath::PiOver2()*h, o, h);
  42. return helix;
  43. }
  44. void get_dca(TString inFileName , TString outFileName)
  45. {
  46. if (inFileName.IsNull() || outFileName.IsNull())
  47. {
  48. std::cout << "Provide an input/output information!" << std::endl;
  49. return;
  50. }
  51. TStopwatch timer;
  52. timer.Start();
  53. // Init output histograms
  54. TH1F* h_dca[Ndim][NptBins][NetaBins];
  55. char name[200];
  56. char title[200];
  57. for (int dim = 0; dim < Ndim; ++dim)
  58. {
  59. for (int ptbin = 0; ptbin < NptBins; ++ptbin)
  60. {
  61. for (int etabin = 0; etabin < NetaBins; ++etabin)
  62. {
  63. sprintf(name,"h_dca_%i_%i_%i",dim,ptbin,etabin);
  64. sprintf(title,"DCA distribution for %.2f < p_{t} < %.2f and %.2f < #eta < %.2f", ptBins[ptbin], ptBins[ptbin+1],etaBins[etabin],etaBins[etabin+1]);
  65. h_dca[dim][ptbin][etabin] = new TH1F(name,title,4000,-50.,50);
  66. }
  67. }
  68. }
  69. TChain *dstTree = new TChain("cbmsim");
  70. dstTree->Add(inFileName.Data());
  71. TFile *outFile = new TFile(outFileName.Data(),"RECREATE");
  72. MpdEvent *MPDEvent = nullptr;
  73. dstTree->SetBranchAddress("MPDEvent.", &MPDEvent);
  74. // TClonesArray *MpdGlobalTracks = nullptr;
  75. // TClonesArray *mpdKalmanTracks = nullptr; //(TClonesArray*) inFile->FindObjectAny("TpcKalmanTrack");
  76. // dstTree->SetBranchAddress("TpcKalmanTrack", &mpdKalmanTracks);
  77. TClonesArray *vertexes = nullptr; //(TClonesArray*) inFile->FindObjectAny("Vertex");
  78. dstTree->SetBranchAddress("Vertex", &vertexes);
  79. Int_t n_events = dstTree->GetEntries();
  80. //Int_t n_events = 50;
  81. for (int event = 0; event < n_events; ++event)
  82. {
  83. cout << "EVENT N "<< event <<endl;
  84. dstTree->GetEntry(event);
  85. // MpdGlobalTracks = MPDEvent->GetGlobalTracks();
  86. Int_t Ntracks = MPDEvent->GetGlobalTracks()->GetEntriesFast();
  87. // MpdVertex *vertex = (MpdVertex*) vertexes->First();
  88. // TVector3 primaryVertex;
  89. // vertex->Position(primaryVertex);
  90. for (int track = 0; track < Ntracks; ++track)
  91. {
  92. // MpdKalmanTrack *kalmanTrack = (MpdKalmanTrack*) mpdKalmanTracks->UncheckedAt(track);
  93. // DCA stuff
  94. // MpdHelix helix = MakeHelix(kalmanTrack);
  95. // Double_t pathLength = helix.pathLength(primaryVertex);
  96. // TVector3 pca;
  97. // pca = helix.at(pathLength);
  98. // pca -= primaryVertex;
  99. // Double_t dcaX = pca.X();
  100. // Double_t dcaY = pca.Y();
  101. // Double_t dcaZ = pca.Z();
  102. MpdTrack *mpdtrack = (MpdTrack*) MPDEvent->GetGlobalTracks()->UncheckedAt(track);
  103. Int_t pt_bin = GetPtBin(TMath::Abs(mpdtrack->GetPt()));
  104. Int_t eta_bin = GetEtaBin(mpdtrack->GetEta());
  105. if ( (eta_bin == -1) || (pt_bin == -1 )) continue;
  106. h_dca[0][pt_bin][eta_bin]->Fill(mpdtrack->GetDCAX());
  107. h_dca[1][pt_bin][eta_bin]->Fill(mpdtrack->GetDCAY());
  108. h_dca[2][pt_bin][eta_bin]->Fill(mpdtrack->GetDCAZ());
  109. }
  110. }
  111. outFile->cd();
  112. for (int dim = 0; dim < Ndim; ++dim)
  113. {
  114. for (int ptbin = 0; ptbin < NptBins; ++ptbin)
  115. {
  116. for (int etabin = 0; etabin < NetaBins; ++etabin)
  117. {
  118. std::cout << h_dca[dim][ptbin][etabin]->GetName() << " ("
  119. << h_dca[dim][ptbin][etabin]->GetEntries() << " entries)"
  120. << std::endl;
  121. h_dca[dim][ptbin][etabin]->Write();
  122. }
  123. }
  124. }
  125. std::cout << "Closing output file." << std::endl;
  126. outFile->Close();
  127. }
  128. int GetPtBin(Float_t pt)
  129. {
  130. int pt_bin = -1;
  131. for (int i = 0; i < NptBins; ++i)
  132. if ((pt > ptBins[i]) && (pt <= ptBins[i + 1]))
  133. pt_bin = i;
  134. return pt_bin;
  135. }
  136. int GetEtaBin(Float_t eta)
  137. {
  138. int eta_bin = -1;
  139. for (int i = 0; i < NetaBins; ++i)
  140. if ((eta > etaBins[i]) && (eta <= etaBins[i + 1]))
  141. eta_bin = i;
  142. return eta_bin;
  143. }
  144. int main(int argc, char** argv)
  145. {
  146. TString iFileName, oFileName;
  147. Bool_t isFirstFit, isSecondFit;
  148. isFirstFit = kFALSE; isSecondFit = kFALSE;
  149. if (argc < 3){
  150. std::cerr << "./get_dca -i inputfile -o outputfile [--firstfit --secondfit]" << std::endl;
  151. return 1;
  152. }
  153. for (int i=1; i<argc; i++){
  154. if(std::string(argv[i]) != "-i" &&
  155. std::string(argv[i]) != "-o" &&
  156. std::string(argv[i]) != "--firstfit" &&
  157. std::string(argv[i]) != "--secondfit"){
  158. std::cerr << "\n[ERROR]: Unknown parameter " << i << ": " << argv[i] << std::endl;
  159. return 1;
  160. } else {
  161. if(std::string(argv[i]) == "-i" && i!=argc-1) {
  162. iFileName = argv[++i];
  163. continue;
  164. }
  165. if(std::string(argv[i]) == "-i" && i==argc-1) {
  166. std::cerr << "\n[ERROR]: Input file name was not specified " << std::endl;
  167. return 1;
  168. }
  169. if(std::string(argv[i]) == "-o" && i!=argc-1) {
  170. oFileName = argv[++i];
  171. continue;
  172. }
  173. if(std::string(argv[i]) == "-o" && i==argc-1){
  174. std::cerr << "\n[ERROR]: Output file name was not specified " << std::endl;
  175. return 1;
  176. }
  177. if(std::string(argv[i]) == "--firstfit") {
  178. isFirstFit = kTRUE;
  179. continue;
  180. }
  181. if(std::string(argv[i]) == "--secondfit"){
  182. isSecondFit = kTRUE;
  183. continue;
  184. }
  185. }
  186. }
  187. if (isFirstFit == kTRUE && isSecondFit == kTRUE){
  188. std::cerr << "\n[ERROR]: Both --firstfit and --secondfit cannot be used at the same time " << std::endl;
  189. return 1;
  190. }
  191. if (isFirstFit == kFALSE && isSecondFit == kFALSE) get_dca(iFileName,oFileName);
  192. if (isFirstFit) get_fit(iFileName,oFileName);
  193. if (isSecondFit) MakeFitDCA(iFileName, oFileName);
  194. return 0;
  195. }