Skip to main content

Posts

Use of TClonesArray Class

void write { TClonesArray * arr = new TClonesArray ( " TVector3 " ); TClonesArray & ar = * arr ; TFile * file = new TFile ( " file.root " , " recreate " ); TTree * tree = new TTree ( " tcl " , " tcl " ); tree -> Branch ( " array " , & arr ); TRandom2 * rand = new TRandom2 ( 1 ); for ( int i = 0 ; i < 100 ; i ++) { arr -> Clear (); for ( int j = 0 ; j < 1000 ; j ++) { double x = rand -> Rndm (); double y = rand -> Rndm (); double z = rand -> Rndm (); new ( ar [ j ]) TVector3 ( x , y , z ); } tree -> Fill (); } file -> Write (); file -> Close (); } void read () { TFile * file = new TFile ( " file.root " ); TTree * tree = ( TTree *) file -> Get ( " tcl " ); TClonesArray * arr = new ...

7a

 # Q 7A import numpy as np from scipy.special import legendre as P theta = np.pi n= 2 lhs = np.sin((n+1)*theta)/np.sin(theta) rhs = sum([np.poly1d(P(l))(np.cos(theta)) * np.poly1d(P(n-l))(np.cos(theta)) for l in range(0, n+1)]) print("LHS = ",lhs) print("RHS = ",rhs) print("Difference = ", abs(lhs-rhs))

6a

 # Q 6A # Integral e^-x2 from 0 --> infinity import numpy as np from scipy.integrate import quad, simps import matplotlib.pyplot as plt f = lambda x: np.exp(-x**2) res = quad(f, 0, np.inf) print("Value of integral is = ", round(res[0], 4))

5b

 # Q 5B import numpy as np from scipy.integrate import quad, simps from scipy.signal import square, sawtooth import matplotlib.pyplot as plt x = np.linspace(-np.pi, np.pi, 1001) f1 = abs(np.sin(x)) L = 2*np.pi # Evaluation of Fourier coefficients a0 = (2.0/L)*simps(f1, x) an = lambda n: (2.0/L) * simps(f1*np.cos(n*x), x) bn = lambda n: (2.0/L) * simps(f1*np.sin(n*x), x) # Summing the serier R = 10 xn = np.linspace(-R, R, 1001) s = 0.5*a0 +  sum([an(n)*np.cos(n*xn) + bn(n)*np.sin(n*xn) for n in range(1, 50)]) signal = abs(np.sin(xn)) #Plotting the Functions plt.plot(xn, signal,'r-' ,label="Original Signal") plt.plot(xn, s,'ko',ms=2, label = "Fourier Approximation") plt.legend() plt.show()

5a

 import numpy as np from scipy.integrate import quad import matplotlib.pyplot as plt f = lambda x: np.sin(x**2) A = [] x = np.arange(0.01, 10, 0.01) for i in x:          res = quad(f, 0, i)     A.append(res[0]) print(sum(A)) plt.plot(x[:600], A[:600],label="Fresnel Integral Si(x)") plt.grid() plt.legend() plt.show() print("From the plot, we find that the solution slowly converges to 0.5")

4b

 # Q 4B # Question is nothing but 1-D Heat Equation with heating at middle # Solve by using finite difference method import numpy as np import matplotlib.pyplot as plt from scipy.integrate import quad x = np.linspace(0, 1, 101) u = np.zeros(101) u[0] = u[100] = 0       # Boundary Conditions u[50] = 1               # Initial Condition for t in range(100):     for i in range(1,100):         u[i] += (u[i+1] + u[i-1] -2*u[i])/4 plt.plot(x, u, 'r-', lw=3) plt.xlabel("Length of rod") plt.ylabel("Temperature (in reduced units)") plt.title("Temperature Profile of Rod") plt.show()

4a

 # Q 4A # Fourier Series Sawtooth import numpy as np from scipy.signal import sawtooth from scipy.integrate import simps import matplotlib.pyplot as plt t = np.linspace(0,1, 1001) f1 = sawtooth(2*np.pi*2*t,1) T = 0.5 #Calculation Of Coefficients a0 = (2./T) * simps(f1, t) an = lambda n : (2./T) * simps(f1*np.cos(2*np.pi*2*n*t), t) bn = lambda n : (2./T) * simps(f1*np.sin(2*np.pi*2*n*t), t) tn = np.linspace(0, 6, 1001) s =0.5*a0 + sum([an(n)*np.cos(2*np.pi*2*n*tn) + bn(n)*np.sin(2*np.pi*2*n*tn) for n in range(10)]) plt.plot(tn, s, 'r',label="Fourier approximation") plt.plot(tn, 2*sawtooth(2*np.pi*2*tn,1) ,label= "Original Signal") plt.legend() plt.show()