Diffcult with vectors and for cycles

Jul 20, 2015 at 9:17pm
I wrote the following bool function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
bool user::Execute(SampleFormat& sample, const EventFormat& event)
{
  if (event.rec()!=0)
  {
    Float_t a1, a2, a3, a4, a5, a6;

    TLorentzVector tlvelec, tlvmu, tlvleptons, tlvleptonmax, tlvelecp, tlvelecn, tlvmup, tlvmun, tlvZ, tlvjetbtag, tlvmup1, tlvmup2, tlvmup3, tlvmun1, tlvmun2, tlvmun3, tlvelec1, tlvelec2, tlvelec3; //With TLorentzVector.h we can work with vectors of 4 dimensons, to know more about TlorentzVecor.h go here https://root.cern.ch/root/html/TLorentzVector.html 

    Float_t min = 81;
    Float_t max = 102;
    Float_t mz = 91.1876;

    for (unsigned int i=0;i<event.rec()->electrons().size();i++)
    {
      const RecLeptonFormat& elec = event.rec()->electrons()[i]; //this array gives the 1, 2 or three electrons presented in the event (event.rec()). 

      tlvelec = elec.momentum() ; //function inside TLorentzVector.h
      HistptELEC->Fill(tlvelec.Pt()); // same ^


   if ( elec.charge() > 0 ) {
         tlvelecp = tlvelec;
         }
    cout << "elec positive" << endl;

   if ( elec.charge() < 0 ) {
         tlvelecn = tlvelec;
    cout << "elec negative" << endl;
    }
   }

    for (unsigned int i=0;i<event.rec()->muons().size();i++)
    {
      const RecLeptonFormat& mu = event.rec()->muons()[i];

      tlvmu = mu.momentum();  

   if ( mu.charge() > 0 ) { //tlvmup is the 4-vector of the muon with charge +1
    tlvmup = mu.momentum();
    cout << "mu positive" << endl;
    }
    
   if ( mu.charge() < 0 ) { //tlvmup is the 4-vector of the muon with charge -1
    tlvmun = mu.momentum();
    cout << "mu negative" << endl;
    }

    tlvelec = tlvelecp + tlvelecn; //sum of vectors

    tlvmu = tlvmup + tlvmun;

    if ( (tlvelec.Pt() > tlvmu.Pt()) )
        {
        tlvleptonmax = tlvelec ;
        HistptLEPTONS->Fill(tlvleptonmax.Pt());
         }

     if ( (tlvmu.Pt() > tlvelec.Pt()) ) // Pt() is another function
        {
        tlvleptonmax = tlvmu ;
        HistptLEPTONS->Fill(tlvleptonmax.Pt());
         }

      }

    if ( (tlvleptons.M() > min) && (tlvleptons.M() < max) ) { //M is a function, so tlvleptons.M() is the value of mass of the 4-vector.
         tlvZ = tlvleptons;
         HistptZ-> Fill(tlvZ.Pt());
         HistmZ-> Fill(tlvZ.M());
         cout << "display pT(Z)" << tlvZ.Pt() << endl;
         cout << "massa = " << tlvZ.M() << endl;
         } 

      }
  return true;
} 


Here we always have, for each event:
- tlvmun + tlvmup + tlvelecp OR // 1 negative muon + 1 negative muon + 1 positive electron
- tlvmun + tlvmup + tlvmup OR
- tlvelecn + tlvelecp + tlvelecp OR
- tlvelecn + tlvelecp + tlvmup.

When we have tlvelecn + tlvelecp + tlvelecp, I don't know how to compare both vectors of the positive electrons. The sum of THE positive electron 4vector and the negative electron 4vector has to be closer to a certain value than the sum of the other positive electron 4vector and the negative electron 4vector.
Last edited on Jul 20, 2015 at 9:19pm
Jul 21, 2015 at 4:13am
The sum of THE positive electron 4vector and the negative electron 4vector has to be closer to a certain value than the sum of the other positive electron 4vector and the negative electron 4vector.
Huh? What do you mean by "closer to a certain value"? Do you mean that norm(sum1 - certain_value) < norm(sum2 - certain_value)?
Jul 22, 2015 at 12:03am
No, I mean norm(sum1) - certain_value < norm(sum2) - certain_value, with certain_value being a constant (this would select the case where norm(sum1) is closer than norm(sum2) to 'certain_value'). Here the certain value would be mz, and instead of the norm we have a function that is sum1.M() (sum1 = tlvelecp + tlvelecn) and this function gives a value (so it is a lot simple than having to calculate the norm). My problem is when we have 3 electron (i=0, 1 and 2) because we would have 2 vectors tlvelecp and I don't know how can I pick the one which satisfies better the condition sum1.M() - mz < sum2.M() - mz (where sum1 = tlvelecp1 + tlvelecn and sum2 = tlvelecp2 + tlvelecn - here the numbers at the end of the vectors tlvelecp1 and tlvelecp2 is only to distinguish one from another). They are in a for loop and I think I have to work with the index of each other, but can't solve the problem.
Last edited on Jul 22, 2015 at 1:31am
Jul 22, 2015 at 2:09pm
No, I mean norm(sum1) - certain_value < norm(sum2) - certain_value, with certain_value being a constant (this would select the case where norm(sum1) is closer than norm(sum2) to 'certain_value').
But this is equivalent to norm(sum1) < norm(sum2). Do you mean abs(norm(sum1) - certain_value) < abs(norm(sum2) - certain_value)?
Either way, let's just say that f(sum1) < f(sum2), where f: V -> R.

My problem is when we have 3 electron (i=0, 1 and 2) because we would have 2 vectors tlvelecp and I don't know how can I pick the one which satisfies better the condition
Do you need to get all the possible sums of the electrons and find the one with the minimum f(sum)?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Given: T electrons[length];

double min = DBL_MAX;
int min_i = 0, min_j = 0;
for (int i = 0; i < length - 1; i++){
    for (int j = i + 1; i < length; i++){
        double f_sum = f(electrons[i] + electrons[j]);
        if (f_sum < min){
            min_i = i;
            min_j = j;
            min = f_sum;
        }
    }
}
Jul 22, 2015 at 3:28pm
Yes, I meant abs(norm(sum1) - certain_value) < abs(norm(sum2) - certain_value). Thank you for your reply, I'll try to solve it considering the example you gave. Have a nice day!
Topic archived. No new replies allowed.