FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMCMatch.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 /*
9  * FairMCMatch.cxx
10  *
11  * Created on: Nov 23, 2009
12  * Author: stockman
13  */
14 
15 #include "FairMCMatch.h"
16 
17 #include "FairLink.h" // for FairLink
18 #include "FairRootManager.h" // for FairRootManager
19 
20 #include <TClonesArray.h> // for TClonesArray
21 #include <iostream> // for operator<<, basic_ostream, etc
22 
24 
26  : TNamed()
27  , fUltimateStage(0)
28  , fList()
29  , fFinalStageML()
30  , fVerbose(0)
31 {
32  fFinalStageML.SetPersistanceCheck(kFALSE);
33 }
34 
36 {
37  for (TListIterator iter = fList.begin(); iter != fList.end(); ++iter) {
38  delete (iter->second);
39  }
40  fList.clear();
41 }
42 
43 void FairMCMatch::AddElement(Int_t sourceType, int index, Int_t targetType, int link)
44 {
45  FairLink myPair(targetType, link);
46  AddElement(sourceType, index, myPair);
47 }
48 
49 void FairMCMatch::AddElement(Int_t type, int index, FairLink link) { fList[type]->AddLink(link, index); }
50 
51 void FairMCMatch::SetElements(Int_t sourceType, int index, FairMultiLinkedData* links)
52 {
53  fList[sourceType]->SetEntry(links, index);
54 }
55 
56 void FairMCMatch::InitStage(Int_t type, const std::string& fileName, const std::string& branchName)
57 {
58  if (fList[type] == 0) {
59  FairMCStage* newStage = new FairMCStage(type, fileName, branchName);
60  fList[type] = newStage;
61  if (fVerbose > 1) {
62  std::cout << "InitStages: " << *newStage;
63  }
64  } else {
65  std::cout << "-W- FairMCMatch::InitStage: Stage " << type << " exists already!" << std::endl;
66  }
67 }
68 
69 void FairMCMatch::InitStage(const std::string& branchName, const std::string& fileName)
70 {
72  if (ioman->CheckBranch(branchName.c_str()) == 1) {
73  Int_t type = ioman->GetBranchId(branchName.c_str());
74  if (type > -1) {
75  InitStage(type, fileName, branchName);
76  } else {
77  std::cout << "-W- FairMCMatch::InitStage: Branch name " << branchName << " not registered!" << std::endl;
78  }
79  } else {
80  std::cout << "-W- FairMCMatch::InitStage: Branch name " << branchName << " not persistant!" << std::endl;
81  }
82 }
83 
84 void FairMCMatch::RemoveStage(Int_t type) { fList.erase(type); }
85 
87 {
88  for (int i = 0; i < GetNMCStages(); ++i) {
89  if (GetMCStage(i)) {
90  GetMCStage(i)->SetWeight(weight);
91  }
92  }
93 }
94 
95 FairMCResult FairMCMatch::GetMCInfo(TString start, TString stop)
96 {
98  return GetMCInfo(ioman->GetBranchId(start), ioman->GetBranchId(stop));
99 }
100 
101 FairMCResult FairMCMatch::GetMCInfo(Int_t start, Int_t stop)
102 {
103  FairMCResult result(start, stop);
104  if (!IsTypeInList(start)) {
105  return result;
106  }
107  if (start < stop) {
108  return GetMCInfoForward(start, stop);
109  } else {
110  return GetMCInfoBackward(start, stop);
111  }
112 }
113 
115 {
117  return GetMCInfoSingle(aLink, ioman->GetBranchId(stop));
118 }
119 
121 {
122  FairMCEntry result;
123  if (!IsTypeInList(static_cast<Int_t>(aLink.GetType()))) {
124  return result;
125  }
126  if (!(fList[static_cast<Int_t>(aLink.GetType())]->GetNEntries() > aLink.GetIndex())) {
127  return result;
128  }
129 
130  if (aLink.GetType() < stop) {
131  return GetMCInfoForwardSingle(aLink, stop);
132  } else {
133  return GetMCInfoBackwardSingle(aLink, stop);
134  }
135 }
136 
137 FairMCResult FairMCMatch::GetMCInfoForward(Int_t start, Int_t stop)
138 {
139  FairMCResult result(start, stop);
140  FairMCStage startVec = *(fList[start]);
141  for (int i = 0; i < startVec.GetNEntries(); ++i) {
142  FairLink tempLink(startVec.GetStageId(), i);
143 
144  FairMCEntry tempEntry(GetMCInfoForwardSingle(tempLink, stop));
145  if (tempEntry.GetNLinks() > 0)
146  if (tempEntry.GetLink(0).GetType() == start) {
147  tempEntry.DeleteLink(tempEntry.GetLink(0));
148  }
149  tempEntry.SetSource(start);
150  tempEntry.SetPos(i);
151  result.SetEntry(tempEntry);
152  }
153  return result;
154 }
155 
156 FairMCEntry FairMCMatch::GetMCInfoForwardSingle(FairLink link, Int_t stop)
157 {
158  FairMCEntry result;
159  ClearFinalStage();
160 
161  FairMultiLinkedData tempStage;
162  tempStage.SetPersistanceCheck(kFALSE);
163  tempStage.AddLink(link, true);
164  FindStagesPointingToLinks(tempStage, stop);
165  result.SetLinks(fFinalStageML);
166  return result;
167 }
168 
169 FairMCResult FairMCMatch::GetMCInfoBackward(Int_t start, Int_t stop)
170 {
171  FairMCResult result(start, stop);
172  FairMCStage startVec = *(fList[start]);
173  for (int i = 0; i < startVec.GetNEntries(); ++i) {
174  FairLink tempLink(start, i);
175  GetMCInfoBackwardSingle(tempLink, stop, startVec.GetWeight());
176  result.SetEntry(&fFinalStageML, result.GetNEntries());
177  }
178  return result;
179 }
180 
181 FairMCEntry FairMCMatch::GetMCInfoBackwardSingle(FairLink aLink, Int_t stop, Double_t weight)
182 {
183  FairMCEntry result;
184  FairMultiLinkedData multiLink = fList[static_cast<Int_t>(aLink.GetType())]->GetEntry(aLink.GetIndex());
185 
186  ClearFinalStage();
187  multiLink.MultiplyAllWeights(weight);
188  GetNextStage(multiLink, stop);
189  result.SetLinks(fFinalStageML);
190 
191  return result;
192 }
193 
194 void FairMCMatch::FindStagesPointingToLinks(FairMultiLinkedData links, Int_t stop)
195 {
196  FairMultiLinkedData tempLinks;
197  tempLinks.SetPersistanceCheck(kFALSE);
198  for (int i = 0; i < links.GetNLinks(); ++i) {
199  FairLink myLink = links.GetLink(i);
200  FairMultiLinkedData myNewLinks = FindStagesPointingToLink(myLink);
201  myNewLinks.SetPersistanceCheck(kFALSE);
202  if (myNewLinks.GetNLinks() == 0) {
203  fFinalStageML.AddLink(myLink, true);
204  } else {
205  for (int j = 0; j < myNewLinks.GetNLinks(); ++j) {
206  if (myNewLinks.GetLink(j).GetType() == static_cast<Int_t>(stop)) {
207  fFinalStageML.AddLink(myNewLinks.GetLink(j), true);
208  } else if (myNewLinks.GetLink(j).GetType() > static_cast<Int_t>(stop)) {
209  fFinalStageML.AddLink(myLink, true);
210  } else {
211  tempLinks.AddLink(myNewLinks.GetLink(j), true);
212  }
213  }
214  }
215  }
216  if (tempLinks.GetNLinks() != 0) {
217  FindStagesPointingToLinks(tempLinks, stop);
218  }
219 }
220 
221 FairMultiLinkedData FairMCMatch::FindStagesPointingToLink(FairLink link)
222 {
223  FairMultiLinkedData result;
224  result.SetPersistanceCheck(kFALSE);
225  TListIteratorConst iter = fList.find(static_cast<Int_t>(link.GetType()));
226  for (; iter != fList.end(); ++iter) {
227  if (iter->second->PosInList(link).GetNLinks() > 0) {
228  result.AddLinks(iter->second->PosInList(link), true);
229  }
230  }
231  return result;
232 }
233 
235 {
236  FairMultiLinkedData result;
237  result.SetPersistanceCheck(kFALSE);
238  for (int i = 0; i < GetNMCStages(); ++i) {
239  result.AddLinks(GetMCStage(i)->GetLinksWithType(stage), true);
240  }
241  return result;
242 }
243 
244 void FairMCMatch::CreateArtificialStage(const TString& branchName, const std::string& fileName)
245 {
247  std::cout << "Create Artificial Stage for " << branchName;
248  std::cout << " with ID " << ioman->GetBranchId(branchName) << std::endl;
249  if (ioman->GetBranchId(branchName) > -1) {
250  CreateArtificialStage(ioman->GetBranchId(branchName), fileName, branchName.Data());
251  } else {
252  std::cout << "-E- FairMCMatch::CreateArtificialStage: Branch does not exist: " << branchName << std::endl;
253  }
254 }
255 
256 void FairMCMatch::CreateArtificialStage(Int_t stage, const std::string& fileName, const std::string& branchName)
257 {
258  FairMultiLinkedData stageLinks = FindLinksToStage(stage);
259  stageLinks.SetPersistanceCheck(kFALSE);
260  if (stageLinks.GetNLinks() > 0) {
261  InitStage(stage, fileName, branchName);
262  FairMultiLinkedData artData;
263  artData.SetLink(FairLink(static_cast<Int_t>(-1), -1));
264  for (int i = 0; i < stageLinks.GetNLinks(); ++i) {
265  if (stageLinks.GetLink(i).GetIndex() > -1) {
266  fList[stage]->SetEntry(&artData, stageLinks.GetLink(i).GetIndex());
267  }
268  }
269  fList[stage]->SetLoaded(kTRUE);
270  }
271 }
272 
273 void FairMCMatch::GetNextStage(FairMultiLinkedData& startStage, Int_t stopStage)
274 {
275 
276  FairMCEntry tempStage;
277 
278  for (int i = 0; i < startStage.GetNLinks(); ++i) {
279  if (startStage.GetLink(i).GetType() == stopStage) {
280  AddToFinalStage(startStage.GetLink(i), 1);
281  if (fVerbose > 0) {
282  std::cout << "FinalStage: " << fFinalStageML << std::endl;
283  std::cout << "---------------------" << std::endl;
284  }
285  } else if (startStage.GetLink(i).GetType() == fUltimateStage) {
286  } else {
287  tempStage = GetEntry(startStage.GetLink(i));
288  if (fVerbose > 0) {
289  std::cout << "TempStage Start";
290  startStage.GetLink(i).PrintLinkInfo();
291  std::cout << " --> " << tempStage << std::endl;
292  }
293  if (tempStage.GetNLinks() == 0) {
294  AddToFinalStage(startStage.GetLink(i), 1);
295  if (fVerbose > 0) {
296  std::cout << "FinalStage: " << fFinalStageML << std::endl;
297  std::cout << "---------------------" << std::endl;
298  }
299  } else {
300  double tempStageWeight = GetMCStageType(static_cast<Int_t>(tempStage.GetSource()))->GetWeight();
301  double startLinkWeight = startStage.GetLink(i).GetWeight();
302 
303  if (fVerbose > 0) {
304  std::cout << "Tempstage " << tempStage.GetSource() << ": weight " << tempStageWeight << std::endl;
305  std::cout << "StartLinkWeight " << startLinkWeight << std::endl;
306  }
307  tempStage.MultiplyAllWeights(tempStageWeight);
308 
309  if ((tempStageWeight * startLinkWeight) == 0) {
310  tempStage.MultiplyAllWeights(tempStageWeight);
311  tempStage.AddAllWeights(startLinkWeight / startStage.GetNLinks());
312  if (fVerbose > 0) {
313  std::cout << " NLinks: " << tempStage.GetNLinks() << " ";
314  std::cout << "AddAllWeights: " << startLinkWeight / startStage.GetNLinks() << std::endl;
315  }
316  } else {
317  tempStage.MultiplyAllWeights(startLinkWeight);
318  if (fVerbose > 0) {
319  std::cout << "MultiplyAllWeights: " << startLinkWeight << std::endl;
320  }
321  }
322  }
323  if (fVerbose > 0) {
324  std::cout << "TempStage Stop: " << tempStage << std::endl;
325  }
326 
327  GetNextStage(tempStage, stopStage);
328  }
329  }
330 }
331 
332 FairMCEntry FairMCMatch::GetEntry(Int_t type, int index)
333 {
334 
335  FairMCEntry empty;
336  if (index < 0) {
337  return empty;
338  }
339  if (fList[type] == 0) {
340  return empty;
341  }
342  if (fList[type]->GetNEntries() > index) {
343  return fList[type]->GetEntry(index);
344  }
345  return empty;
346 }
347 
349 {
350  return GetEntry(static_cast<Int_t>(link.GetType()), link.GetIndex());
351 }
352 
353 void FairMCMatch::AddToFinalStage(FairLink hitPair, Float_t mult) { fFinalStageML.AddLink(hitPair, true, mult); }
354 
355 void FairMCMatch::ClearFinalStage() { fFinalStageML.Reset(); }
356 
358 {
359  for (TListIterator iter = fList.begin(); iter != fList.end(); ++iter) {
360  if (iter->second != 0) {
361  iter->second->ClearEntries();
362  }
363  }
364  fList.clear();
365 }
366 
368 {
369  for (TListIterator iter = fList.begin(); iter != fList.end(); ++iter) {
370  if (iter->first == type) {
371  return true;
372  }
373  }
374  return false;
375 }
376 
377 void FairMCMatch::LoadInMCLists(TClonesArray* myLinkArray)
378 {
379  for (int i = 0; i < myLinkArray->GetEntriesFast(); ++i) {
380  FairMCEntry* myLink = static_cast<FairMCEntry*>(myLinkArray->At(i));
381  if (IsTypeInList(static_cast<Int_t>(myLink->GetSource()))) {
382  fList[static_cast<Int_t>(myLink->GetSource())]->SetEntry(*myLink);
383  fList[static_cast<Int_t>(myLink->GetSource())]->SetLoaded(kTRUE);
384  }
385  }
386 }
FairMCResult GetMCInfo(Int_t start, Int_t stop)
FairMCEntry GetEntry(Int_t type, int index)
virtual ~FairMCMatch()
Definition: FairMCMatch.cxx:35
void SetWeight(Double_t weight)
Definition: FairMCStage.h:60
int GetNMCStages() const
Definition: FairMCMatch.h:73
void SetCommonWeightStages(Float_t weight)
Definition: FairMCMatch.cxx:86
virtual void AddAllWeights(Double_t weight)
Adds weight to all Links.
virtual void SetLinks(FairMultiLinkedData links, Float_t mult=1.0)
Sets the links as vector of FairLink.
bool IsTypeInList(Int_t type)
virtual void SetPersistanceCheck(Bool_t check)
Controls if a persistance check of a link is done or not.
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
void InitStage(Int_t type, const std::string &fileName, const std::string &branchName)
Definition: FairMCMatch.cxx:56
virtual void MultiplyAllWeights(Double_t weight)
Multiplies all Links with weight.
void SetElements(Int_t sourceType, int index, FairMultiLinkedData *links)
Definition: FairMCMatch.cxx:51
FairMCStage * GetMCStageType(TString branch)
Definition: FairMCMatch.h:84
FairMCEntry GetMCInfoSingle(FairLink aLink, Int_t stop)
virtual void AddLinks(FairMultiLinkedData links, Float_t mult=1.0)
Adds a List of FairLinks (FairMultiLinkedData) to fLinks.
void CreateArtificialStage(const TString &branchName, const std::string &fileName="")
Double_t GetWeight(void) const
Definition: FairMCStage.h:66
virtual void SetLink(FairLink link, Bool_t bypass=kFALSE, Float_t mult=1.0)
Sets the Links with a single FairLink.
int GetNEntries() const
Definition: FairMCObject.h:80
virtual Int_t GetNLinks() const
returns the number of stored links
void RemoveStage(Int_t type)
Definition: FairMCMatch.cxx:84
void ClearMCList()
virtual FairLink GetLink(Int_t pos) const
returns the FairLink at the given position
std::map< Int_t, FairMCStage * >::iterator TListIterator
Definition: FairMCMatch.h:33
Int_t CheckBranch(const char *BrName)
std::map< Int_t, FairMCStage * >::const_iterator TListIteratorConst
Definition: FairMCMatch.h:36
Int_t GetStageId(void) const
Definition: FairMCObject.h:76
void AddElement(Int_t type, int index, FairLink link)
Definition: FairMCMatch.cxx:49
void LoadInMCLists(TClonesArray *myLinkArray)
virtual void AddLink(FairLink link, Bool_t bypass=kFALSE, Float_t mult=1.0)
Int_t GetSource() const
Definition: FairMCEntry.h:48
FairMultiLinkedData FindLinksToStage(Int_t stage)
FairMCStage * GetMCStage(int index) const
Definition: FairMCMatch.h:75
Int_t GetBranchId(TString const &BrName)