FairRoot/PandaRoot
anaRadLength.C
Go to the documentation of this file.
1 // root macro to analyze the radiation length of sub-detector components
2 // requires a simulation file with activated RadLenRegister and RadLenPoints
3 // To test different sub-detectors or components of them only modify the marked parts of the anaRadLength() macro
4 
5 struct histos {
6 
7  histos(TString compName, std::function<bool(TString&)> selector, int thetaRes = 181, int phiRes = 361,
8  float thetaStart = -0.5, float thetaEnd = 180.5, float phiStart = -180.5, float phiEnd = 180.5):
9  fComponentName(compName), fSelector(selector), fRadLengthOnTrack(10, 0.0)
10  {
11  TString radLen ="RadiationLength_";
12  radLen+=compName;
13  hisRadLen = new TH1D(radLen.Data(), radLen.Data(), 1000,0,100);
14  hisRadLen->GetXaxis()->SetTitle("Radiation Length [%]");
15 
16  TString radLen2D = "RadiationLength2D_";
17  radLen2D+=compName;
18  hisRadLen2D = new TH2D(radLen2D.Data(), radLen2D.Data(), thetaRes, thetaStart, thetaEnd, phiRes, phiStart, phiEnd);
19  hisRadLen2D->GetXaxis()->SetTitle("Theta [/°]");
20  hisRadLen2D->GetYaxis()->SetTitle("Phi [/°]");
21  TString radLenCount = "RadiationLengthCount_";
22  radLenCount+=compName;
23  hisRadLenCount = new TH2D(radLenCount.Data(), radLenCount.Data(), thetaRes, thetaStart, thetaEnd, phiRes, phiStart, phiEnd);
24 
25  }
26 
27  void Write(){
28  hisRadLen->Write();
29  hisRadLen2D->Write();
30  hisRadLenCount->Write();
31  }
32 
33  std::function<bool(TString&)> fSelector;
35 
36  TH1D* hisRadLen;
37  TH2D* hisRadLen2D;
39 
40  std::vector<double> fRadLengthOnTrack;
41 
42 };
43 
45 {
46 
47  gStyle->SetPalette(1);
48  // ----- Timer --------------------------------------------------------
49  TStopwatch timer;
50  timer.Start();
51  // ------------------------------------------------------------------------
52 
53  std::vector<histos*> histosVector;
54 
55  PndFileNameCreator namecreator("radlength_geantinos"); //Set file prefix here
56  std::string inFile = namecreator.GetSimFileName();
57  std::string parFile = namecreator.GetParFileName();
58  std::string outFile = namecreator.GetCustomFileName("radLenHistos");
59 
60  TFile* out = new TFile(outFile.c_str(),"RECREATE");
61 
62  TFile* f = new TFile(inFile.c_str());
63  TTree *t=(TTree *) f->Get("pndsim") ;
64 
65 
66  TFile* par = new TFile(parFile.c_str());
67  (TGeoManager*)par->Get("FairGeoParSet");
68 
69  // The part below is to be modified by the user
70 
71  int nEvents = 1000000; //Set it to the number of events to be analyzed. If the value is bigger than the number of events in the sim file it is ignored
72  int startEvent = 0;
73  bool verbose = false;
74 
75  //Add your sub detector here: Give an individual name and a selector which returns true if a unique part of the component name is in the geo path of the object.
76  //If the steps for theta and phi are not one degree, the number of steps has to be adopted
77  histosVector.push_back(new histos("stt", [](TString& path){ return path.Contains("stt"); }, 181, 361));
78  histosVector.push_back(new histos("stt_support", [](TString& path){ return (path.Contains("stt") && !path.Contains("tubestt")); }));
79  histosVector.push_back(new histos("stt_straw", [](TString& path){ return (path.Contains("stt") && path.Contains("tubestt")); }));
80  histosVector.push_back(new histos("mvd", [](TString& path){ return (path.Contains("Mvd")); }));
81  histosVector.push_back(new histos("gem", [](TString& path){ return (path.Contains("Gem")); }));
82  //End of the part which should be modified by the user
83 
84 
85  TClonesArray* radlen_array = new TClonesArray("FairRadLenPoint");
86  t->SetBranchAddress("RadLen", &radlen_array);
87 
88  TClonesArray* mc_array=new TClonesArray("PndMCTrack");
89  t->SetBranchAddress("MCTrack",&mc_array);//Branch names
90 
91  TVector3 vecs,veco;
92  std::map<int,int> trackHitMap;
93 
94  for (Int_t j=startEvent; j<(nEvents+startEvent) && j<t->GetEntriesFast(); j++)
95  {
96  t->GetEntry(j);
97  //if (verbose)
98  if (j % 100 == 0)
99  cout<<">>>> Event No "<<j<<endl;
100  for (auto detector : histosVector){
101  detector->fRadLengthOnTrack.clear();
102  detector->fRadLengthOnTrack.resize(10,0.0);
103  }
104 
105  for (Int_t i=0; i<radlen_array->GetEntriesFast(); i++)
106  {
107  if(verbose) cout<<"Point No "<<i<<endl;
108  FairRadLenPoint *point=(FairRadLenPoint*)radlen_array->At(i);
109  if (verbose) cout << "Track ID: " << point->GetTrackID() << std::endl;
110  if (point->GetTrackID() > 10) continue; //larger trackIDs are usually from secondaries one could even think to reduce it to == 0
111  TVector3 pos, posOut, res;
112  pos = point->GetPosition();
113  posOut = point->GetPositionOut();
114  res = posOut - pos;
115  PndMCTrack* myTrack = (PndMCTrack*)(mc_array->At(point->GetTrackID()));
116  if (myTrack == nullptr) {
117  // std::cout << "MyTrack Nullptr" << std::endl;
118  continue;
119  }
120  TGeoNode* node = gGeoManager->FindNode(point->GetX(),point->GetY(),point->GetZ());
121  if( 0==node)
122  {
123  std::cout<<"Warning: There is a node not defined properly!"<<std::endl;
124  continue;
125  }
126  node->cd();
127  TString detname = gGeoManager->GetPath();
128  if (myTrack->GetMotherID() < 0){
129  if (verbose){
130  std::cout << "Time: " << point->GetTime() << " Length: " << point->GetLength() << std::endl;
131  std::cout << "Pos: " << pos.x() << "/" << pos.y() << "/" << pos.z();
132  std::cout << " OutPos: " << posOut.x() << "/" << posOut.y() << "/" << posOut.z() << std::endl;
133  std::cout << "TrackLength: " << res.Mag() << " RadLength: " << point->GetRadLength() << " Res: " << res.Mag() / point->GetRadLength() << std::endl;
134  std::cout << "Detname: " << detname.Data() << std::endl;
135  }
136  for(auto detector : histosVector) {
137  //std::cout << "if Statement " << point->GetTrackID() << " selector " << detector->fSelector(detname) << std::endl;
138  if (point->GetTrackID() > -1 && point->GetTrackID() < 10 && detector->fSelector(detname)){
139  detector->fRadLengthOnTrack[point->GetTrackID()] += res.Mag()/point->GetRadLength()*100;
140  //std::cout << detector->fRadLengthOnTrack[point->GetTrackID()] << std::endl;
141  }
142  }
143  }
144  } // end of radLength Array
145  //for (int k = 0; k < RadLengthOnTrack.size(); k++){
146  for(auto detector : histosVector) {
147  int k = 0;
148  if ( detector->fRadLengthOnTrack.size() > 0 && detector->fRadLengthOnTrack[k] > 0){
149  PndMCTrack* mcTrack = (PndMCTrack*)(mc_array->At(k));
150 
151  detector->hisRadLen->Fill(detector->fRadLengthOnTrack[k]);
152  detector->hisRadLen2D->Fill(mcTrack->GetMomentum().Theta()*TMath::RadToDeg(), mcTrack->GetMomentum().Phi()*TMath::RadToDeg(), detector->fRadLengthOnTrack[k]);
153  detector->hisRadLenCount->Fill(mcTrack->GetMomentum().Theta()*TMath::RadToDeg(), mcTrack->GetMomentum().Phi()*TMath::RadToDeg());
154  }
155  }
156  }
157 
158  for(auto detector : histosVector) {
159  detector->hisRadLen2D->Divide(detector->hisRadLenCount);
160 
161  TCanvas* can1 = new TCanvas();
162  can1->Divide(2,1);
163 
164  gStyle->SetPalette(1);
165  can1->cd(1);
166  detector->hisRadLen->DrawCopy();
167  can1->cd(2);
168  detector->hisRadLen2D->DrawCopy("colz");
169 
170  out->cd();
171  detector->Write();
172  }
173 
174  // ----- Finish -------------------------------------------------------
175  timer.Stop();
176  Double_t rtime = timer.RealTime();
177  Double_t ctime = timer.CpuTime();
178  cout << endl << endl;
179  cout << "Macro finished succesfully." << endl;
180  //cout << "Output file is " << outFile << endl;
181  //cout << "Parameter file is " << parFile << endl;
182  cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
183  cout << endl;
184  // ------------------------------------------------------------------------
185 
186 }
TVector3 pos
std::map< int, int > trackHitMap
std::string GetParFileName(std::string addon="", bool cut=false)
Int_t res
Definition: anadigi.C:166
void Write()
Definition: anaRadLength.C:27
Int_t i
Definition: run_full.C:25
TString outFile
Definition: hit_dirc.C:17
TString fComponentName
Definition: anaRadLength.C:34
#define verbose
Double_t par[3]
TString detname
Definition: anasim.C:61
TVector3 GetMomentum() const
Definition: PndMCTrack.h:78
Int_t startEvent
TH2D * hisRadLenCount
Definition: anaRadLength.C:38
TClonesArray * mc_array
Definition: anaLmdCluster.C:25
cout<< "POINTs for new FwEndCap == "<< tsim-> GetEntriesFast()
TGeoManager * gGeoManager
std::string GetSimFileName(std::string addon="", bool cut=false)
TVector3 vecs
TString inFile
Definition: hit_dirc.C:8
std::function< bool(TString &)> fSelector
Definition: anaRadLength.C:33
A simple class which adds the corresponding file extensions to a given base class.
Double_t
TString parFile
Definition: hit_dirc.C:14
Int_t nEvents
Definition: hit_dirc.C:11
TStopwatch timer
Definition: hit_dirc.C:51
std::string GetCustomFileName(std::string ext, std::string addon="", bool cut=false)
TFile * f
Definition: bump_analys.C:12
TFile * out
Definition: reco_muo.C:20
TVector3 veco
Definition: anaMvdSim.C:33
PndFileNameCreator namecreator("../data/Lars/MvdDtsSim.root")
Double_t ctime
Definition: hit_dirc.C:114
std::vector< double > fRadLengthOnTrack
Definition: anaRadLength.C:40
histos(TString compName, std::function< bool(TString &)> selector, int thetaRes=181, int phiRes=361, float thetaStart=-0.5, float thetaEnd=180.5, float phiStart=-180.5, float phiEnd=180.5)
Definition: anaRadLength.C:7
TH1D * hisRadLen
Definition: anaRadLength.C:36
void anaRadLength()
Definition: anaRadLength.C:44
TTree * t
Definition: bump_analys.C:13
TCanvas * can1
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
Double_t rtime
Definition: hit_dirc.C:113
TClonesArray * radlen_array
TH2D * hisRadLen2D
Definition: anaRadLength.C:37
PndSdsMCPoint * point
Definition: anaLmdCluster.C:72