EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CbmMCMatch.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CbmMCMatch.cxx
1 /*
2  * CbmMCMatch.cxx
3  *
4  * Created on: Nov 23, 2009
5  * Author: stockman
6  */
7 
8 #include "CbmMCMatch.h"
9 
10 #include "TClonesArray.h"
11 //#include <iostream>
12 
14 
16  : TNamed(),
17  fUltimateStage(kMCTrack),
18  fList(),
19  fFinalStageML()
20 {
21  // TODO Auto-generated constructor stub
22 
23 }
24 
26  for (TListIterator iter = fList.begin(); iter != fList.end(); iter++){
27  delete(iter->second);
28  }
29  fList.clear();
30 }
31 
32 void CbmMCMatch::AddElement(DataType sourceType, int index, DataType targetType, int link)
33 {
34  FairLink myPair(targetType, link);
35  AddElement(sourceType, index, myPair);
36 }
37 
38 void CbmMCMatch::AddElement(DataType type, int index, FairLink link){
39  fList[type]->AddLink(link, index);
40 }
41 
42 
43 
44 void CbmMCMatch::SetElements(DataType sourceType, int index, FairMultiLinkedData* links){
45  fList[sourceType]->SetEntry(links, index);
46 }
47 
48 void CbmMCMatch::InitStage(DataType type, std::string fileName, std::string branchName)
49 {
50  if (fList[type] == 0){
51  CbmMCStage* newStage = new CbmMCStage(type, fileName, branchName);
52  fList[type] = newStage;
53  }
54  else{
55  std::cout << "-W- CbmMCMatch::InitStage: Stage " << type << " exists already!" << std::endl;
56  }
57 }
58 
60  fList.erase(type);
61 }
62 
63 
65 {
66  for (int i = 0; i < GetNMCStages(); i++){
67  GetMCStage(i)->SetWeight(weight);
68  }
69 }
70 
72 
73  CbmMCResult result(start, stop);
74  if(!IsTypeInList(start))
75  return result;
76  if (start < stop)
77  return GetMCInfoForward(start, stop);
78  else
79  return GetMCInfoBackward(start, stop);
80 }
81 
83 {
84  CbmMCEntry result;
85  if(!IsTypeInList((DataType)aLink.GetType()))
86  return result;
87  if(!(fList[(DataType)aLink.GetType()]->GetNEntries() > aLink.GetIndex()))
88  return result;
89 
90  if (aLink.GetType() < stop)
91  return GetMCInfoForwardSingle(aLink, stop);
92  else
93  return GetMCInfoBackwardSingle(aLink, stop);
94 }
95 
97 {
98  CbmMCResult result(start, stop);
99  CbmMCStage startVec = *(fList[start]);
100  for (int i = 0; i < startVec.GetNEntries(); i++){
101  FairLink tempLink(startVec.GetStageId(), i);
102 
103 /* FairMultiLinkedData tempStage;
104  tempStage.AddLink(tempLink, true);
105  FindStagesPointingToLinks(tempStage, stop);
106  //FindStagesPointingToLink(tempLink);
107  result.SetEntry(&fFinalStageML, i);
108  fFinalStageML.Reset();
109  */
110  std::cout << "FairLink: " << tempLink << std::endl;
111  CbmMCEntry tempEntry(GetMCInfoForwardSingle(tempLink, stop));
112  tempEntry.SetSource(start);
113  tempEntry.SetPos(i);
114  std::cout << "CbmMCEntry: " << tempEntry << std::endl;
115  result.SetEntry(tempEntry);
116  }
117  return result;
118 
119 }
120 
122 {
123  CbmMCEntry result;
124  ClearFinalStage();
125 
126  FairMultiLinkedData tempStage;
127  tempStage.AddLink(link, true);
128  FindStagesPointingToLinks(tempStage, stop);
129  result.SetLinks(fFinalStageML);
130  return result;
131 
132 }
133 
135 {
136  CbmMCResult result(start, stop);
137  CbmMCStage startVec = *(fList[start]);
138  for (int i = 0; i < startVec.GetNEntries(); i++){
139 // std::cout << "Stage: " << i << std::endl;
140 /* ClearFinalStage();
141  FairMultiLinkedData temp = startVec.GetEntry(i);
142  temp.MultiplyAllWeights(startVec.GetWeight());
143  GetNextStage(temp, stop);
144  //if (result.GetNEntries() > 0)
145 */
146  FairLink tempLink(start, i);
147  GetMCInfoBackwardSingle(tempLink, stop, startVec.GetWeight());
148  result.SetEntry(&fFinalStageML, result.GetNEntries());
149  }
150  return result;
151 }
152 
154 {
155  CbmMCEntry result;
156  FairMultiLinkedData multiLink = fList[(DataType)aLink.GetType()]->GetEntry(aLink.GetIndex());
157 
158  ClearFinalStage();
159  multiLink.MultiplyAllWeights(weight);
160  GetNextStage(multiLink, stop);
161  //if (result.GetNEntries() > 0)
162  result.SetLinks(fFinalStageML);
163 
164  return result;
165 }
166 
167 
169 {
170  FairMultiLinkedData tempLinks;
171  for (int i = 0; i < links.GetNLinks(); i++){
172  FairLink myLink = links.GetLink(i);
173  FairMultiLinkedData myNewLinks = FindStagesPointingToLink(myLink);
174  if (myNewLinks.GetNLinks() == 0)
175  fFinalStageML.AddLink(myLink, true);
176  else{
177  for (int j = 0; j < myNewLinks.GetNLinks(); j++){
178  if (myNewLinks.GetLink(j).GetType() == (Int_t)stop)
179  fFinalStageML.AddLink(myNewLinks.GetLink(j), true);
180  else if (myNewLinks.GetLink(j).GetType() > (Int_t)stop)
181  fFinalStageML.AddLink(myLink, true);
182  else
183  tempLinks.AddLink(myNewLinks.GetLink(j), true);
184  }
185  }
186  }
187  if (tempLinks.GetNLinks() != 0)
188  FindStagesPointingToLinks(tempLinks, stop);
189 }
190 
191 
193 {
194  FairMultiLinkedData result;
195  TListIteratorConst iter = fList.find((DataType)link.GetType());
196  for(;iter!= fList.end(); iter++){
197  if (iter->second->PosInList(link).GetNLinks() > 0){
198  result.AddLinks(iter->second->PosInList(link), true);
199  }
200  }
201  return result;
202 }
203 
204 
206 {
207  FairMultiLinkedData result;
208  for (int i = 0; i < GetNMCStages(); i++){
209  result.AddLinks(GetMCStage(i)->GetLinksWithType(stage), true);
210  }
211  return result;
212 }
213 
214 
215 void CbmMCMatch::CreateArtificialStage(DataType stage, std::string fileName, std::string branchName)
216 {
217  FairMultiLinkedData stageLinks = FindLinksToStage(stage);
218  if (stageLinks.GetNLinks() > 0){
219  InitStage(stage, fileName, branchName);
220  FairMultiLinkedData artData;
221  artData.SetLink(FairLink((Int_t)kUnknown, -1));
222  for (int i = 0; i < stageLinks.GetNLinks(); i++){
223  fList[stage]->SetEntry(&artData, stageLinks.GetLink(i).GetIndex());
224  }
225  }
226 }
227 
228 
229 
231 
232  CbmMCEntry tempStage;
233 
234  for (int i = 0; i < startStage.GetNLinks(); i++){
235  if (startStage.GetLink(i).GetType() == stopStage){
236  AddToFinalStage(startStage.GetLink(i),1);
237  }
238  else if (startStage.GetLink(i).GetType() == fUltimateStage){
239  }
240  else{
241  tempStage = GetEntry(startStage.GetLink(i));
242 // std::cout << "Link ";
243  //startStage.GetLink(i).Print();
244 
245 // for (int j = 0; j < tempStage.size(); j++){
246 // if (tempStage[j].first > stopStage)
247 // AddToFinalStage(startStage[i]);
248 // }
249  if (tempStage.GetNLinks() == 0){
250  AddToFinalStage(startStage.GetLink(i),1);
251  }
252  else{
253  double tempStageWeight = GetMCStageType(static_cast<DataType>(tempStage.GetSource()))->GetWeight();
254  double startLinkWeight = startStage.GetLink(i).GetWeight()/startStage.GetNLinks();
255  //std::cout << " StageWeight: " << tempStageWeight << " startLinkWeight: " << startLinkWeight;
256  tempStage.MultiplyAllWeights(tempStageWeight);
257 
258  if ((tempStageWeight * startLinkWeight) == 0.){
259 // std::cout << " NLinks: " << tempStage.GetNLinks();
260  tempStage.AddAllWeights(startLinkWeight/tempStage.GetNLinks());
261  }
262  else
263  tempStage.MultiplyAllWeights(startLinkWeight*startStage.GetNLinks());
264 
265 
266  }
267 // std::cout << " TempStage: " << tempStage;
268 
269  GetNextStage(tempStage, stopStage);
270  }
271  }
272 }
273 
275 
276  CbmMCEntry empty;
277  if (index < 0) return empty;
278  if (fList[type] == 0)
279  return empty;
280  if (fList[type]->GetNEntries() > index)
281  return fList[type]->GetEntry(index);
282  return empty;
283 }
284 
286  return GetEntry(static_cast<DataType>(link.GetType()), link.GetIndex());
287 }
288 
289 void CbmMCMatch::AddToFinalStage(FairLink hitPair, Float_t mult){
290  fFinalStageML.AddLink(hitPair, true, mult);
291 }
292 
295 }
296 
298 {
299  for (TListIterator iter = fList.begin(); iter != fList.end(); iter++){
300  if (iter->second != 0)
301  iter->second->ClearEntries();
302  else fList.erase(iter);
303  }
304  //fList.clear();
305 }
306 
308  for(TListIterator iter = fList.begin(); iter != fList.end(); iter++){
309  if (iter->first == type)
310  return true;
311  }
312  return false;
313 }
314 
315 void CbmMCMatch::LoadInMCLists(TClonesArray* myLinkArray){
316  for (int i = 0; i < myLinkArray->GetEntriesFast(); i++){
317  CbmMCEntry* myLink = (CbmMCEntry*)myLinkArray->At(i);
318  //std::cout << "myLink.size(): " << myLink->GetNLinks() << ":";
319  if (IsTypeInList((DataType)myLink->GetSource())){
320  //fList[(DataType)myLink->GetSource()]->ClearEntries();
321  fList[(DataType)myLink->GetSource()]->SetEntry(*myLink);
322  fList[(DataType)myLink->GetSource()]->SetLoaded(kTRUE);
323  }
324  }
325 }